Skip to content

Verify script enforcement applies even if the source text is modified before require-trusted-types-for 'script' is set.#56548

Merged
fred-wang merged 1 commit intomasterfrom
script-enforcement-modified-before-tt-enabled
Dec 8, 2025
Merged

Verify script enforcement applies even if the source text is modified before require-trusted-types-for 'script' is set.#56548
fred-wang merged 1 commit intomasterfrom
script-enforcement-modified-before-tt-enabled

Conversation

@fred-wang
Copy link
Copy Markdown
Contributor

Current spec [1] essentially caches the "script text" associated to a script element: it is initially empty and updated by various APIs calls. The "prepare the script element" algorithm is modified [2], so that "child text content" would go through the default policy if it does not match the cached "script text" [3].

Script enforcement could be alternatively be implemented by flags [4]. In that case, it might be tempting not to update the flags when the API calls modifying a script are performed before Trusted Types are actually enforced, with the rationale that these API calls are not considered untrusted at that time. For a cache-based implementations, this would be equivalent to not caching the "script text" until the first time it is set in a context when Trusted Types is enforced.

However, WebKit and Chromium follow the spec and really try and run the default policy on the script text, even if the script text was modified before TrustedTypes enforcement got enabled. This PR adds a test to verify this behavior for HTML and SVG scripts.

[1] https://w3c.github.io/trusted-types/dist/spec/#enforcement-in-scripts
[2] https://w3c.github.io/trusted-types/dist/spec/#slot-value-verification
[3] https://w3c.github.io/trusted-types/dist/spec/#prepare-the-script-text
[4] w3c/trusted-types#579

@fred-wang
Copy link
Copy Markdown
Contributor Author

fred-wang commented Dec 6, 2025

cc @smaug---- @otherdaniel

@smaug----
Copy link
Copy Markdown
Contributor

Yeah, the spec is a bit silly here. If the script was modified before require-trusted-types-for, why would it be considered not-trusted. But perhaps not worth to change it now.

@fred-wang
Copy link
Copy Markdown
Contributor Author

Yeah, the spec is a bit silly here. If the script was modified before require-trusted-types-for, why would it be considered not-trusted. But perhaps not worth to change it now.

On the other hand, when the require-trusted-types-for 'script' is initially present but removed before insertion, then I believe we would not run the default policy (whether or not we decide to check the CSP when modifying the script text). So there is some kind of consistency to only check the CSP just before running the script.

@fred-wang
Copy link
Copy Markdown
Contributor Author

Yeah, the spec is a bit silly here. If the script was modified before require-trusted-types-for, why would it be considered not-trusted. But perhaps not worth to change it now.

On the other hand, when the require-trusted-types-for 'script' is initially present but removed before insertion, then I believe we would not run the default policy (whether or not we decide to check the CSP when modifying the script text). So there is some kind of consistency to only check the CSP just before running the script.

(Probably I should add a similar test when the require-trusted-types-for is initially present and removed)

@fred-wang fred-wang force-pushed the script-enforcement-modified-before-tt-enabled branch from 4a574b1 to c1bbeb1 Compare December 8, 2025 08:44
@fred-wang
Copy link
Copy Markdown
Contributor Author

Yeah, the spec is a bit silly here. If the script was modified before require-trusted-types-for, why would it be considered not-trusted. But perhaps not worth to change it now.

On the other hand, when the require-trusted-types-for 'script' is initially present but removed before insertion, then I believe we would not run the default policy (whether or not we decide to check the CSP when modifying the script text). So there is some kind of consistency to only check the CSP just before running the script.

(Probably I should add a similar test when the require-trusted-types-for is initially present and removed)

OK, I stand corrected. If we remove the require-trusted-types-for meta tag then TT enforcement still applies per https://www.w3.org/TR/CSP3/#meta-element ; I added tests to verify that.

Copy link
Copy Markdown
Member

@lukewarlow lukewarlow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test for module scripts too, just in case they're processed differently to classic ones?

… before require-trusted-types-for 'script' is set.

Current spec [1] essentially caches the "script text" associated to a
script element: it is initially empty and updated by various APIs calls.
The "prepare the script element" algorithm is modified [2], so that
"child text content" would go through the default policy if it does
not match the cached "script text" [3].

Script enforcement could be alternatively be implemented by flags [4].
In that case, it might be tempting not to update the flags when the API
calls modifying a script are performed before Trusted Types are
actually enforced, with the rationale that these API calls are not
considered untrusted at that time. For a cache-based implementations,
this would be equivalent to not caching the "script text" until the
first time it is set in a context when Trusted Types is enforced.

However, WebKit and Chromium follow the spec and really try and run
the default policy on the script text, even if the script text was
modified before TrustedTypes enforcement got enabled. This PR adds
tests to verify this behavior for HTML and SVG scripts.

For completeness, this also verifies that if we remove the
require-trusted-types-for meta tag before the test is executed, then
TrustedTypes enforcement remains enabled per [5].

[1] https://w3c.github.io/trusted-types/dist/spec/#enforcement-in-scripts
[2] https://w3c.github.io/trusted-types/dist/spec/#slot-value-verification
[3] https://w3c.github.io/trusted-types/dist/spec/#prepare-the-script-text
[4] w3c/trusted-types#579
[5] https://www.w3.org/TR/CSP3/#meta-element
@fred-wang fred-wang force-pushed the script-enforcement-modified-before-tt-enabled branch from c1bbeb1 to 0834c38 Compare December 8, 2025 12:51
@fred-wang fred-wang merged commit 6981356 into master Dec 8, 2025
24 checks passed
@fred-wang fred-wang deleted the script-enforcement-modified-before-tt-enabled branch December 8, 2025 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants