UNPKG

tslint-config-security

Version:
60 lines (59 loc) 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var isSafeRegexp = require("safe-regex"); var Lint = require("tslint"); var ts = require("typescript"); var node_kind_1 = require("../node-kind"); 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-unsafe-regexp', description: 'Warns when potential unsafe regular expression is found', descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["Any usage of potential unsafe regular expression will trigger a warning.\n See https://github.com/webschik/tslint-config-security#tsr-detect-unsafe-regexp"], ["Any usage of potential unsafe regular expression will trigger a warning.\n See https://github.com/webschik/tslint-config-security#tsr-detect-unsafe-regexp"]))), 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.RegularExpressionLiteral: { var text = node.text; if (text && !isSafeRegexp(text)) { ctx.addFailureAtNode(node, 'Unsafe Regular Expression'); } break; } case ts.SyntaxKind.NewExpression: { var _a = node, expression = _a.expression, args = _a.arguments; var firstArgument = args && args[0]; var firstArgumentText = firstArgument && firstArgument.text; if (expression && firstArgument && expression.text === 'RegExp' && node_kind_1.stringLiteralKinds.includes(firstArgument.kind) && firstArgumentText && !isSafeRegexp(firstArgumentText)) { ctx.addFailureAtNode(node, 'Unsafe Regular Expression (new RegExp)'); } break; } default: } return ts.forEachChild(node, visitNode); } return ts.forEachChild(ctx.sourceFile, visitNode); } var templateObject_1;