fix: check cursor fields instead of re-encoding

628b5f2 accepted a cursor only if re-encoding the parsed key gave the
same string. That closed the format both ways: any encoding change, even
a compatible one (an optional field, key order, to_rfc3339 shared with
API responses), would turn cursors issued by a replica of the other
version into 400 during make rolling_update, which the tests cannot see.

decode_cursor now checks the fields one by one: v must be exactly the
integer 1, t and i must be strings, i must be in the server's notation
(lowercase 8-4-4-4-12). Unknown fields are ignored, so a tampered cursor
is still a 400 while the format stays open for compatible changes. The
rule records how to change the format: a new CURSOR_VERSION, with both
versions accepted while replicas are replaced.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-16 13:11:57 +03:00
co-authored by Claude Opus 5.5
parent 628b5f2278
commit 8fe467ace0
8 changed files with 170 additions and 76 deletions
+19 -5
View File
@@ -15,8 +15,9 @@ from contract import assert_error, assert_errors, assert_ok
# подмена в содержимом курсора: какие поля заменить
type Change = Callable[[dict[str, Any]], dict[str, Any]]
# подмены выданного курсора (сценарий «Изменённый курсор»): ключ элемента
# не строка, тот же id в другой записи, версия true, лишнее поле
# подмены выданного курсора, которые дают 400 (сценарий «Изменённый
# курсор»): ключ элемента не строка, тот же id в другой записи, версия
# не ровно целое 1
CURSOR_CHANGES: dict[str, Change] = {
"i=[]": lambda _: {"i": []},
"i={}": lambda _: {"i": {}},
@@ -25,6 +26,12 @@ CURSOR_CHANGES: dict[str, Change] = {
"i without hyphens": lambda payload: {"i": payload["i"].replace("-", "")},
"i upper case": lambda payload: {"i": payload["i"].upper()},
"v=true": lambda _: {"v": True},
"v=1.0": lambda _: {"v": 1.0},
}
# и которые принимаются: незнакомое поле игнорируется, иначе новое
# необязательное поле ломало бы курсоры при поочерёдной замене реплик
CURSOR_KEPT: dict[str, Change] = {
"unchanged": lambda _: {},
"extra field": lambda _: {"x": 1},
}
@@ -236,9 +243,16 @@ def test_tampered_cursor(issued: IssuedCursor) -> None:
"""Курсор не собирается с нуля: с чужими `v` или `s` его отсекла бы
проверка версии или области раньше разбора ключа."""
page = issued.listing.page
# контроль: тот же курсор, перекодированный без подмены, принимается
unchanged = tamper(issued.cursor, lambda _: {})
assert_ok(page(cursor=unchanged), 200, issued.listing.schema)
schema = issued.listing.schema
# контроль: без подмены и с незнакомым полем — та же вторая страница
second = assert_ok(page(cursor=issued.cursor), 200, schema)
for name, change in CURSOR_KEPT.items():
kept = assert_ok(
page(cursor=tamper(issued.cursor, change)),
200,
schema,
)
assert kept == second, f"{name}: another page"
assert_errors(
{
name: page(cursor=tamper(issued.cursor, change))