fix(usage): reject non-finite spend and empty key in page cursors

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
kerry 2026-09-24 04:05:18 +00:00
parent 01a5db4194
commit 650dbf49b4
2 changed files with 8 additions and 1 deletions

View file

@ -2,6 +2,7 @@ import asyncio
import base64
import binascii
import json
import math
from collections.abc import Awaitable, Callable, Mapping, Sequence
from collections.abc import Set as AbstractSet
from dataclasses import dataclass
@ -840,9 +841,12 @@ def decode_key_page_cursor(raw: str) -> KeyPageCursor | None:
except (ValueError, binascii.Error):
return None
try:
return _KEY_PAGE_CURSOR_ADAPTER.validate_json(decoded)
cursor: Final = _KEY_PAGE_CURSOR_ADAPTER.validate_json(decoded)
except ValidationError:
return None
if not math.isfinite(cursor.spend) or cursor.api_key == "":
return None
return cursor
def _build_aggregated_sql_query(

View file

@ -2888,6 +2888,9 @@ class TestKeyPageCursorCodec:
"aGVsbG8=", # valid base64, not json
"eyJmb28iOiAxfQ==", # json object missing both fields
"eyJzcGVuZCI6ICIxLjUiLCAiYXBpX2tleSI6IDd9", # wrong types
"eyJzcGVuZCI6IE5hTiwgImFwaV9rZXkiOiAiayJ9", # {"spend": NaN, "api_key": "k"}
"eyJzcGVuZCI6IEluZmluaXR5LCAiYXBpX2tleSI6ICJrIn0=", # {"spend": Infinity, "api_key": "k"}
"eyJzcGVuZCI6IDEuMCwgImFwaV9rZXkiOiAiIn0=", # {"spend": 1.0, "api_key": ""}
],
)
def test_malformed_returns_none(self, raw):