From 36aa6b8b2c41ddb4dd8c8511ebd75c7ec0ad3546 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Wed, 2 Dec 2026 14:49:32 -0600 Subject: [PATCH] security: reject common example passwords --- agent/web/http_login_test.go | 46 ++++++++++++++++++++---------------- pkg/users/passphrase.go | 18 ++++++++++++++ pkg/users/profiles_test.go | 8 +++++++ 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/agent/web/http_login_test.go b/agent/web/http_login_test.go index cae70db..a9015af 100644 --- a/agent/web/http_login_test.go +++ b/agent/web/http_login_test.go @@ -22,6 +22,10 @@ import ( "github.com/shoenig/test/must" ) +// testPassphrase is a passphrase that satisfies every validation rule and is +// not on the forbidden list, so it can stand in for a real signup credential. +const testPassphrase = "purple monkey dishwasher 42 bingo" + func makeLoginRequest(t *testing.T, values url.Values) *http.Request { t.Helper() @@ -87,8 +91,8 @@ func TestSignupFields(t *testing.T) { request := makeLoginRequest(t, url.Values{ "signup-email": {"you@example.com"}, "signup-username": {"alice"}, - "signup-passphrase": {"correct horse battery staple"}, - "signup-confirm": {"correct horse battery staple"}, + "signup-passphrase": {testPassphrase}, + "signup-confirm": {testPassphrase}, "notifications": {"true"}, "marketing": {"true"}, }) @@ -97,8 +101,8 @@ func TestSignupFields(t *testing.T) { must.Eq(t, "you@example.com", got.Email) must.Eq(t, "alice", got.Username) - must.Eq(t, "correct horse battery staple", got.Passphrase.Unveil()) - must.Eq(t, "correct horse battery staple", got.Confirm.Unveil()) + must.Eq(t, testPassphrase, got.Passphrase.Unveil()) + must.Eq(t, testPassphrase, got.Confirm.Unveil()) must.True(t, got.Notifications) must.True(t, got.Spam) must.Nil(t, got.Salt) @@ -108,21 +112,21 @@ func TestSignupFields(t *testing.T) { request := makeLoginRequest(t, url.Values{ "signup-email": {"you@example.com"}, "signup-username": {"alice"}, - "signup-passphrase": {"correct horse battery staple"}, - "signup-confirm": {"correct horse battery staple"}, + "signup-passphrase": {testPassphrase}, + "signup-confirm": {testPassphrase}, }) got := (&loginHTTP{}).signupFields(request) must.False(t, got.Notifications) must.False(t, got.Spam) - must.Eq(t, "correct horse battery staple", got.Confirm.Unveil()) + must.Eq(t, testPassphrase, got.Confirm.Unveil()) }) t.Run("missing required field", func(t *testing.T) { request := makeLoginRequest(t, url.Values{ "signup-username": {"alice"}, - "signup-passphrase": {"correct horse battery staple"}, + "signup-passphrase": {testPassphrase}, }) must.Panic(t, func() { @@ -134,8 +138,8 @@ func TestSignupFields(t *testing.T) { request := makeLoginRequest(t, url.Values{ "signup-email": {"you@example.com"}, "signup-username": {"alice"}, - "signup-passphrase": {"correct horse battery staple"}, - "signup-confirm": {"correct horse battery staple"}, + "signup-passphrase": {testPassphrase}, + "signup-confirm": {testPassphrase}, "notifications": {"on"}, "marketing": {"on"}, }) @@ -150,8 +154,8 @@ func TestSignupFields(t *testing.T) { request := makeLoginRequest(t, url.Values{ "signup-email": {"you@example.com"}, "signup-username": {"alice"}, - "signup-passphrase": {"correct horse battery staple"}, - "signup-confirm": {"correct horse battery staple"}, + "signup-passphrase": {testPassphrase}, + "signup-confirm": {testPassphrase}, "not-used-elsewhere": {"true"}, "no-recovery": {"true"}, }) @@ -160,7 +164,7 @@ func TestSignupFields(t *testing.T) { must.Eq(t, "you@example.com", got.Email) must.Eq(t, "alice", got.Username) - must.Eq(t, "correct horse battery staple", got.Passphrase.Unveil()) + must.Eq(t, testPassphrase, got.Passphrase.Unveil()) }) } @@ -172,8 +176,8 @@ func TestSignup_NewUser_Redirects(t *testing.T) { r := makeLoginRequest(t, url.Values{ "signup-email": {"alice@example.com"}, "signup-username": {"alice"}, - "signup-passphrase": {"correct horse battery staple"}, - "signup-confirm": {"correct horse battery staple"}, + "signup-passphrase": {testPassphrase}, + "signup-confirm": {testPassphrase}, }) w := httptest.NewRecorder() routes.ServeHTTP(w, r) @@ -192,8 +196,8 @@ func TestSignup_DuplicateUsername_RendersNiceError(t *testing.T) { r1 := makeLoginRequest(t, url.Values{ "signup-email": {"alice@example.com"}, "signup-username": {"alice"}, - "signup-passphrase": {"correct horse battery staple"}, - "signup-confirm": {"correct horse battery staple"}, + "signup-passphrase": {testPassphrase}, + "signup-confirm": {testPassphrase}, }) w1 := httptest.NewRecorder() routes.ServeHTTP(w1, r1) @@ -227,8 +231,8 @@ func TestSignup_InvalidEmail_RendersValidationError(t *testing.T) { r := makeLoginRequest(t, url.Values{ "signup-email": {"not-an-email"}, "signup-username": {"alice"}, - "signup-passphrase": {"correct horse battery staple"}, - "signup-confirm": {"correct horse battery staple"}, + "signup-passphrase": {testPassphrase}, + "signup-confirm": {testPassphrase}, }) w := httptest.NewRecorder() routes.ServeHTTP(w, r) @@ -247,7 +251,7 @@ func TestSignup_MismatchedPassphrases_ReturnsError(t *testing.T) { r := makeLoginRequest(t, url.Values{ "signup-email": {"alice@example.com"}, "signup-username": {"alice"}, - "signup-passphrase": {"correct horse battery staple"}, + "signup-passphrase": {testPassphrase}, "signup-confirm": {"completely different but still long enough"}, }) w := httptest.NewRecorder() @@ -286,7 +290,7 @@ func TestLogin_RateLimited(t *testing.T) { r := makeLoginRequest(t, url.Values{ "username": {"alice"}, - "passphrase": {"correct horse battery staple"}, + "passphrase": {testPassphrase}, }) r.RemoteAddr = ip + ":1234" diff --git a/pkg/users/passphrase.go b/pkg/users/passphrase.go index b590421..355d522 100644 --- a/pkg/users/passphrase.go +++ b/pkg/users/passphrase.go @@ -10,6 +10,7 @@ import ( "unicode/utf8" "cattlecloud.net/onlylogins/pkg/entropy" + "github.com/hashicorp/go-set/v3" "github.com/shoenig/go-conceal" ) @@ -58,11 +59,28 @@ func ValidateSalt(salt *conceal.Bytes) error { return nil } +var ( + forbidden = set.From([]string{ + // XKCD + "correcthorsebatterystaple", + "CorrectHorseBatteryStaple", + "correct horse battery staple", + "correct-horse-battery-staple", + + // RFC, documentation + "f47ac10b-58cc-4372-a567-0e02b2c3d479", + }) +) + func ValidatePassphrase(text *conceal.Text) error { if text == nil { return errors.New("passphrase must not be nil") } + if forbidden.Contains(text.Unveil()) { + return errors.New("passphrase is a well known string") + } + if utf8.RuneCountInString(text.Unveil()) < minimumPassphraseLength { return fmt.Errorf("passphrase must be at least %d characters long", minimumPassphraseLength) } diff --git a/pkg/users/profiles_test.go b/pkg/users/profiles_test.go index 76f3190..0a87d6d 100644 --- a/pkg/users/profiles_test.go +++ b/pkg/users/profiles_test.go @@ -48,6 +48,14 @@ func TestValidatePassphrase(t *testing.T) { err := ValidatePassphrase(p) must.Error(t, err) }) + + t.Run("Forbidden", func(t *testing.T) { + // "correct horse battery staple" is a well known string and is + // rejected before any length or entropy checks run. + p := conceal.New("correct horse battery staple") + err := ValidatePassphrase(p) + must.Error(t, err) + }) } func TestProfile_Active(t *testing.T) {