Skip to content

fix: prevent path traversal in download_file via Content-Disposition header#180

Open
kartikganesh wants to merge 1 commit into
fabiocaccamo:mainfrom
kartikganesh:fix/path-traversal-download-file
Open

fix: prevent path traversal in download_file via Content-Disposition header#180
kartikganesh wants to merge 1 commit into
fabiocaccamo:mainfrom
kartikganesh:fix/path-traversal-download-file

Conversation

@kartikganesh
Copy link
Copy Markdown

Summary

  • Fixes a path traversal vulnerability in download_file() where a malicious server could return a Content-Disposition header with directory traversal sequences (e.g. filename="../../.bashrc")
  • The unsanitized filename was passed directly to join_path(), allowing file writes outside the intended download directory
  • Fix applies os.path.basename() to strip directory components from the extracted filename

Security Impact

Severity: High
Type: CWE-22 (Path Traversal)

A malicious HTTP server can cause arbitrary file writes on the filesystem by responding with a crafted Content-Disposition header:

Content-Disposition: attachment; filename="../../.ssh/authorized_keys"

Any application using fsutil.download_file() to download from untrusted URLs is affected.

Test plan

  • Verify download_file with normal URLs still works
  • Verify filenames with ../ sequences are sanitized to just the basename
  • Verify empty basenames (e.g. filename="../../../") fall back to UUID name

🤖 Generated with Claude Code

…header

A malicious server could return a Content-Disposition header with a
filename containing path traversal sequences (e.g. "../../.bashrc"),
causing files to be written outside the intended download directory.

This fix applies os.path.basename() to sanitize the filename after
extraction from headers or URL, ensuring it cannot escape the target
directory.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.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