Skip to content

Restrict HTML comments to the HTML subset with jsoup's Cleaner - #353

Open
mraible wants to merge 6 commits into
masterfrom
html-subset-jsoup
Open

mraible wants to merge 6 commits into
masterfrom
html-subset-jsoup

Conversation

@mraible

@mraible mraible commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

This builds on #318, which adds jsoup, so its diff includes #318's commits until that merges. The new commit is c7e255b.

The HTML subset, used by the default HTMLSubset comment plugin and by HTML comment notification e-mails, 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. Now:

  • Utilities.sanitizeToHTMLSubset runs 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 plugin and the notification e-mails call it directly.
  • transformToHTMLSubset stays for templates ($utils.transformToHTMLSubset). It decodes its HTML-encoded input and then cleans it.
  • The 21 patterns and restoreSubsetEntities are removed.

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. The full unit suite passes (770 tests).

6.1.x keeps its current HTML subset code.

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 &amp; 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.
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