Skip to content

Cap ASGI middleware request body buffering - #43

Merged
mstuart merged 4 commits into
mainfrom
codex/propose-fix-for-asgi-middleware-vulnerability
Aug 25, 2026
Merged

Cap ASGI middleware request body buffering#43
mstuart merged 4 commits into
mainfrom
codex/propose-fix-for-asgi-middleware-vulnerability

Conversation

@mstuart

@mstuart mstuart commented Aug 25, 2026

Copy link
Copy Markdown
Owner

Motivation

  • The existing CompressionMiddleware indiscriminately buffered and JSON-parsed entire request bodies, allowing an unauthenticated client to force large in-memory allocations and cause availability/DoS issues.
  • The middleware needed to fail fast and align with the repository's upstream guard (32 MiB) rather than buffering arbitrarily large requests.

Description

  • Add DEFAULT_MAX_BODY_BYTES and a max_body_bytes constructor parameter to CompressionMiddleware with validation to ensure a positive limit.
  • Perform an early Content-Length check and send an immediate HTTP 413 response when the declared size exceeds the configured limit, avoiding invocation of the inner app.
  • Replace repeated bytes concatenation with bounded chunk collection and a running byte counter that raises and triggers a 413 before buffering exceeds the limit for chunked request bodies.
  • Add a _send_payload_too_large ASGI helper to emit a standards-compatible 413 response and add regression tests that cover oversized declared Content-Length and oversized chunked bodies.

Testing

  • cargo build -p tare-py completed successfully.
  • With a locally-built Python extension, PYTHONPATH=/tmp/tare-python-test python -m pytest crates/tare-py/tests/test_integrations.py -q passed (13 tests).
  • Running the test module without building the native extension results in tests being skipped via 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.py completed successfully.

Codex Task

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread crates/tare-py/python/tare/integrations.py
Comment thread crates/tare-py/python/tare/integrations.py Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread crates/tare-py/python/tare/integrations.py Outdated

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment thread crates/tare-py/python/tare/integrations.py
_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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge 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 👍 / 👎.

@mstuart
mstuart merged commit 1b9b91c into main Aug 25, 2026
7 checks passed
@mstuart
mstuart deleted the codex/propose-fix-for-asgi-middleware-vulnerability branch August 25, 2026 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant