Skip to content

Commit 05392b3

Browse files
fix(server): reject boolean JSON-RPC request IDs (a2aproject#1350)
# Description JSON-RPC requests with `id: true` or `id: false` currently reach the request handler and produce responses containing the same boolean ID. Python's `bool` satisfies the existing integer check, but [JSON-RPC 2.0](https://www.jsonrpc.org/specification#request_object) distinguishes Boolean from Number and permits only String, Number or Null identifiers. Reject boolean IDs before dispatch with Invalid Request (`-32600`) and a null response ID. Add request-level tests for both boolean values and passing controls for integer zero, a positive integer, an empty string and a nonempty string. The invalid requests must not invoke the handler. - [x] Follow the `CONTRIBUTING` guide. - [x] Use a Conventional Commits PR title. - [x] Ensure tests and linter pass. - [x] Update appropriate docs if necessary. No documentation change is needed; this enforces the existing JSON-RPC identifier contract. No existing issue found for boolean request IDs. Validation: - On the unchanged base, both boolean regressions fail and all four valid-ID controls pass (initial reproduction on Python 3.12). - `bash scripts/lint.sh`: Ruff lint/format and ty pass. ty reports three unused-ignore warnings in unchanged optional gRPC imports. - `uv run pytest`: 2,290 passed, 183 skipped, 3 xfailed and 1 xpassed on the repository-pinned Python 3.10.22. - `uv run pytest --cov=src --cov-report=term-missing --cov-fail-under=88`: same results; 93.29% coverage, exceeding the 88% requirement. - `git diff --check`: passed. The xfail/xpass cases are marked against a2aproject#869. Skips include PostgreSQL/MySQL and externally configured multi-server cases. Local HTTP/gRPC and sample integration tests ran as part of the full suite; no live model-provider calls were used. AI assistance: OpenAI Codex implementation, reproduction and validation. --------- Co-authored-by: mykytanetipa <mykytanetipa@google.com>
1 parent ad85806 commit 05392b3

2 files changed

Lines changed: 47 additions & 3 deletions

File tree

‎src/a2a/server/routes/jsonrpc_dispatcher.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,11 @@ async def handle_requests(self, request: Request) -> Response: # noqa: PLR0911,
233233
body = await request.json()
234234
if isinstance(body, dict):
235235
request_id = body.get('id')
236-
# Ensure request_id is valid for JSON-RPC response (str/int/None only)
237-
if request_id is not None and not isinstance(
238-
request_id, str | int
236+
# Ensure request_id is valid for JSON-RPC response (str/int/None only;
237+
# exclude bool since bool is a subclass of int in Python)
238+
if isinstance(request_id, bool) or (
239+
request_id is not None
240+
and not isinstance(request_id, str | int)
239241
):
240242
request_id = None
241243
logger.debug('Request body: %s', body)
@@ -257,6 +259,13 @@ async def handle_requests(self, request: Request) -> Response: # noqa: PLR0911,
257259
message="Invalid request: 'jsonrpc' must be exactly '2.0'"
258260
),
259261
)
262+
if isinstance(base_request._id, bool): # noqa: SLF001
263+
return self._generate_error_response(
264+
request_id,
265+
InvalidRequestError(
266+
message='Invalid request: id must be a string, integer, or null'
267+
),
268+
)
260269
except Exception as e:
261270
logger.exception('Failed to validate base JSON-RPC request')
262271
return self._generate_error_response(

‎tests/server/routes/test_jsonrpc_dispatcher.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,41 @@ def _make_send_message_request(
9494
}
9595

9696

97+
class TestJsonRpcRequestIds:
98+
"""JSON booleans are not numeric JSON-RPC request identifiers."""
99+
100+
@pytest.mark.parametrize('request_id', [True, False])
101+
def test_boolean_id_is_rejected(
102+
self, client: TestClient, mock_handler: AsyncMock, request_id: bool
103+
) -> None:
104+
request = _make_send_message_request()
105+
request['id'] = request_id
106+
107+
response = client.post('/', json=request)
108+
body = response.json()
109+
110+
assert response.status_code == 200
111+
assert body['id'] is None
112+
assert body['error']['code'] == -32600
113+
mock_handler.on_message_send.assert_not_awaited()
114+
115+
@pytest.mark.parametrize('request_id', [0, 1, '', 'request'])
116+
def test_integer_and_string_ids_are_preserved(
117+
self, client: TestClient, mock_handler: AsyncMock, request_id: int | str
118+
) -> None:
119+
request = _make_send_message_request()
120+
request['id'] = request_id
121+
122+
response = client.post('/', json=request)
123+
body = response.json()
124+
125+
assert response.status_code == 200
126+
assert body['id'] == request_id
127+
assert type(body['id']) is type(request_id)
128+
assert 'result' in body
129+
mock_handler.on_message_send.assert_awaited_once()
130+
131+
97132
class TestUnrecognizedFields:
98133
"""Unrecognized fields are ignored, for forward compatibility."""
99134

0 commit comments

Comments
 (0)