Harden BodyLimitMiddleware edge cases and tie its rules to the real routes

Open
#80 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
fastapi, python

Research direction

Start with server/orcaster_server/body_limit.py and its tests in server/tests/test_body_limit.py and server/tests/test_body_limit_stack.py; then inspect the upload routes in server/orcaster_server/api/tasks.py, their rules in server/orcaster_server/main.py, and the operator manual. Reproduce the multipart temporary-file case before changing its cleanup. Done means each listed edge case is tested or closed with a reason, the real-app route and chunked-body tests pass, and the Unreleased changelog includes the change.

Written by the indexing model from the issue text.

Description

enhancement P2 question server

Problem

BodyLimitMiddleware caps every request body (server/orcaster_server/body_limit.py:1-15). Two rules give the worker upload routes larger limits (server/orcaster_server/main.py:368-377). The limits hold, but some edge cases and test gaps remain. Each item is independent.

  1. Upload bodies are read before authentication. POST /v1/tasks/{id}/output and POST /v1/tasks/{id}/artifacts (server/orcaster_server/api/tasks.py:133, tasks.py:823) authenticate the worker in a dependency. FastAPI reads and parses the body before it resolves dependencies. A request without valid worker credentials can therefore send a body up to the route limit (server/orcaster_server/config.py:177, config.py:290-296) before it gets 401. The read is bounded, but it is larger than it needs to be.
  2. A response that has started can end truncated. After the limit is crossed, tracking_send drops every later message (body_limit.py:100-108). A handler that starts a streaming response and keeps reading the body ends with a truncated 200 that looks complete to the client.
  3. A wrapped limit exception becomes a 500. contextlib.suppress(_BodyTooLarge) (body_limit.py:113-116) does not catch the exception when a library re-raises it as another type.
  4. Content-Length parsing is lax. int(value) accepts values such as +5, 1_0 and 5, and only the first of duplicated headers is read (body_limit.py:71-84). The received-bytes counter still enforces the limit, so this is not a bypass.
  5. Rules ignore the method. _limit_for matches only the path (body_limit.py:55-64), so a GET or PUT to an upload path gets the large limit before the 405.
  6. Trailing slash. /v1/tasks/1/output/ does not match the rules (main.py:373-375) and gets the default limit. The Caddy matchers also require the exact path (setup-scripts/debian13/06-caddy.sh:96, 06-caddy.sh:103). The worker calls the exact path, so no current caller is affected, but the behavior is not documented.
  7. Multipart temporary files. When the limit is crossed after a file part has started, the parser's cleanup may not run for the limit exception, and a temporary file may stay open until garbage collection. This needs a reproduction first.
  8. The rules are not tested against the real routes. server/tests/test_body_limit.py uses the real rules (test_body_limit.py:119) but stub routes with hand-typed paths (test_body_limit.py:26-27, test_body_limit.py:132-133). A change of router prefix would silently put the upload routes back on the default limit.
  9. No chunked oversize test through the real app. The chunked JSON test uses a bare FastAPI() (test_body_limit.py:66). server/tests/test_body_limit_stack.py covers the middleware order, not a chunked oversize body on a JSON route of the real app.

Proposed change

  1. Pre-auth reads: see Open questions.
  2. After the limit is crossed with a response already started, abort the connection (raise from tracking_send) so the client cannot see a complete response.
  3. When exceeded is true, treat any exception from the app as the 413.
  4. Accept only ASCII digits in Content-Length, and answer 400 for duplicated headers with different values.
  5. Add methods to BodyLimitRule and set POST on both upload rules.
  6. Keep exact paths, and document in the operator manual that the upload limits apply only to the exact paths. This needs no Caddy change.
  7. Write a reproduction for the temporary file. If it is confirmed, close the form or route the limit exception through the parser cleanup, and keep the 413.
  8. Add a test that finds the two upload routes in app.routes, formats their paths with an id, and asserts the effective limit of each. It also asserts that no other route matches a rule.
  9. Add a chunked oversize test on a JSON route through the real app.

Compatibility

  • The worker sends POST to the exact upload paths with a valid Content-Length or chunked body and its credentials. It keeps working with the same limits.
  • The Caddy configuration in setup-scripts/debian13/06-caddy.sh does not change. If a later change touches the matched paths or methods, the app rules and the Caddy matchers must change in the same release.
  • Clients that send a malformed or duplicated Content-Length now get 400. Standard HTTP clients do not do this.
  • No configuration change and no new setting.

Acceptance criteria

  • Each item above is fixed with a test, or closed with a one-line reason in this issue.
  • A test shows that an oversize body after a started response does not end in a complete-looking response.
  • A test shows that a wrapped limit exception returns 413, not 500.
  • Tests cover Content-Length values +5, 1_0, 5 and a duplicated header.
  • A test shows that GET and PUT to an upload path get the default limit.
  • The route-table test fails if an upload route path changes without its rule.
  • A chunked oversize body on a JSON route of the real app returns 413.
  • Existing workers keep uploading output and artifacts under the same limits, and existing deployments keep working without config changes. The Caddy configuration is unchanged.
  • CHANGELOG entry under Unreleased.

Open questions

  • Pre-auth reads on the upload routes:
    • Option A: authenticate the worker before the body is read (a custom APIRoute, or read the body by hand after the dependency).
    • Option B: apply a small pre-auth limit, and raise it only for requests that carry a well-formed worker credential.
    • Option C: keep the current behavior and document the bounded read.
      All three keep existing workers working, because workers always send their credentials. Option A changes no limit and needs no proxy change.
Dominant language
Python
Stars
0
Forks
0
Avg merge
1d 1h
Merged PRs (30d)
10

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from AInvirion/orcaster

All issues in AInvirion/orcaster

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.