Skip to content

Add trusted-types and require-trusted-types-for CSP directives#273

Closed
sideshowbarker wants to merge 2 commits intoshapesecurity:mainfrom
sideshowbarker:trusted-types
Closed

Add trusted-types and require-trusted-types-for CSP directives#273
sideshowbarker wants to merge 2 commits intoshapesecurity:mainfrom
sideshowbarker:trusted-types

Conversation

@sideshowbarker
Copy link
Contributor

@sideshowbarker sideshowbarker commented Jan 2, 2026

This change implements support for the Trusted Types CSP directives as specified in the W3C Trusted Types spec.
Fixes #232.

The trusted-types directive supports:

  • Policy names (with characters: ALPHA/DIGIT/-/#/=/_/@/./%)
  • Keywords: 'none', 'allow-duplicates'
  • Wildcard: *

The require-trusted-types-for directive supports:

  • Keyword: 'script'

Policy names are handled case-sensitively, matching browser behavior (verified via Playwright tests against Chromium and WebKit). Keywords are handled case-insensitively, per the ABNF spec.

Note: Chromium incorrectly treats 'allow-duplicates' as case-sensitive, while WebKit correctly treats it as case-insensitive. We follow the spec. See https://issues.chromium.org/issues/472892238


This PR branch also includes some Playwright browser tests for trusted-types case-sensitivity. The tests verify browser behavior for the trusted-types CSP directive, specifically checking whether keywords and policy names are treated as case-sensitive or case-insensitive.

  • Policy names

    case-sensitive in both Chromium and WebKit

  • Keywords ('none')

    case-insensitive in both browsers

  • Keywords ('allow-duplicates')

    case-insensitive in WebKit, incorrectly case-sensitive in Chromium
    https://issues.chromium.org/issues/472892238


shapesecurity/CLA#40

These tests verify browser behavior for the trusted-types CSP directive,
specifically checking whether keywords and policy names are treated as
case-sensitive or case-insensitive.

- Policy names
    > case-sensitive in both Chromium and WebKit

- Keywords ('none')
    > case-insensitive in both browsers

- Keywords ('allow-duplicates')
    > case-insensitive in WebKit, incorrectly case-sensitive in Chromium
      https://issues.chromium.org/issues/472892238
@sideshowbarker
Copy link
Contributor Author

👋 friendly bump~

This change implements support for the Trusted Types CSP directives as
specified in the W3C Trusted Types spec.

The trusted-types directive supports:

- Policy names (with characters: ALPHA/DIGIT/-/#/=/_/@/./%)
- Keywords: 'none', 'allow-duplicates'
- Wildcard: *

The require-trusted-types-for directive supports:

- Keyword: 'script'

Policy names are handled case-sensitively, matching browser behavior
(verified via Playwright tests against Chromium and WebKit). Keywords
are handled case-insensitively per the ABNF spec.

Note: Chromium incorrectly treats 'allow-duplicates' as case-sensitive,
while WebKit correctly treats it as case-insensitive. We follow the
spec. See https://issues.chromium.org/issues/472892238

Fixes shapesecurity#232
@rbri
Copy link

rbri commented Jan 28, 2026

👋 friendly bump~

Hi @sideshowbarker,
because this project seems to be not moving forward for a long time, I started my branch at the end of 2023. Maybe you like to have a look. https://github.com/HtmlUnit/htmlunit-csp

@kingthorin is working on trusted-types and require-trusted-types-for at the moment.
HtmlUnit/htmlunit-csp#9
Maybe we can work together

@kingthorin
Copy link
Contributor

kingthorin commented Jan 28, 2026

@rbri / @sideshowbarker
This seems further along than my unfinished changes. I'd be happy to adapt/adopt this. Let me know if you're okay with that.

rbri added a commit to HtmlUnit/htmlunit-csp that referenced this pull request Jan 31, 2026
@rbri
Copy link

rbri commented Jan 31, 2026

@sideshowbarker - have taken this as base for 34b56357d237dcdc9e1876cbefd81017c5581fe0

At least your Playwrite tests and the add methods have not been adopted yet. If you want, I can add more elements to support your cases, and hopefully we can continuously improve this together.

So far, this is also used by ZAK (https://github.com/owasp-zap/zaproxy), and @kingthorin and I are working together to improve this lib to support HtmlUnit and ZAK.

@sideshowbarker
Copy link
Contributor Author

because this project seems to be not moving forward for a long time, I started my branch at the end of 2023. Maybe you like to have a look. HtmlUnit/htmlunit-csp

Thanks, that’s really great news — in validator/validator@9870c6ecc0 I’ve gone ahead and switched the HTML checker to using htmlunit-csp, and pushed the change to https://validator.w3.org/nu/.

@sideshowbarker
Copy link
Contributor Author

@sideshowbarker - have taken this as base for 34b56357d237dcdc9e1876cbefd81017c5581fe0

Beautiful~ ⭐️

At least your Playwrite tests and the add methods have not been adopted yet.

I’m happy to go ahead and take a shot at porting those over myself, if you want. Shall I go ahead and do that?

If you want, I can add more elements to support your cases

Not sure what you mean by “elements” — but right now, for the HTML checker, the immediate need is just for handling the trusted-types and require-trusted-types-for directives.

But lemme turn it around and say: If there’s anything additional y’all would like to see the HTML checker doing to help web developers with CSP more, I’m all for adding more of that.

and hopefully we can continuously improve this together.

Yes~ I’d be very happy to contribute further, wherever I might be useful.

So far, this is also used by ZAK (owasp-zap/zaproxy), and @kingthorin and I are working together to improve this lib to support HtmlUnit and ZAK.

👍

@sideshowbarker
Copy link
Contributor Author

sideshowbarker commented Feb 1, 2026

At least your Playwrite tests and the add methods have not been adopted yet.

I’m happy to go ahead and take a shot at porting those over myself, if you want. Shall I go ahead and do that?

Actually, I think we don’t necessarily really need the Playwrite tests, right? I mean, they’re not really even tests as such — not for this code, at least; they’re not testing the behavior of this code in any way at all.

So they’re more just … demos, to document what browser engines currently do with case sensitivity for the directives.

and the add methods have not been adopted yet.

So the add/remove methods I’ be happy to port over. Or is there a reason you didn't already go ahead and do that?

sideshowbarker added a commit to sideshowbarker/htmlunit-csp that referenced this pull request Feb 1, 2026
Port the remaining manipulation API from
shapesecurity/salvation#273 that was not
included in 34b5635:

- Add ManipulationErrorConsumer interface to Directive
- Add removeValueExact() to Directive for case-sensitive value removal
- Add addPolicyName() and removePolicyName() to TrustedTypesDirective
- Add tests for validation, duplicates, case sensitivity, round-trip
  serialization, and edge cases
@sideshowbarker
Copy link
Contributor Author

So the add/remove methods I’ be happy to port over. Or is there a reason you didn't already go ahead and do that?

I went ahead and wrote a patch for it, and opened HtmlUnit/htmlunit-csp#11

So I’m going ahead and closing this PR here.

@sideshowbarker sideshowbarker deleted the trusted-types branch February 1, 2026 03:13
rbri added a commit to HtmlUnit/htmlunit-csp that referenced this pull request Feb 10, 2026
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.

Trusted Types

3 participants