Skip to content

Reject path traversal filenames during HMEF attachment extraction#1066

Open
jmestwa-coder wants to merge 1 commit into
apache:trunkfrom
jmestwa-coder:hmef-attachment-path-traversal
Open

Reject path traversal filenames during HMEF attachment extraction#1066
jmestwa-coder wants to merge 1 commit into
apache:trunkfrom
jmestwa-coder:hmef-attachment-path-traversal

Conversation

@jmestwa-coder
Copy link
Copy Markdown
Contributor

Summary

Reject attachment filenames that would escape the selected extraction directory during HMEF/TNEF attachment extraction.

Previously, attachment filenames from TNEF metadata were written directly using:

new File(dir, filename)

This allowed traversal-style filenames such as ../escaped.txt to resolve outside the intended extraction directory.

Changes

  • normalize and validate resolved attachment output paths before writing files
  • reject attachment filenames that resolve outside the chosen extraction directory
  • add regression coverage for traversal-style attachment names

Test

Added a regression test that:

  • creates an in-memory malicious TNEF attachment using a traversal filename
  • verifies extraction throws IOException
  • verifies no file is created outside the extraction directory

private static File getOutputFile(File dir, String filename) throws IOException {
Path outputDir = dir.toPath().toAbsolutePath().normalize();
Path outputFile = outputDir.resolve(filename).normalize();
if (!outputFile.startsWith(outputDir)) {
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.

Can you have a look at

* There is a check to prevent path traversal attacks. Only path traversal
and adjust that to have a shared method to do this check? Code reuse is important.

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