forked from mtusi2026/mtusi2026lab2
Enforce UTF-8 JSON and accept decimal limits with leading zeros
This commit is contained in:
@@ -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
@@ -42,7 +42,7 @@ async def body(request: Request, model: type[Input]) -> Input:
|
|||||||
data = await request.body()
|
data = await request.body()
|
||||||
return model.model_validate(
|
return model.model_validate(
|
||||||
json.loads(
|
json.loads(
|
||||||
data,
|
data.decode("utf-8"),
|
||||||
parse_constant=lambda _: (_ for _ in ()).throw(ValueError()),
|
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:
|
if len(limits) > 1 or len(cursors) > 1:
|
||||||
raise invalid()
|
raise invalid()
|
||||||
raw = limits[0] if limits else "50"
|
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()
|
raise invalid()
|
||||||
limit = int(raw)
|
limit = int(raw)
|
||||||
if not 1 <= limit <= 100:
|
if not 1 <= limit <= 100:
|
||||||
|
|||||||
@@ -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")
|
||||||
Reference in New Issue
Block a user