-
Notifications
You must be signed in to change notification settings - Fork 49.8k
Add trusted types to react on client side #16157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f54c1e0
Add trusted types to react on client side
f9e83d2
Implement changes according to review
384a8f0
Merge master, resolve conflicts
c338e62
Remove support for trusted URLs, change TrustedTypes to trustedTypes
1bfb1eb
Add support for deprecated trusted URLs
9b1ec8c
Apply PR suggesstions
abff150
Warn only once, remove forgotten check, put it behind a flag
eb1a123
Merge master
1e5b0bf
Move comment
245d89b
Fix PR comments
b1d6e19
Fix html toString concatenation
57e405f
Fix forgotten else branch
941ec84
Fix PR comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/react-dom/src/client/__tests__/trustedTypes-test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| describe('when Trusted Types are available in global object', () => { | ||
| let React; | ||
| let ReactDOM; | ||
|
|
||
| beforeEach(() => { | ||
| React = require('react'); | ||
| ReactDOM = require('react-dom'); | ||
| window.TrustedTypes = { | ||
| isHTML: () => true, | ||
| isScript: () => false, | ||
| isScriptURL: () => false, | ||
Siegrift marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| isURL: () => false, | ||
| }; | ||
| }); | ||
|
|
||
| it('should not stringify trusted values', () => { | ||
| const container = document.createElement('div'); | ||
| const trustedObject = {toString: () => 'I look like a trusted object'}; | ||
| class Component extends React.Component { | ||
| state = {inner: undefined}; | ||
| render() { | ||
| return <div dangerouslySetInnerHTML={{__html: this.state.inner}} />; | ||
| } | ||
| } | ||
|
|
||
| const isHTMLSpy = jest.spyOn(window.TrustedTypes, ['isHTML']); | ||
| const instace = ReactDOM.render(<Component />, container); | ||
| instace.setState({inner: trustedObject}); | ||
|
|
||
| expect(container.firstChild.innerHTML).toBe(trustedObject.toString()); | ||
| expect(isHTMLSpy).toHaveBeenCalledWith(trustedObject); | ||
| }); | ||
|
|
||
| describe('dangerouslySetInnerHTML in svg elements in Internet Explorer', () => { | ||
| let innerHTMLDescriptor; | ||
|
|
||
| // simulate svg elements in Internet Explorer which don't have 'innerHTML' property | ||
| beforeEach(() => { | ||
| innerHTMLDescriptor = Object.getOwnPropertyDescriptor( | ||
| Element.prototype, | ||
| 'innerHTML', | ||
| ); | ||
| delete Element.prototype.innerHTML; | ||
| Object.defineProperty( | ||
| HTMLDivElement.prototype, | ||
| 'innerHTML', | ||
| innerHTMLDescriptor, | ||
| ); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| delete HTMLDivElement.prototype.innerHTML; | ||
| Object.defineProperty( | ||
| Element.prototype, | ||
| 'innerHTML', | ||
| innerHTMLDescriptor, | ||
| ); | ||
| }); | ||
|
|
||
| it('should log a warning', () => { | ||
| class Component extends React.Component { | ||
| state = {inner: undefined}; | ||
| render() { | ||
| return <svg dangerouslySetInnerHTML={{__html: this.state.inner}} />; | ||
| } | ||
| } | ||
| const errorSpy = spyOnDev(console, 'error'); | ||
|
|
||
| const container = document.createElement('div'); | ||
| const instace = ReactDOM.render(<Component />, container); | ||
| instace.setState({inner: 'anyValue'}); | ||
|
|
||
| if (__DEV__) { | ||
| expect(errorSpy).toHaveBeenCalledWith( | ||
| "Warning: Using 'dangerouslySetInnerHTML' in an svg element with " + | ||
| 'Trusted Types enabled in an Internet Explorer will cause ' + | ||
| 'the trusted value to be converted to string. Assigning string ' + | ||
| "to 'innerHTML' will throw an error if Trusted Types are enforced. " + | ||
| "You can try to wrap your svg element inside a div and use 'dangerouslySetInnerHTML' " + | ||
| 'on the enclosing div instead.', | ||
| ); | ||
| } | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/react-dom/src/shared/trustedTypesAwareToString.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /** | ||
| * Copyright (c) Facebook, Inc. and its affiliates. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| * | ||
| * @flow | ||
| */ | ||
|
|
||
| /** | ||
| * Returns true only if Trusted Types are available in global object and the value | ||
| * is a trusted type. | ||
| */ | ||
| function isTrustedTypesValue(value: any) { | ||
| if (typeof window.TrustedTypes === 'undefined') { | ||
Siegrift marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return false; | ||
| } else { | ||
| return ( | ||
| window.TrustedTypes.isHTML(value) || | ||
| window.TrustedTypes.isScript(value) || | ||
| window.TrustedTypes.isScriptURL(value) || | ||
| window.TrustedTypes.isURL(value) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * We allow passing objects with toString method as element attributes or in dangerouslySetInnerHTML | ||
| * and we do validations that the value is safe. Once we do validation we want to use the validated | ||
| * value instead of the object (because object.toString may return something else on next call). | ||
| * | ||
| * If application uses Trusted Types we don't stringify trusted values, but preserve them as objects. | ||
| */ | ||
| function trustedTypesAwareToString(value: any) { | ||
Siegrift marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (isTrustedTypesValue(value)) { | ||
Siegrift marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return value; | ||
| } else { | ||
| return '' + value; | ||
| } | ||
| } | ||
|
|
||
| export default trustedTypesAwareToString; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.