From 5021df4ea8202b4e24e490a122c5c2ca719199a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D1=83=D1=80=D0=BB=D1=8B=D0=B3=D0=B0=D1=8F=D0=BD?= =?UTF-8?q?=D0=BE=D0=B2=D0=B0=20=D0=95=D0=BA=D0=B0=D1=82=D0=B5=D1=80=D0=B8?= =?UTF-8?q?=D0=BD=D0=B0?= Date: Thu, 29 Oct 2026 14:34:10 +0300 Subject: [PATCH] fixed a bug with NUL in strings --- app/internal/store/db/chats.go | 4 +- app/internal/store/db/integration_test.go | 47 +++++++++++++++++++++++ app/internal/store/db/messages.go | 3 +- app/internal/store/db/text.go | 22 +++++++++++ app/internal/store/db/text_test.go | 18 +++++++++ app/internal/store/db/users.go | 8 ++-- evidence/integration-tests.txt | 36 +++++++++-------- evidence/migrations.txt | 28 +++++++------- migrations/00005_escape_nul_in_text.sql | 45 ++++++++++++++++++++++ scripts/integration-tests.sh | 2 +- 10 files changed, 177 insertions(+), 36 deletions(-) create mode 100644 app/internal/store/db/text.go create mode 100644 app/internal/store/db/text_test.go create mode 100644 migrations/00005_escape_nul_in_text.sql diff --git a/app/internal/store/db/chats.go b/app/internal/store/db/chats.go index 1cae9e3c..104ef5f4 100644 --- a/app/internal/store/db/chats.go +++ b/app/internal/store/db/chats.go @@ -50,7 +50,7 @@ func (c *Chats) CreateChat(ctx context.Context, callerID, title string, memberID INSERT INTO chats (title) VALUES ($1) RETURNING id::text, created_at`, - title, + encodeText(title), ).Scan(&chat.ID, &chat.CreatedAt) if err != nil { return domain.Chat{}, mapErr(err) @@ -95,6 +95,7 @@ func (c *Chats) ChatByID(ctx context.Context, callerID, chatID string) (domain.C } return domain.Chat{}, err } + chat.Title = decodeText(chat.Title) chat.CreatedAt = chat.CreatedAt.UTC() return chat, nil } @@ -134,6 +135,7 @@ func (c *Chats) ListChats(ctx context.Context, callerID, cursor string, limit in out, err := pgx.CollectRows(rows, func(r pgx.CollectableRow) (domain.Chat, error) { var ch domain.Chat err := r.Scan(&ch.ID, &ch.Title, &ch.CreatedAt, &ch.MemberIDs) + ch.Title = decodeText(ch.Title) ch.CreatedAt = ch.CreatedAt.UTC() return ch, err }) diff --git a/app/internal/store/db/integration_test.go b/app/internal/store/db/integration_test.go index 65904ec4..b688b64f 100644 --- a/app/internal/store/db/integration_test.go +++ b/app/internal/store/db/integration_test.go @@ -6,6 +6,7 @@ import ( "encoding/hex" "errors" "os" + "strings" "sync" "testing" "time" @@ -128,3 +129,49 @@ func TestMessageRequiresMembership(t *testing.T) { t.Fatalf("err = %v, want ErrNotFound (→ 404)", err) } } + +func TestNULRoundTrip(t *testing.T) { + d, _ := testDB(t) + ctx := context.Background() + const name = "A\x00B\\0\\" + u, err := d.CreateUser(ctx, "nul_"+suffix(), name, "$argon2id$test") + if err != nil { + t.Fatalf("CreateUser: %v", err) + } + if got, err := d.UserByID(ctx, u.ID); err != nil || got.DisplayName != name { + t.Fatalf("UserByID = %q, %v; want %q", got.DisplayName, err, name) + } + if _, _, err := d.CredentialsByUsername(ctx, "a\x00b"); !errors.Is(err, domain.ErrNotFound) { + t.Fatalf("CredentialsByUsername(NUL) err = %v, want ErrNotFound", err) + } + + chat, err := d.CreateChat(ctx, u.ID, name, []string{u.ID}) + if err != nil { + t.Fatalf("CreateChat: %v", err) + } + if got, err := d.ChatByID(ctx, u.ID, chat.ID); err != nil || got.Title != name { + t.Fatalf("ChatByID = %q, %v; want %q", got.Title, err, name) + } + + texts := []string{"\x00", "a\x00b", `\0`, `\\`, strings.Repeat("\x00", 4000), strings.Repeat(`\`, 4000)} + for _, text := range texts { + if _, err := d.AddMessage(ctx, u.ID, chat.ID, text); err != nil { + t.Fatalf("AddMessage(%q...): %v", text[:min(len(text), 8)], err) + } + } + if _, err := d.AddMessage(ctx, u.ID, chat.ID, strings.Repeat(`\`, 4001)); !errors.Is(err, domain.ErrInvalidArgument) { + t.Fatalf("AddMessage(4001 code points) err = %v, want ErrInvalidArgument", err) + } + page, err := d.HistoryPage(ctx, chat.ID, "", 100) + if err != nil { + t.Fatal(err) + } + if len(page.Items) != len(texts) { + t.Fatalf("history len = %d, want %d", len(page.Items), len(texts)) + } + for i, m := range page.Items { + if m.Text != texts[i] { + t.Fatalf("history[%d] = %q, want %q", i, m.Text, texts[i]) + } + } +} diff --git a/app/internal/store/db/messages.go b/app/internal/store/db/messages.go index 6ca80881..78b5118a 100644 --- a/app/internal/store/db/messages.go +++ b/app/internal/store/db/messages.go @@ -26,7 +26,7 @@ func (m *Messages) AddMessage(ctx context.Context, callerID, chatID, text string INSERT INTO messages (chat_id, sender_id, text) VALUES ($1, $2, $3) RETURNING id::text, created_at`, - chatID, callerID, text, + chatID, callerID, encodeText(text), ).Scan(&msg.ID, &msg.CreatedAt) if err != nil { if isFKViolation(err) { @@ -107,6 +107,7 @@ func (m *Messages) HistoryPage(ctx context.Context, chatID, cursor string, limit out, err := pgx.CollectRows(rows, func(r pgx.CollectableRow) (domain.Message, error) { var msg domain.Message err := r.Scan(&msg.ID, &msg.ChatID, &msg.AuthorID, &msg.Text, &msg.CreatedAt) + msg.Text = decodeText(msg.Text) msg.CreatedAt = msg.CreatedAt.UTC() return msg, err }) diff --git a/app/internal/store/db/text.go b/app/internal/store/db/text.go new file mode 100644 index 00000000..4c9e30f1 --- /dev/null +++ b/app/internal/store/db/text.go @@ -0,0 +1,22 @@ +package db + +import "strings" + +var ( + textEncoder = strings.NewReplacer(`\`, `\\`, "\x00", `\0`) + textDecoder = strings.NewReplacer(`\\`, `\`, `\0`, "\x00") +) + +func encodeText(s string) string { + if !strings.ContainsAny(s, "\\\x00") { + return s + } + return textEncoder.Replace(s) +} + +func decodeText(s string) string { + if !strings.Contains(s, `\`) { + return s + } + return textDecoder.Replace(s) +} diff --git a/app/internal/store/db/text_test.go b/app/internal/store/db/text_test.go new file mode 100644 index 00000000..a957c8d5 --- /dev/null +++ b/app/internal/store/db/text_test.go @@ -0,0 +1,18 @@ +package db + +import "testing" + +func TestTextEscapeRoundTrip(t *testing.T) { + cases := []string{"", "plain", "\x00", "a\x00b", `\`, `\0`, `\\0`, "\\\x00", "\x00\\0\x00", "привет\x00мир"} + for _, s := range cases { + enc := encodeText(s) + for i := 0; i < len(enc); i++ { + if enc[i] == 0 { + t.Fatalf("encodeText(%q) = %q содержит NUL", s, enc) + } + } + if got := decodeText(enc); got != s { + t.Fatalf("decodeText(encodeText(%q)) = %q", s, got) + } + } +} diff --git a/app/internal/store/db/users.go b/app/internal/store/db/users.go index 1ce69d3b..11e372b0 100644 --- a/app/internal/store/db/users.go +++ b/app/internal/store/db/users.go @@ -24,7 +24,7 @@ func (u *Users) CreateUser(ctx context.Context, username, displayName, passwordH INSERT INTO users (username, display_name, password_hash) VALUES ($1, $2, $3) RETURNING id::text, created_at`, - username, displayName, passwordHash, + username, encodeText(displayName), passwordHash, ).Scan(&user.ID, &user.CreatedAt) if err != nil { return domain.User{}, mapErr(err) @@ -51,6 +51,7 @@ func (u *Users) UserByID(ctx context.Context, id string) (domain.User, error) { } return domain.User{}, err } + user.DisplayName = decodeText(user.DisplayName) user.CreatedAt = user.CreatedAt.UTC() return user, nil } @@ -65,11 +66,12 @@ func (u *Users) CredentialsByUsername(ctx context.Context, username string) (dom SELECT id::text, username, display_name, created_at, password_hash FROM users WHERE username = $1`, - username, + encodeText(username), ).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &hash) if err != nil { return domain.User{}, "", mapErr(err) } + user.DisplayName = decodeText(user.DisplayName) user.CreatedAt = user.CreatedAt.UTC() return user, hash, nil } @@ -81,7 +83,7 @@ func (u *Users) UsernameExists(ctx context.Context, username string) bool { var exists bool err := u.pool.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM users WHERE username = $1)`, - username, + encodeText(username), ).Scan(&exists) return err == nil && exists } diff --git a/evidence/integration-tests.txt b/evidence/integration-tests.txt index 7c09c6da..10db3380 100644 --- a/evidence/integration-tests.txt +++ b/evidence/integration-tests.txt @@ -1,34 +1,36 @@ === RUN TestUsernameRace - integration_test.go:83: 20 параллельных регистраций: 1 created, 19 conflict, строк в users: 1 ---- PASS: TestUsernameRace (0.03s) + integration_test.go:78: 20 параллельных регистраций: 1 created, 19 conflict, строк в users: 1 +--- PASS: TestUsernameRace (0.02s) === RUN TestCreateChatRollback - integration_test.go:123: неизвестный участник → 400, chats/chat_members без изменений; валидный вызов создаёт 1 чат ---- PASS: TestCreateChatRollback (0.03s) + integration_test.go:116: неизвестный участник → 400, chats/chat_members без изменений; валидный вызов создаёт 1 чат +--- PASS: TestCreateChatRollback (0.01s) === RUN TestMessageRequiresMembership ---- PASS: TestMessageRequiresMembership (0.02s) +--- PASS: TestMessageRequiresMembership (0.01s) +=== RUN TestNULRoundTrip +--- PASS: TestNULRoundTrip (0.07s) PASS -ok gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store/db 0.973s +ok gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store/db 0.861s === RUN TestReadAfterWrite_FillRace === RUN TestReadAfterWrite_FillRace/naive - history_test.go:150: naive: чтение после подтверждённой записи stale=true + history_test.go:141: naive: чтение после подтверждённой записи stale=true === RUN TestReadAfterWrite_FillRace/safe - history_test.go:150: safe: чтение после подтверждённой записи stale=false ---- PASS: TestReadAfterWrite_FillRace (0.32s) + history_test.go:141: safe: чтение после подтверждённой записи stale=false +--- PASS: TestReadAfterWrite_FillRace (0.33s) --- PASS: TestReadAfterWrite_FillRace/naive (0.11s) - --- PASS: TestReadAfterWrite_FillRace/safe (0.21s) + --- PASS: TestReadAfterWrite_FillRace/safe (0.22s) === RUN TestStampede_SQLCount === RUN TestStampede_SQLCount/naive - history_test.go:195: naive: 50 одновременных промахов → 50 запросов страницы в БД + history_test.go:184: naive: 50 одновременных промахов → 50 запросов страницы в БД === RUN TestStampede_SQLCount/safe - history_test.go:195: safe: 50 одновременных промахов → 1 запросов страницы в БД ---- PASS: TestStampede_SQLCount (0.16s) - --- PASS: TestStampede_SQLCount/naive (0.09s) - --- PASS: TestStampede_SQLCount/safe (0.07s) + history_test.go:184: safe: 50 одновременных промахов → 1 запросов страницы в БД +--- PASS: TestStampede_SQLCount (0.13s) + --- PASS: TestStampede_SQLCount/naive (0.07s) + --- PASS: TestStampede_SQLCount/safe (0.06s) === RUN TestAccessCheckedBeforeCache --- PASS: TestAccessCheckedBeforeCache (0.01s) === RUN TestCacheLossIsOnlyMiss --- PASS: TestCacheLossIsOnlyMiss (0.01s) === RUN TestCacheDown_Bypass ---- PASS: TestCacheDown_Bypass (0.03s) +--- PASS: TestCacheDown_Bypass (0.00s) PASS -ok gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/cache 1.197s +ok gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/cache 1.621s diff --git a/evidence/migrations.txt b/evidence/migrations.txt index 28983e1b..41a7600a 100644 --- a/evidence/migrations.txt +++ b/evidence/migrations.txt @@ -1,13 +1,14 @@ -# Миграции (2026-10-29T07:04:02Z) +# Миграции (2026-10-29T11:33:55Z) ## 1. С нуля (проект mtusi-lab2-fresh, пустой volume) - Container mtusi-lab2-fresh-postgres-1 Healthy + Container mtusi-lab2-fresh-postgres-1 Healthy volume: mtusi-lab2-fresh_pgdata -2026/10/29 07:04:09 OK 00001_create_users_table.sql (14.67ms) -2026/10/29 07:04:09 OK 00002_create_chats_table.sql (6.31ms) -2026/10/29 07:04:09 OK 00003_create_chat_members_table.sql (9.24ms) -2026/10/29 07:04:09 OK 00004_create_messages_table.sql (10.81ms) -2026/10/29 07:04:09 goose: successfully migrated database to version: 4 +2026/10/29 11:34:02 OK 00001_create_users_table.sql (2.16ms) +2026/10/29 11:34:02 OK 00002_create_chats_table.sql (1.07ms) +2026/10/29 11:34:02 OK 00003_create_chat_members_table.sql (1.56ms) +2026/10/29 11:34:02 OK 00004_create_messages_table.sql (1.74ms) +2026/10/29 11:34:02 OK 00005_escape_nul_in_text.sql (1.64ms) +2026/10/29 11:34:02 goose: successfully migrated database to version: 5 List of relations Schema | Name | Type | Owner --------+------------------+-------+----------- @@ -23,12 +24,13 @@ volume: mtusi-lab2-fresh_pgdata 2|t 3|t 4|t +5|t ### повторный migrate на том же свежем volume -2026/10/29 07:04:11 goose: no migrations to run. current version: 4 - Volume mtusi-lab2-fresh_pgdata Removed - Network mtusi-lab2-fresh_default Removed +2026/10/29 11:34:03 goose: no migrations to run. current version: 5 + Volume mtusi-lab2-fresh_pgdata Removed + Network mtusi-lab2-fresh_default Removed ## 2. Повторный migrate на рабочем стенде (volume mtusi-lab2_pgdata сохраняется) -до: 136 54 125 69369 -2026/10/29 07:04:14 goose: no migrations to run. current version: 4 -после: 136 54 125 69369 +до: 54 26 37 95882 +2026/10/29 11:34:04 goose: no migrations to run. current version: 5 +после: 54 26 37 95882 diff --git a/migrations/00005_escape_nul_in_text.sql b/migrations/00005_escape_nul_in_text.sql new file mode 100644 index 00000000..2aede12c --- /dev/null +++ b/migrations/00005_escape_nul_in_text.sql @@ -0,0 +1,45 @@ +-- PostgreSQL text не хранит U+0000, а контракт допускает его в display_name, +-- title и text. Приложение пишет эти поля экранированными: `\` → `\\`, +-- U+0000 → `\0` (app/internal/store/db/text.go). Ограничения длины проверяют +-- раскодированное значение: regexp_replace(x, '\\(.)', '\1', 'g') сворачивает +-- каждую escape-пару в один символ, поэтому char_length совпадает с числом +-- code points исходной строки. + +-- +goose Up +-- +goose StatementBegin +ALTER TABLE users DROP CONSTRAINT users_display_name_len; +ALTER TABLE chats DROP CONSTRAINT chats_title_len; +ALTER TABLE messages DROP CONSTRAINT messages_text_len; + +UPDATE users SET display_name = replace(display_name, '\', '\\') WHERE strpos(display_name, '\') > 0; +UPDATE chats SET title = replace(title, '\', '\\') WHERE strpos(title, '\') > 0; +UPDATE messages SET text = replace(text, '\', '\\') WHERE strpos(text, '\') > 0; + +ALTER TABLE users ADD CONSTRAINT users_display_name_len + CHECK (char_length(regexp_replace(display_name, '\\(.)', '\1', 'g')) BETWEEN 1 AND 100 + AND btrim(regexp_replace(display_name, '\\(.)', '\1', 'g')) <> ''); +ALTER TABLE chats ADD CONSTRAINT chats_title_len + CHECK (char_length(regexp_replace(title, '\\(.)', '\1', 'g')) BETWEEN 1 AND 100 + AND btrim(regexp_replace(title, '\\(.)', '\1', 'g')) <> ''); +ALTER TABLE messages ADD CONSTRAINT messages_text_len + CHECK (char_length(regexp_replace(text, '\\(.)', '\1', 'g')) BETWEEN 1 AND 4000); +-- +goose StatementEnd + +-- +goose Down +-- Откат с потерей: U+0000 непредставим в text и превращается в '0'. +-- +goose StatementBegin +ALTER TABLE users DROP CONSTRAINT users_display_name_len; +ALTER TABLE chats DROP CONSTRAINT chats_title_len; +ALTER TABLE messages DROP CONSTRAINT messages_text_len; + +UPDATE users SET display_name = regexp_replace(display_name, '\\(.)', '\1', 'g') WHERE strpos(display_name, '\') > 0; +UPDATE chats SET title = regexp_replace(title, '\\(.)', '\1', 'g') WHERE strpos(title, '\') > 0; +UPDATE messages SET text = regexp_replace(text, '\\(.)', '\1', 'g') WHERE strpos(text, '\') > 0; + +ALTER TABLE users ADD CONSTRAINT users_display_name_len + CHECK (char_length(display_name) BETWEEN 1 AND 100 AND btrim(display_name) <> ''); +ALTER TABLE chats ADD CONSTRAINT chats_title_len + CHECK (char_length(title) BETWEEN 1 AND 100 AND btrim(title) <> ''); +ALTER TABLE messages ADD CONSTRAINT messages_text_len + CHECK (char_length(text) BETWEEN 1 AND 4000); +-- +goose StatementEnd diff --git a/scripts/integration-tests.sh b/scripts/integration-tests.sh index 7fbc27ec..79da5292 100644 --- a/scripts/integration-tests.sh +++ b/scripts/integration-tests.sh @@ -11,4 +11,4 @@ TEST_HISTORY_CACHE_URL="redis://:$(cat secrets/valkey_cache_password)@127.0.0.1: export TEST_DATABASE_URL TEST_HISTORY_CACHE_URL cd app -exec go test -count=1 -v -run 'TestUsernameRace|TestCreateChatRollback|TestMessageRequiresMembership|TestReadAfterWrite|TestStampede|TestAccessChecked|TestCacheLoss|TestCacheDown' ./internal/store/db/ ./internal/cache/ +exec go test -count=1 -v -run 'TestUsernameRace|TestCreateChatRollback|TestMessageRequiresMembership|TestReadAfterWrite|TestStampede|TestAccessChecked|TestNULRoundTrip|TestCacheLoss|TestCacheDown' ./internal/store/db/ ./internal/cache/