Skip to content

Clean untrusted weblog HTML with jsoup's Cleaner - #356

Open
mraible wants to merge 7 commits into
masterfrom
weblog-sanitizer-jsoup
Open

mraible wants to merge 7 commits into
masterfrom
weblog-sanitizer-jsoup

Conversation

@mraible

@mraible mraible commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

This builds on #353 (which builds on #318, where jsoup comes in), so its diff includes their commits until they merge. The new commit is the last one.

When weblogAdminsUntrusted is true, HTMLSanitizer cleans entry text, titles, summaries, user names, bookmark names and weblog taglines. It was a 528-line regular-expression parser from Open Lab (2009), and CodeQL flags it six times for polynomial ReDoS (alerts 89–94). In the new test, a few kilobytes of crafted markup took the old class 43.6 seconds; it takes 28 ms now.

It now uses jsoup's Cleaner with a Safelist that keeps the same rules:

  • the same tags, without the forbidden script, object, embed, link, style, form and input
  • links to http, https and mailto addresses, and images from http and https or image data that InlineImageData accepts
  • width and height only as pixels or percentages
  • style only when it can't load anything or run script (no url(), image-set(), expression(), @import, behaviors, escapes or comments)
  • no event handlers

What changes:

  • Attributes that aren't listed are removed instead of being kept HTML-encoded. The list covers the common ones: class, id, title, lang, dir, align, alt, border, colspan, rowspan, valign, summary, cellpadding, cellspacing, start, type, name.
  • The unused helpers (isSanitized, getText, sanitizer, encode, htmlEncode*, convertLineFeedToBR, removeLineFeed, replaceAllNoRegex) go with the old parser, so none of the Open Lab code is left.

The new HTMLSanitizerTest covers kept markup, scripts, styles, event handlers, javascript: and relative links, iframes, plugins, forms, SVG, unsafe styles and sizes, and the slow inputs. The existing inline image tests pass unchanged, and so does the full unit suite (774 tests).

6.1.x keeps its current sanitizer.

UserWrapper now returns screen and full names as HTML-escaped text, encoding them exactly once whether they were stored as typed or already encoded by the sanitizer, and the built-in feed macros turn them back into text before XML-escaping them for Atom author names and RSS dc:creator.

HTML comment notification emails escape the commenter's name and email address. WeblogEntryCommentWrapper escapes HTML comments when the HTMLSubset plugin isn't both configured and listed on the comment, and its Javadoc now describes what getContent does.

Utilities.transformToHTMLSubset and addNofollow find each link's closing bracket with one forward search and stop when none is left, so their time is linear in the input, and addNofollow now adds rel="nofollow" to links that don't already have it.

TagDataServlet writes tag names and the weblog handle with JSON and XML escaping. The planet group pages pass handles and feed URLs to their delete handlers through data attributes, and JspOutputEncodingTest rejects property values inside inline event handlers. CommentDataServlet returns comment content as JSON, which Comments.jsp reads with jQuery's json data type, and UserDataServlet declares its plain text response.
WeblogEntryCommentWrapper.getContent now HTML-escapes any comment that isn't text/html with the HTMLSubset plugin applied, including comments without a content type.
…isting rel values in addNofollow

The comment feed macros and the media file feed now turn author names back into text with unescapeHTML before XML-escaping them, as the entry author macros do, so a name like "A & B" no longer shows as "A & B" in feed readers. FeedAuthorEscapingTest covers the Atom and RSS comment feeds and the media file feed.

Utilities.addNofollow adds nofollow to a link's existing rel value instead of adding a second rel attribute, and a ">" inside a quoted attribute value no longer ends the link early. The linear-time test now allows 2 seconds so it doesn't fail on slow build machines.
addNofollow looked for rel= anywhere in the tag, so a link like <a href="/x" title='foo rel=external' rel="noopener"> had nofollow added to the title text while its real rel stayed followable. It now walks the tag's attributes while tracking quotes, the same way findTagEnd finds the end of the tag, and changes only a real rel attribute. A rel with no value gets rel="nofollow".
addNofollow now parses the HTML with jsoup 1.24.1 and adds nofollow to the rel attribute of each a element with an href, instead of scanning the markup by hand. Only a link's own rel is changed, and links with no href are left alone. Text with no links is returned as is.

The result is jsoup's serialization of the fragment, so open tags are closed, attribute values are double-quoted, and a tag that never ends is dropped, as browsers do. Escaped text stays escaped.

jsoup is credited in the binary LICENSE.
The HTML subset was built with regular expressions: the comment was HTML-encoded, then 21 patterns turned the encoded tags in the subset back into tags and rebuilt links. Utilities.sanitizeToHTMLSubset now uses jsoup's Cleaner with a Safelist of the same tags (a, b, blockquote, br, i, li, ol, p, pre, ul) and the same link schemes (http, https, mailto) on the raw HTML. The HTMLSubset comment plugin and HTML comment notification e-mails call it directly. transformToHTMLSubset stays for templates and decodes its HTML-encoded input before cleaning it.

What changes for commenters:
- A tag outside the subset is removed and its text kept, instead of being shown as encoded markup. Script and style contents are removed.
- A link keeps its href when it has other attributes, such as a title; the other attributes are removed.
- The output is jsoup's: tags are balanced, <br> has no slash, and the link scheme is lowercase.

The new tests cover script, style, event handler, javascript: and data: links, an entity-split javascript: scheme, SVG, inline styles, HTML comments, iframes, and a quote inside an href.
When weblogAdminsUntrusted is true, HTMLSanitizer cleans entry text, titles, summaries, user names, bookmark names and weblog taglines. It was a 528-line regular-expression parser from Open Lab (2009) that CodeQL flags six times for polynomial ReDoS: in the new test, a few kilobytes of crafted markup took the old class 43.6 seconds, and takes 28 ms now.

It now uses jsoup's Cleaner with a Safelist that keeps the same rules:
- the same tags, without the forbidden script, object, embed, link, style, form and input
- links to http, https and mailto addresses, and images from http and https or image data that InlineImageData accepts
- width and height only as pixels or percentages
- style only when it can't load anything or run script (no url(), image-set(), expression(), @import, behaviors, escapes or comments)
- no event handlers

Unlisted attributes are now removed instead of being kept HTML-encoded; the list covers the common ones (class, id, title, lang, dir, align, alt, border, colspan, rowspan, valign, summary, cellpadding, cellspacing, start, type, name). The unused helpers (isSanitized, getText, sanitizer, encode, htmlEncode*, convertLineFeedToBR, removeLineFeed, replaceAllNoRegex) are removed with the old parser, so none of the Open Lab code is left.
@mraible
mraible requested a review from snoopdave November 18, 2026 17:44
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.

1 participant