Skip to content

fix(api_server): enforce chunked request body limits - #3949

Closed
Gutslabs wants to merge 1 commit into
NousResearch:mainfrom
Gutslabs:fix/api-server-chunked-body-limit
Closed

fix(api_server): enforce chunked request body limits#3949
Gutslabs wants to merge 1 commit into
NousResearch:mainfrom
Gutslabs:fix/api-server-chunked-body-limit

Conversation

@Gutslabs

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes a request-size limit bypass in the API server.

The API server tried to reject oversized POST bodies early by checking Content-Length, but chunked requests do not send that header. A client could send an oversized JSON body with Transfer-Encoding: chunked and bypass the 1 MB limit entirely.

This change enables aiohttp's real body-size limit, preserves the existing JSON 413 error envelope for oversized requests, and adds a regression test proving chunked requests no longer slip through.

Type of Change

  • Bug fix
  • Security fix
  • Tests

Changes Made

  • Enabled client_max_size=MAX_REQUEST_BYTES on the API server app
  • Normalized middleware order so security headers still wrap body-limit responses
  • Added shared JSON-body parsing that preserves 413 for oversized bodies instead of degrading to 400 invalid JSON
  • Added a regression test for chunked oversized /v1/chat/completions requests

How to Test

  1. Run source .venv/bin/activate
  2. Run python -m pytest tests/gateway/test_api_server.py -q
  3. Send a JSON request larger than 1 MB with Transfer-Encoding: chunked to /v1/chat/completions
  4. Confirm the server returns 413 with code: body_too_large
  5. Confirm normal-sized requests still succeed

Validation

  • python -m pytest tests/gateway/test_api_server.py -q89 passed
  • Manual repro before the fix: a chunked oversized request to /v1/chat/completions returned 200
  • Manual repro after the fix: the same request returns 413 with the OpenAI-style body_too_large error payload

@alt-glitch alt-glitch added P2 Medium — degraded but workaround exists type/security Security vulnerability or hardening comp/gateway Gateway runner, session dispatch, delivery labels May 2, 2026
@alt-glitch

Copy link
Copy Markdown
Collaborator

Related to #3955 (webhook variant) and #12543. Same class of bug — chunked request bypass — in the API server endpoint.

@alt-glitch

Copy link
Copy Markdown
Collaborator

Related to #3955 (webhook variant) and #12543. Same class of bug in the API server endpoint.

teknium1 pushed a commit that referenced this pull request Jul 4, 2026
api_server already caps every read via client_max_size (chunked
included), but when the limit tripped mid-read the handler's broad JSON
except turned it into 400 'Invalid JSON'. Catch
HTTPRequestEntityTooLarge in body_limit_middleware and return the
OpenAI-style 413.

Status-code polish extracted from PR #3949 by @Gutslabs — the PR's core
client_max_size change already exists on main.
teknium1 added a commit that referenced this pull request Jul 5, 2026
…ked-limit

fix(gateway): enforce body-size limits on chunked requests (salvage #3955 + #3949)
habarmc1223-sudo pushed a commit to habarmc1223-sudo/hermes-agent-fluxmem that referenced this pull request Jul 8, 2026
api_server already caps every read via client_max_size (chunked
included), but when the limit tripped mid-read the handler's broad JSON
except turned it into 400 'Invalid JSON'. Catch
HTTPRequestEntityTooLarge in body_limit_middleware and return the
OpenAI-style 413.

Status-code polish extracted from PR NousResearch#3949 by @Gutslabs — the PR's core
client_max_size change already exists on main.
habarmc1223-sudo pushed a commit to habarmc1223-sudo/hermes-agent-fluxmem that referenced this pull request Jul 8, 2026
…webhook-chunked-limit

fix(gateway): enforce body-size limits on chunked requests (salvage NousResearch#3955 + NousResearch#3949)
@teknium1

Copy link
Copy Markdown
Contributor

Thanks for the security report and regression coverage. This has already been implemented on main via a salvage of this PR's behavior.

  • gateway/platforms/api_server.py:4767 configures aiohttp with client_max_size=MAX_REQUEST_BYTES, covering chunked bodies without Content-Length.
  • gateway/platforms/api_server.py:674-681 catches HTTPRequestEntityTooLarge and returns the OpenAI-style 413 / body_too_large envelope.
  • Commit 2b4ec0082a4734758a4cda43cf81d1b9b93f9e7e explicitly credits PR fix(api_server): enforce chunked request body limits #3949 for the status-code handling; it shipped in v2026.7.7.

Automated hermes-sweeper review.

@teknium1 teknium1 closed this Jul 12, 2026
@teknium1 teknium1 added the sweeper:implemented-on-main Sweeper: behavior already present on current main label Jul 12, 2026
santhreal pushed a commit to santhreal/hermes-agent that referenced this pull request Jul 13, 2026
api_server already caps every read via client_max_size (chunked
included), but when the limit tripped mid-read the handler's broad JSON
except turned it into 400 'Invalid JSON'. Catch
HTTPRequestEntityTooLarge in body_limit_middleware and return the
OpenAI-style 413.

Status-code polish extracted from PR NousResearch#3949 by @Gutslabs — the PR's core
client_max_size change already exists on main.
santhreal pushed a commit to santhreal/hermes-agent that referenced this pull request Jul 13, 2026
…webhook-chunked-limit

fix(gateway): enforce body-size limits on chunked requests (salvage NousResearch#3955 + NousResearch#3949)
Gravezzz pushed a commit to Gravezzz/hermes-agent that referenced this pull request Jul 21, 2026
api_server already caps every read via client_max_size (chunked
included), but when the limit tripped mid-read the handler's broad JSON
except turned it into 400 'Invalid JSON'. Catch
HTTPRequestEntityTooLarge in body_limit_middleware and return the
OpenAI-style 413.

Status-code polish extracted from PR NousResearch#3949 by @Gutslabs — the PR's core
client_max_size change already exists on main.
Gravezzz pushed a commit to Gravezzz/hermes-agent that referenced this pull request Jul 21, 2026
…webhook-chunked-limit

fix(gateway): enforce body-size limits on chunked requests (salvage NousResearch#3955 + NousResearch#3949)
leewenjie pushed a commit to leewenjie/hermes-agent that referenced this pull request Aug 7, 2026
api_server already caps every read via client_max_size (chunked
included), but when the limit tripped mid-read the handler's broad JSON
except turned it into 400 'Invalid JSON'. Catch
HTTPRequestEntityTooLarge in body_limit_middleware and return the
OpenAI-style 413.

Status-code polish extracted from PR NousResearch#3949 by @Gutslabs — the PR's core
client_max_size change already exists on main.
leewenjie pushed a commit to leewenjie/hermes-agent that referenced this pull request Aug 7, 2026
…webhook-chunked-limit

fix(gateway): enforce body-size limits on chunked requests (salvage NousResearch#3955 + NousResearch#3949)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp/gateway Gateway runner, session dispatch, delivery P2 Medium — degraded but workaround exists sweeper:implemented-on-main Sweeper: behavior already present on current main type/security Security vulnerability or hardening

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants