Enforce UTF-8 JSON and accept decimal limits with leading zeros

This commit is contained in:
2026-11-07 08:46:57 +03:00
parent a38c889df2
commit 5e7c93a036
3 changed files with 41 additions and 2 deletions
+10
View File
@@ -0,0 +1,10 @@
{
"result": "EXPECTED_FAILURE_BEFORE_FIX",
"checks": {
"utf16_json": "201, expected 400",
"utf32_json": "201, expected 400",
"leading_zero_limit": "400, expected 200"
},
"command": "BASE_URL=<old-image-url> .venv/bin/python -m pytest student-tests/test_contract_edges.py -q",
"old_revision": "2f0a8672be67af9e62d2693c3df9bd683fcdae57"
}
+5 -2
View File
@@ -42,7 +42,7 @@ async def body(request: Request, model: type[Input]) -> Input:
data = await request.body()
return model.model_validate(
json.loads(
data,
data.decode("utf-8"),
parse_constant=lambda _: (_ for _ in ()).throw(ValueError()),
)
)
@@ -56,7 +56,10 @@ def pagination(request: Request) -> tuple[int, str | None]:
if len(limits) > 1 or len(cursors) > 1:
raise invalid()
raw = limits[0] if limits else "50"
if not raw.isascii() or not raw.isdecimal() or len(raw) > 3:
if not raw.isascii() or not raw.isdecimal():
raise invalid()
raw = raw.lstrip("0") or "0"
if len(raw) > 3:
raise invalid()
limit = int(raw)
if not 1 <= limit <= 100:
+26
View File
@@ -0,0 +1,26 @@
import json
from uuid import uuid4
import pytest
from conftest import check, headers
@pytest.mark.parametrize("encoding", ["utf-16", "utf-32"])
def test_only_utf8_json(client, encoding):
payload = {"username": "u_" + uuid4().hex[:20], "display_name": "UTF"}
payload["password"] = "Synthetic-encoding-test-123"
result = client.post(
"/api/v1/users",
content=json.dumps(payload).encode(encoding),
headers={"Content-Type": "application/json"},
)
check(result, 400, "Error")
def test_leading_zero_limit(client, actors, chat):
result = client.get(
"/api/v1/chats/" + chat["id"] + "/messages",
headers=headers(actors[0]),
params={"limit": "0001"},
)
check(result, 200, "MessagePage")