Skip to content

Cleaner stopped removing script content #2320

Closed

Description

@eric-halvorson

I have been using the Cleaner since v1.16.1 to sanitize SVG by removing script.
After upgrading from v1.19.1 to v1.20.1, the behavior has changed and the text of the script is no longer removed.

A simplified example would be:

String dirtyString = "<svg><script>alert(\"test\")</script></svg>";
Document dirty = Parser.parse(dirtyString, "");
Safelist safelist = new Safelist().addTags("svg");
Cleaner cleaner = new Cleaner(safelist);
Document clean = cleaner.clean(dirty);
Document.OutputSettings outputSettings = new Document.OutputSettings().syntax(Document.OutputSettings.Syntax.xml);
String cleanString = clean.outputSettings(outputSettings).body().html();

With all versions from v1.16.1 through v1.19.1, this results in <svg></svg> with all the script removed.
But with v1.20.1, it now results in <svg>alert("test")</svg>.

Activity

  1. added this to the 1.21.1 milestone on May 6, 2025
  2. added
    fixed
    on May 6, 2025
  3. jhy commented on May 6, 2025

    @jhy
    Owner

    Interesting find! And thanks for the clear report.

    The underlying issue was that as we improved namespace support, the SVG Script element's contents were not parsed as Data, but as regular Text.

    In jsoup, when the Cleaner encounters an unsafe tag and drops it, it preserves that element's Text, but will drop Data.

    As far as I can tell we were parsing foreign content according to the spec. There are no tokenization steps there to push the tokenizer into a Data text state, and so the content will be parsed as Text.

    However on comparison to Chrome I see the content is parsed as Data. So I have added an explicit push to the Data state in jsoup's impl.

    I also added support for custom tags when in foreign content to be parsed as data.

  4. jhy commented on May 6, 2025

    @jhy
    Owner

    Just looking at SVG Script spec, the examples have the script content wrapped as <![CDATA[ ... ]]>, as its in XML. And CData is supported in HTML in foreign content mode, so I guess that's how the spec expects script data to be provided. But in practice UAs work as mentioned above without the CData requirement.

  5. added a commit that references this issue on May 6, 2025
    9c437b4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions