From 3001507ecb924dc8b31c1530ff2c433ca4c159a3 Mon Sep 17 00:00:00 2001 From: kudesn1k Date: Tue, 6 Oct 2026 15:41:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D1=82=D0=B2=D0=B5=D1=87=D0=B0=D1=82?= =?UTF-8?q?=D1=8C=20400=20=D0=B8=20401=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82?= =?UTF-8?q?=D0=BE=20500=20=D0=BD=D0=B0=20NUL=20=D0=B8=20=D0=BD=D0=B5=D0=B2?= =?UTF-8?q?=D0=B0=D0=BB=D0=B8=D0=B4=D0=BD=D1=8B=D0=B9=20UTF-8=20=D0=B2?= =?UTF-8?q?=D0=BE=20=D0=B2=D1=85=D0=BE=D0=B4=D0=BD=D1=8B=D1=85=20=D0=B4?= =?UTF-8?q?=D0=B0=D0=BD=D0=BD=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/app/app_test.go | 2 +- internal/app/auth_test.go | 2 ++ internal/chat/chat.go | 4 ++++ internal/chat/chat_test.go | 4 +++- internal/chat/message.go | 4 ++++ internal/chat/message_test.go | 2 +- internal/user/service.go | 7 +++++++ internal/user/service_test.go | 8 +++++++- internal/user/user.go | 4 ++++ internal/user/user_test.go | 2 +- 10 files changed, 34 insertions(+), 5 deletions(-) diff --git a/internal/app/app_test.go b/internal/app/app_test.go index e27fd8aa..b9ba72b6 100644 --- a/internal/app/app_test.go +++ b/internal/app/app_test.go @@ -396,7 +396,7 @@ func TestMessagesEndToEnd(t *testing.T) { first := ts.message(alice.token, room.ID, "Привет, мир 👋") second := ts.message(bob.token, room.ID, "Сообщение Боба") if first.SenderID != alice.ID || second.SenderID != bob.ID { - t.Fatalf("sender must come from X-User-Id: %+v %+v", first, second) + t.Fatalf("sender must come from the bearer token: %+v %+v", first, second) } if first.ChatID != room.ID || first.Text != "Привет, мир 👋" { t.Fatalf("message = %+v", first) diff --git a/internal/app/auth_test.go b/internal/app/auth_test.go index 3ee7d0ec..602d1e2a 100644 --- a/internal/app/auth_test.go +++ b/internal/app/auth_test.go @@ -54,6 +54,8 @@ func TestLoginFailuresAreUniformBasicChallenges(t *testing.T) { "bearer scheme": {"Authorization": "Bearer " + strings.Repeat("A", 43)}, "not base64": {"Authorization": "Basic !!!"}, "no colon": {"Authorization": "Basic " + encoded("alice")}, + "nul username": basic("a\x00b", testPassword), + "non-utf-8 name": basic("\xff\xfe", testPassword), } messages := map[string]string{} for name, headers := range cases { diff --git a/internal/chat/chat.go b/internal/chat/chat.go index 0c1b2e77..c9281351 100644 --- a/internal/chat/chat.go +++ b/internal/chat/chat.go @@ -46,6 +46,10 @@ func NewChat(id uuid.UUID, title string) (Chat, error) { if utf8.RuneCountInString(title) > MaxTitleCodePoints { return Chat{}, apperr.Invalid(fmt.Sprintf("title must be at most %d characters", MaxTitleCodePoints)) } + // PostgreSQL text cannot hold U+0000. + if strings.ContainsRune(title, 0) { + return Chat{}, apperr.Invalid("title must not contain the NUL character") + } return Chat{ID: id, Title: title, CreatedAt: ident.TimeOf(id)}, nil } diff --git a/internal/chat/chat_test.go b/internal/chat/chat_test.go index 829856c9..f63f60e3 100644 --- a/internal/chat/chat_test.go +++ b/internal/chat/chat_test.go @@ -23,7 +23,9 @@ func TestNewChatDerivesCreatedAtFromID(t *testing.T) { } func TestNewChatValidatesTitle(t *testing.T) { - for _, title := range []string{"", " ", strings.Repeat("я", 101)} { + // PostgreSQL text cannot hold U+0000, so a title with it is invalid input, + // not a storage failure. + for _, title := range []string{"", " ", strings.Repeat("я", 101), "nul\x00inside"} { _, err := chat.NewChat(ident.New(), title) if apperr.KindOf(err) != apperr.KindInvalid { t.Errorf("title %q must be rejected as invalid, got %v", title, err) diff --git a/internal/chat/message.go b/internal/chat/message.go index a825c2bc..c28f6d7d 100644 --- a/internal/chat/message.go +++ b/internal/chat/message.go @@ -30,6 +30,10 @@ func NewMessage(id, chatID, senderID uuid.UUID, text string) (Message, error) { if utf8.RuneCountInString(text) > MaxTextCodePoints { return Message{}, apperr.Invalid(fmt.Sprintf("text must be at most %d characters", MaxTextCodePoints)) } + // PostgreSQL text cannot hold U+0000. + if strings.ContainsRune(text, 0) { + return Message{}, apperr.Invalid("text must not contain the NUL character") + } return Message{ ID: id, ChatID: chatID, diff --git a/internal/chat/message_test.go b/internal/chat/message_test.go index 714b3dad..3cb76738 100644 --- a/internal/chat/message_test.go +++ b/internal/chat/message_test.go @@ -28,7 +28,7 @@ func TestNewMessageValidatesTextByCodePoints(t *testing.T) { if _, err := chat.NewMessage(ident.New(), chatID, alice, strings.Repeat("я", 4000)); err != nil { t.Errorf("4000 code points must be accepted: %v", err) } - for _, text := range []string{"", " ", "\n\t ", strings.Repeat("я", 4001)} { + for _, text := range []string{"", " ", "\n\t ", strings.Repeat("я", 4001), "nul\x00inside"} { _, err := chat.NewMessage(ident.New(), chatID, alice, text) if apperr.KindOf(err) != apperr.KindInvalid { t.Errorf("text %q must be rejected as invalid, got %v", text, err) diff --git a/internal/user/service.go b/internal/user/service.go index 312474e9..708ddb9b 100644 --- a/internal/user/service.go +++ b/internal/user/service.go @@ -45,7 +45,14 @@ var dummyHash = sync.OnceValue(func() string { // Authenticate returns the user whose password matches, or // ErrInvalidCredentials for an unknown username and a wrong password alike. +// The username comes straight from the Basic header, so one that cannot +// exist (including NUL or invalid UTF-8, which the database would reject) +// is answered without a query, but after the same KDF work. func (s *Service) Authenticate(ctx context.Context, username, plain string) (User, error) { + if !usernamePattern.MatchString(username) { + _, _ = password.Verify(dummyHash(), plain) + return User{}, ErrInvalidCredentials + } u, err := s.repo.GetByUsername(ctx, username) if errors.Is(err, ErrNotFound) { _, _ = password.Verify(dummyHash(), plain) diff --git a/internal/user/service_test.go b/internal/user/service_test.go index 99aecf4a..3406ddd8 100644 --- a/internal/user/service_test.go +++ b/internal/user/service_test.go @@ -172,7 +172,13 @@ func TestAuthenticateFailuresLookTheSame(t *testing.T) { } _, wrong := svc.Authenticate(ctx, "alice", pw+"!") _, unknown := svc.Authenticate(ctx, "nobody", pw) - for name, err := range map[string]error{"wrong password": wrong, "unknown user": unknown} { + // Basic credentials are client bytes: a username the database cannot + // even compare must still be a plain credential failure. + _, nul := svc.Authenticate(ctx, "a\x00b", pw) + _, notUTF8 := svc.Authenticate(ctx, "\xff\xfe", pw) + for name, err := range map[string]error{ + "wrong password": wrong, "unknown user": unknown, "nul username": nul, "invalid utf-8 username": notUTF8, + } { if !errors.Is(err, user.ErrInvalidCredentials) || apperr.KindOf(err) != apperr.KindUnauthorized { t.Errorf("%s: err = %v, want ErrInvalidCredentials", name, err) } diff --git a/internal/user/user.go b/internal/user/user.go index f890c414..d05e1c43 100644 --- a/internal/user/user.go +++ b/internal/user/user.go @@ -51,6 +51,10 @@ func New(id uuid.UUID, username, displayName string) (User, error) { if utf8.RuneCountInString(displayName) > MaxDisplayNameCodePoints { return User{}, apperr.Invalid(fmt.Sprintf("display_name must be at most %d characters", MaxDisplayNameCodePoints)) } + // PostgreSQL text cannot hold U+0000. + if strings.ContainsRune(displayName, 0) { + return User{}, apperr.Invalid("display_name must not contain the NUL character") + } return User{ ID: id, Username: username, diff --git a/internal/user/user_test.go b/internal/user/user_test.go index ada07ff2..ede9ec52 100644 --- a/internal/user/user_test.go +++ b/internal/user/user_test.go @@ -68,7 +68,7 @@ func TestNewValidatesDisplayNameByCodePoints(t *testing.T) { t.Errorf("display_name %q must be accepted: %v", name, err) } } - invalid := []string{"", " ", "\t\n", strings.Repeat("я", 101)} + invalid := []string{"", " ", "\t\n", strings.Repeat("я", 101), "nul\x00inside"} for _, name := range invalid { _, err := user.New(ident.New(), "alice", name) if apperr.KindOf(err) != apperr.KindInvalid || !strings.Contains(err.Error(), "display_name") {