From 900d1b65eb87375de6327cd0a34c7910fbaef567 Mon Sep 17 00:00:00 2001 From: Cosolar Date: Sat, 21 Nov 2026 19:41:46 +0800 Subject: [PATCH] fix(gateway): reject an end_at past the UTC range (#8465) * fix(gateway): reject an end_at past the UTC range resolve_end_at only mapped ValueError to the coded 422. A valid wall time near datetime.max moves past year 9999 when converted to UTC, so the OverflowError escaped admission and the request failed with a 500. Map it to the same 422 invalid_request, naming end_at. * fix(gateway): name run_at in UTC overflow errors --------- Co-authored-by: Willem Jiang --- .../app/gateway/scheduled_task_validation.py | 6 ++- backend/docs/API.md | 2 + .../tests/test_scheduled_task_wall_times.py | 41 +++++++++++++++++-- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/backend/app/gateway/scheduled_task_validation.py b/backend/app/gateway/scheduled_task_validation.py index ec15bbeaf0..79dbf10d03 100644 --- a/backend/app/gateway/scheduled_task_validation.py +++ b/backend/app/gateway/scheduled_task_validation.py @@ -75,7 +75,7 @@ def _validate_once_wall_time(schedule_spec: dict[str, Any], timezone: str) -> No if isinstance(run_at, str): try: _resolve_local_datetime(datetime.fromisoformat(run_at), timezone) - except ValueError as exc: + except (ValueError, OverflowError) as exc: raise ValueError(f"run_at: {exc}") from exc @@ -95,7 +95,9 @@ def resolve_end_at(value: datetime | str | None, timezone: str) -> datetime | No raise scheduler_error(422, "invalid_request", "end_at must be an ISO 8601 date-time") try: return _resolve_local_datetime(value, timezone) - except ValueError as exc: + except (ValueError, OverflowError) as exc: + # A valid wall time near datetime.max moves past year 9999 in UTC; + # report that as an unusable end_at instead of letting it escape. raise scheduler_error(422, "invalid_request", f"end_at: {exc}") from exc diff --git a/backend/docs/API.md b/backend/docs/API.md index 044f9d3cd3..c05fab2a2f 100644 --- a/backend/docs/API.md +++ b/backend/docs/API.md @@ -1776,6 +1776,8 @@ specified instant. Unchanged live once definitions keep their previously accepted execution time; a changed definition or a PATCH that rearms a terminal task is validated again. Ordinary Resume retains the admitted stored schedule without revalidating its wall time; a renewed `end_at` is still checked. +Values outside the supported date-time range after UTC conversion return the +same 422 codes, with the offending field named in the message. In a PATCH, sending `null` clears `goal_objective`, `max_runs`, `end_at` or `stop_condition` or `notification_group_connection_id`; for every other field `null` means unchanged (`assistant_id: diff --git a/backend/tests/test_scheduled_task_wall_times.py b/backend/tests/test_scheduled_task_wall_times.py index cbc631d55d..97ce32b5ee 100644 --- a/backend/tests/test_scheduled_task_wall_times.py +++ b/backend/tests/test_scheduled_task_wall_times.py @@ -111,6 +111,31 @@ def test_end_at_preserves_an_aware_second_fold(): assert resolve_end_at(end_at, "America/New_York") == datetime(2027, 11, 7, 6, 30, tzinfo=UTC) +@pytest.mark.asyncio +@pytest.mark.parametrize("operation", ["create", "update", "resume"]) +@pytest.mark.parametrize("end_at", ["9999-12-31T23:59:00", "9999-12-31T23:59:00-05:00"]) +async def test_end_at_past_the_utc_range_is_rejected(operation, end_at): + with pytest.raises(HTTPException) as caught: + if operation == "create": + body = ScheduledTaskCreateRequest( + title="Report", + prompt="Write the report", + schedule_type="once", + schedule_spec={"run_at": "2027-01-01T09:00:00"}, + timezone="America/New_York", + end_at=end_at, + ) + await validate_scheduled_task_create(body, user_id="owner", thread_store=None, scheduler_config=SchedulerConfig(), assistant_resolver=AsyncMock(return_value="lead_agent"), now=NOW) + elif operation == "update": + body = ScheduledTaskUpdateRequest(end_at=end_at) + await validate_scheduled_task_update(_existing("America/New_York"), body.model_dump(exclude_unset=True), user_id="owner", thread_store=None, scheduler_config=SchedulerConfig(), assistant_resolver=AsyncMock(), used=0, now=NOW) + else: + prepare_resume_updates(_existing("America/New_York", status="paused"), {"end_at": end_at}, used=0, now=NOW) + assert caught.value.status_code == 422 + assert caught.value.detail["code"] == "invalid_request" + assert caught.value.detail["message"].startswith("end_at: ") + + @pytest.mark.asyncio @pytest.mark.parametrize("status", ["enabled", "paused"]) async def test_unchanged_legacy_gap_schedule_keeps_its_already_accepted_instant(status): @@ -141,15 +166,23 @@ async def test_gap_schedule_is_revalidated_when_reinterpreted_or_rearmed(change) @pytest.mark.asyncio @pytest.mark.parametrize("operation", ["create", "update", "reactivate"]) -@pytest.mark.parametrize("run_at", ["not-a-date", "2027-02-30T09:00:00"]) -async def test_invalid_once_date_names_run_at_in_the_admission_error(operation, run_at): +@pytest.mark.parametrize( + ("timezone", "run_at"), + [ + ("UTC", "not-a-date"), + ("UTC", "2027-02-30T09:00:00"), + ("America/New_York", "9999-12-31T23:59:00"), + ("UTC", "9999-12-31T23:59:00-05:00"), + ], +) +async def test_invalid_once_date_names_run_at_in_the_admission_error(operation, timezone, run_at): spec = {"run_at": run_at} with pytest.raises(HTTPException) as caught: if operation == "create": - body = ScheduledTaskCreateRequest(title="Report", prompt="Write the report", schedule_type="once", schedule_spec=spec, timezone="UTC") + body = ScheduledTaskCreateRequest(title="Report", prompt="Write the report", schedule_type="once", schedule_spec=spec, timezone=timezone) await validate_scheduled_task_create(body, user_id="owner", thread_store=None, scheduler_config=SchedulerConfig(), assistant_resolver=AsyncMock(), now=NOW) else: - existing = _existing("UTC", status="completed" if operation == "reactivate" else "enabled") + existing = _existing(timezone, status="completed" if operation == "reactivate" else "enabled") await validate_scheduled_task_update(existing, {"schedule_spec": spec}, user_id="owner", thread_store=None, scheduler_config=SchedulerConfig(), assistant_resolver=AsyncMock(), used=0, now=NOW) assert caught.value.status_code == 422 assert caught.value.detail["code"] == "invalid_schedule"