Skip to content

Commit a38d438

Browse files
authored
fix: Add Input Validation for Task IDs in TaskManager (#310)
Previously, the `TaskManager` class in `a2a/server/tasks/task_manager.py` did not perform any validation on the `task_id` during initialization. This could lead to silent failures or inconsistencies in task storage if an invalid ID (such as an empty string) was provided. ### Impact Adding this validation improves the robustness of the `TaskManager` and prevents downstream errors in task management, especially for database or in-memory stores. ### Fix In `a2a/server/tasks/task_manager.py`, a check has been added to the `__init__` method of the `TaskManager` to validate the `task_id`. It now ensures that if a `task_id` is provided, it is a non-empty string. If the validation fails, a `ValueError` is raised. A corresponding unit test has been added to `tests/server/tasks/test_task_manager.py` to verify that the validation works as expected.
1 parent ecbbb7e commit a38d438

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/a2a/server/tasks/task_manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def __init__(
4141
initial_message: The `Message` that initiated the task, if any.
4242
Used when creating a new task object.
4343
"""
44+
if task_id is not None and not (isinstance(task_id, str) and task_id):
45+
raise ValueError('Task ID must be a non-empty string')
46+
4447
self.task_id = task_id
4548
self.context_id = context_id
4649
self.task_store = task_store

tests/server/tasks/test_task_manager.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,20 @@ def task_manager(mock_task_store: AsyncMock) -> TaskManager:
4545
)
4646

4747

48+
@pytest.mark.parametrize('invalid_task_id', ['', 123])
49+
def test_task_manager_invalid_task_id(
50+
mock_task_store: AsyncMock, invalid_task_id: Any
51+
):
52+
"""Test that TaskManager raises ValueError for an invalid task_id."""
53+
with pytest.raises(ValueError, match='Task ID must be a non-empty string'):
54+
TaskManager(
55+
task_id=invalid_task_id,
56+
context_id='test_context',
57+
task_store=mock_task_store,
58+
initial_message=None,
59+
)
60+
61+
4862
@pytest.mark.asyncio
4963
async def test_get_task_existing(
5064
task_manager: TaskManager, mock_task_store: AsyncMock

0 commit comments

Comments
 (0)