fix whitespace and NULL validation

This commit is contained in:
2026-10-28 20:31:49 +03:00
parent 7a72faf0e9
commit f75ad8cc97
15 changed files with 248 additions and 145 deletions
+33
View File
@@ -263,3 +263,36 @@ async def test_unknown_user_takes_comparable_time_to_wrong_password(
unknown_user = await measure("no_such_user")
assert unknown_user > wrong_password * 0.5
async def test_invalid_basic_username_returns_401_not_500(client: AsyncClient):
user = await register(client)
reference = await client.post(
"/api/v1/auth/sessions",
headers=basic_header(user["username"], "wrong-password-123"),
)
for username in ("bad\u0000name", "UPPER", "no", "x" * 33, "имя", "a b c"):
response = await client.post(
"/api/v1/auth/sessions",
headers=basic_header(username, "wrong-password-123"),
)
assert response.status_code == 401, username
assert (
response.json()["error"]["message"] == reference.json()["error"]["message"]
)
assert (
response.headers["WWW-Authenticate"]
== reference.headers["WWW-Authenticate"]
)
async def test_whitespace_only_password_at_login_returns_401(client: AsyncClient):
user = await register(client)
response = await client.post(
"/api/v1/auth/sessions", headers=basic_header(user["username"], " " * 12)
)
assert response.status_code == 401
assert response.headers["WWW-Authenticate"].startswith("Basic")
+13
View File
@@ -194,3 +194,16 @@ async def test_chat_endpoints_require_bearer(client: AsyncClient):
assert (
await client.post("/api/v1/chats", json={"title": "x", "member_ids": []})
).status_code in (401, 400)
async def test_nul_in_chat_title_is_rejected(client: AsyncClient):
alice = await create_user(client)
response = await client.post(
"/api/v1/chats",
headers=bearer(alice),
json={"title": "chat\u0000title", "member_ids": [alice["id"]]},
)
assert response.status_code == 400
assert response.json()["error"]["code"] == "invalid_request"
+10
View File
@@ -225,3 +225,13 @@ async def test_history_order_is_created_at_then_id(client: AsyncClient):
assert [m["id"] for m in listed] == [m["id"] for m in created]
keys = [(m["created_at"], m["id"]) for m in listed]
assert keys == sorted(keys)
async def test_nul_in_message_text_is_rejected(client: AsyncClient):
alice = await create_user(client)
chat = await create_chat(client, alice)
response = await _message(client, alice, chat, "hello\u0000world")
assert response.status_code == 400
assert response.json()["error"]["code"] == "invalid_request"
+26 -1
View File
@@ -52,7 +52,9 @@ async def test_invalid_user_payloads(client: AsyncClient, body: dict):
async def test_password_length_boundaries_are_accepted(client: AsyncClient):
for index, password in enumerate(["x" * 12, "y" * 128, " " * 12, "п" * 12]):
for index, password in enumerate(
["x" * 12, "y" * 128, "п" * 12, " spaces inside "]
):
response = await client.post(
"/api/v1/users",
json={
@@ -101,3 +103,26 @@ async def test_user_exact_length_boundaries_are_accepted(client: AsyncClient):
user = await register(client, username=("a" * length), display_name="d" * 100)
assert len(user["username"]) == length
assert len(user["display_name"]) == 100
@pytest.mark.parametrize("password", [" " * 12, "\t" * 12, " \n" * 6])
async def test_whitespace_only_password_is_rejected(client: AsyncClient, password: str):
response = await client.post(
"/api/v1/users",
json={"username": "blank_pw", "display_name": "Blank", "password": password},
)
assert response.status_code == 400
assert response.json()["error"]["code"] == "invalid_request"
assert_request_id(response)
@pytest.mark.parametrize("field", ["display_name", "password"])
async def test_nul_in_user_fields_is_rejected(client: AsyncClient, field: str):
body = {"username": "nul_user", "display_name": "Nul", "password": PASSWORD}
body[field] = body[field] + "\u0000tail"
response = await client.post("/api/v1/users", json=body)
assert response.status_code == 400
assert response.json()["error"]["code"] == "invalid_request"