Skip to content

Escape file names and paths in directory listing HTML - #2005

Open
anshal-lang wants to merge 1 commit into
undertow-io:mainfrom
anshal-lang:directory-listing-escape-names
Open

anshal-lang wants to merge 1 commit into
undertow-io:mainfrom
anshal-lang:directory-listing-escape-names

Conversation

@anshal-lang

Copy link
Copy Markdown

The directory listing produced by DirectoryUtils.renderDirectoryListing (used by ResourceHandler and DefaultServlet when listing is enabled) writes each resource name, the parent link and the request path straight into single-quoted href attributes and element text. None of those values are HTML-escaped, and on most filesystems a file name can legally contain ', <, > and &. A file named x' onmouseover='alert(1).txt placed in a listed directory, for example through an upload feature that writes into it, renders as <a class='icon file' href='/path/d'q/x' onmouseover='alert(1)&.txt'>, so script runs in the origin of anyone who browses the listing. I found it while reading the listing code and confirmed it with the new FileHandlerTestCase.testDirectoryListingEscapesNames, which fails on main and passes with this change. The fix escapes &, <, >, " and ' inside DirectoryUtils at the point the markup is built, which keeps link targets the same for browsers because entity references in attributes decode back to the original characters.

DirectoryUtils wrote resource names, the parent link and the request path
into single-quoted href attributes and element text without escaping, so
a file named x' onmouseover='alert(1).txt broke out of the attribute and
injected script into the generated listing page.

Signed-off-by: anshal ossaimi <anshal@medios.digiscrypt.com>
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