Harden BodyLimitMiddleware edge cases and tie its rules to the real routes
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
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
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.
- Upload bodies are read before authentication.
POST /v1/tasks/{id}/outputandPOST /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. - A response that has started can end truncated. After the limit is crossed,
tracking_senddrops 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. - 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. Content-Lengthparsing is lax.int(value)accepts values such as+5,1_0and5, 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.- Rules ignore the method.
_limit_formatches only the path (body_limit.py:55-64), so aGETorPUTto an upload path gets the large limit before the 405. - 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. - 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.
- The rules are not tested against the real routes.
server/tests/test_body_limit.pyuses 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. - 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.pycovers the middleware order, not a chunked oversize body on a JSON route of the realapp.
Proposed change
- Pre-auth reads: see Open questions.
- 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. - When
exceededis true, treat any exception from the app as the 413. - Accept only ASCII digits in
Content-Length, and answer 400 for duplicated headers with different values. - Add
methodstoBodyLimitRuleand setPOSTon both upload rules. - Keep exact paths, and document in the operator manual that the upload limits apply only to the exact paths. This needs no Caddy change.
- 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.
- 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. - Add a chunked oversize test on a JSON route through the real
app.
Compatibility
- The worker sends
POSTto the exact upload paths with a validContent-Lengthor chunked body and its credentials. It keeps working with the same limits. - The Caddy configuration in
setup-scripts/debian13/06-caddy.shdoes 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-Lengthnow 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-Lengthvalues+5,1_0,5and a duplicated header. - A test shows that
GETandPUTto 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
appreturns 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.
- Option A: authenticate the worker before the body is read (a custom
- Dominant language
- Python
- Stars
- 0
- Forks
- 0
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 10
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from AInvirion/orcaster
-
enhancement P3 server
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
Maintainers usually reply within 1 day
-
enhancement good first issue P3 server
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
bug P2 server
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 1 day
-
bug P3 server
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
enhancement P3 server
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Maintainers usually reply within 1 day
All issues in AInvirion/orcaster
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
daniel-ospina/tortoise#15308 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
Maintainers usually reply within 1 day
-
difficulty:easy prio:low type:bug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
NatronGitHub/Natron#2405 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Jstar269/nakagawa-recomp#3746 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day