Skip to content

Enable createParserOptions, passing a sanitizer to trusted types - #606

Open
noamr wants to merge 29 commits into
mainfrom
create-parser-options
Open

Enable createParserOptions, passing a sanitizer to trusted types#606
noamr wants to merge 29 commits into
mainfrom
create-parser-options

Conversation

@noamr

@noamr noamr commented Feb 17, 2026

Copy link
Copy Markdown

createParserOptions receives a SetHTML{Unsafe}Options and turns them into a TrustedParserOptions using the given method from the policy config.

This is the trusted-types part of #594

Note that this should only be merged when wired into the HTML standard, once the sanitizer is itself upstreamed.


Preview | Diff

@noamr
noamr requested a review from lukewarlow February 17, 2026 11:13
@noamr noamr mentioned this pull request Feb 17, 2026
Comment thread spec/index.bs Outdated
Comment thread spec/index.bs Outdated
@lukewarlow

Copy link
Copy Markdown
Member

LGTM

@noamr
noamr force-pushed the create-parser-options branch from ced3862 to d44dcea Compare June 1, 2026 13:44
@noamr

noamr commented Jun 1, 2026

Copy link
Copy Markdown
Author

I've made some changes:

  • Some of the algorithm calls from "get Trusted Type compliant parser options" assumed stringifications. Made that algorithm do things on its own and passing a new object (repersented as an ordered map) instead.
  • Allow passing throwIfMissing from the outside. streamHTMLUnsafe methods should throw if the policy cannot create parser options, while setHTMLUnsafe and friends can rely on createHTML.
  • Support runScripts as per Add runScripts to SetHTMLUnsafeOptions whatwg/html#12495

(Note: this PR will fail build until we merge the latter)

@lukewarlow

Copy link
Copy Markdown
Member

Are you able to fix the build failure now?

@noamr noamr closed this Jun 9, 2026
@noamr noamr reopened this Jun 9, 2026
@noamr

noamr commented Jun 9, 2026

Copy link
Copy Markdown
Author

Are you able to fix the build failure now?

I'll revisit this once I have the HTML counterpart ready.

Comment thread spec/index.bs Outdated

## Get Trusted Type parser input ## {#get-trusted-type-parser-input-algorithm}

To <dfn export>get Trusted Type compliant input</dfn> given a {{TrustedType}} |expectedType|, a [=realm/global object=] (|global|), string, or <code data-x="">Stream</code> |input|,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What is Stream?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It's a special value meaning that we don't have a string yet and we are going to stream the HTML. is null better? This seemed more descriptive.

Comment thread spec/index.bs Outdated
Comment thread spec/index.bs Outdated
Comment thread spec/index.bs Outdated
Comment thread spec/index.bs
Comment thread spec/index.bs Outdated
Comment thread spec/index.bs Outdated
Comment thread spec/index.bs Outdated
Comment thread spec/index.bs Outdated
Comment thread spec/index.bs Outdated
@annevk

annevk commented Jun 22, 2026

Copy link
Copy Markdown
Member

Also, reportedly this impacts innerHTML and friends as well somehow? I think that warrants some examples.

@noamr

noamr commented Jun 23, 2026

Copy link
Copy Markdown
Author

Also, reportedly this impacts innerHTML and friends as well somehow? I think that warrants some examples.

Added prose and examples for this

@noamr

noamr commented Jun 23, 2026

Copy link
Copy Markdown
Author

(The build failure is in main, trying to resolve separately. Do you have an idea maybe @lukewarlow ?)

@evilpie

evilpie commented Jun 23, 2026

Copy link
Copy Markdown

In TrustedParserOptions sanitizer member is readonly, but a Sanitizer instance allows for internal modification. Isn't that unexpected?

Also I feel like we should probably always return a Sanitizer object, instead of one of three options? Otherwise it seems easy to accidentally write code (not sure what for though?) that only works with a subset.

@noamr

noamr commented Jun 23, 2026

Copy link
Copy Markdown
Author

In TrustedParserOptions sanitizer member is readonly, but a Sanitizer instance allows for internal modification. Isn't that unexpected?

It's a good point. I guess we need to return a clone of the underlying sanitizer if the getter is invoked so that you couldn't get a TrustedParserOptions and then mutate it.

Also I feel like we should probably always return a Sanitizer object, instead of one of three options? Otherwise it seems easy to accidentally write code (not sure what for though?) that only works with a subset.

Yea I was contemplating that. SGTM

noamr and others added 12 commits June 23, 2026 13:32
`createParserOptions` receives a `SetHTML{Unsafe}Options` and turns them into
a `TrustedParserOptions` using the given method from the policy config.

This is the `trusted-types` part of #594
(Need to wire it to HTML as well)
Co-authored-by: Anne van Kesteren <annevk@annevk.nl>
Co-authored-by: Anne van Kesteren <annevk@annevk.nl>
Co-authored-by: Anne van Kesteren <annevk@annevk.nl>
@noamr

noamr commented Jul 13, 2026

Copy link
Copy Markdown
Author

