Closed Bug 1928932 Opened 1 year ago Closed 11 months ago

Implement Trusted Types enforcement for scripts

Categories

(Core :: DOM: Security, task)

task

Tracking

()

RESOLVED FIXED
144 Branch
Tracking Status
firefox144 --- fixed

People

(Reporter: fwang, Assigned: fwang)

References

Details

(Whiteboard: [domsecurity-active])

Attachments

(6 files, 1 obsolete file)

We also need a similar mechanism for SVG script element, but it's not in the spec yet: https://github.com/w3c/trusted-types/issues/483

Luke had commented the implementation would be easier with https://github.com/w3c/trusted-types/pull/579 so I will give it a try.

Assignee: nobody → fwang
Depends on: 1968383
Attachment #9488322 - Attachment description: WIP: Bug 1928932 - Implement Trusted Types enforcement for scripts. → WIP: Bug 1928932 - Part 1 - Add an mIsTrusted flag on script elements.
Depends on: 1970336
Attachment #9488322 - Attachment is obsolete: true
Attachment #9491954 - Attachment description: WIP: Bug 1928932 - Part 1 - Add an mIsTrusted flag on script elements. → Bug 1928932 - Part 1 - Add an mIsTrusted flag on script elements. r=smaug
Attachment #9491716 - Attachment description: WIP: Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. → Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug
Attachment #9491954 - Attachment description: Bug 1928932 - Part 1 - Add an mIsTrusted flag on script elements. r=smaug → WIP: Bug 1928932 - Part 1 - Add an mIsTrusted flag on script elements. r=smaug
Attachment #9491954 - Attachment description: WIP: Bug 1928932 - Part 1 - Add an mIsTrusted flag on script elements. r=smaug → Bug 1928932 - Part 1 - Add an mIsTrusted flag on script elements. r=smaug
Attachment #9491716 - Attachment description: Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug → WIP: Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug
Attachment #9494580 - Attachment description: Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug → WIP: Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug
Attachment #9494581 - Attachment description: Bug 1928932 - Part 4 - Update WPT tests for script enforcements. r=smaug → WIP: Bug 1928932 - Part 4 - Update WPT tests for script enforcements. r=smaug
Attachment #9494581 - Attachment description: WIP: Bug 1928932 - Part 4 - Update WPT tests for script enforcements. r=smaug → Bug 1928932 - Part 4 - Update WPT tests for script enforcements. r=smaug
Attachment #9491716 - Attachment description: WIP: Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug → Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug
Attachment #9494580 - Attachment description: WIP: Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug → Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug
Depends on: 1972280
Attachment #9491716 - Attachment description: Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug → WIP: Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug
Attachment #9494580 - Attachment description: Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug → WIP: Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug
Attachment #9491716 - Attachment description: WIP: Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug → Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug
Keywords: leave-open
Pushed by fwang@igalia.com: https://github.com/mozilla-firefox/firefox/commit/ce2493cdd420 https://hg.mozilla.org/integration/autoland/rev/8f6d960e567b Part 1 - Add an mIsTrusted flag on script elements. r=smaug,credential-management-reviewers,dimi
Attachment #9491716 - Attachment description: Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug → WIP: Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug
Pushed by csabou@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/a15f457eb620 https://hg.mozilla.org/integration/autoland/rev/307d09ef826c Revert "Bug 1928932 - Part 1 - Add an mIsTrusted flag on script elements. r=smaug,credential-management-reviewers,dimi" for causing macOS build bustages on nsMenuGroupOwnerX.mm

Backed out for causing macOS build bustages on nsMenuGroupOwnerX.mm

Push with failures

Failure log

Backout link

Flags: needinfo?(fwang)
Pushed by fwang@igalia.com: https://github.com/mozilla-firefox/firefox/commit/4cca1cbaa379 https://hg.mozilla.org/integration/autoland/rev/bb51e4ab0800 Part 1 - Add an mIsTrusted flag on script elements. r=smaug,credential-management-reviewers,dimi
Flags: needinfo?(fwang)
Attachment #9494581 - Attachment description: Bug 1928932 - Part 4 - Update WPT tests for script enforcements. r=smaug → WIP: Bug 1928932 - Part 4 - Update WPT tests for script enforcements. r=smaug
Attachment #9494580 - Attachment description: WIP: Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug → Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug
Attachment #9494581 - Attachment description: WIP: Bug 1928932 - Part 4 - Update WPT tests for script enforcements. r=smaug → Bug 1928932 - Part 4 - Update WPT tests for script enforcements. r=smaug
Attachment #9494580 - Attachment description: Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug → WIP: Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug

