Skip to content

escape html in FileAndResourceDirectives - #1191

Merged
pjfanning merged 4 commits into
apache:mainfrom
pjfanning:dir-listing
Jul 31, 2026
Merged

escape html in FileAndResourceDirectives#1191
pjfanning merged 4 commits into
apache:mainfrom
pjfanning:dir-listing

Conversation

@pjfanning

@pjfanning pjfanning commented Jul 29, 2026

Copy link
Copy Markdown
Member

Added escapeHtml methods to the DirectoryListing object that escape the five HTML special characters (&, <, >, ", '). Applied to all user-controlled strings in the directory listing HTML:

  • path in <title> and <h1> tags
  • path in the "../" parent directory link href
  • path + name in file/directory link hrefs (via the two-argument overload, avoiding an intermediate string concatenation)
  • name as link text content

Pretty unlikely but there is a theoretical chance that filenames could have problematic XSS type names.
Claude AI thinks that we should harden the code as a result.

@pjfanning pjfanning added this to the 2.0.0-M2 milestone Jul 29, 2026

@He-Pin He-Pin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall LGTM — the escaping logic is correct and covers all user-controlled insertion points. One small suggestion inline.

val escapedPath = escapeHtml(path)
sb.append(html(0)).append(escapedPath).append(html(1)).append(escapedPath).append(html(2))
if (!isRoot) {
val secondToLastSlash = path.lastIndexOf('/', path.lastIndexOf('/', path.length - 1) - 1)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nit: escapeHtml(path) here is called once per file/directory entry, but you already computed escapedPath a few lines above. Could just reuse it:

sb.append("<a href=\"").append(escapedPath).append(escapedName).append("\">").append(escapedName).append("</a>")

Avoids redundant escaping in directories with many entries.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

escapedPath is already handled this way

@pjfanning
pjfanning merged commit 1379d7b into apache:main Jul 31, 2026
6 checks passed
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.

2 participants