From 66b33e29f263d837d14a2a7cfdeeda2015b3a03d Mon Sep 17 00:00:00 2001 From: Benjamin Schwing <63053132+BenjiAvH@users.noreply.github.com> Date: Wed, 25 Nov 2026 01:31:34 +0000 Subject: [PATCH] fix(notebook): reject non-finite numbers in a PATCHed verdict FastAPI's JSON parser accepts NaN/Infinity, so a crafted entry PATCH stored a verdict that made every later read of the entry fail. Reject it with a 422 in the route: a model validator's 422 body would echo the value and fail to serialise as well. Signed-off-by: Benjamin Schwing <63053132+BenjiAvH@users.noreply.github.com> --- deeptutor/api/routers/question_notebook.py | 10 ++++++++++ tests/api/test_practice.py | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/deeptutor/api/routers/question_notebook.py b/deeptutor/api/routers/question_notebook.py index 699f3745f..9e542dbb7 100644 --- a/deeptutor/api/routers/question_notebook.py +++ b/deeptutor/api/routers/question_notebook.py @@ -453,6 +453,16 @@ async def get_entry(entry_id: int) -> NotebookEntryItem: @router.patch("/entries/{entry_id}") async def update_entry(entry_id: int, payload: EntryUpdateRequest): + if payload.ai_verdict is not None: + try: + json.dumps(payload.ai_verdict, allow_nan=False) + except ValueError: + # Stored, ``NaN``/``Infinity`` would break every later read. Not a + # model validator: its 422 body echoes the value and cannot be + # serialised either. + raise HTTPException( + status_code=422, detail="ai_verdict must not contain NaN or Infinity" + ) from None store = get_sqlite_session_store() updates = payload.model_dump(exclude_none=True) if not updates: diff --git a/tests/api/test_practice.py b/tests/api/test_practice.py index 561fa517b..afa48b044 100644 --- a/tests/api/test_practice.py +++ b/tests/api/test_practice.py @@ -647,6 +647,26 @@ def test_entry_patch_rejects_contradictory_or_malformed_grades(bank, patch): assert client.patch(f"/bank/entries/{entry['id']}", json=patch).status_code == 422 +def test_entry_patch_rejects_non_finite_verdict_numbers(bank): + store, _, client = bank + entry = seed(store) + # ``json.loads`` accepts these literals; stored, they would break every read. + for literal in ("Infinity", "NaN", "1e999"): + body = ( + '{"ai_verdict": {"result": "correct", "final": true, ' + f'"awarded_points": {literal}}}}}' + ) + response = client.patch( + f"/bank/entries/{entry['id']}", + content=body, + headers={"content-type": "application/json"}, + ) + assert response.status_code == 422 + stored = client.get(f"/bank/entries/{entry['id']}") + assert stored.status_code == 200 + assert stored.json()["ai_verdict"] is None + + def test_entry_patch_derives_correctness_from_the_result(bank): store, _, client = bank entry = seed(store, **_open_question(result="ungraded"))