UNPKG

tslint-config-security

Version:
86 lines (85 loc) 3.05 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var Lint = require("tslint"); var ts = require("typescript"); var readMethods = [ 'readUInt8', 'readUInt16LE', 'readUInt16BE', 'readUInt32LE', 'readUInt32BE', 'readInt8', 'readInt16LE', 'readInt16BE', 'readInt32LE', 'readInt32BE', 'readFloatLE', 'readFloatBE', 'readDoubleL', 'readDoubleBE' ]; var writeMethods = [ 'writeUInt8', 'writeUInt16LE', 'writeUInt16BE', 'writeUInt32LE', 'writeUInt32BE', 'writeInt8', 'writeInt16LE', 'writeInt16BE', 'writeInt32LE', 'writeInt32BE', 'writeFloatLE', 'writeFloatBE', 'writeDoubleLE', 'writeDoubleBE' ]; 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-buffer-noassert', description: 'Warns when Buffer with noAssert flag is used', descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["Any usage of Buffer\n with noAssert flag will trigger a warning.\n See https://github.com/webschik/tslint-config-security#tsr-detect-buffer-noassert"], ["Any usage of Buffer\n with noAssert flag will trigger a warning.\n See https://github.com/webschik/tslint-config-security#tsr-detect-buffer-noassert"]))), optionsDescription: '', options: null, type: 'functionality', requiresTypeInfo: false, typescriptOnly: false }; return Rule; }(Lint.Rules.AbstractRule)); exports.Rule = Rule; function walk(ctx) { function visitNode(node) { if (node.kind === ts.SyntaxKind.PropertyAccessExpression) { var _a = node, name = _a.name, expression = _a.expression; var parent = node.parent; if (parent && parent.kind === ts.SyntaxKind.CallExpression && expression && name) { var methodName = name.text; var argumentIndex = -1; if (readMethods.indexOf(methodName) !== -1) { argumentIndex = 1; } else if (writeMethods.indexOf(methodName) !== -1) { argumentIndex = 2; } if (argumentIndex !== -1 && parent.arguments && parent.arguments[argumentIndex] && parent.arguments[argumentIndex].kind === ts.SyntaxKind.TrueKeyword) { ctx.addFailureAtNode(node, "Found Buffer." + methodName + " with noAssert flag set true"); } } } return ts.forEachChild(node, visitNode); } return ts.forEachChild(ctx.sourceFile, visitNode); } var templateObject_1;