fix(app): prevent path traversal via user_id header in python file upload (#3104)#3118
Open
qfmy83 wants to merge 1 commit into
Open
fix(app): prevent path traversal via user_id header in python file upload (#3104)#3118qfmy83 wants to merge 1 commit into
qfmy83 wants to merge 1 commit into
Conversation
15 tasks
…load (eosphoros-ai#3104) Signed-off-by: 戢祥 <qfmy_250803@qq.com>
8cf3823 to
f0ee57c
Compare
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.
What
The
POST /api/v1/python/file/uploadhandler builds the destination directory from the untrusteduser_idrequest header:user_idis used directly as a path component with no validation, and the existing_resolve_upload_pathcontainment check only validates the filename againstupload_dir— which has already been poisoned byuser_id. A header such asuser_id: ../../../../tmp/xtherefore escapes the uploads directory, letting a client write attacker-controlled content to an arbitrary location on the server (which can escalate to RCE via a startup hook,usercustomize.py, a cron dir, etc.). The default auth dependency is a mock, so the endpoint is unauthenticated. Fixes #3104.Fix
Add
_resolve_upload_dir(base_dir, user_id)that treatsuser_idas a single, opaque path segment: it rejects absolute paths, multi-segment values and./.., then canonicalizes the directory and verifies it stays inside thepython_uploadsroot (mirroring the existing_resolve_upload_pathguard). An invaliduser_idnow returns a clean failure instead of writing outside the root. Legitimate ids (uuid / email-like) are unaffected.Testing
Added
test_python_upload_path_traversal.py:../../tmp/...exploit and other traversal/absolute/multi-segment ids are rejectedNote: this addresses the traversal. The mock auth dependency is a separate hardening concern and is out of scope for this PR.