Skip to content

Some settings (e.g., HTML_INTEGRATION_POINTS, and likely others) are "sticky" #1119

@Zlatkovsky

Description

@Zlatkovsky

When running DOMPurify.sanitize(<...>), some settings are "sticky".

Specifically, if passing in the HTML_INTEGRATION_POINTS: {'foreignobject': true} on the config the first time, the setting will carry over even to future invocations of sanitize that do not have the HTML_INTEGRATION_POINTS on the config. That shouldn't be the case... right?

Running example:

  1. Open https://zlatkovsky.github.io/DOMPurifyMystery/
  2. Select version 3.2.4 or 3.2.6 in the "Select a version" dropdown. Leave the "HTML integration points" checkbox as OFF for now.
  3. Click "Sanitize!". Note that in the "After sanitization" section, the text in the blue box is missing. This is a known issue due to DOMPurify 3.1.7 breaks Mermaid diagrams using foreignObject #1002 .
    Image
  4. Now turn ON the "Enable HTML integration points" checkbox and press "Sanitize!" again. This time the label will show up in the blue box, as expected.
  5. Finally, turn the "Enable HTML integration points" OFF again and press "Sanitize!"

I would have expected the result of #5 to be the same as #3, i.e., the label should be missing again since the checkbox is OFF. But instead, the label is still visible because the HTML_INTEGRATION_POINTS isn't reset.

The source code for the above site can be found at https://github.com/Zlatkovsky/DOMPurifyMystery/blob/main/index.html.

Tracing through DOMPurify code, it appears that:

  1. On invocation of DOMPurify.sanitize, _parseConfig is called:
    Image
  2. Specifically, this means that the following line is invoked:
    HTML_INTEGRATION_POINTS = cfg.HTML_INTEGRATION_POINTS || HTML_INTEGRATION_POINTS;
  1. If the first call to DOMPurify.sanitize had HTML_INTEGRATION_POINTS: {'foreignobject': true} in the config, the bundle-scoped HTML_INTEGRATION_POINTS variable will be set.
  2. On follow-up invocations of DOMPurify.sanitize that do NOT have HTML_INTEGRATION_POINTS set, the line from #2 is hit again. Because cfg.HTML_INTEGRATION_POINTS is undefined, HTML_INTEGRATION_POINTS gets set to just itself. BUT, since it never gets reset across DOMPurify.sanitize invocations, it just stays as whatever the last config that had this setting set it to.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions