-
Notifications
You must be signed in to change notification settings - Fork 48.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
Changes from all commits
f54c1e0
f9e83d2
384a8f0
c338e62
1bfb1eb
9b1ec8c
abff150
eb1a123
1e5b0bf
245d89b
b1d6e19
57e405f
941ec84
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -149,5 +149,6 @@ module.exports = { | |
spyOnProd: true, | ||
__PROFILE__: true, | ||
__UMD__: true, | ||
trustedTypes: true, | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
* @flow | ||
*/ | ||
|
||
import {enableTrustedTypesIntegration} from 'shared/ReactFeatureFlags'; | ||
|
||
export opaque type ToStringValue = | ||
| boolean | ||
| number | ||
|
@@ -35,3 +37,45 @@ export function getToStringValue(value: mixed): ToStringValue { | |
return ''; | ||
} | ||
} | ||
|
||
/** | ||
* Returns true only if Trusted Types are available in global object and the value is a trusted type. | ||
*/ | ||
let isTrustedTypesValue: (value: any) => boolean; | ||
// $FlowExpectedError - TrustedTypes are defined only in some browsers or with polyfill | ||
if (enableTrustedTypesIntegration && typeof trustedTypes !== 'undefined') { | ||
isTrustedTypesValue = (value: any) => | ||
trustedTypes.isHTML(value) || | ||
trustedTypes.isScript(value) || | ||
trustedTypes.isScriptURL(value) || | ||
// TrustedURLs are deprecated and will be removed soon: https://github.com/WICG/trusted-types/pull/204 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we even want to support them then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think at least for now we shouldn't. Both chrome and the polyfill currently support them and if we don't, trying out the integration will for the users will be harder (because the values will be stringified). They would need to create a default policy that would bless all URLs. |
||
(trustedTypes.isURL && trustedTypes.isURL(value)); | ||
} else { | ||
isTrustedTypesValue = () => false; | ||
} | ||
|
||
/** Trusted value is a wrapper for "safe" values which can be assigned to DOM execution sinks. */ | ||
export opaque type TrustedValue: {toString(): string, valueOf(): string} = { | ||
toString(): string, | ||
valueOf(): string, | ||
}; | ||
|
||
/** | ||
* 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. | ||
*/ | ||
export function toStringOrTrustedType(value: any): string | TrustedValue { | ||
if ( | ||
enableTrustedTypesIntegration && | ||
Siegrift marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// fast-path string values as it's most frequent usage of the function | ||
typeof value !== 'string' && | ||
isTrustedTypesValue(value) | ||
) { | ||
return value; | ||
} else { | ||
return '' + value; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
describe('when Trusted Types are available in global object', () => { | ||
let React; | ||
let ReactDOM; | ||
let ReactFeatureFlags; | ||
let container; | ||
|
||
beforeEach(() => { | ||
container = document.createElement('div'); | ||
window.trustedTypes = { | ||
isHTML: () => true, | ||
isScript: () => false, | ||
isScriptURL: () => false, | ||
}; | ||
ReactFeatureFlags = require('shared/ReactFeatureFlags'); | ||
ReactFeatureFlags.enableTrustedTypesIntegration = true; | ||
React = require('react'); | ||
ReactDOM = require('react-dom'); | ||
}); | ||
|
||
afterEach(() => { | ||
delete window.trustedTypes; | ||
ReactFeatureFlags.enableTrustedTypesIntegration = false; | ||
}); | ||
|
||
it('should not stringify trusted values', () => { | ||
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 instance = ReactDOM.render(<Component />, container); | ||
instance.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 { | ||
render() { | ||
return <svg dangerouslySetInnerHTML={{__html: 'unsafe html'}} />; | ||
} | ||
} | ||
expect(() => { | ||
ReactDOM.render(<Component />, container); | ||
}).toWarnDev( | ||
"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.', | ||
); | ||
}); | ||
}); | ||
|
||
it('should warn once when rendering script tag in jsx on client', () => { | ||
expect(() => { | ||
ReactDOM.render(<script>alert("I am not executed")</script>, container); | ||
}).toWarnDev( | ||
'Warning: Encountered a script tag while rendering React component. ' + | ||
'Scripts inside React components are never executed when rendering ' + | ||
'on the client. Consider using template tag instead ' + | ||
'(https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template).\n' + | ||
' in script (at **)', | ||
); | ||
|
||
// check that the warning is print only once | ||
ReactDOM.render(<script>alert("I am not executed")</script>, container); | ||
}); | ||
}); |
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)); | ||
} |
Uh oh!
There was an error while loading. Please reload this page.