Might be a silly question but why does the TrustedParserOptions need to expose the value of sanitizer and run scripts? Can it not be an opaque container than only web APIs can reach into? That way there's no weirdness about mutating a value that doesn't affect anything?

I guess it's kind of expected as a reflection, the same way you can stringify a TrustedHTML?

The mutability thing is a bit awkward but in most cases it wouldn't have any effect.
I'm also open to changing this reflection behavior, it's not material to the design.

@lukewarlow

Copy link
Copy Markdown
Member

I have a feeling that a function is a better way to do the access for the sanitizer object. It feels more logical that a function returns a clone of an object rather than directly allowing mutations. But I'm curious for others thoughts here.

@noamr

noamr commented Jul 14, 2026

Copy link
Copy Markdown
Author

I have a feeling that a function is a better way to do the access for the sanitizer object. It feels more logical that a function returns a clone of an object rather than directly allowing mutations. But I'm curious for others thoughts here.

you mean something like sanitizer() that returns a fresh Sanitizer object in each call?
Yea that also works for me.

@noamr

noamr commented Jul 15, 2026

Copy link
Copy Markdown
Author

I have a feeling that a function is a better way to do the access for the sanitizer object. It feels more logical that a function returns a clone of an object rather than directly allowing mutations. But I'm curious for others thoughts here.

you mean something like sanitizer() that returns a fresh Sanitizer object in each call? Yea that also works for me.

Done

@noamr

noamr commented Jul 16, 2026

Copy link
Copy Markdown
Author

@lukewarlow having checked this, I think I have slight preference to keeping it as a getter, as it has the nice ergonomic benefit of being able to pass it to setHTML (and the other "safe" variant) as if it was a SetHTMLOptions - it reads the sanitizer property from the getter. If it's a method, it fails silently as it doesn't recognize that method as a sanitizer.

@lukewarlow

Copy link
Copy Markdown
Member

Can we change it so that just works though? It feels like we could. I'm happy to leave it as a getter if that's preferred though.

@noamr

noamr commented Jul 16, 2026

Copy link
Copy Markdown
Author

Can we change it so that just works though? It feels like we could. I'm happy to leave it as a getter if that's preferred though.

Yea I guess its nicer as a method, and we can allow TrustedParserOptions in the IDL explicitly

beckysiegel pushed a commit to chromium/chromium that referenced this pull request Jul 16, 2026
This is instead of retaining a frozen copy.
See w3c/trusted-types#606

Bug: 491743369
Change-Id: Ida4d1a661edb8475cbc4a1b2ce0d0e3d38913024
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8097523
Reviewed-by: Daniel Vogelheim <vogelheim@chromium.org>
Commit-Queue: Noam Rosenthal <nrosenthal@google.com>
Cr-Commit-Position: refs/heads/main@{#1663095}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Jul 16, 2026
This is instead of retaining a frozen copy.
See w3c/trusted-types#606

Bug: 491743369
Change-Id: Ida4d1a661edb8475cbc4a1b2ce0d0e3d38913024
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8097523
Reviewed-by: Daniel Vogelheim <vogelheim@chromium.org>
Commit-Queue: Noam Rosenthal <nrosenthal@google.com>
Cr-Commit-Position: refs/heads/main@{#1663095}
chromium-wpt-export-bot pushed a commit to web-platform-tests/wpt that referenced this pull request Jul 16, 2026
This is instead of retaining a frozen copy.
See w3c/trusted-types#606

Bug: 491743369
Change-Id: Ida4d1a661edb8475cbc4a1b2ce0d0e3d38913024
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8097523
Reviewed-by: Daniel Vogelheim <vogelheim@chromium.org>
Commit-Queue: Noam Rosenthal <nrosenthal@google.com>
Cr-Commit-Position: refs/heads/main@{#1663095}
Comment thread spec/index.bs
Comment thread spec/index.bs Outdated
Comment thread spec/index.bs Outdated
Comment thread spec/index.bs Outdated
@lukewarlow

Copy link
Copy Markdown
Member

Other than the 3 changes above this PR LGTM. Haven't reviewed the HTML side yet.

lando-worker Bot pushed a commit to mozilla-firefox/firefox that referenced this pull request Jul 20, 2026
…izer in each call, a=testonly

Automatic update from web-platform-tests
TrustedParserOptions: return a new sanitizer in each call

This is instead of retaining a frozen copy.
See w3c/trusted-types#606

Bug: 491743369
Change-Id: Ida4d1a661edb8475cbc4a1b2ce0d0e3d38913024
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/8097523
Reviewed-by: Daniel Vogelheim <vogelheim@chromium.org>
Commit-Queue: Noam Rosenthal <nrosenthal@google.com>
Cr-Commit-Position: refs/heads/main@{#1663095}

--

wpt-commits: c1e067e2346c22c0fc98bafed6618c3d92b06af0
wpt-pr: 61339
Comment thread spec/index.bs Outdated
Comment thread spec/index.bs Outdated
Comment thread spec/index.bs Outdated
Comment thread spec/index.bs Outdated
noamr and others added 3 commits August 10, 2026 16:35
Co-authored-by: Simon Pieters <zcorpan@gmail.com>
Co-authored-by: Simon Pieters <zcorpan@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants