Отвечать 400 и 401 вместо 500 на NUL и невалидный UTF-8 во входных данных

This commit is contained in:
2026-10-06 15:41:36 +03:00
parent 8f9252a5c1
commit 3001507ecb
10 changed files with 34 additions and 5 deletions
+7
View File
@@ -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)