Cap ASGI middleware request body buffering - #43
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 5e59cda10a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: afb3c3176f
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 55e0e337be
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
_RequestBodyTooLarge subclassed Exception, so when CompressionMiddleware wraps an app with its own catch-all exception middleware, that inner middleware caught the signal, emitted a 500, and marked the response started before our handler could send the 413. Subclass BaseException so the signal propagates cleanly to the 413 handler regardless of inner application exception handling. Addresses Codex review comment on #43.
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 330bc7965a
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| DEFAULT_MAX_BODY_BYTES = 32 * 1024 * 1024 | ||
|
|
||
|
|
||
| class _RequestBodyTooLarge(BaseException): |
There was a problem hiding this comment.
Avoid passing a BaseException through BaseHTTPMiddleware
When the wrapped Starlette/FastAPI app contains BaseHTTPMiddleware (including middleware registered with @app.middleware("http")), this signal escapes its except Exception task, while call_next concurrently raises RuntimeError("No response returned."); AnyIO consequently produces a multi-error BaseExceptionGroup, which the outer except _RequestBodyTooLarge does not match. Oversized chunked requests then get an uncaught server error instead of the documented 413. Fresh evidence is a reproduction against Starlette's BaseHTTPMiddleware with this revised BaseException signal.
Useful? React with 👍 / 👎.
Motivation
CompressionMiddlewareindiscriminately buffered and JSON-parsed entire request bodies, allowing an unauthenticated client to force large in-memory allocations and cause availability/DoS issues.Description
DEFAULT_MAX_BODY_BYTESand amax_body_bytesconstructor parameter toCompressionMiddlewarewith validation to ensure a positive limit.Content-Lengthcheck and send an immediate HTTP 413 response when the declared size exceeds the configured limit, avoiding invocation of the inner app._send_payload_too_largeASGI helper to emit a standards-compatible 413 response and add regression tests that cover oversized declaredContent-Lengthand oversized chunked bodies.Testing
cargo build -p tare-pycompleted successfully.PYTHONPATH=/tmp/tare-python-test python -m pytest crates/tare-py/tests/test_integrations.py -qpassed (13 tests).pytest.importorskip, as expected for this repository's test setup.python -m compileall -q crates/tare-py/python/tare crates/tare-py/tests/test_integrations.pycompleted successfully.Codex Task