feat(identity): hash passwords with Argon2id and bounded inputs
This commit is contained in:
1 parent
6153887eb5
commit
dda3825786
26 files changed
+1475
-32
No files matched your search
@@ -0,0 +1,132 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewPasswordCountsCodePointsAfterNFKC(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
raw string
|
||||
want string
|
||||
}{
|
||||
"15 ASCII characters": {raw: strings.Repeat("a", 15), want: strings.Repeat("a", 15)},
|
||||
"128 ASCII characters": {raw: strings.Repeat("a", 128), want: strings.Repeat("a", 128)},
|
||||
// 15 code points in 30 bytes: the limit counts code points, not bytes.
|
||||
"15 Cyrillic letters": {raw: strings.Repeat("ж", 15), want: strings.Repeat("ж", 15)},
|
||||
// 128 code points in 512 bytes: the largest input the byte cap allows.
|
||||
"128 four-byte characters": {raw: strings.Repeat("\U0001F600", 128), want: strings.Repeat("\U0001F600", 128)},
|
||||
// Fullwidth forms normalize to ASCII.
|
||||
"fullwidth letters": {raw: strings.Repeat("pass", 4), want: strings.Repeat("pass", 4)},
|
||||
// 14 raw code points become 15 after NFKC expands the ligature.
|
||||
"expanding ligature": {raw: strings.Repeat("a", 13) + "fi", want: strings.Repeat("a", 13) + "fi"},
|
||||
// Spaces and surrounding whitespace are part of the password.
|
||||
"spaces kept": {raw: " correct horse battery ", want: " correct horse battery "},
|
||||
}
|
||||
for name, test := range tests {
|
||||
password, err := NewPassword(test.raw)
|
||||
if err != nil {
|
||||
t.Fatalf("NewPassword(%s) error = %v", name, err)
|
||||
}
|
||||
if password.Reveal() != test.want {
|
||||
t.Fatalf("NewPassword(%s) did not normalize to the expected NFKC form", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPasswordRejectsValuesOutsideThePolicy(t *testing.T) {
|
||||
tests := map[string]string{
|
||||
"empty": "",
|
||||
"14 ASCII characters": strings.Repeat("a", 14),
|
||||
"129 ASCII characters": strings.Repeat("a", 129),
|
||||
"over the byte cap": strings.Repeat("a", maxPasswordInputBytes+1),
|
||||
"129 four-byte runes": strings.Repeat("\U0001F600", 129),
|
||||
"one megabyte": strings.Repeat("a", 1<<20),
|
||||
"invalid UTF-8": strings.Repeat("a", 20) + "\xff",
|
||||
"truncated UTF-8": strings.Repeat("a", 20) + "\xe2\x82",
|
||||
"expands past the limit": strings.Repeat("fi", 65),
|
||||
// 16 raw code points compose into 8 after NFKC.
|
||||
"composes below the minimum": strings.Repeat("é", 8),
|
||||
}
|
||||
for name, raw := range tests {
|
||||
_, err := NewPassword(raw)
|
||||
if !errors.Is(err, ErrInvalidPassword) {
|
||||
t.Fatalf("NewPassword(%s) error = %v, want ErrInvalidPassword", name, err)
|
||||
}
|
||||
if raw != "" && strings.Contains(err.Error(), raw) {
|
||||
t.Fatalf("NewPassword(%s) error exposes the input", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPasswordChecksTheByteCapBeforeNormalizing(t *testing.T) {
|
||||
// Valid UTF-8 would normalize; the byte cap must answer first.
|
||||
_, err := NewPassword(strings.Repeat("fi", maxPasswordInputBytes))
|
||||
if !errors.Is(err, ErrInvalidPassword) || !strings.Contains(err.Error(), "bytes before normalization") {
|
||||
t.Fatalf("NewPassword(oversized) error = %v, want the byte cap", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseLoginPasswordAppliesOnlyTheHashingBound(t *testing.T) {
|
||||
for _, raw := range []string{"a", "short", strings.Repeat("a", 128)} {
|
||||
if _, err := ParseLoginPassword(raw); err != nil {
|
||||
t.Fatalf("ParseLoginPassword(%d bytes) error = %v", len(raw), err)
|
||||
}
|
||||
}
|
||||
for _, raw := range []string{"", strings.Repeat("a", 129), strings.Repeat("a", maxPasswordInputBytes+1), "\xff"} {
|
||||
if _, err := ParseLoginPassword(raw); !errors.Is(err, ErrInvalidPassword) {
|
||||
t.Fatalf("ParseLoginPassword(%d bytes) error = %v, want ErrInvalidPassword", len(raw), err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewPasswordAndLoginPasswordNormalizeAlike(t *testing.T) {
|
||||
registered, err := NewPassword("correct horse battery")
|
||||
if err != nil {
|
||||
t.Fatalf("NewPassword() error = %v", err)
|
||||
}
|
||||
login, err := ParseLoginPassword("correct horse battery")
|
||||
if err != nil {
|
||||
t.Fatalf("ParseLoginPassword() error = %v", err)
|
||||
}
|
||||
if registered.Reveal() != login.Reveal() {
|
||||
t.Fatal("equivalent registration and login input normalized differently")
|
||||
}
|
||||
if !(Password{}).IsZero() || registered.IsZero() {
|
||||
t.Fatal("IsZero() does not distinguish a built password from the zero value")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPasswordAndPasswordHashAreRedacted(t *testing.T) {
|
||||
const raw = "correct horse battery staple"
|
||||
password, err := NewPassword(raw)
|
||||
if err != nil {
|
||||
t.Fatalf("NewPassword() error = %v", err)
|
||||
}
|
||||
passwordHash, err := NewPasswordHash(testPasswordHash)
|
||||
if err != nil {
|
||||
t.Fatalf("NewPasswordHash() error = %v", err)
|
||||
}
|
||||
var logs bytes.Buffer
|
||||
slog.New(slog.NewTextHandler(&logs, nil)).Info("redaction", "password", password, "hash", passwordHash)
|
||||
slog.New(slog.NewJSONHandler(&logs, nil)).Info("redaction", "password", password, "hash", passwordHash)
|
||||
encoded, err := json.Marshal(map[string]any{"password": password, "hash": passwordHash})
|
||||
if err != nil {
|
||||
t.Fatalf("json.Marshal() error = %v", err)
|
||||
}
|
||||
formatted := fmt.Sprintf("%v %+v %#v %s %q %d", password, password, password, password, password, password) +
|
||||
logs.String() + string(encoded)
|
||||
for _, secret := range []string{raw, testPasswordHash, "c2FsdHNhbHRzYWx0"} {
|
||||
if strings.Contains(formatted, secret) {
|
||||
t.Fatalf("formatted output exposes a secret: %s", formatted)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(string(encoded), redactedSecret) {
|
||||
t.Fatalf("json.Marshal() = %s, want redacted values", encoded)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user