Closed Bug 1952250 Opened 1 year ago Closed 1 year ago

Implement the Sanitizer removeUnsafe method

Categories

(Core :: DOM: Security, task)

task

Tracking

()

RESOLVED FIXED
138 Branch
Tracking Status
firefox138 --- fixed

People

(Reporter: tschuster, Assigned: tschuster)

References

Details

(Keywords: dev-doc-complete)

Attachments

(1 external file)

No description provided.
Keywords: dev-doc-needed
Assignee: nobody → tschuster
Attachment #9471151 - Attachment description: WIP: Bug 1952250 - Implement the Sanitizer removeUnsafe method → Bug 1952250 - Implement the Sanitizer removeUnsafe method. r?emilio
Status: NEW → ASSIGNED
Pushed by tschuster@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/7d1e8538f4ae Implement the Sanitizer removeUnsafe method. r=emilio https://hg.mozilla.org/integration/autoland/rev/a70b7694f15c 1952276, 1953720: apply code formatting via Lando
Created web-platform-tests PR https://github.com/web-platform-tests/wpt/pull/51348 for changes under testing/web-platform/tests
Upstream PR was closed without merging
Pushed by tschuster@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/824d8c4e02e1 Implement the Sanitizer removeUnsafe method. r=emilio
Status: ASSIGNED → RESOLVED
Closed: 1 year ago
Resolution: --- → FIXED
Target Milestone: --- → 138 Branch
Upstream PR merged by moz-wptsync-bot

FF138 MDN docs work for this can be tracked in https://github.com/mdn/content/issues/38890

This release seems to include the prototype Sanitizer (https://bugzilla.mozilla.org/show_bug.cgi?id=1950605) along with this issue that adds support for removeUnsafe() method, everything behind a preference dom.security.sanitizer.enabled.

My questions are:

  1. Is this all too early to document? The normal trigger for this is enough of the work done to make it possible and desirable to test, in which case we add an entry in the Experimental features page and a note in the first release note where it makes sense.
  2. If it is ready, what has been implemented - i.e. can I start calling the setHTML and setHTML unsafe with a default configuration? Can I add and remove elements?
  3. How close is the Explainer to the spec? Explainers are much more human readable, and if I can somewhat trust this it is a much better base for understanding the overall behaviour than the spec in the first instance.
  4. Are there any parts of the spec that we think are likely to change, or that FF or others have stated an objection to implementation?
  5. The unsafe methods can take a TrustedType while the safe methods take a string. Can I therefore assume:
    • Safe methods do not need any kind of extra sanitization
    • Unsafe methods can choose to use trusted types.
    • Are there reasons why you would choose to use the unsafe methods, trusted types, and your own parser configuration rather than using the safe methods?
    • Actually, you can supply your own sanitizer even to the unsafe methods. Does that mean that your sanitiser gets run on the trusted type passed in? I.e. you can use both approaches?
    • Summary is what use cases would push developers towards each path?
  6. When we're done, this should get technical review. Is there a good person to ask for that?
Flags: needinfo?(tschuster)

(In reply to Hamish Willee from comment #8)

FF138 MDN docs work for this can be tracked in https://github.com/mdn/content/issues/38890

This release seems to include the prototype Sanitizer (https://bugzilla.mozilla.org/show_bug.cgi?id=1950605) along with this issue that adds support for removeUnsafe() method, everything behind a preference dom.security.sanitizer.enabled.

My questions are:

  1. Is this all too early to document? The normal trigger for this is enough of the work done to make it possible and desirable to test, in which case we add an entry in the Experimental features page and a note in the first release note where it makes sense.

We don't have it enabled in Nightly yet, but I think it's probably a good idea to start on the documentation. I think it's very unlikely that we are going to see huge changes at this point.

  1. If it is ready, what has been implemented - i.e. can I start calling the setHTML and setHTML unsafe with a default configuration? Can I add and remove elements?

setHTML with a sanitizer is supported, not specifying the option will use the default sanitizer. Every API of the Sanitizer interface is fully supported. What is not supported is parseHTML and setHTMLUnsafe with a Sanitizer.

  1. How close is the Explainer to the spec? Explainers are much more human readable, and if I can somewhat trust this it is a much better base for understanding the overall behaviour than the spec in the first instance.

I haven't read it in a while, but from a quick skim it still seems mostly up-to-date.

  1. Are there any parts of the spec that we think are likely to change, or that FF or others have stated an objection to implementation?

There are still some open issue for v1: https://github.com/WICG/sanitizer-api/milestone/3. Most of these are basically tiny changes. I don't think there is anything that we or Chrome object to.

  1. The unsafe methods can take a TrustedType while the safe methods take a string. Can I therefore assume:
    • Safe methods do not need any kind of extra sanitization

Well the safe methods (i.e setHTML) do the sanitization by default so they don't need any extra sanitization. The trick with the safe methods is that the Sanitizer is always forced to be "safe", by removing all unsafe elements and attributes (read mainly scripts), even when the user passed Sanitizer is "unsafe". (You can imagine this works by calling the removeUnsafe() method on the passed in Sanitizer object)

  • Unsafe methods can choose to use trusted types.

Right. From my understanding if you enable Trusted Types for a document you can't just skip this. But as as I said, we don't implement Sanitizer support for this, yet.

  • Are there reasons why you would choose to use the unsafe methods, trusted types, and your own parser configuration rather than using the safe methods?

The Sanitizer can also be used in a unsafe method. Using it that way it's basically an API for removing/replacing specific elements/attributes. It really depends on the use case. Maybe for some reason you want to allow scripts in your user code or something like that? Or maybe allow embedding iframes.

  • Actually, you can supply your own sanitizer even to the unsafe methods. Does that mean that your sanitiser gets run on the trusted type passed in? I.e. you can use both approaches?

Yes. Basically first trusted types can check if the passed in string is ok and afterwards (if specified) the Sanitizer gets run on the "trusted type compliant string".

  • Summary is what use cases would push developers towards each path?

Of course we probably want everyone to use setHTML or barring that setHTMLUnsafe with Trusted Types. setHTMLUnsafe is like innerHTML prone to injection attacks.

Most use cases that don't require scripts to be executed are well served by setHTML. In the unlikely case that you need to preserve scripts/frames/etc. setHTMLUnsafe can be used.

  1. When we're done, this should get technical review. Is there a good person to ask for that?

Me and freddy are probably the obvious choices from the Mozilla side.

Flags: needinfo?(tschuster)

Thanks Tom,

What is not supported is parseHTML and setHTMLUnsafe with a Sanitizer.

  1. Is there a plan to fully implement those methods? Is there an issue number?

  2. Are the ShadowRoot methods implemented? https://searchfox.org/mozilla-central/source/dom/webidl/ShadowRoot.webidl seems to indicate that setHTMLUnsafe() is supported but not behind the preference, and setHTML is not supported at all.

Flags: needinfo?(tschuster)

I am adding all missing methods in bug 1959727.

Flags: needinfo?(tschuster)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: