Skip to content

[Bug]: POST /api/file/upload onto an existing directory, or below a file, returns a masked 500 instead of 400 #5930

Closed

Description

@enyst

I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.

Is there an existing issue for the same bug?

  • I have searched existing issues and this is not a duplicate.

Bug Description

POST /api/file/upload (and the conversation-scoped upload, which forwards to it) answers a wrong destination path with a 500 instead of a client error. Uploading onto a path that is an existing directory, or to a path whose parent is a regular file, returns 500 {"detail": "Internal Server Error", "exception": "500: Failed to upload file: [Errno 21] Is a directory: ..."} (or [Errno 17] File exists). The caller cannot tell a bad path from a server fault, and the same collision on POST /api/file/create_directory is already a 400 Path exists and is not a directory.

Expected Behavior

Both cases are a 4xx (400, consistent with create_directory) with a clear detail, and nothing already on disk is touched:

  • target is an existing directory: 400, e.g. Path is a directory;
  • a parent component is a file: 400, e.g. A parent of the path is not a directory.

Actual Behavior

  • path=<dir> (existing directory): 500 ... Failed to upload file: [Errno 21] Is a directory: '<dir>'.
  • path=<file>/child (parent is a file): 500 ... Failed to upload file: [Errno 17] File exists: '<file>'; a deeper path such as <file>/a/b gives [Errno 20] Not a directory.

Steps to Reproduce

  1. In a checkout of main: uv sync, then SESSION_API_KEY=dev-key uv run python -m openhands.agent_server --host 127.0.0.1 --port 8000.
  2. Prepare paths:
W=$(mktemp -d); mkdir "$W/is-a-dir"; printf 'x\n' > "$W/a-file.txt"
  1. curl -s -H 'X-Session-API-Key: dev-key' -F file=@"$W/a-file.txt" "http://127.0.0.1:8000/api/file/upload?path=$W/is-a-dir" returns 500 [Errno 21] Is a directory.
  2. curl -s -H 'X-Session-API-Key: dev-key' -F file=@"$W/a-file.txt" "http://127.0.0.1:8000/api/file/upload?path=$W/a-file.txt/child" returns 500 [Errno 17] File exists.
  3. Control: curl -s -X POST -H 'X-Session-API-Key: dev-key' "http://127.0.0.1:8000/api/file/create_directory?path=$W/a-file.txt/child" returns 400 Path exists and is not a directory.

With the verify-agent-server skill: control-agent-server map run --file F14 --only F14.upload-onto-directory --fresh reports the known-bug bullet as an expected failure while the bug lasts.

Acceptance Criteria

  • Uploading onto an existing directory returns 400 with a clear detail, and the directory is unchanged.
  • Uploading to a path whose parent (direct or further up) is a regular file returns 400 with a clear detail, and the file is unchanged.
  • Other upload failures (I/O errors, etc.) still return 500.
  • tests/agent_server/test_file_router.py covers both cases.
  • The map bullet F14.upload-onto-directory passes with its # bug marker removed.

Installation Method

uv sync in a checkout of main

Version

main at 6e7a853

Additional Context

Root cause, in openhands-agent-server/openhands/agent_server/file_router.py:

  • _upload_file (line 85) runs target_path.parent.mkdir(parents=True, exist_ok=True) (line 98) and open(target_path, "wb") (line 104); mkdir raises FileExistsError / NotADirectoryError when a parent is a file, and open raises IsADirectoryError when the target is a directory.
  • The handler's except Exception (lines 113-118) turns every non-HTTPException into a 500.
  • create_directory (lines 731-740) already maps FileExistsError / NotADirectoryError to 400 Path exists and is not a directory.

Related: the same _upload_file is touched by the open upload fixes for the response file_size (F14.upload-size) and atomic overwrites (F14.upload-atomic).

Found by the first full agentic run of verify-agent-server; tracked in #5653.


OpenHands AI triage

The following comments and acceptance criteria were added by the OpenHands AI agent.

Triage

