feat(identity): hash passwords with Argon2id and bounded inputs

This commit is contained in:
shira committed 2026-10-06 01:07:55 +03:00
1 parent 6153887eb5
commit dda3825786
26 files changed
+1475 -32

No files matched your search

Whitespace-only changes.
@@ -0,0 +1,39 @@
// Package identity holds the account usecases.
package identity
import (
"context"
"fmt"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
// Credentials applies the password rules in the order that keeps hashing
// bounded: the cheap length and encoding checks first, the expensive KDF only
// for a password that passes them.
type Credentials struct {
hasher outbound.PasswordHasher
}
func NewCredentials(hasher outbound.PasswordHasher) *Credentials {
return &Credentials{hasher: hasher}
}
// HashNewPassword applies the registration password policy to raw and hashes
// the normalized password. A password outside the policy returns an error
// wrapping domain.ErrInvalidPassword without calling the hasher. Errors
// never contain raw. Registration (TASK-053, not implemented yet) must call
// it on every branch before it looks up the email, so that neither the
// outcome nor the time depends on whether the account exists.
func (credentials *Credentials) HashNewPassword(ctx context.Context, raw string) (domain.PasswordHash, error) {
password, err := domain.NewPassword(raw)
if err != nil {
return domain.PasswordHash{}, fmt.Errorf("Credentials.HashNewPassword - validate password: %w", err)
}
hash, err := credentials.hasher.Hash(ctx, password)
if err != nil {
return domain.PasswordHash{}, fmt.Errorf("Credentials.HashNewPassword - hash password: %w", err)
}
return hash, nil
}
@@ -0,0 +1,83 @@
package identity
import (
"bytes"
"context"
"errors"
"log/slog"
"strings"
"testing"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
// countingHasher records calls so a test can prove that validation stops a
// password before any hashing.
type countingHasher struct {
hashCalls int
hashed domain.Password
}
func (hasher *countingHasher) Hash(_ context.Context, password domain.Password) (domain.PasswordHash, error) {
hasher.hashCalls++
hasher.hashed = password
return domain.NewPasswordHash("$argon2id$v=19$m=65536,t=3,p=4$c2FsdHNhbHRzYWx0c2FsdA$aGFzaGhhc2hoYXNoaGFzaGhhc2hoYXNoaGFzaGhhc2g")
}
func (hasher *countingHasher) Verify(context.Context, domain.Password, domain.PasswordHash) (bool, error) {
return false, errors.New("countingHasher.Verify - not used")
}
func (hasher *countingHasher) VerifyDummy(context.Context, domain.Password) error {
return errors.New("countingHasher.VerifyDummy - not used")
}
var _ outbound.PasswordHasher = (*countingHasher)(nil)
func TestHashNewPasswordRefusesAnOversizedPasswordBeforeHashing(t *testing.T) {
const marker = "do-not-log-this-password-"
tests := map[string]string{
"129 code points": marker + strings.Repeat("x", 129-len(marker)),
"over the raw byte cap": marker + strings.Repeat("\U0001F600", 200),
"one megabyte": marker + strings.Repeat("y", 1<<20),
"too long after normalization": marker + strings.Repeat("fi", 60),
}
for name, raw := range tests {
hasher := &countingHasher{}
var logs bytes.Buffer
// The handling boundary logs the error as an HTTP handler would.
logger := slog.New(slog.NewJSONHandler(&logs, nil))
_, err := NewCredentials(hasher).HashNewPassword(context.Background(), raw)
logger.Error("registration refused", "error", err)
if !errors.Is(err, domain.ErrInvalidPassword) {
t.Fatalf("HashNewPassword(%s) error = %v, want ErrInvalidPassword", name, err)
}
if hasher.hashCalls != 0 {
t.Fatalf("HashNewPassword(%s) called the hasher %d times, want none", name, hasher.hashCalls)
}
if strings.Contains(logs.String(), marker) || strings.Contains(err.Error(), marker) {
t.Fatalf("HashNewPassword(%s) exposed the input in the error or the log: %s", name, logs.String())
}
}
}
func TestHashNewPasswordRefusesAShortPasswordBeforeHashing(t *testing.T) {
hasher := &countingHasher{}
_, err := NewCredentials(hasher).HashNewPassword(context.Background(), "fourteen chars")
if !errors.Is(err, domain.ErrInvalidPassword) || hasher.hashCalls != 0 {
t.Fatalf("HashNewPassword(14 code points) = %v after %d hash calls, want ErrInvalidPassword and no call", err, hasher.hashCalls)
}
}
func TestHashNewPasswordHashesTheNormalizedPassword(t *testing.T) {
hasher := &countingHasher{}
if _, err := NewCredentials(hasher).HashNewPassword(context.Background(), "correct horse battery"); err != nil {
t.Fatalf("HashNewPassword() error = %v", err)
}
if hasher.hashCalls != 1 || hasher.hashed.Reveal() != "correct horse battery" {
t.Fatalf("hasher received %d calls, want one with the NFKC form", hasher.hashCalls)
}
}