diff --git a/README.md b/README.md index 3a06ef705d..0131710eff 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ Enable the rules that you would like to use. * [react/forbid-foreign-prop-types](docs/rules/forbid-foreign-prop-types.md): Forbid using another component's propTypes * [react/forbid-prop-types](docs/rules/forbid-prop-types.md): Forbid certain propTypes * [react/function-component-definition](docs/rules/function-component-definition.md): Standardize the way function component get defined (fixable) +* [react/iframe-missing-sandbox](docs/rules/iframe-missing-sandbox.md): Enforce sandbox attribute on iframe elements * [react/no-access-state-in-setstate](docs/rules/no-access-state-in-setstate.md): Reports when this.state is accessed within setState * [react/no-adjacent-inline-elements](docs/rules/no-adjacent-inline-elements.md): Prevent adjacent inline elements not separated by whitespace. * [react/no-array-index-key](docs/rules/no-array-index-key.md): Prevent usage of Array index in keys diff --git a/docs/rules/iframe-missing-sandbox.md b/docs/rules/iframe-missing-sandbox.md new file mode 100644 index 0000000000..b0c4341f9c --- /dev/null +++ b/docs/rules/iframe-missing-sandbox.md @@ -0,0 +1,29 @@ +# Enforce sandbox attribute on iframe elements (react/iframe-missing-sandbox) + +The sandbox attribute enables an extra set of restrictions for the content in the iframe. Using sandbox attribute is considered a good security practice. + +See https://www.w3schools.com/tags/att_iframe_sandbox.asp + +## Rule Details + +This rule checks all JSX iframe elements and verifies that there is sandbox attribute and that it's value is valid. In addition to that it also reports cases where attribute contains `allow-scripts` and `allow-same-origin` at the same time as this combination allows the embedded document to remove the sandbox attribute and bypass the restrictions. + +The following patterns are considered warnings: + +```jsx +var React = require('react'); + +var Frame = ; +``` + +The following patterns are **not** considered warnings: + +```jsx +var React = require('react'); + +var Frame = ; +``` + +## When not to use + +If you don't want to enforce sandbox attribute on iframe elements. diff --git a/index.js b/index.js index bc0c30a43d..55c168fbc2 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,7 @@ const allRules = { 'forbid-foreign-prop-types': require('./lib/rules/forbid-foreign-prop-types'), 'forbid-prop-types': require('./lib/rules/forbid-prop-types'), 'function-component-definition': require('./lib/rules/function-component-definition'), + 'iframe-missing-sandbox': require('./lib/rules/iframe-missing-sandbox'), 'jsx-boolean-value': require('./lib/rules/jsx-boolean-value'), 'jsx-child-element-spacing': require('./lib/rules/jsx-child-element-spacing'), 'jsx-closing-bracket-location': require('./lib/rules/jsx-closing-bracket-location'), diff --git a/lib/rules/iframe-missing-sandbox.js b/lib/rules/iframe-missing-sandbox.js new file mode 100644 index 0000000000..f6078825ee --- /dev/null +++ b/lib/rules/iframe-missing-sandbox.js @@ -0,0 +1,103 @@ +/** + * @fileoverview TBD + */ + +'use strict'; + +const docsUrl = require('../util/docsUrl'); + +module.exports = { + meta: { + docs: { + description: 'Enforce sandbox attribute on iframe elements', + category: 'Best Practices', + recommended: false, + url: docsUrl('iframe-missing-sandbox') + }, + + schema: [], + + messages: { + attributeMissing: 'An iframe element is missing a sandbox attribute', + invalidValue: 'An iframe element defines a sandbox attribute with invalid value "{{ value }}"', + invalidCombination: 'An iframe element defines a sandbox attribute with both allow-scripts and allow-same-origin which is invalid' + } + }, + + create(context) { + const ALLOWED_VALUES = [ + // From https://www.w3schools.com/tags/att_iframe_sandbox.asp + '', + 'allow-forms', + 'allow-modals', + 'allow-orientation-lock', + 'allow-pointer-lock', + 'allow-popups', + 'allow-popups-to-escape-sandbox', + 'allow-presentation', + 'allow-same-origin', + 'allow-scripts', + 'allow-top-navigation', + 'allow-top-navigation-by-user-activation' + ]; + + function validateSandboxAttribute(node, attribute) { + const values = attribute.value.value.split(' '); + let allowScripts = false; + let allowSameOrigin = false; + values.forEach((attributeValue) => { + const trimmedAttributeValue = attributeValue.trim(); + if (ALLOWED_VALUES.indexOf(trimmedAttributeValue) === -1) { + context.report({ + node, + messageId: 'invalidValue', + data: { + value: trimmedAttributeValue + } + }); + } + if (trimmedAttributeValue === 'allow-scripts') { + allowScripts = true; + } + if (trimmedAttributeValue === 'allow-same-origin') { + allowSameOrigin = true; + } + }); + if (allowScripts && allowSameOrigin) { + context.report({ + node, + messageId: 'invalidCombination' + }); + } + } + + return { + 'JSXOpeningElement[name.name="iframe"]'(node) { + let sandboxAttributeFound = false; + node.attributes.forEach((attribute) => { + if (attribute.type === 'JSXAttribute' + && attribute.name + && attribute.name.type === 'JSXIdentifier' + && attribute.name.name === 'sandbox' + ) { + sandboxAttributeFound = true; + if ( + attribute.value + && attribute.value.type === 'Literal' + && attribute.value.value + ) { + // Only string literals are supported for now + validateSandboxAttribute(node, attribute); + } + } + }); + if (!sandboxAttributeFound) { + context.report({ + node, + messageId: 'attributeMissing' + }); + } + } + }; + } +}; diff --git a/tests/lib/rules/iframe-missing-sandbox.js b/tests/lib/rules/iframe-missing-sandbox.js new file mode 100644 index 0000000000..50bfe6661a --- /dev/null +++ b/tests/lib/rules/iframe-missing-sandbox.js @@ -0,0 +1,83 @@ +/** + * @fileoverview TBD + */ + +'use strict'; + +// ------------------------------------------------------------------------------ +// Requirements +// ------------------------------------------------------------------------------ + +const RuleTester = require('eslint').RuleTester; +const rule = require('../../../lib/rules/iframe-missing-sandbox'); + +const parserOptions = { + ecmaVersion: 2018, + sourceType: 'module', + ecmaFeatures: { + jsx: true + } +}; + +// ------------------------------------------------------------------------------ +// Tests +// ------------------------------------------------------------------------------ + +const ruleTester = new RuleTester({parserOptions}); +ruleTester.run('iframe-missing-sandbox', rule, { + valid: [ + {code: '
;'}, + {code: ';'}, + {code: ''}, + {code: ''}, + {code: ''}, + {code: ''}, + {code: ''}, + {code: ''}, + {code: ''}, + {code: ''}, + {code: ''}, + {code: ''}, + {code: ''}, + {code: ''}, + {code: ''}, + {code: ''}, + {code: ''}, + {code: ''} + ], + invalid: [{ + code: ';', + errors: [{messageId: 'attributeMissing'}] + }, + { + code: ';', + errors: [{messageId: 'attributeMissing'}] + }, + { + code: '', + errors: [{messageId: 'invalidValue', data: {value: '__unknown__'}}] + }, + { + code: '', + errors: [{messageId: 'invalidValue', data: {value: '__unknown__'}}] + }, + { + code: '', + errors: [{messageId: 'invalidValue', data: {value: '__unknown__'}}] + }, + { + code: '', + errors: [ + {messageId: 'invalidValue', data: {value: '__unknown__'}}, + {messageId: 'invalidValue', data: {value: '__unknown__'}} + ] + }, + { + code: ';', + errors: [{messageId: 'invalidCombination'}] + }, + { + code: ';', + errors: [{messageId: 'invalidCombination'}] + }] +});