-
Notifications
You must be signed in to change notification settings - Fork 48.7k
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 5 commits
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
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
88 changes: 88 additions & 0 deletions
88
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,88 @@ | ||
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
|
||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
delete window.trustedTypes; | ||
}); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* 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 | ||
*/ | ||
|
||
import type {TrustedValue} from './ToStringValue'; | ||
|
||
/** | ||
* Set attribute for a node. The attribute value can be either string or | ||
* Trusted value (if application uses Trusted Types). | ||
*/ | ||
export function setAttribute( | ||
node: Element, | ||
attributeName: string, | ||
attributeValue: string | TrustedValue, | ||
) { | ||
node.setAttribute(attributeName, (attributeValue: any)); | ||
} | ||
|
||
/** | ||
* Set attribute with namespace for a node. The attribute value can be either string or | ||
* Trusted value (if application uses Trusted Types). | ||
*/ | ||
export function setAttributeNS( | ||
node: Element, | ||
attributeNamespace: string, | ||
attributeName: string, | ||
attributeValue: string | TrustedValue, | ||
) { | ||
node.setAttributeNS(attributeNamespace, attributeName, (attributeValue: any)); | ||
} |
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
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
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.