tslint-config-security
Version:
TSLint security rules
58 lines (57 loc) • 2.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var Lint = require("tslint");
var ts = require("typescript");
var node_kind_1 = require("../node-kind");
var syntax_kind_to_name_1 = require("../syntax-kind-to-name");
var Rule = (function (_super) {
tslib_1.__extends(Rule, _super);
function Rule() {
return _super !== null && _super.apply(this, arguments) || this;
}
Rule.prototype.apply = function (sourceFile) {
return this.applyWithFunction(sourceFile, walk);
};
Rule.metadata = {
ruleName: 'tsr-detect-eval-with-expression',
description: 'Warns when eval() with non-literal argument is used',
descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["Any usage of eval()\n with non-literal argument will trigger a warning.\n See https://github.com/webschik/tslint-config-security#tsr-detect-eval-with-expression"], ["Any usage of eval()\n with non-literal argument will trigger a warning.\n See https://github.com/webschik/tslint-config-security#tsr-detect-eval-with-expression"]))),
optionsDescription: '',
options: null,
type: 'functionality',
requiresTypeInfo: false,
typescriptOnly: false
};
return Rule;
}(Lint.Rules.AbstractRule));
exports.Rule = Rule;
function walk(ctx) {
function visitNode(node) {
switch (node.kind) {
case ts.SyntaxKind.CallExpression: {
var _a = node, expression = _a.expression, args = _a.arguments;
var firstArgument = args && args[0];
if (firstArgument &&
expression.text === 'eval' &&
!node_kind_1.stringLiteralKinds.includes(firstArgument.kind)) {
ctx.addFailureAtNode(node, "eval with argument of type " + syntax_kind_to_name_1.default(firstArgument.kind));
}
break;
}
case ts.SyntaxKind.NewExpression: {
var _b = node, expression = _b.expression, args = _b.arguments;
if (args &&
expression.text === 'Function' &&
args.some(function (node) { return !node_kind_1.stringLiteralKinds.includes(node.kind); })) {
ctx.addFailureAtNode(node, 'Found function constructor with non-literal argument');
}
break;
}
default:
}
return ts.forEachChild(node, visitNode);
}
return ts.forEachChild(ctx.sourceFile, visitNode);
}
var templateObject_1;