Skip to content

Commit f451ed7

Browse files
lj1166Iwaniukooo11
andauthored
fix(server): reject out-of-range ListTasks cursor timestamps (a2aproject#1451)
# Description Fixes a2aproject#1450. `ListTasks` with `DatabaseTaskStore` returns HTTP 500 when a structurally valid page token contains an out-of-range timestamp. Protobuf 7.36.2 raises `ValueError` from `Timestamp.FromNanoseconds()`, while `_after_cursor` only converts `OverflowError` into `InvalidParamsError`. Handle both exceptions so these tokens use the existing HTTP 400 / `INVALID_ARGUMENT` / `Invalid page token` response. The production change is one exception clause. Add four regression cases covering values immediately outside the timestamp range and very large positive/negative integers. ## Verification Tested source `main` at `2cc1dd5`, Python 3.12.16, Windows, Protobuf 7.36.2, and SQLite/aiosqlite. Final checks used the exact dependencies exported from `uv.lock` with hash verification. - Before the fix, all four new SQLite cases failed with `ValueError`; after the fix they pass. PostgreSQL/MySQL variants skip because their DSNs are unset. - Exercised real Starlette REST routes, `DefaultRequestHandlerV2`, and an in-memory SQLite `DatabaseTaskStore` via `httpx.ASGITransport`, without mocking the conversion or store: three invalid timestamps changed from HTTP 500 to HTTP 400; `ts=null` and `ts=0` remained HTTP 200. No provider requests. - `scripts/lint.sh`: passed Ruff check/format and ty. ty reports three existing unused-ignore warnings in untouched client files; no type errors. - The suite with the two Windows-incompatible integration modules below excluded: **2,556 passed, 215 skipped, 4 xfailed, 1 xpassed**. - The same suite with `--cov=src --cov-report=term-missing --cov-fail-under=88`: **93.57%** coverage, same test counts. - `git diff --check`: passed. The passing suite command (with UTF-8 enabled for sample CLI subprocesses): ```sh PYTHONUTF8=1 uv run pytest --timeout=45 -q \ --ignore=tests/integration/cross_version/client_server/test_client_server.py \ --ignore=tests/integration/push_notifications/test_default_push_notification_support.py ``` ### Full-suite limitations I also ran the unfiltered suite; it was **not green on this Windows host**. Its initial result was 2,553 passed, 215 skipped, 4 xfailed, 1 xpassed, 3 failed, and 8 errors. The three sample CLI failures were GBK console-encoding errors and passed with `PYTHONUTF8=1`. The two excluded modules fail on the unchanged production source too (verified by restoring the original exception clause and rerunning them): the cross-version harness uses POSIX `os.killpg` and `:`-joined `PYTHONPATH` and has short process startup/client timeouts; the push-notification harness cannot pickle a locally created FastAPI application under Windows spawn (`FastAPI.setup.<locals>.openapi`). I left these unrelated harnesses unchanged. Linux, PostgreSQL, and MySQL runs have not been performed locally. ## Checklist - [x] Followed `CONTRIBUTING.md` and read the repository's AI workflow documents. - [x] Conventional Commit title and focused bug fix with regression coverage. - [x] Ran the lint script, unfiltered tests, and the passing suite/coverage commands described above. - [x] No documentation update needed: no public API or schema change. AI disclosure: Codex investigated the code, reproduced the issue, prepared and reviewed this patch, and ran the checks under the account holder's instruction. Independent human review or reproduction is not claimed. Co-authored-by: Mateusz Iwaniuk <45974414+Iwaniukooo11@users.noreply.github.com>
1 parent 14e799c commit f451ed7

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎src/a2a/server/tasks/database_task_store.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ def _after_cursor(self, cursor: ListTasksCursor) -> Any:
345345
)
346346
try:
347347
cursor_timestamp = _ns_to_datetime(cursor.timestamp_ns)
348-
except OverflowError as e:
348+
except (OverflowError, ValueError) as e:
349349
raise InvalidParamsError('Invalid page token') from e
350350
return or_(
351351
timestamp_col < cursor_timestamp,

‎tests/server/tasks/test_database_task_store.py‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@
3636
)
3737
from a2a.utils.constants import DEFAULT_LIST_TASKS_PAGE_SIZE
3838
from a2a.utils.errors import InvalidParamsError
39-
from a2a.utils.task import decode_list_tasks_cursor
39+
from a2a.utils.task import (
40+
ListTasksCursor,
41+
decode_list_tasks_cursor,
42+
encode_list_tasks_cursor,
43+
)
4044
from google.protobuf.json_format import MessageToDict
4145
from sqlalchemy.ext.asyncio import create_async_engine
4246
from sqlalchemy.inspection import inspect
@@ -1170,4 +1174,27 @@ async def test_list_tasks_rejects_malformed_cursor_token(
11701174
)
11711175

11721176

1177+
@pytest.mark.asyncio
1178+
@pytest.mark.parametrize(
1179+
'timestamp_ns',
1180+
[
1181+
pytest.param(-62_135_596_800_000_000_001, id='before-min-timestamp'),
1182+
pytest.param(253_402_300_800_000_000_000, id='after-max-timestamp'),
1183+
pytest.param(-(10**100), id='huge-negative'),
1184+
pytest.param(10**100, id='huge-positive'),
1185+
],
1186+
)
1187+
async def test_list_tasks_rejects_out_of_range_cursor_timestamp(
1188+
db_store_parameterized: DatabaseTaskStore, timestamp_ns: int
1189+
) -> None:
1190+
token = encode_list_tasks_cursor(
1191+
ListTasksCursor(timestamp_ns=timestamp_ns, task_id='t1')
1192+
)
1193+
1194+
with pytest.raises(InvalidParamsError, match='Invalid page token'):
1195+
await db_store_parameterized.list(
1196+
ListTasksRequest(page_size=2, page_token=token), TEST_CONTEXT
1197+
)
1198+
1199+
11731200
# Ensure aiosqlite, asyncpg, and aiomysql are installed in the test environment (added to pyproject.toml).

0 commit comments

Comments
 (0)