Отвечать 400 и 401 вместо 500 на NUL и невалидный UTF-8 во входных данных
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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") {
|
||||
|
||||
Reference in New Issue
Block a user