Confirmed bug with a clear, reproducible cause. _upload_file in openhands-agent-server/openhands/agent_server/file_router.py runs target_path.parent.mkdir(parents=True, exist_ok=True) and open(target_path, "wb"), then its except Exception maps every non-HTTPException to a 500. A target that is an existing directory raises IsADirectoryError, and a parent component that is a regular file raises FileExistsError/NotADirectoryError; both are caller path errors. The sibling handler create_directory in the same file already maps FileExistsError/NotADirectoryError to 400 Path exists and is not a directory, so the fix should bring upload to the same 4xx behavior. The global POST /api/file/upload route and the conversation-scoped POST /api/conversations/{runtime_conversation_id}/file/upload route share this handler (the same file_router is mounted under the runtime prefix in runtime_router.py), so one fix covers both.

Scope: classify the two destination-path collisions in _upload_file as 400 with a detail that names the cause, leave genuine I/O failures at 500, and cover both cases in tests/agent_server/test_file_router.py.

Non-goals: the response file_size (F14.upload-size) and atomic-overwrite (F14.upload-atomic) fixes are separate and out of scope; no new endpoint; no change to the global route accepting any absolute path the server user can reach; no change to successful upload/overwrite behavior or to parent-directory creation.

Acceptance Criteria

  • Uploading to a path that is an existing directory returns HTTP 400, and the directory and its contents are unchanged.
  • Uploading to a path whose immediate parent is a regular file returns HTTP 400, and the existing file is unchanged.
  • Uploading to a deeper path where a non-immediate ancestor is a regular file (e.g. <file>/a/b) returns HTTP 400, and the existing file is unchanged.
  • In all three cases the response detail is non-empty and identifies the cause: a directory target, or a parent component that is not a directory.
  • Upload failures that are not caller path errors (e.g. an I/O error such as a full disk) still return HTTP 500.
  • The conversation-scoped route POST /api/conversations/{conversation_id}/file/upload returns the same 400 for both collisions, since it forwards to the same handler.
  • Existing success behavior is unchanged: uploading to a new path creates missing parents and returns 200 {"success": true}, and uploading over an existing regular file overwrites it and returns 200.
  • tests/agent_server/test_file_router.py covers both collisions (target is a directory; parent is a file), asserting 400 and that nothing on disk changed.
  • The feature-map bullet F14.upload-onto-directory passes with its # bug marker removed (both recipes return 4xx).

Activity

  1. added
    bug
    on Oct 14, 2026
  2. added
    ready-for-dev
    on Oct 14, 2026
  3. added a commit that references this issue on Oct 14, 2026
    0f38cdb
  4. enyst commented on Oct 14, 2026

    @enyst
    MemberAuthor

    I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.

    Re the triage: no open questions remain, and #5931 implements the scope as written.

    • Status code: 400, as triage specifies and matching create_directory. 409 Conflict was the other option for a directory target. It would make upload and create_directory disagree, so I did not use it. A maintainer can still ask for 409. Neither client (Python RemoteWorkspace.file_upload, TypeScript fileUpload) needs a change either way, because both treat any non-2xx as a failed upload.
    • Details: Path is a directory (the target is a directory) and A parent of the path is not a directory (the direct parent or any ancestor is a file, i.e. FileExistsError / NotADirectoryError from mkdir(parents=True)). Every other exception still maps to 500.
    • Tests: the global route is covered for all three shapes (directory target, direct parent file, deeper ancestor file) in tests/agent_server/test_file_router.py. The conversation-scoped route is covered in tests/agent_server/test_runtime_router.py. All tests assert that nothing on disk changed. The existing success and overwrite tests are unchanged and still pass.
    • Map: F14.upload-onto-directory returns xfail x2 on main and xpass x2 with the fix. With the # bug markers removed it passes, and it now asserts the exact 400 details.
    • Out of scope, as triage says: F14.upload-size (fix(agent-server): report the uploaded file_size in the upload response #5856) and F14.upload-atomic (fix(agent-server): make file upload overwrites atomic #5902). Those PRs touch the same function, so small merge overlaps are expected.

    Generated by Claude Code

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugready-for-dev

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions