Skip to content

fix(backend): contain client-supplied upload filenames (CWE-22) - #19483

Open
custodioomiguelangel-boop wants to merge 1 commit into
BasedHardware:mainfrom
custodioomiguelangel-boop:fix/path-traversal-upload-filenames
Open

custodioomiguelangel-boop wants to merge 1 commit into
BasedHardware:mainfrom
custodioomiguelangel-boop:fix/path-traversal-upload-filenames

Conversation

@custodioomiguelangel-boop

@custodioomiguelangel-boop custodioomiguelangel-boop commented Sep 27, 2026 •

Copy link
Copy Markdown

Disclosure note

Your SECURITY.md asks 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, black and ruff clean.

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/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 affected route joins a client-supplied UploadFile.filename into 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:

Route Site Was
POST /v3/upload-audio routers/speech_profile.py f"_temp/{uid}/{file.filename}"
app logo create / clone / update (4 sites) routers/apps.py f"_temp/apps/{file.filename}"
sync staging v2 utils/sync/pipeline.py f"{directory}{filename}", no separator check

The 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, because parse_sync_filename_timestamp reads path.split('/')[-1] and therefore happily validates the trailing component of ../../x_1750000000.bin. retrieve_file_paths in utils/sync/files.py has always rejected separators for the same input.

Root cause, not just the call 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 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.py holds require_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.py keeps its audio.wav default 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.
  • 65 tests pass together with the existing test_file_upload_endpoint_security.py and test_file_upload_security.py.
  • black --line-length=120 --skip-string-normalization clean on every file touched. routers/apps.py has 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 py39 introduces no new findings. utils/sync/pipeline.py reports the same 7 pre-existing ones as origin/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

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.
@Git-on-my-level

Copy link
Copy Markdown
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):

  • backend/routers/speech_profile.py:112 - f"_temp/{uid}/{file.filename}" writes client-controlled bytes at a client-chosen path (POST /v3/upload-audio, auth-gated).
  • backend/routers/apps.py:873/930/971/1100 - the same raw join at all four app-logo/persona sites.
  • backend/utils/sync/pipeline.py _retrieve_file_paths_v2 - concatenated the raw name onto syncing//<job_id>/ with no separator check, unlike its v1 twin retrieve_file_paths in backend/utils/sync/files.py. I also reproduced the masking claim: parse_sync_filename_timestamp("../../x_1750000000.bin") returns 1750000000, so the timestamp validator genuinely cannot catch this.

Verified in this PR:

  • backend/utils/other/safe_filename.py - executed the sanitizer directly: traversal, absolute-path, Windows-separator, dot-only, NUL-byte, and overlong inputs all reduce to a single safe component (an exhaustive short-input sweep found no escapes), and ordinary names like omi-logo.v2.png pass through unchanged.
  • backend/utils/request_validation.py - require_safe_sync_filename preserves v1's reject-don't-sanitize behavior and adds a rejection for dot-only names ("..", "...") that a separator-only check misses.
  • backend/routers/stt.py - pure delegation to the shared sanitizer; the existing audio.wav default and call site are preserved, so no behavior change for that route.
  • backend/routers/apps.py + backend/routers/speech_profile.py - all five joins now go through safe_filename with sensible defaults (logo, sample.wav).
  • backend/utils/sync/files.py + backend/utils/sync/pipeline.py - both staging helpers now share one check, removing the drift that caused this.
  • backend/tests/unit/test_upload_filename_traversal.py - good hermetic coverage (39 cases), including filesystem-level proofs that the old concatenation escapes and the new construction cannot.

Two minor notes, neither blocking:

  1. In test_staging_write_cannot_escape_the_job_directory, the evil/victim setup block is duplicated (the same lines appear twice back to back); harmless, but could be deduped.
  2. Follow-up outside this PR's scope: backend/routers/imports.py:65 joins f"{job.id}{file.filename}" into TEMP_DIR. The job.id prefix means traversal needs a cooperating prefix, and the route requires a .zip name plus auth, but routing it through safe_filename too would close the last cousin.

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 need human response.

@Git-on-my-level Git-on-my-level added positive-signal Automation verified a genuine fix/quality contribution security-review Touches auth, provider routing, secrets, or security-sensitive surfaces needs-maintainer-review Needs a human maintainer to sign off before merge backend Backend Task (python) labels Sep 27, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend Backend Task (python) needs-maintainer-review Needs a human maintainer to sign off before merge positive-signal Automation verified a genuine fix/quality contribution security-review Touches auth, provider routing, secrets, or security-sensitive surfaces

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants