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

Commit

Permalink
[bug] fixed tsr-detect-non-literal-fs-filename rule (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
webschik committed Dec 29, 2018
1 parent 8bcde56 commit da4138a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
2 changes: 1 addition & 1 deletion npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tslint-config-security",
"version": "1.13.0",
"version": "1.14.0",
"description": "TSLint security rules",
"main": "./index.js",
"files": [
Expand Down
46 changes: 44 additions & 2 deletions src/rules/tsrDetectNonLiteralFsFilenameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export class Rule extends Lint.Rules.AbstractRule {
}
}

const expressionsToCheck: string[] = ['fs', `require('fs')`, `require("fs")`];
const expressionsToCheck: string[] = ['fs', `require('fs')`, 'require("fs")', 'require(`fs`)'];
const reservedIdentifiers: string[] = ['__dirname'];

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

return Boolean(arg && !stringLiteralKinds.includes(arg.kind));
if (!arg) {
return false;
}
const {kind} = arg;

if (kind === ts.SyntaxKind.BinaryExpression) {
const {left, right} = arg as ts.BinaryExpression;

if (
left &&
left.kind === ts.SyntaxKind.Identifier &&
reservedIdentifiers.includes(left.getText())
) {
return Boolean(right && !stringLiteralKinds.includes(right.kind));
}

if (
right &&
right.kind === ts.SyntaxKind.Identifier &&
reservedIdentifiers.includes(right.getText())
) {
return Boolean(left && !stringLiteralKinds.includes(left.kind));
}
}

if (kind === ts.SyntaxKind.TemplateExpression) {
const {templateSpans = []} = arg as ts.TemplateExpression;
const [firstTemplateSpan] = templateSpans;
const firstTemplateSpanExpr: ts.Expression | void =
firstTemplateSpan && firstTemplateSpan.expression;

if (
firstTemplateSpanExpr &&
firstTemplateSpanExpr.kind === ts.SyntaxKind.Identifier &&
reservedIdentifiers.includes(firstTemplateSpanExpr.getText()) &&
!templateSpans[1]
) {
return false;
}
}

return !stringLiteralKinds.includes(kind);
});

if (invalidArgumentIndices[0] !== undefined) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ require('lodash-exists');

if (_.exists(memberId)) {
this.memberId = memberId;
}
}

fs.readFileSync(__dirname + 'filename.txt', 'utf-8')
fs.readFileSync(`${__dirname}filename.txt`, 'utf-8')
fs.readFileSync(`${__dirname}${path1}.txt`, 'utf-8')
~~~~~~~~~~~~~~~ [Found fs.readFileSync with non-literal argument at index 0]

0 comments on commit da4138a

Please sign in to comment.