Skip to content

Sanitize download attribute file names in DownloadingAttachmentHandler - #1184

Merged
rbri merged 1 commit into
HtmlUnit:masterfrom
Vidya-m78:download-attribute-filename-sanitize
Nov 2, 2026
Merged

rbri merged 1 commit into
HtmlUnit:masterfrom
Vidya-m78:download-attribute-filename-sanitize

Conversation

@Vidya-m78

Copy link
Copy Markdown
Contributor

Repro: set webClient.setAttachmentHandler(new DownloadingAttachmentHandler(dir)) and load a page whose script does a.download = '../evil.txt'; a.click();, or sets an absolute path such as /home/user/.config/autostart/x.desktop. The response of the link is written to the path the page chose, outside of dir, and nothing is saved in dir. Only new files are created (an existing name gets the (1) suffix), but anywhere the JVM user can write.
Cause: for a download attribute, WebClient.loadWebResponseInto() passes the attribute value to the AttachmentHandler unchanged; only names from the Content-Disposition header go through Attachment.getSuggestedFilename(). DownloadingAttachmentHandler.determineDestionationFile() then calls downloadFolder_.resolve(fileName), which follows .. and returns an absolute name as it is.
Fix: run the name through the existing Attachment.sanitizeFilename() (now package private) in DownloadingAttachmentHandler before resolving it. ../../evil.txt becomes _.._evil.txt, the name the header path already produces (AttachmentSuggestedFilename2Test.pathTraversal); browsers do not let the download attribute pick a directory either. Doing it in the handler covers every source of the name, not only the anchor.

DownloadingAttachmentHandlerTest.fileNameFromDownloadAttributeIsSanitized covers a relative and an absolute name and fails without the change. The attachment, anchor and area tests and CodeStyleTest pass.

The file name of a download started by an anchor with a download
attribute is passed to the AttachmentHandler as it is; only names from
the Content-Disposition header are sanitized. DownloadingAttachmentHandler
resolved this name against the download folder, so a page could use
download="../x" or an absolute path to create a file anywhere outside
of it. Sanitize the name the same way as the header name before
resolving it.
@sonarqubecloud

sonarqubecloud Bot commented Nov 2, 2026

Copy link
Copy Markdown

@rbri

rbri commented Nov 2, 2026

Copy link
Copy Markdown
Member

great - thanks

@rbri
rbri merged commit 08c2d1e into HtmlUnit:master Nov 2, 2026
8 checks passed
@Vidya-m78

Copy link
Copy Markdown
Contributor Author

Thanks for merging this, glad it went in cleanly.

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