UNPKG

2.47 kBJavaScriptView Raw
1"use strict";
2var __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})();
15Object.defineProperty(exports, "__esModule", { value: true });
16var ts = require("typescript");
17var Lint = require("tslint");
18var tsutils = require("tsutils");
19var MATH_FAIL_STRING = 'Math.random produces insecure random numbers. Use crypto.randomBytes() or window.crypto.getRandomValues() instead';
20var NODE_FAIL_STRING = 'crypto.pseudoRandomBytes produces insecure random numbers. Use crypto.randomBytes() instead';
21var Rule = (function (_super) {
22 __extends(Rule, _super);
23 function Rule() {
24 return _super !== null && _super.apply(this, arguments) || this;
25 }
26 Rule.prototype.apply = function (sourceFile) {
27 return this.applyWithFunction(sourceFile, walk);
28 };
29 Rule.metadata = {
30 ruleName: 'insecure-random',
31 type: 'functionality',
32 description: 'Do not use insecure sources for random bytes',
33 options: null,
34 optionsDescription: '',
35 typescriptOnly: true,
36 issueClass: 'SDL',
37 issueType: 'Error',
38 severity: 'Important',
39 level: 'Opportunity for Excellence',
40 group: 'Security',
41 commonWeaknessEnumeration: '330'
42 };
43 return Rule;
44}(Lint.Rules.AbstractRule));
45exports.Rule = Rule;
46function walk(ctx) {
47 function cb(node) {
48 if (tsutils.isPropertyAccessExpression(node)) {
49 if (node.expression.getText() === 'Math' && node.name.text === 'random') {
50 ctx.addFailureAt(node.getStart(), node.getWidth(), MATH_FAIL_STRING);
51 }
52 else if (node.name.text === 'pseudoRandomBytes') {
53 ctx.addFailureAt(node.getStart(), node.getWidth(), NODE_FAIL_STRING);
54 }
55 }
56 return ts.forEachChild(node, cb);
57 }
58 return ts.forEachChild(ctx.sourceFile, cb);
59}
60//# sourceMappingURL=insecureRandomRule.js.map
\No newline at end of file