Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit da4138a

Browse files
committed
[bug] fixed tsr-detect-non-literal-fs-filename rule (#13)
1 parent 8bcde56 commit da4138a

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

npm-shrinkwrap.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "tslint-config-security",
3-
"version": "1.13.0",
3+
"version": "1.14.0",
44
"description": "TSLint security rules",
55
"main": "./index.js",
66
"files": [

src/rules/tsrDetectNonLiteralFsFilenameRule.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export class Rule extends Lint.Rules.AbstractRule {
99
}
1010
}
1111

12-
const expressionsToCheck: string[] = ['fs', `require('fs')`, `require("fs")`];
12+
const expressionsToCheck: string[] = ['fs', `require('fs')`, 'require("fs")', 'require(`fs`)'];
13+
const reservedIdentifiers: string[] = ['__dirname'];
1314

1415
class RuleWalker extends Lint.RuleWalker {
1516
visitPropertyAccessExpression(node: ts.PropertyAccessExpression) {
@@ -25,7 +26,48 @@ class RuleWalker extends Lint.RuleWalker {
2526
const invalidArgumentIndices: number[] = fsArgsInfo.filter((index: number) => {
2627
const arg: ts.Expression = methodArguments[index];
2728

28-
return Boolean(arg && !stringLiteralKinds.includes(arg.kind));
29+
if (!arg) {
30+
return false;
31+
}
32+
const {kind} = arg;
33+
34+
if (kind === ts.SyntaxKind.BinaryExpression) {
35+
const {left, right} = arg as ts.BinaryExpression;
36+
37+
if (
38+
left &&
39+
left.kind === ts.SyntaxKind.Identifier &&
40+
reservedIdentifiers.includes(left.getText())
41+
) {
42+
return Boolean(right && !stringLiteralKinds.includes(right.kind));
43+
}
44+
45+
if (
46+
right &&
47+
right.kind === ts.SyntaxKind.Identifier &&
48+
reservedIdentifiers.includes(right.getText())
49+
) {
50+
return Boolean(left && !stringLiteralKinds.includes(left.kind));
51+
}
52+
}
53+
54+
if (kind === ts.SyntaxKind.TemplateExpression) {
55+
const {templateSpans = []} = arg as ts.TemplateExpression;
56+
const [firstTemplateSpan] = templateSpans;
57+
const firstTemplateSpanExpr: ts.Expression | void =
58+
firstTemplateSpan && firstTemplateSpan.expression;
59+
60+
if (
61+
firstTemplateSpanExpr &&
62+
firstTemplateSpanExpr.kind === ts.SyntaxKind.Identifier &&
63+
reservedIdentifiers.includes(firstTemplateSpanExpr.getText()) &&
64+
!templateSpans[1]
65+
) {
66+
return false;
67+
}
68+
}
69+
70+
return !stringLiteralKinds.includes(kind);
2971
});
3072

3173
if (invalidArgumentIndices[0] !== undefined) {

test/rules/tsr-detect-non-literal-fs-filename/default/test.ts.lint

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,9 @@ require('lodash-exists');
3333

3434
if (_.exists(memberId)) {
3535
this.memberId = memberId;
36-
}
36+
}
37+
38+
fs.readFileSync(__dirname + 'filename.txt', 'utf-8')
39+
fs.readFileSync(`${__dirname}filename.txt`, 'utf-8')
40+
fs.readFileSync(`${__dirname}${path1}.txt`, 'utf-8')
41+
~~~~~~~~~~~~~~~ [Found fs.readFileSync with non-literal argument at index 0]

0 commit comments

Comments
 (0)