Skip to content

perf(asgi): avoid quadratic request body aggregation and add explicit ASGI body cap #70

Closed

Description

@ZhuchkaTriplesix

Context

Current ASGI bridge request-body read path aggregates chunks using repeated bytes concatenation (body += chunk), which causes repeated realloc/copy under fragmented payloads. It also has no explicit ASGI-side cap before full aggregation.

This can create avoidable CPU/memory pressure and is a practical DoS vector for large or highly chunked bodies.

Current code

  • oxyroute/asgi.py in asgi_to_rsgi(...):
    • read loop appends chunks to body (bytes)
    • no ASGI-side max-body guard

Goals

  1. Replace repeated bytes concatenation with an O(n) accumulation strategy.
  2. Introduce explicit ASGI-side max-body limit (aligned with existing body-size policy).
  3. Return a clear error/termination behavior on limit breach.

Proposed direction

  • Accumulate into bytearray or list[bytes] and join once.
  • Enforce max bytes while reading chunks.
  • Keep behavior consistent with framework-level 413 handling semantics where possible.

Acceptance criteria

  • No body += chunk pattern in ASGI bridge hot read path.
  • Payload limit is enforced during read (not only after full aggregation).
  • Add tests for:
    • normal chunked body path;
    • over-limit chunked body path.
  • Existing ASGI tests continue to pass.

Out of scope

  • Full streaming request-body API for handlers (separate issue).

Activity

  1. added this to the v0.3.0 milestone on Apr 27, 2026
  2. linked a pull request that will close this issueIssue 70 asgi body cap #80on Apr 27, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions