GeezerVison is showing github.com — Add trusted types to react on client side (#16157) · react/react@b8d079b
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .eslintrc.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -149,5 +149,6 @@ module.exports = { | 149 | 149 | spyOnProd: true, | 150 | 150 | __PROFILE__: true, | 151 | 151 | __UMD__: true, | | 152 | + trustedTypes: true, | 152 | 153 | }, | 153 | 154 | }; |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/DOMPropertyOperations.js +9-5Lines changed: 9 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,7 +16,9 @@ import { | 16 | 16 | OVERLOADED_BOOLEAN, | 17 | 17 | } from '../shared/DOMProperty'; | 18 | 18 | import sanitizeURL from '../shared/sanitizeURL'; | | 19 | +import {toStringOrTrustedType} from './ToStringValue'; | 19 | 20 | import {disableJavaScriptURLs} from 'shared/ReactFeatureFlags'; | | 21 | +import {setAttribute, setAttributeNS} from './setAttribute'; | 20 | 22 |
| 21 | 23 | import type {PropertyInfo} from '../shared/DOMProperty'; | 22 | 24 |
| @@ -142,7 +144,7 @@ export function setValueForProperty( | 142 | 144 | if (value === null) { | 143 | 145 | node.removeAttribute(attributeName); | 144 | 146 | } else { | 145 | | - node.setAttribute(attributeName, '' + (value: any)); | | 147 | + setAttribute(node, attributeName, toStringOrTrustedType(value)); | 146 | 148 | } | 147 | 149 | } | 148 | 150 | return; | @@ -168,19 +170,21 @@ export function setValueForProperty( | 168 | 170 | const {type} = propertyInfo; | 169 | 171 | let attributeValue; | 170 | 172 | if (type === BOOLEAN || (type === OVERLOADED_BOOLEAN && value === true)) { | | 173 | + // If attribute type is boolean, we know for sure it won't be an execution sink | | 174 | + // and we won't require Trusted Type here. | 171 | 175 | attributeValue = ''; | 172 | 176 | } else { | 173 | 177 | // `setAttribute` with objects becomes only `[object]` in IE8/9, | 174 | 178 | // ('' + value) makes it output the correct toString()-value. | 175 | | - attributeValue = '' + (value: any); | | 179 | + attributeValue = toStringOrTrustedType(value); | 176 | 180 | if (propertyInfo.sanitizeURL) { | 177 | | - sanitizeURL(attributeValue); | | 181 | + sanitizeURL(attributeValue.toString()); | 178 | 182 | } | 179 | 183 | } | 180 | 184 | if (attributeNamespace) { | 181 | | - node.setAttributeNS(attributeNamespace, attributeName, attributeValue); | | 185 | + setAttributeNS(node, attributeNamespace, attributeName, attributeValue); | 182 | 186 | } else { | 183 | | - node.setAttribute(attributeName, attributeValue); | | 187 | + setAttribute(node, attributeName, attributeValue); | 184 | 188 | } | 185 | 189 | } | 186 | 190 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ReactDOMComponent.js +22-2Lines changed: 22 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -85,11 +85,16 @@ import possibleStandardNames from '../shared/possibleStandardNames'; | 85 | 85 | import {validateProperties as validateARIAProperties} from '../shared/ReactDOMInvalidARIAHook'; | 86 | 86 | import {validateProperties as validateInputProperties} from '../shared/ReactDOMNullInputValuePropHook'; | 87 | 87 | import {validateProperties as validateUnknownProperties} from '../shared/ReactDOMUnknownPropertyHook'; | | 88 | +import {toStringOrTrustedType} from './ToStringValue'; | 88 | 89 |
| 89 | | -import {enableFlareAPI} from 'shared/ReactFeatureFlags'; | | 90 | +import { | | 91 | + enableFlareAPI, | | 92 | + enableTrustedTypesIntegration, | | 93 | +} from 'shared/ReactFeatureFlags'; | 90 | 94 |
| 91 | 95 | let didWarnInvalidHydration = false; | 92 | 96 | let didWarnShadyDOM = false; | | 97 | +let didWarnScriptTags = false; | 93 | 98 |
| 94 | 99 | const DANGEROUSLY_SET_INNER_HTML = 'dangerouslySetInnerHTML'; | 95 | 100 | const SUPPRESS_CONTENT_EDITABLE_WARNING = 'suppressContentEditableWarning'; | @@ -422,6 +427,18 @@ export function createElement( | 422 | 427 | // Create the script via .innerHTML so its "parser-inserted" flag is | 423 | 428 | // set to true and it does not execute | 424 | 429 | const div = ownerDocument.createElement('div'); | | 430 | + if (__DEV__) { | | 431 | + if (enableTrustedTypesIntegration && !didWarnScriptTags) { | | 432 | + warning( | | 433 | + false, | | 434 | + 'Encountered a script tag while rendering React component. ' + | | 435 | + 'Scripts inside React components are never executed when rendering ' + | | 436 | + 'on the client. Consider using template tag instead ' + | | 437 | + '(https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template).', | | 438 | + ); | | 439 | + didWarnScriptTags = true; | | 440 | + } | | 441 | + } | 425 | 442 | div.innerHTML = '<script><' + '/script>'; // eslint-disable-line | 426 | 443 | // This is guaranteed to yield a script element. | 427 | 444 | const firstChild = ((div.firstChild: any): HTMLScriptElement); | @@ -776,7 +793,10 @@ export function diffProperties( | 776 | 793 | const lastHtml = lastProp ? lastProp[HTML] : undefined; | 777 | 794 | if (nextHtml != null) { | 778 | 795 | if (lastHtml !== nextHtml) { | 779 | | - (updatePayload = updatePayload || []).push(propKey, '' + nextHtml); | | 796 | + (updatePayload = updatePayload || []).push( | | 797 | + propKey, | | 798 | + toStringOrTrustedType(nextHtml), | | 799 | + ); | 780 | 800 | } | 781 | 801 | } else { | 782 | 802 | // TODO: It might be too late to clear this if we have children | |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/ToStringValue.js +44Lines changed: 44 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | * @flow | 8 | 8 | */ | 9 | 9 |
| | 10 | +import {enableTrustedTypesIntegration} from 'shared/ReactFeatureFlags'; | | 11 | + | 10 | 12 | export opaque type ToStringValue = | 11 | 13 | | boolean | 12 | 14 | | number | @@ -35,3 +37,45 @@ export function getToStringValue(value: mixed): ToStringValue { | 35 | 37 | return ''; | 36 | 38 | } | 37 | 39 | } | | 40 | + | | 41 | +/** | | 42 | + * Returns true only if Trusted Types are available in global object and the value is a trusted type. | | 43 | + */ | | 44 | +let isTrustedTypesValue: (value: any) => boolean; | | 45 | +// $FlowExpectedError - TrustedTypes are defined only in some browsers or with polyfill | | 46 | +if (enableTrustedTypesIntegration && typeof trustedTypes !== 'undefined') { | | 47 | + isTrustedTypesValue = (value: any) => | | 48 | + trustedTypes.isHTML(value) || | | 49 | + trustedTypes.isScript(value) || | | 50 | + trustedTypes.isScriptURL(value) || | | 51 | + // TrustedURLs are deprecated and will be removed soon: https://github.com/WICG/trusted-types/pull/204 | | 52 | + (trustedTypes.isURL && trustedTypes.isURL(value)); | | 53 | +} else { | | 54 | + isTrustedTypesValue = () => false; | | 55 | +} | | 56 | + | | 57 | +/** Trusted value is a wrapper for "safe" values which can be assigned to DOM execution sinks. */ | | 58 | +export opaque type TrustedValue: {toString(): string, valueOf(): string} = { | | 59 | + toString(): string, | | 60 | + valueOf(): string, | | 61 | +}; | | 62 | + | | 63 | +/** | | 64 | + * We allow passing objects with toString method as element attributes or in dangerouslySetInnerHTML | | 65 | + * and we do validations that the value is safe. Once we do validation we want to use the validated | | 66 | + * value instead of the object (because object.toString may return something else on next call). | | 67 | + * | | 68 | + * If application uses Trusted Types we don't stringify trusted values, but preserve them as objects. | | 69 | + */ | | 70 | +export function toStringOrTrustedType(value: any): string | TrustedValue { | | 71 | + if ( | | 72 | + enableTrustedTypesIntegration && | | 73 | + // fast-path string values as it's most frequent usage of the function | | 74 | + typeof value !== 'string' && | | 75 | + isTrustedTypesValue(value) | | 76 | + ) { | | 77 | + return value; | | 78 | + } else { | | 79 | + return '' + value; | | 80 | + } | | 81 | +} |
Collapse file Copy file name to clipboard +101Lines changed: 101 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +describe('when Trusted Types are available in global object', () => { | | 2 | + let React; | | 3 | + let ReactDOM; | | 4 | + let ReactFeatureFlags; | | 5 | + let container; | | 6 | + | | 7 | + beforeEach(() => { | | 8 | + container = document.createElement('div'); | | 9 | + window.trustedTypes = { | | 10 | + isHTML: () => true, | | 11 | + isScript: () => false, | | 12 | + isScriptURL: () => false, | | 13 | + }; | | 14 | + ReactFeatureFlags = require('shared/ReactFeatureFlags'); | | 15 | + ReactFeatureFlags.enableTrustedTypesIntegration = true; | | 16 | + React = require('react'); | | 17 | + ReactDOM = require('react-dom'); | | 18 | + }); | | 19 | + | | 20 | + afterEach(() => { | | 21 | + delete window.trustedTypes; | | 22 | + ReactFeatureFlags.enableTrustedTypesIntegration = false; | | 23 | + }); | | 24 | + | | 25 | + it('should not stringify trusted values', () => { | | 26 | + const trustedObject = {toString: () => 'I look like a trusted object'}; | | 27 | + class Component extends React.Component { | | 28 | + state = {inner: undefined}; | | 29 | + render() { | | 30 | + return <div dangerouslySetInnerHTML={{__html: this.state.inner}} />; | | 31 | + } | | 32 | + } | | 33 | + | | 34 | + const isHTMLSpy = jest.spyOn(window.trustedTypes, ['isHTML']); | | 35 | + const instance = ReactDOM.render(<Component />, container); | | 36 | + instance.setState({inner: trustedObject}); | | 37 | + | | 38 | + expect(container.firstChild.innerHTML).toBe(trustedObject.toString()); | | 39 | + expect(isHTMLSpy).toHaveBeenCalledWith(trustedObject); | | 40 | + }); | | 41 | + | | 42 | + describe('dangerouslySetInnerHTML in svg elements in Internet Explorer', () => { | | 43 | + let innerHTMLDescriptor; | | 44 | + | | 45 | + // simulate svg elements in Internet Explorer which don't have 'innerHTML' property | | 46 | + beforeEach(() => { | | 47 | + innerHTMLDescriptor = Object.getOwnPropertyDescriptor( | | 48 | + Element.prototype, | | 49 | + 'innerHTML', | | 50 | + ); | | 51 | + delete Element.prototype.innerHTML; | | 52 | + Object.defineProperty( | | 53 | + HTMLDivElement.prototype, | | 54 | + 'innerHTML', | | 55 | + innerHTMLDescriptor, | | 56 | + ); | | 57 | + }); | | 58 | + | | 59 | + afterEach(() => { | | 60 | + delete HTMLDivElement.prototype.innerHTML; | | 61 | + Object.defineProperty( | | 62 | + Element.prototype, | | 63 | + 'innerHTML', | | 64 | + innerHTMLDescriptor, | | 65 | + ); | | 66 | + }); | | 67 | + | | 68 | + it('should log a warning', () => { | | 69 | + class Component extends React.Component { | | 70 | + render() { | | 71 | + return <svg dangerouslySetInnerHTML={{__html: 'unsafe html'}} />; | | 72 | + } | | 73 | + } | | 74 | + expect(() => { | | 75 | + ReactDOM.render(<Component />, container); | | 76 | + }).toWarnDev( | | 77 | + "Warning: Using 'dangerouslySetInnerHTML' in an svg element with " + | | 78 | + 'Trusted Types enabled in an Internet Explorer will cause ' + | | 79 | + 'the trusted value to be converted to string. Assigning string ' + | | 80 | + "to 'innerHTML' will throw an error if Trusted Types are enforced. " + | | 81 | + "You can try to wrap your svg element inside a div and use 'dangerouslySetInnerHTML' " + | | 82 | + 'on the enclosing div instead.', | | 83 | + ); | | 84 | + }); | | 85 | + }); | | 86 | + | | 87 | + it('should warn once when rendering script tag in jsx on client', () => { | | 88 | + expect(() => { | | 89 | + ReactDOM.render(<script>alert("I am not executed")</script>, container); | | 90 | + }).toWarnDev( | | 91 | + 'Warning: Encountered a script tag while rendering React component. ' + | | 92 | + 'Scripts inside React components are never executed when rendering ' + | | 93 | + 'on the client. Consider using template tag instead ' + | | 94 | + '(https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template).\n' + | | 95 | + ' in script (at **)', | | 96 | + ); | | 97 | + | | 98 | + // check that the warning is print only once | | 99 | + ReactDOM.render(<script>alert("I am not executed")</script>, container); | | 100 | + }); | | 101 | +}); |
Collapse file Copy file name to clipboard +35Lines changed: 35 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/** | | 2 | + * Copyright (c) Facebook, Inc. and its affiliates. | | 3 | + * | | 4 | + * This source code is licensed under the MIT license found in the | | 5 | + * LICENSE file in the root directory of this source tree. | | 6 | + * | | 7 | + * @flow | | 8 | + */ | | 9 | + | | 10 | +import type {TrustedValue} from './ToStringValue'; | | 11 | + | | 12 | +/** | | 13 | + * Set attribute for a node. The attribute value can be either string or | | 14 | + * Trusted value (if application uses Trusted Types). | | 15 | + */ | | 16 | +export function setAttribute( | | 17 | + node: Element, | | 18 | + attributeName: string, | | 19 | + attributeValue: string | TrustedValue, | | 20 | +) { | | 21 | + node.setAttribute(attributeName, (attributeValue: any)); | | 22 | +} | | 23 | + | | 24 | +/** | | 25 | + * Set attribute with namespace for a node. The attribute value can be either string or | | 26 | + * Trusted value (if application uses Trusted Types). | | 27 | + */ | | 28 | +export function setAttributeNS( | | 29 | + node: Element, | | 30 | + attributeNamespace: string, | | 31 | + attributeName: string, | | 32 | + attributeValue: string | TrustedValue, | | 33 | +) { | | 34 | + node.setAttributeNS(attributeNamespace, attributeName, (attributeValue: any)); | | 35 | +} |
Collapse file Copy file name to clipboardExpand all lines: packages/react-dom/src/client/setInnerHTML.js +31-12Lines changed: 31 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 9 | 9 |
| 10 | 10 | import {Namespaces} from '../shared/DOMNamespaces'; | 11 | 11 | import createMicrosoftUnsafeLocalFunction from '../shared/createMicrosoftUnsafeLocalFunction'; | | 12 | +import warning from 'shared/warning'; | | 13 | +import type {TrustedValue} from './ToStringValue'; | | 14 | +import {enableTrustedTypesIntegration} from 'shared/ReactFeatureFlags'; | 12 | 15 |
| 13 | 16 | // SVG temp container for IE lacking innerHTML | 14 | 17 | let reusableSVGContainer; | @@ -22,25 +25,41 @@ let reusableSVGContainer; | 22 | 25 | */ | 23 | 26 | const setInnerHTML = createMicrosoftUnsafeLocalFunction(function( | 24 | 27 | node: Element, | 25 | | - html: string, | | 28 | + html: string | TrustedValue, | 26 | 29 | ): void { | 27 | 30 | // IE does not have innerHTML for SVG nodes, so instead we inject the | 28 | 31 | // new markup in a temp node and then move the child nodes across into | 29 | 32 | // the target node | 30 | | - | 31 | | - if (node.namespaceURI === Namespaces.svg && !('innerHTML' in node)) { | 32 | | - reusableSVGContainer = | 33 | | - reusableSVGContainer || document.createElement('div'); | 34 | | - reusableSVGContainer.innerHTML = '<svg>' + html + '</svg>'; | 35 | | - const svgNode = reusableSVGContainer.firstChild; | 36 | | - while (node.firstChild) { | 37 | | - node.removeChild(node.firstChild); | | 33 | + if (node.namespaceURI === Namespaces.svg) { | | 34 | + if (enableTrustedTypesIntegration && __DEV__) { | | 35 | + warning( | | 36 | + // $FlowExpectedError - trustedTypes are defined only in some browsers or with polyfill | | 37 | + typeof trustedTypes === 'undefined', | | 38 | + "Using 'dangerouslySetInnerHTML' in an svg element with " + | | 39 | + 'Trusted Types enabled in an Internet Explorer will cause ' + | | 40 | + 'the trusted value to be converted to string. Assigning string ' + | | 41 | + "to 'innerHTML' will throw an error if Trusted Types are enforced. " + | | 42 | + "You can try to wrap your svg element inside a div and use 'dangerouslySetInnerHTML' " + | | 43 | + 'on the enclosing div instead.', | | 44 | + ); | 38 | 45 | } | 39 | | - while (svgNode.firstChild) { | 40 | | - node.appendChild(svgNode.firstChild); | | 46 | + if (!('innerHTML' in node)) { | | 47 | + reusableSVGContainer = | | 48 | + reusableSVGContainer || document.createElement('div'); | | 49 | + reusableSVGContainer.innerHTML = | | 50 | + '<svg>' + html.valueOf().toString() + '</svg>'; | | 51 | + const svgNode = reusableSVGContainer.firstChild; | | 52 | + while (node.firstChild) { | | 53 | + node.removeChild(node.firstChild); | | 54 | + } | | 55 | + while (svgNode.firstChild) { | | 56 | + node.appendChild(svgNode.firstChild); | | 57 | + } | | 58 | + } else { | | 59 | + node.innerHTML = (html: any); | 41 | 60 | } | 42 | 61 | } else { | 43 | | - node.innerHTML = html; | | 62 | + node.innerHTML = (html: any); | 44 | 63 | } | 45 | 64 | }); | 46 | 65 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/ReactFeatureFlags.js +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -100,3 +100,5 @@ export const warnAboutStringRefs = false; | 100 | 100 | export const disableLegacyContext = false; | 101 | 101 |
| 102 | 102 | export const disableSchedulerTimeoutBasedOnReactExpirationTime = false; | | 103 | + | | 104 | +export const enableTrustedTypesIntegration = false; |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.native-fb.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -43,6 +43,7 @@ export const warnAboutDefaultPropsOnFunctionComponents = false; | 43 | 43 | export const warnAboutStringRefs = false; | 44 | 44 | export const disableLegacyContext = false; | 45 | 45 | export const disableSchedulerTimeoutBasedOnReactExpirationTime = false; | | 46 | +export const enableTrustedTypesIntegration = false; | 46 | 47 |
| 47 | 48 | // Only used in www builds. | 48 | 49 | export function addUserTimingListener() { | |
Collapse file Copy file name to clipboardExpand all lines: packages/shared/forks/ReactFeatureFlags.native-oss.js +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,6 +38,7 @@ export const warnAboutDefaultPropsOnFunctionComponents = false; | 38 | 38 | export const warnAboutStringRefs = false; | 39 | 39 | export const disableLegacyContext = false; | 40 | 40 | export const disableSchedulerTimeoutBasedOnReactExpirationTime = false; | | 41 | +export const enableTrustedTypesIntegration = false; | 41 | 42 |
| 42 | 43 | // Only used in www builds. | 43 | 44 | export function addUserTimingListener() { | |
|