UNPKG

4.16 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 Utils_1 = require("./utils/Utils");
17var JsxAttribute_1 = require("./utils/JsxAttribute");
18var FAILURE_STRING = 'Anchor tags with target="_blank" should also include rel="noopener noreferrer"';
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 if (sourceFile.languageVariant === ts.LanguageVariant.JSX) {
26 return this.applyWithWalker(new ReactAnchorBlankNoopenerRuleWalker(sourceFile, this.getOptions()));
27 }
28 else {
29 return [];
30 }
31 };
32 Rule.metadata = {
33 ruleName: 'react-anchor-blank-noopener',
34 type: 'functionality',
35 description: 'Anchor tags with target="_blank" should also include rel="noopener noreferrer"',
36 options: null,
37 optionsDescription: '',
38 typescriptOnly: true,
39 issueClass: 'SDL',
40 issueType: 'Error',
41 severity: 'Critical',
42 level: 'Mandatory',
43 group: 'Security',
44 commonWeaknessEnumeration: '242,676'
45 };
46 return Rule;
47}(Lint.Rules.AbstractRule));
48exports.Rule = Rule;
49var ReactAnchorBlankNoopenerRuleWalker = (function (_super) {
50 __extends(ReactAnchorBlankNoopenerRuleWalker, _super);
51 function ReactAnchorBlankNoopenerRuleWalker() {
52 return _super !== null && _super.apply(this, arguments) || this;
53 }
54 ReactAnchorBlankNoopenerRuleWalker.prototype.visitJsxElement = function (node) {
55 var openingElement = node.openingElement;
56 this.validateOpeningElement(openingElement);
57 _super.prototype.visitJsxElement.call(this, node);
58 };
59 ReactAnchorBlankNoopenerRuleWalker.prototype.visitJsxSelfClosingElement = function (node) {
60 this.validateOpeningElement(node);
61 _super.prototype.visitJsxSelfClosingElement.call(this, node);
62 };
63 ReactAnchorBlankNoopenerRuleWalker.prototype.validateOpeningElement = function (openingElement) {
64 if (openingElement.tagName.getText() === 'a') {
65 var allAttributes = JsxAttribute_1.getJsxAttributesFromJsxElement(openingElement);
66 var target = allAttributes['target'];
67 var rel = allAttributes['rel'];
68 if (target != null && JsxAttribute_1.getStringLiteral(target) === '_blank' && !isRelAttributeValue(rel)) {
69 this.addFailureAt(openingElement.getStart(), openingElement.getWidth(), FAILURE_STRING);
70 }
71 }
72 };
73 return ReactAnchorBlankNoopenerRuleWalker;
74}(ErrorTolerantWalker_1.ErrorTolerantWalker));
75function isRelAttributeValue(attribute) {
76 if (JsxAttribute_1.isEmpty(attribute)) {
77 return false;
78 }
79 if (attribute.initializer.kind === ts.SyntaxKind.JsxExpression) {
80 var expression = attribute.initializer;
81 if (expression.expression != null && expression.expression.kind !== ts.SyntaxKind.StringLiteral) {
82 return true;
83 }
84 }
85 var stringValue = JsxAttribute_1.getStringLiteral(attribute);
86 if (stringValue == null || stringValue.length === 0) {
87 return false;
88 }
89 var relValues = stringValue.split(/\s+/);
90 return Utils_1.Utils.contains(relValues, 'noreferrer') && Utils_1.Utils.contains(relValues, 'noopener');
91}
92//# sourceMappingURL=reactAnchorBlankNoopenerRule.js.map
\No newline at end of file