-
Notifications
You must be signed in to change notification settings - Fork 46.8k
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
Follow-up to initial Trusted Types support #16795
Merged
Merged
Changes from all commits
Commits
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 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 |
---|---|---|
|
@@ -38,22 +38,6 @@ export function getToStringValue(value: mixed): ToStringValue { | |
} | ||
} | ||
|
||
/** | ||
* 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 | ||
(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, | ||
|
@@ -67,15 +51,21 @@ export opaque type TrustedValue: {toString(): string, valueOf(): string} = { | |
* | ||
* 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 && | ||
// fast-path string values as it's most frequent usage of the function | ||
typeof value !== 'string' && | ||
isTrustedTypesValue(value) | ||
) { | ||
return value; | ||
} else { | ||
return '' + value; | ||
} | ||
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)) | ||
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. This fails with Illegal invocation error (is* functions need to be bound to the factory i.e. |
||
) { | ||
// Pass Trusted Types through. | ||
return value; | ||
} | ||
return toString(value); | ||
}; | ||
} |
This file contains 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 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like it would make this function not inlinable if the flag is on or dynamic which isn’t great because it used to be a simple
’’ +
that gets inlined.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That’s actually what got me looking at it in the first place — it wasn’t getting inlined with a dynamic flag in FB bundle. Lemme think about this more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the flag is on, it's already not being inlined, even before this PR. Probably because the body is bigger. Since we're doing a function call either way, I think a smaller function (as in this PR) would be better than a larger one. It's also a single call (in this PR) rather than two calls (as in before).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to get this in because it's not being inlined either way with the flag on. If we want to fix this, we'll have to inline it manually at concrete callsites.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we toString in user space instead of letting the browser do it again?
Seems like we only need it in some IE legacy thing and/or for the
javascript:
URL check so we should only ever do this at all in those cases.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's for IE9 I think? We could feature-test it I guess.