Skip to content

Commit

Permalink
Fixed a bug with illegal invocation for Trusted Types (facebook#17083)
Browse files Browse the repository at this point in the history
* Fixed a bug with illegal invocation.

* Fixed the test.
  • Loading branch information
koto authored and gaearon committed Oct 15, 2019
1 parent a8c6a1b commit fdba0e5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
11 changes: 5 additions & 6 deletions packages/react-dom/src/client/ToStringValue.js
Expand Up @@ -53,15 +53,14 @@ export opaque type TrustedValue: {toString(): string, valueOf(): string} = {
*/
export let toStringOrTrustedType: any => string | TrustedValue = toString;
if (enableTrustedTypesIntegration && typeof trustedTypes !== 'undefined') {
const isHTML = trustedTypes.isHTML;
const isScript = trustedTypes.isScript;
const isScriptURL = trustedTypes.isScriptURL;
// TrustedURLs are deprecated and will be removed soon: https://github.com/WICG/trusted-types/pull/204
const isURL = trustedTypes.isURL ? trustedTypes.isURL : value => false;
toStringOrTrustedType = value => {
if (
typeof value === 'object' &&
(isHTML(value) || isScript(value) || isScriptURL(value) || isURL(value))
(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 */
(trustedTypes.isURL && trustedTypes.isURL(value)))
) {
// Pass Trusted Types through.
return value;
Expand Down
Expand Up @@ -22,7 +22,12 @@ describe('when Trusted Types are available in global object', () => {
container = document.createElement('div');
const fakeTTObjects = new Set();
window.trustedTypes = {
isHTML: value => fakeTTObjects.has(value),
isHTML: function(value) {
if (this !== window.trustedTypes) {
throw new Error(this);
}
return fakeTTObjects.has(value);
},
isScript: () => false,
isScriptURL: () => false,
};
Expand Down

0 comments on commit fdba0e5

Please sign in to comment.