fix: harden SSE transport and enforce request body size limit - #1184
Open
ez-lbz wants to merge 3 commits into
Open
fix: harden SSE transport and enforce request body size limit#1184ez-lbz wants to merge 3 commits into
ez-lbz wants to merge 3 commits into
Conversation
🧪 Code Coverage (vs
|
| Base | PR | Delta | |
|---|---|---|---|
| src/a2a/server/events/event_queue_v2.py | 91.79% | 91.28% | 🔴 -0.51% |
| src/a2a/server/routes/common.py | 89.66% | 88.33% | 🔴 -1.32% |
| src/a2a/server/routes/jsonrpc_dispatcher.py | 85.44% | 86.59% | 🟢 +1.15% |
| src/a2a/server/routes/rest_dispatcher.py | 94.25% | 92.43% | 🔴 -1.82% |
| src/a2a/utils/telemetry.py | 91.47% | 90.70% | 🔴 -0.78% |
| Total | 93.00% | 92.96% | 🔴 -0.04% |
Generated by coverage-comment.yml
Use the literal 413 as the ImportError fallback instead of the Any special form, and move the ty ignore comment to the line that actually errors so the type checker suppression takes effect.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
1. Configure SSE heartbeat and stream timeout explicitly
Problem:
EventSourceResponsewas created without explicitping/send_timeoutinsrc/a2a/server/routes/jsonrpc_dispatcher.py(_create_response) andsrc/a2a/server/routes/rest_dispatcher.py(_handle_streaming). With the installedsse-starlette, the defaultpingisNone— no heartbeat is emitted — so idle streams send nothing and dead clients are never detected; there is also no cap on how long a stream may sit without producing output.Fix (src/a2a/server/routes/jsonrpc_dispatcher.py, src/a2a/server/routes/rest_dispatcher.py):
SSE_PING_INTERVAL_SECONDS = 15andSSE_SEND_TIMEOUT_SECONDS = 300tosrc/a2a/utils/constants.py.EventSourceResponsecreated by both dispatchers now passesping=15(heartbeat frame every 15s) andsend_timeout=300(tear down a stream that makes no progress for 5 minutes).2. Enforce a request body size limit
Problem: No limit was enforced on the HTTP request body. A client could POST an arbitrarily large payload, which the JSON-RPC (
request.json()) and REST (request.body()) paths buffer fully in memory — a memory-exhaustion / DoS vector.Fix (src/a2a/server/routes/common.py, src/a2a/server/routes/jsonrpc_dispatcher.py, src/a2a/server/routes/rest_dispatcher.py):
MAX_REQUEST_BODY_SIZE = 10 MiBtosrc/a2a/utils/constants.py.read_request_body_with_limit()inroutes/common.py: it fast-rejects via theContent-Lengthheader when present, and otherwise streams the body in chunks with an incremental cap, so oversized chunked bodies are rejected while being read rather than buffered unboundedly. The body is cached on the request so existingrequest.body()/stream()consumers keep working.handle_requestsnow reads through the limited reader; the existingHTTPException(413)handling returnsInvalidRequestError("Payload too large")._handle_streamingpre-consume,on_message_send,on_message_send_stream,set_push_notification) now use a wrapper that converts the 413 intoInvalidRequestError("Payload too large")(400).Testing
./.venv/Scripts/python -m pytest tests/server/routes/test_jsonrpc_dispatcher.py tests/server/routes/test_rest_dispatcher.py -q→ 39 passed (includes new tests: oversized body rejected via body content and viaContent-Length, andping_interval/send_timeoutassertions on the returnedEventSourceResponsefor both dispatchers)../.venv/Scripts/python -m pytest tests/server/ tests/compat/ -q→ 924 passed, 90 skipped, 3 xfailed (pre-existing xfails reference upstream issue [Feat]: Improve server concurrency architecture #869)../.venv/Scripts/python -m ruff checkon all modified files: clean."Payload too large"instead of being buffered.