Sanitize Content-Disposition filename to a safe basename - #334
Open
Flerpharos wants to merge 1 commit into
Open
Conversation
The direct-API attachment path stored msg.get_filename() verbatim, while the S3-fallback path already reduced its filename to a sanitized basename. A server response with a Content-Disposition filename like "../../x" could therefore leave Attachment.filename carrying path-traversal sequences. Factor the sanitization into a shared _safe_basename() helper (used by both _s3_filename_from_url and the Content-Disposition path) that also normalizes backslashes before reducing to a basename, so Windows-style traversal sequences are handled too. Fixes #326 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #326
src/labapi/entry/entries/attachment.pyused the server-suppliedContent-Dispositionfilename (msg.get_filename()) verbatim, while the S3-fallback path (_s3_filename_from_url) already reduced its filename to a sanitized basename. A download response with aContent-Dispositionfilename such as../../xcould therefore leaveAttachment.filenamecarrying path-traversal sequences.Fix
_s3_filename_from_urlinto a shared_safe_basename()helper: reduces toPurePosixPath(name).name, rejectsNone/empty/./.., and normalizes backslashes to forward slashes first so Windows-style traversal sequences (..\..\x) are also handled._ensure_attachmentnow applies_safe_basename()tomsg.get_filename()before it becomesAttachment.filename.self._filename, supplied at upload time) is left as-is — unrelated to this server-response parsing path, and already covered by an existing test asserting it takes precedence unchanged.Tests
Added
test_attachment_entry_sanitizes_content_disposition_filename(parametrized) intests/entry/entries/test_attachment.pycovering:../../x→x..\..\x(backslash variant) →xreport.pdf) is unchangedVerification
uv run pytest --ignore=tests/test_integration.py -q— 433 passed, 1 skippeduv run ruff check .— all checks passeduv run ruff format --check .— all files formatted