forked from mtusi2026/mtusi2026lab2
fixed a bug with NUL in strings
This commit is contained in:
@@ -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
|
||||
})
|
||||
|
||||
@@ -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])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
})
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
+15
-13
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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/
|
||||
|
||||
Reference in New Issue
Block a user