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