fix: validate task ID length and message content - #1176
Open
ez-lbz wants to merge 2 commits into
Open
Conversation
🧪 Code Coverage (vs
|
| Base | PR | Delta | |
|---|---|---|---|
| src/a2a/server/events/event_queue_v2.py | 91.79% | 91.28% | 🔴 -0.51% |
| src/a2a/server/request_handlers/default_request_handler.py | 98.13% | 98.20% | 🟢 +0.07% |
| src/a2a/server/request_handlers/default_request_handler_v2.py | 94.17% | 94.47% | 🟢 +0.30% |
| src/a2a/utils/task.py | 95.16% | 96.70% | 🟢 +1.54% |
| src/a2a/utils/telemetry.py | 91.47% | 90.70% | 🔴 -0.78% |
| Total | 93.00% | 93.02% | 🟢 +0.02% |
Generated by coverage-comment.yml
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. Reject over-long task IDs before persistence
Problem: No upper bound is enforced on
taskId. A client can send a 1000+ character task ID intasks/get,tasks/cancel,message/send,SubscribeToTaskor any push-notification-config method and the ID is passed straight to the task store and persisted. There is also no check that a provided task ID is non-empty. Affected files:src/a2a/server/request_handlers/default_request_handler.py,src/a2a/server/request_handlers/default_request_handler_v2.py.Fix (src/a2a/utils/task.py, src/a2a/server/request_handlers/default_request_handler.py, src/a2a/server/request_handlers/default_request_handler_v2.py):
MAX_TASK_ID_LENGTH = 1000andvalidate_task_id()insrc/a2a/utils/task.py; it raisesInvalidParamsErrorfor empty task IDs or IDs longer than 1000 characters.validate_task_id()at the entry of every task-ID-taking handler method in both V1 (LegacyRequestHandler) and V2 (DefaultRequestHandlerV2):on_get_task,on_cancel_task,on_subscribe_to_task,on_message_send/on_message_send_stream(via_setup_message_execution/_setup_active_task, only when a task ID is supplied) and the four push-notification-config methods. Validation happens before any store read/write.2. Reject messages with empty content
Problem:
MessageSendaccepts messages with no parts or with parts that carry no content (notext,raw,urlor non-nulldata). Such empty payloads are persisted into task history. The framework's schema validation already rejects an emptypartslist withInvalidParamsError("Validation failed"), but a part with no content (e.g. onlymedia_typeset) passes through unchecked.Fix (src/a2a/utils/task.py, src/a2a/server/request_handlers/default_request_handler.py, src/a2a/server/request_handlers/default_request_handler_v2.py):
validate_message_content()insrc/a2a/utils/task.py, which raisesInvalidParamsErrorif the message has no parts or any part is empty (notext/raw/url, anddatais unset or null)._setup_message_execution(V1) and_setup_active_task(V2), which bothon_message_sendandon_message_send_streamroute through, so the check applies to the streaming path too.Testing
./.venv/Scripts/python -m pytest tests/utils/test_task.py tests/server/request_handlers/test_default_request_handler.py tests/server/request_handlers/test_default_request_handler_v2.py -q→ 159 passed (includes 13 new regression tests: task-ID length limits and empty-content rejection at both the utility and handler level)../.venv/Scripts/python -m ruff checkon all modified files: clean (the twotoo-many-positional-argumentsfindings on the handler__init__lines pre-date this change and exist onmain).InvalidParamsErrorinstead of being processed. This is the intended hardening; clients must send valid task IDs and non-empty message content.