This is a testcase trying to set innerText/textContent on a script via

HTMLElement.prototype.__lookupSetter__("innerText").call(scriptElement, ...)
Node.prototype.__lookupSetter__("textContent").call(scriptElement, ...)

In WebKit, this is always causing a TT security violation.
In Chromium, it is only causing a TT security violation if the specified text is untrusted. And if it is trusted, the script runs normally.

(In reply to Frédéric Wang (:fredw) from comment #16)

In WebKit, this is always causing a TT security violation.
In Chromium, it is only causing a TT security violation if the specified text is untrusted. And if it is trusted, the script runs normally.

After some checks & discussions, WebKit's behavior is the one aligned with the spec here. I'm adding new tests here: https://github.com/web-platform-tests/wpt/pull/54548

Attachment #9494580 - Attachment description: WIP: Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug → Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug
See Also: → 1985583
Depends on: 1985570
Attachment #9491716 - Attachment description: WIP: Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug → Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug
Attachment #9494580 - Attachment description: Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug → WIP: Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug
Attachment #9491716 - Attachment description: Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug → WIP: Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug
Attachment #9491716 - Attachment description: WIP: Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug → Bug 1928932 - Part 2 - Implement enforcements for untrusted script elements. r=smaug
Attachment #9494580 - Attachment description: WIP: Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug → Bug 1928932 - Part 3 - Transmit proper MutationEffectOnScript to mutation observers. r=smaug
Status: NEW → RESOLVED
Closed: 11 months ago
Keywords: leave-open
Resolution: --- → FIXED
Regressions: 1987801
Target Milestone: --- → 144 Branch

Can I please confirm what this delivers, because it isn't simple for me to read the code.

From the title I assume that this adds support for Content-Security-Policy: require-trusted-types-for right?
Presumably enabled behind the same preference as the trusted types used in API?

Flags: needinfo?(fwang)

It's indeed behind the TrustedTypes flag.

Support for require-trusted-types-for 'script' was implemented in previous patches. This CSP rule basically just enables Trusted Types checks so that things like

let script = document.createElement("script");
script.text = "/* arbitrary JS code */";

would cause a security violation (or run the default policy on the JS string, if such a policy defined) when we try to set the script text.

Other APIs that can be used to set the script's text, but they don't necessarily run a the Trusted Type check. The patch in this bug implements adds such a check when the script is about to be executed i.e.

let script = document.createElement("script");
script.appendChild(document.createTextNode("/* arbitrary JS code */"));
document.body.appendChild(script);

would cause a security violation (or again, run the default policy) when we append the script to the document.

Flags: needinfo?(fwang)

Thanks Fred.

So in the first case I can avoid a security violation by passing a TrustedScript or having a default policy - right?

let script = document.createElement("script");
script.text = someTrustedType; // A TrustedScript

In the second case for which this patch exists is there a way OTHER than using a default policy to pass a trusted type to the document.createTextNode()?

My assumption is "no" based on https://w3c.github.io/trusted-types/dist/spec/#enforcement-in-scripts - which seems to indicate that the recommended way to set the content is the one shown above.

Flags: needinfo?(fwang)

(In reply to Hamish Willee from comment #23)

Thanks Fred.

So in the first case I can avoid a security violation by passing a TrustedScript or having a default policy - right?

let script = document.createElement("script");
script.text = someTrustedType; // A TrustedScript

In the second case for which this patch exists is there a way OTHER than using a default policy to pass a trusted type to the document.createTextNode()?

My assumption is "no" based on https://w3c.github.io/trusted-types/dist/spec/#enforcement-in-scripts - which seems to indicate that the recommended way to set the content is the one shown above.

You are right, and there is no way to pass a TrustedScript in the second case, this is mainly to prevent attacker to bypass the trusted type check via the many alternative means of setting the script text. However, someone could still do something like

script.text = mypolicy.createScript(script.text) // create a TrustedScript

to ensure script text is trusted before appending the script. But I'm not sure that's really useful in practice.

And just to be clear, there are many ways to set the script text (see https://wpt.fyi/results/trusted-types/script-enforcement-001.html for example) and multiple of them can be combined. The TT check is only run when the script is about to run (so when document.body.appendChild(script); is called in the example) or immediately if you use a TrustedScript sink like script.text, script.textContent or script.innerText.

(Note sure it's worth documenting, but one subtle thing is that script.innerHTML is a TrustedHTML sink so it will run the TT check to make sure the content is trusted HTML but that does not guarantee it is a TrustedScript)

Flags: needinfo?(fwang)
QA Whiteboard: [qa-triage-done-c145/b144]
Regressions: 1997818
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: