fix: reject tampered cursors and non-canonical UUIDs
decode_cursor checked only for KeyError, TypeError and ValueError, so a
server-issued cursor whose item id was replaced with [], {}, 123 or
false crashed UUID() with AttributeError and answered 500. It now checks
the types of "t" and "i" before parsing and accepts only the exact
string the server would issue for the parsed key: re-encoding must give
the received cursor, so v=true, an extra field or the id in another
notation is a 400 instead of a page.
pydantic's UUID also accepts ids without hyphens, with the urn:uuid:
prefix and in braces, wider than the contract's format: uuid. chat_id in
the path of the three chat endpoints and member_ids elements now use
UuidIn, which requires the 8-4-4-4-12 form in any case. Response models
and the OpenAPI schema are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -87,7 +87,7 @@
|
||||
- [x] 2.5 `make lint`, затем commit тестов и синхронизированных спеков
|
||||
(после подтверждения; сообщение вроде `test: cover tampered cursors
|
||||
and UUID input forms`). Проверка: `git status` чист.
|
||||
- [ ] 2.6 Красный прогон на этом commit: `make test_isolated`. Ожидание
|
||||
- [x] 2.6 Красный прогон на этом commit: `make test_isolated`. Ожидание
|
||||
(решение 5):
|
||||
- красные: «Изменённый курсор» на обоих списках (500 у `"i"` не того
|
||||
типа, 200 у остальных подмен), «Другая запись UUID» (200 / 201);
|
||||
@@ -99,7 +99,7 @@
|
||||
|
||||
## 3. Исправление (решения 1–2)
|
||||
|
||||
- [ ] 3.1 `UUID_PATTERN` (8-4-4-4-12, шестнадцатеричные цифры в любом
|
||||
- [x] 3.1 `UUID_PATTERN` (8-4-4-4-12, шестнадцатеричные цифры в любом
|
||||
регистре) в `src/utils/constants.py`. В `src/api/schemas/common.py` —
|
||||
тип UUID входа: `Annotated[UUID, BeforeValidator(...)]` с
|
||||
`re.fullmatch`; не-строку пропускает дальше. Проверка: `uv run python
|
||||
@@ -107,12 +107,12 @@
|
||||
- канон в обоих регистрах принимается;
|
||||
- запись без дефисов, `urn:uuid:`, `{…}`, пробел, `\n` в конце,
|
||||
`[]`, `{}`, `123`, `false`, `null` — ошибка валидации.
|
||||
- [ ] 3.2 Тип UUID входа у `member_ids` (`src/api/schemas/chat.py`) и у
|
||||
- [x] 3.2 Тип UUID входа у `member_ids` (`src/api/schemas/chat.py`) и у
|
||||
`chat_id` трёх ручек (`src/api/routers/v1/chat.py`, `message.py`).
|
||||
Модели ответов не меняются. Проверка: `make run_dev`, в
|
||||
`/openapi.json` у `chat_id` по-прежнему `{"type": "string",
|
||||
"format": "uuid"}`.
|
||||
- [ ] 3.3 `decode_cursor` (`src/utils/cursor.py`):
|
||||
- [x] 3.3 `decode_cursor` (`src/utils/cursor.py`):
|
||||
- `t` и `i` — строки;
|
||||
- `except` только `ValueError`;
|
||||
- обратная сборка через `encode_cursor` и сравнение со входом;
|
||||
@@ -121,10 +121,10 @@
|
||||
Проверка: `uv run python -c` — на всех вариантах 2.1 и на
|
||||
`garbage`, `eyJ4IjoxfQ`, `!!!` только `CursorError`, другого
|
||||
исключения нет; на выданном курсоре — тот же ключ.
|
||||
- [ ] 3.4 `make lint`, `make test_isolated` — все тесты зелёные, 4 теста
|
||||
- [x] 3.4 `make lint`, `make test_isolated` — все тесты зелёные, 4 теста
|
||||
реплик пропущены с причиной «one API process». Вывод — в журнал
|
||||
этапа.
|
||||
- [ ] 3.5 Правило `.claude/rules/api-contract.md`:
|
||||
- [x] 3.5 Правило `.claude/rules/api-contract.md`:
|
||||
- «Валидация → 400»: pydantic принимает UUID шире `format: uuid`;
|
||||
вход с UUID (путь и тело) — только через тип UUID входа из
|
||||
`common.py`, ответы — `UUID`;
|
||||
|
||||
Reference in New Issue
Block a user