fix: preserve Unicode NUL in messages and metadata
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
2026-10-16 12:10:23 +03:00
parent 6c8e152899
commit cbccd04810
10 changed files with 562 additions and 16 deletions
+120
View File
@@ -120,6 +120,126 @@ def test_empty_message_is_400(client: httpx.Client, text: str) -> None:
assert items == []
@pytest.mark.parametrize(
"text",
[
"a\x00b",
"\x00",
"\x00Привет 👋\x00\x00",
r"a\u0000b",
"\x00" * 4000,
"👋" * 3999 + "\x00",
],
ids=["embedded", "only-nul", "unicode", "literal-escape", "max-nul", "max-unicode"],
)
def test_message_with_nul_round_trips(client: httpx.Client, text: str) -> None:
alice = register(client, "alice")
chat = create_chat(client, alice)
sent = []
for value in ("before", text, "after"):
response = send(client, alice, chat, value)
assert response.status_code == 201, response.text
message = response.json()
assert message["text"] == value
assert message["chat_id"] == chat["id"]
assert message["sender_id"] == alice["id"]
assert response.headers["X-Request-Id"]
sent.append(message)
items, sizes = read_history(client, alice, chat, limit=1)
assert sizes == [1, 1, 1]
assert items == sent
def test_message_with_nul_over_limit_is_400(client: httpx.Client) -> None:
alice = register(client, "alice")
chat = create_chat(client, alice)
assert_error(send(client, alice, chat, "\x00" * 4001), 400, "invalid_request")
items, _ = read_history(client, alice, chat, limit=50)
assert items == []
@pytest.mark.parametrize(
"value",
[
"a\x00b",
"\x00",
"\x00Привет 👋\x00",
r"a\u0000b",
"\x00" * 100,
"👋" * 99 + "\x00",
],
ids=["embedded", "only-nul", "unicode", "literal-escape", "max-nul", "max-unicode"],
)
def test_user_display_name_with_nul_round_trips(
client: httpx.Client, value: str
) -> None:
username, password = unique_username("nul_name"), unique_password()
response = client.post(
USERS,
json={"username": username, "display_name": value, "password": password},
)
assert response.status_code == 201, response.text
user = response.json()
assert user["display_name"] == value
session = login_response(client, username, password)
assert session.status_code == 201
assert session.json()["user"] == user
actor = {**user, "token": session.json()["token"]}
me = client.get(ME, headers=headers(actor))
assert me.status_code == 200
assert me.json() == user
@pytest.mark.parametrize(
"value",
[
"a\x00b",
"\x00",
"\x00Привет 👋\x00",
r"a\u0000b",
"\x00" * 100,
"👋" * 99 + "\x00",
],
ids=["embedded", "only-nul", "unicode", "literal-escape", "max-nul", "max-unicode"],
)
def test_chat_title_with_nul_round_trips(client: httpx.Client, value: str) -> None:
alice = register(client, "alice")
chat = create_chat(client, alice, title=value)
assert chat["title"] == value
# Карточка читается через кэш, список — напрямую из PostgreSQL.
for _ in range(2):
response = client.get(f"{CHATS}/{chat['id']}", headers=headers(alice))
assert response.status_code == 200
assert response.json() == chat
response = client.get(CHATS, headers=headers(alice))
assert response.status_code == 200
assert response.json()["items"] == [chat]
@pytest.mark.parametrize("field", ["display_name", "title"])
def test_unicode_metadata_with_nul_over_limit_is_400(
client: httpx.Client, field: str
) -> None:
if field == "display_name":
response = client.post(
USERS,
json={
"username": unique_username("long_name"),
"display_name": "\x00" * 101,
"password": unique_password(),
},
)
else:
alice = register(client, "alice")
response = client.post(
CHATS,
json={"title": "\x00" * 101, "member_ids": [alice["id"]]},
headers=headers(alice),
)
assert_error(response, 400, "invalid_request")
def test_duplicate_username_is_409(client: httpx.Client) -> None:
first = register(client, "dup")
second = client.post(