mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-03 20:49:41 +08:00
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>
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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"))
|
||||
|
||||
Reference in New Issue
Block a user