fix: sanitize upload filename to prevent path traversal (#638)#641
Open
swaylq wants to merge 1 commit into
Open
fix: sanitize upload filename to prevent path traversal (#638)#641swaylq wants to merge 1 commit into
swaylq wants to merge 1 commit into
Conversation
`AttachmentService.save_upload_file` joined the client-supplied multipart filename onto a fresh temp directory without sanitization. A filename with `../` segments escaped the temp dir, so the upload wrote attacker-controlled bytes to an arbitrary host path and the `finally` cleanup `unlink` then deleted that same traversed path — arbitrary file write and delete behind an endpoint that only requires an unauthenticated session id. Reduce the filename to its basename (normalising POSIX and Windows separators, falling back to `upload.bin` for empty/`.`/`..`) before building the temp path, so writes stay confined to the temp directory. Adds regression tests: a parametrized check of the basename normalisation and an end-to-end test proving a traversal filename can no longer overwrite or delete a victim file outside the temp/WareHouse area. Co-Authored-By: Claude Opus 4.8 <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.
Summary
Fixes #638 — an arbitrary file write/delete in the ChatDev workflow server.
AttachmentService.save_upload_file(server/services/attachment_service.py) built the upload's temp path by joining the client-supplied multipart filename onto a fresh temp directory with no sanitization:A filename like
../../../path/to/victimescapes the temp directory: the upload writes attacker bytes to that path and thefinallycleanup thenunlinks it.register_filebasenames the name after the write, so it doesn't mitigate this. The endpoint (POST /api/uploads/{session_id}) only needs a known session id, which is minted without authentication.Fix
Reduce the filename to its basename before building the temp path — normalising both POSIX (
/) and Windows (\) separators and falling back toupload.binfor empty /./... The write target now always stays inside themkdtempdirectory.Tests
tests/test_attachment_upload_filename.py:./..).save_upload_filewith a traversal filename and asserts a victim file outside the temp/WareHouse area is not overwritten or deleted, and that the stored record uses the sanitized basename.Verified the end-to-end test fails without the fix (victim file is deleted) and passes with it. Full file: 12 passed.
🤖 AI-assisted contribution (Claude Code). Fix and tests reviewed and verified locally.