Skip to content

fix(backend): keep the picked file name on chat uploads - #15336

Merged
Git-on-my-level merged 1 commit into
mainfrom
fix/chat-upload-keep-picked-name
Sep 21, 2026
Merged

Git-on-my-level merged 1 commit into
mainfrom
fix/chat-upload-keep-picked-name

Conversation

@Git-on-my-level

@Git-on-my-level Git-on-my-level commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #15179. Supersedes the dogfood comparison drafts #15207 and #15210 (best of each: the provider-record fix from the first, the stronger test file from the second).

Chat file uploads saved the backend's uuid-prefixed temp name (ef0c40fc…_photo.png) as FileChat.name instead of the name the user picked. Both POST /v1/files and POST /v2/files stage bytes under Path(tempfile.gettempdir()) / f"{uuid.hex}_{safe_suffix}" for path-traversal safety, and FileChatTool.upload sent that temp handle to openai.files.create, so the provider echoed the temp basename back into response.filename — the value stored on the chat file record.

Fix at the shared upload path: FileChatTool.upload(file_path, file_name=None) accepts the caller's picked name and sends the SDK's (filename, fileobj) tuple form, so the provider record itself carries the original name and result["file_name"] follows it. The v1/v2 handlers pass their already-sanitized safe_suffix (Path(file.filename).name), and upload re-takes Path(file_name).name defensively, so a directory-carrying picked name (../../photo.txt) reduces to its basename. No file_name → unchanged behavior for direct callers.

Differs from #14970, which overrode FileChat.name at each call site; this fixes the name at the upload seam so the provider-side record is correct too.

Verification

  • backend/.venv/bin/python -m pytest tests/unit/test_chat_file_upload_picked_name.py — 6 pass with the fix (both routes × image/document, provider-double sabotage, fallback, path-strip).
  • Same file on the unfixed tree (git stash of the two production files): 5 fail, reproducing the issue exactly (assert '96b2a5a72e49…_photo.png' == 'photo.png' on the provider double; TypeError on the direct kwarg tests).
  • 75 related chat-file / upload-security tests pass (test_chat_file_upload_unsupported, test_chat_file_completions, test_chat_file_gateway_surface, test_chat_file_input_types, test_chat_file_skip_malformed, test_chat_file_vision_request, test_chat_file_vision_params, test_file_upload_endpoint_security, test_file_upload_security).
  • scripts/pr-preflight with this body: clean.

Product invariants affected

none

Failure-Class: none

Review in cubic

Both /v1/files and /v2/files stage each upload under a uuid temp name for
path-traversal safety, and openai.files.create echoes the filename it was
given back as response.filename. Sending the bare temp handle made the
provider record — and the FileChat.name stored from it — carry
<uuid32>_photo.png instead of photo.png (#15179).

FileChatTool.upload now accepts the caller's picked name and sends the
SDK's (filename, fileobj) tuple form so the provider record itself keeps
the picked basename; the routes pass their already-sanitized safe_suffix,
and upload re-takes Path(file_name).name defensively. No file_name means
unchanged behavior for direct callers.

Supersedes the dogfood comparison drafts #15207 and #15210.

Failure-Class: none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bounty proposal] fix(backend): chat file uploads show a uuid in the file name ($25 proposed)

1 participant