UNPKG

3.35 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var ts = require("typescript");
14var Lint = require("tslint");
15var ErrorTolerantWalker_1 = require("./utils/ErrorTolerantWalker");
16var AstUtils_1 = require("./utils/AstUtils");
17var Utils_1 = require("./utils/Utils");
18var FAILURE_STRING = 'Non-literal (insecure) parameter passed to require(): ';
19var Rule = (function (_super) {
20 __extends(Rule, _super);
21 function Rule() {
22 return _super !== null && _super.apply(this, arguments) || this;
23 }
24 Rule.prototype.apply = function (sourceFile) {
25 return this.applyWithWalker(new NonLiteralRequireRuleWalker(sourceFile, this.getOptions()));
26 };
27 Rule.metadata = {
28 ruleName: 'non-literal-require',
29 type: 'functionality',
30 description: 'Detect require includes that are not for string literals',
31 options: null,
32 optionsDescription: '',
33 typescriptOnly: true,
34 issueClass: 'SDL',
35 issueType: 'Error',
36 severity: 'Critical',
37 level: 'Mandatory',
38 group: 'Security',
39 commonWeaknessEnumeration: '95,676'
40 };
41 return Rule;
42}(Lint.Rules.AbstractRule));
43exports.Rule = Rule;
44var NonLiteralRequireRuleWalker = (function (_super) {
45 __extends(NonLiteralRequireRuleWalker, _super);
46 function NonLiteralRequireRuleWalker() {
47 return _super !== null && _super.apply(this, arguments) || this;
48 }
49 NonLiteralRequireRuleWalker.prototype.visitCallExpression = function (node) {
50 var _this = this;
51 if (AstUtils_1.AstUtils.getFunctionName(node) === 'require'
52 && AstUtils_1.AstUtils.getFunctionTarget(node) == null
53 && node.arguments.length > 0) {
54 if (node.arguments[0].kind === ts.SyntaxKind.ArrayLiteralExpression) {
55 var arrayExp = node.arguments[0];
56 arrayExp.elements.forEach(function (initExpression) {
57 if (initExpression.kind !== ts.SyntaxKind.StringLiteral) {
58 _this.fail(initExpression);
59 }
60 });
61 }
62 else if (node.arguments[0].kind !== ts.SyntaxKind.StringLiteral) {
63 this.fail(node.arguments[0]);
64 }
65 }
66 _super.prototype.visitCallExpression.call(this, node);
67 };
68 NonLiteralRequireRuleWalker.prototype.fail = function (expression) {
69 var start = expression.getStart();
70 var width = expression.getWidth();
71 var message = FAILURE_STRING + Utils_1.Utils.trimTo(expression.getText(), 25);
72 this.addFailureAt(start, width, message);
73 };
74 return NonLiteralRequireRuleWalker;
75}(ErrorTolerantWalker_1.ErrorTolerantWalker));
76//# sourceMappingURL=nonLiteralRequireRule.js.map
\No newline at end of file