Repository navigation
fix(backend): contain client-supplied upload filenames (CWE-22) - #19483
Open
custodioomiguelangel-boop wants to merge 1 commit into
Open
custodioomiguelangel-boop wants to merge 1 commit into
custodioomiguelangel-boop wants to merge 1 commit into
Conversation
BasedHardware#6804 fixed path traversal in the /v1/files and /v2/files chat upload routes, but the same pattern survived in five sibling write sites, and the sync staging helper never ported the check its v1 twin already had. Every one of these routes joins UploadFile.filename into a path and then writes the caller's bytes there, so an unsanitized name is an arbitrary file write, not a cosmetic issue: - routers/speech_profile.py: /v3/upload-audio joined the raw name onto _temp/<uid>/. - routers/apps.py: all four app-logo sites (create, clone, update) joined it onto _temp/apps/. - utils/sync/pipeline.py: _retrieve_file_paths_v2 concatenated the name onto syncing/<uid>/<job_id>/ with no separator check, unlike retrieve_file_paths in utils/sync/files.py. The sync case is the most direct: the staging directory exists, so ../../.. resolves through real directories and the write lands wherever the process can write. The timestamp validator cannot catch it, because parse_sync_filename_timestamp reads path.split('/')[-1] and happily parses the trailing component of "../../x_1750000000.bin". Two changes, both aimed at root cause rather than the individual sites: - utils/other/safe_filename.py holds one sanitizer, promoted from the private _safe_upstream_filename in routers/stt.py. routers/chat.py already used Path(file.filename).name for BasedHardware#6804; the logo and audio routes now share a single implementation instead of four copies. - utils/request_validation.py holds require_safe_sync_filename, used by both staging helpers. The duplicated check is what let v1 and v2 drift, so both now call one function. It also rejects dot-only names (".."), which carry no separator and so slip through a separator-only check. Behavior is unchanged for well-formed names, and the temp directories themselves are left as they were. routers/stt.py keeps its 'audio.wav' default and its call site, so that route is only a delegation. Tests: tests/unit/test_upload_filename_traversal.py, 39 hermetic cases covering traversal, absolute, Windows-separator, dot-only, NUL-byte and overlong names. The staging cases assert against the filesystem, first proving the old concatenation escapes and then that the fix confines it. No fastapi import chain required.
Member
|
Verified end to end against main and the PR head: the vulnerabilities are real and the fix is correct. Thank you for the careful work and for the straightforward disclosure note. Confirmed on main (before this PR):
Verified in this PR:
Two minor notes, neither blocking:
For maintainers: main is vulnerable today in these auth-gated routes, so prompt merge attention is worthwhile. The contributor's question - review here vs. take the fix through the private advisory channel per SECURITY.md - needs a human decision. Formal approval left to a human maintainer. by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
This branch has not been deployed
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.
Disclosure note
Your
SECURITY.mdasks that no public issue or PR be opened for an unpatched vulnerability, and asks for private reporting first. I got that order wrong: I opened this PR and a bounty proposal publicly, and the proposal has now been withdrawn and closed. I am submitting the report through private vulnerability reporting instead.I can close this PR if you would rather take the fix through the advisory channel — the branch is on my fork and nothing depends on the PR staying open. If you would rather review it here, it is ready: 6 sites fixed, root cause removed, 39 regression tests,
blackandruffclean.The class is not new, so the risk from this PR being open is mostly the pointers. I would rather you had the patch early than not, which is why I am leaving it up unless you tell me otherwise.
Summary
#6804 fixed path traversal in the
/v1/filesand/v2/fileschat upload routes, but the same pattern survived in five sibling write sites, and the sync staging helper never ported the check its v1 twin already had.Every affected route joins a client-supplied
UploadFile.filenameinto a path and then writes the caller's bytes there, so an unsanitized name is an arbitrary file write (CWE-22) rather than a cosmetic issue:POST /v3/upload-audiorouters/speech_profile.pyf"_temp/{uid}/{file.filename}"routers/apps.pyf"_temp/apps/{file.filename}"utils/sync/pipeline.pyf"{directory}{filename}", no separator checkThe sync case is the most direct. Its staging directory exists, so
../../..resolves through real directories and the write lands anywhere the process can write. The timestamp validator cannot catch it, becauseparse_sync_filename_timestampreadspath.split('/')[-1]and therefore happily validates the trailing component of../../x_1750000000.bin.retrieve_file_pathsinutils/sync/files.pyhas always rejected separators for the same input.Root cause, not just the call sites
utils/other/safe_filename.pyholds one sanitizer, promoted from the private_safe_upstream_filenameinrouters/stt.py.routers/chat.pyalready usedPath(file.filename).namefor fix(security): prevent path traversal in file upload endpoints #6804, so the logo and audio routes now share a single implementation rather than adding a fourth copy.utils/request_validation.pyholdsrequire_safe_sync_filename, called by both staging helpers. The duplicated check is what allowed v1 and v2 to drift apart, so the check now exists once. It also rejects dot-only names (".."), which carry no separator and therefore slip through a separator-only check.Behavior
Unchanged for well-formed names. The temp directories themselves are untouched.
routers/stt.pykeeps itsaudio.wavdefault and its call site, so that route is only a delegation.Verification
tests/unit/test_upload_filename_traversal.py— 39 hermetic cases covering traversal, absolute paths, Windows separators, dot-only names, NUL bytes and overlong names. The staging cases assert against the filesystem: each first proves the old concatenation escapes the directory, then that the fix confines the write.test_file_upload_endpoint_security.pyandtest_file_upload_security.py.black --line-length=120 --skip-string-normalizationclean on every file touched.routers/apps.pyhas pre-existing formatting drift in the OAuth block (lines ~2058-2119) that this change deliberately leaves alone; the four hunks black reports there are all in that untouched code.ruff check --target-version py39introduces no new findings.utils/sync/pipeline.pyreports the same 7 pre-existing ones asorigin/main.Security note
The vulnerability class is already public: #6804 fixed the same pattern in the chat upload routes, and the unsanitized joins are visible in
origin/main. What is new here is which other routes still had them, so this is an incomplete-remediation report rather than a new disclosure.These routes are live, so a maintainer may prefer to stage this. If so, the five raw joins are the entire vulnerable surface and are straightforward to patch by hand; say the word and I will hold.
Product invariants affected: none
Failure-Class: none