Skip to content

Escape user names, comments, commenter details and tag names where they're output - #318

Merged
mraible merged 6 commits into
masterfrom
escape-user-display-names
Nov 20, 2026
Merged

mraible merged 6 commits into
masterfrom
escape-user-display-names

Conversation

@mraible

@mraible mraible commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Escaping and parsing fixes where names, comments and tags are output:

  • UserWrapper returns screen and full names as HTML-escaped text, encoded exactly once whether they were stored as typed or already encoded by the sanitizer (weblogAdminsUntrusted). The built-in feed macros write them as plain text in Atom <author><name> and RSS dc:creator, and the comment feed macros and the media file feed do the same for comment and media file authors, so a name like A & B no longer shows as A &amp; B in feed readers.
  • HTML comment notification emails escape the commenter's name and email.
  • WeblogEntryCommentWrapper.getContent HTML-escapes any comment that isn't text/html with the HTMLSubset plugin both configured and applied, including comments without a content type. Its Javadoc now says what it does.
  • Utilities.transformToHTMLSubset finds each link's closing &gt; with one forward search, so its time is linear in the input. addNofollow parses the HTML with jsoup, a new bundled dependency (1.24.1, listed in binary-includes/LICENSE.txt). It now adds nofollow to links that lack it (it used to touch only links that already had it), adding it to a link's existing rel value rather than a second rel attribute, and a > inside a quoted attribute value no longer ends the link early.
  • TagDataServlet JSON- and XML-escapes tag names and the weblog handle.
  • The planet group pages pass handles and feed URLs to their delete handlers through data-* attributes, and JspOutputEncodingTest now rejects property values inside inline event handlers.
  • CommentDataServlet GET returns JSON as application/json, and Comments.jsp reads it with dataType: "json" instead of eval. UserDataServlet sets an explicit content type.

Tests: UserWrapperTest, FeedAuthorEscapingTest (entry, comment and media file feeds), MailUtilCommentNotificationTest, TagDataServletTest, new cases in WeblogEntryCommentWrapperTest, UtilitiesTest, CommentDataServletTest, UserDataServletTest and JspOutputEncodingTest; fourteen of the original cases fail without the change. The linear-time UtilitiesTest case allows 2 seconds so it doesn't fail on slow build machines. mvn test passes, and the Playwright database-login suite passes.

CHANGES entries for this and the related fixes will come in one PR.

A further review pass added these fixes:

  • A comment saved without a name, or a media file whose creator can't be found, has an empty author in the feeds instead of printing the template expression.

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.

@snoopdave snoopdave left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of bc98f9c8c: The shared fix matches #330, with only branch-specific file and dependency differences. One comment-feed author path still double-encodes ordinary names.

…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.
@mraible

mraible commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

A second review raised a few more points, now addressed in the latest commits:

  • addNofollow now adds nofollow to a link's existing rel value instead of adding a second rel attribute, and skips a ">" inside a quoted attribute value when finding the end of the link. UtilitiesTest covers both, and the linear-time test now allows 2 seconds so slow build machines don't fail it. Escape user names, commenter details and tag names where they are output #330 has the same change.

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.
A comment saved without a name, or a media file whose creator can't be found, made the comment and media file feeds print the template expression itself as the author, because unescapeHTML passed null through and Velocity prints a null reference as its source text. The template unescapeHTML helper now returns an empty string for null, as removeHTML does, so the author element is empty again.

@snoopdave snoopdave left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review of 6d9f0775e: The current diff addresses the earlier feed author encoding point. The corresponding fix in #347 is present here; the nofollow implementations differ by branch. I found no new correctness or test issue in this read-only review.

Earlier comments: 3 of 3 resolved.

No new issues found.

@mraible
mraible merged commit 2b91937 into master Nov 20, 2026
9 checks passed
@mraible
mraible deleted the escape-user-display-names branch November 20, 2026 00:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants