This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#255] new security rule: detect-non-literal-require
closes #255
- Loading branch information
Showing
6 changed files
with
515 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import * as ts from 'typescript'; | ||
import * as Lint from 'tslint/lib/lint'; | ||
|
||
import {ErrorTolerantWalker} from './utils/ErrorTolerantWalker'; | ||
import {ExtendedMetadata} from './utils/ExtendedMetadata'; | ||
import {SyntaxKind} from './utils/SyntaxKind'; | ||
import {AstUtils} from './utils/AstUtils'; | ||
import {Utils} from './utils/Utils'; | ||
|
||
const FAILURE_STRING: string = 'Non-literal (insecure) parameter passed to require(): '; | ||
|
||
/** | ||
* Implementation of the non-literal-require rule. | ||
*/ | ||
export class Rule extends Lint.Rules.AbstractRule { | ||
|
||
public static metadata: ExtendedMetadata = { | ||
ruleName: 'non-literal-require', | ||
type: 'functionality', | ||
description: 'Detect require includes that are not for string literals', | ||
options: null, | ||
issueClass: 'SDL', | ||
issueType: 'Error', | ||
severity: 'Critical', | ||
level: 'Mandatory', | ||
group: 'Security', | ||
commonWeaknessEnumeration: '95,676' | ||
}; | ||
|
||
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { | ||
return this.applyWithWalker(new NonLiteralRequireRuleWalker(sourceFile, this.getOptions())); | ||
} | ||
} | ||
|
||
class NonLiteralRequireRuleWalker extends ErrorTolerantWalker { | ||
|
||
protected visitCallExpression(node: ts.CallExpression): void { | ||
if (AstUtils.getFunctionName(node) === 'require' | ||
&& AstUtils.getFunctionTarget(node) == null | ||
&& node.arguments.length > 0) { | ||
|
||
if (node.arguments[0].kind === SyntaxKind.current().ArrayLiteralExpression) { | ||
const arrayExp: ts.ArrayLiteralExpression = <ts.ArrayLiteralExpression>node.arguments[0]; | ||
arrayExp.elements.forEach((initExpression: ts.Expression): void => { | ||
if (initExpression.kind !== SyntaxKind.current().StringLiteral) { | ||
this.fail(initExpression); | ||
} | ||
}); | ||
} else if (node.arguments[0].kind !== SyntaxKind.current().StringLiteral) { | ||
this.fail(node.arguments[0]); | ||
} | ||
} | ||
super.visitCallExpression(node); | ||
} | ||
|
||
private fail(expression: ts.Expression): void { | ||
const start: number = expression.getStart(); | ||
const width: number = expression.getWidth(); | ||
const message: string = FAILURE_STRING + Utils.trimTo(expression.getText(), 25); | ||
this.addFailure(this.createFailure(start, width, message)); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import {TestHelper} from './TestHelper'; | ||
|
||
/** | ||
* Unit tests. | ||
*/ | ||
describe('nonLiteralRequireRule', () : void => { | ||
|
||
const ruleName : string = 'non-literal-require'; | ||
|
||
it('should pass on imports', () : void => { | ||
const script : string = ` | ||
import React = require('react'); | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ ]); | ||
}); | ||
|
||
it('should pass on string literals', () : void => { | ||
const script : string = ` | ||
const myModule = require('myModule'); | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ ]); | ||
}); | ||
|
||
it('should pass on empty array', () : void => { | ||
const script : string = ` | ||
const myModule = require([]); | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ ]); | ||
}); | ||
|
||
it('should pass on array of strings', () : void => { | ||
const script : string = ` | ||
const myModule = require(['myModule']); | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ ]); | ||
}); | ||
|
||
it('should fail on non string literal', () : void => { | ||
const script : string = ` | ||
const moduleName = 'myModule'; | ||
const myModule = require(moduleName);`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ | ||
{ | ||
"failure": "Non-literal (insecure) parameter passed to require(): moduleName", | ||
"name": "file.ts", | ||
"ruleName": "non-literal-require", | ||
"startPosition": { "character": 38, "line": 3 } | ||
} | ||
]); | ||
}); | ||
|
||
it('should fail on non-string array element', () : void => { | ||
const script : string = ` | ||
let myModule = require([ | ||
'myModule', | ||
somethingElse, | ||
'otherModule', | ||
getModuleName() | ||
]); | ||
`; | ||
|
||
TestHelper.assertViolations(ruleName, script, [ | ||
{ | ||
"failure": "Non-literal (insecure) parameter passed to require(): somethingElse", | ||
"name": "file.ts", | ||
"ruleName": "non-literal-require", | ||
"startPosition": { "character": 17, "line": 4 } | ||
}, | ||
{ | ||
"failure": "Non-literal (insecure) parameter passed to require(): getModuleName()", | ||
"name": "file.ts", | ||
"ruleName": "non-literal-require", | ||
"startPosition": { "character": 17, "line": 6 } | ||
} | ||
]); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/// <reference path="../typings/react.d.ts" /> | ||
/// <reference path="../typings/underscore.d.ts" /> | ||
/// <reference path="../typings/require.d.ts" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.