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 FAILURE_NOT_FOUND = 'An iframe element requires a sandbox attribute';
|
20 | var FAILURE_INVALID_ENTRY = 'An iframe element defines an invalid sandbox attribute: ';
|
21 | var FAILURE_INVALID_COMBINATION = 'An iframe element defines a sandbox with both allow-scripts and allow-same-origin';
|
22 | var ALLOWED_VALUES = [
|
23 | '',
|
24 | 'allow-forms',
|
25 | 'allow-modals',
|
26 | 'allow-orientation-lock',
|
27 | 'allow-pointer-lock',
|
28 | 'allow-popups',
|
29 | 'allow-popups-to-escape-sandbox',
|
30 | 'allow-same-origin',
|
31 | 'allow-scripts',
|
32 | 'allow-top-navigation'
|
33 | ];
|
34 | var Rule = (function (_super) {
|
35 | __extends(Rule, _super);
|
36 | function Rule() {
|
37 | return _super !== null && _super.apply(this, arguments) || this;
|
38 | }
|
39 | Rule.prototype.apply = function (sourceFile) {
|
40 | if (sourceFile.languageVariant === ts.LanguageVariant.JSX) {
|
41 | return this.applyWithFunction(sourceFile, walk);
|
42 | }
|
43 | else {
|
44 | return [];
|
45 | }
|
46 | };
|
47 | Rule.metadata = {
|
48 | ruleName: 'react-iframe-missing-sandbox',
|
49 | type: 'functionality',
|
50 | description: 'React iframes must specify a sandbox attribute',
|
51 | options: null,
|
52 | optionsDescription: '',
|
53 | typescriptOnly: true,
|
54 | issueClass: 'SDL',
|
55 | issueType: 'Error',
|
56 | severity: 'Critical',
|
57 | level: 'Opportunity for Excellence',
|
58 | group: 'Security',
|
59 | commonWeaknessEnumeration: '915'
|
60 | };
|
61 | return Rule;
|
62 | }(Lint.Rules.AbstractRule));
|
63 | exports.Rule = Rule;
|
64 | function walk(ctx) {
|
65 | function handleJsxOpeningElement(node) {
|
66 | if (node.tagName.getText() !== 'iframe') {
|
67 | return;
|
68 | }
|
69 | var sandboxAttributeFound = false;
|
70 | node.attributes.properties.forEach(function (attribute) {
|
71 | if (attribute.kind === ts.SyntaxKind.JsxAttribute) {
|
72 | var jsxAttribute = attribute;
|
73 | var attributeName = jsxAttribute.name.text;
|
74 | if (attributeName === 'sandbox') {
|
75 | sandboxAttributeFound = true;
|
76 | if (jsxAttribute.initializer !== undefined && jsxAttribute.initializer.kind === ts.SyntaxKind.StringLiteral) {
|
77 | validateSandboxValue(jsxAttribute.initializer);
|
78 | }
|
79 | }
|
80 | }
|
81 | });
|
82 | if (!sandboxAttributeFound) {
|
83 | ctx.addFailureAt(node.getStart(), node.getWidth(), FAILURE_NOT_FOUND);
|
84 | }
|
85 | }
|
86 | function validateSandboxValue(node) {
|
87 | var values = node.text.split(' ');
|
88 | var allowScripts = false;
|
89 | var allowSameOrigin = false;
|
90 | values.forEach(function (attributeValue) {
|
91 | if (ALLOWED_VALUES.indexOf(attributeValue) === -1) {
|
92 | ctx.addFailureAt(node.getStart(), node.getWidth(), FAILURE_INVALID_ENTRY + attributeValue);
|
93 | }
|
94 | if (attributeValue === 'allow-scripts') {
|
95 | allowScripts = true;
|
96 | }
|
97 | if (attributeValue === 'allow-same-origin') {
|
98 | allowSameOrigin = true;
|
99 | }
|
100 | });
|
101 | if (allowScripts && allowSameOrigin) {
|
102 | ctx.addFailureAt(node.getStart(), node.getWidth(), FAILURE_INVALID_COMBINATION);
|
103 | }
|
104 | }
|
105 | function cb(node) {
|
106 | if (tsutils.isJsxElement(node)) {
|
107 | handleJsxOpeningElement(node.openingElement);
|
108 | }
|
109 | else if (tsutils.isJsxSelfClosingElement(node)) {
|
110 | handleJsxOpeningElement(node);
|
111 | }
|
112 | return ts.forEachChild(node, cb);
|
113 | }
|
114 | return ts.forEachChild(ctx.sourceFile, cb);
|
115 | }
|
116 |
|
\ | No newline at end of file |