Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions agent/web/http_login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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"},
})
Expand All @@ -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)
Expand All @@ -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() {
Expand All @@ -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"},
})
Expand All @@ -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"},
})
Expand All @@ -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())
})
}

Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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"

Expand Down
18 changes: 18 additions & 0 deletions pkg/users/passphrase.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"unicode/utf8"

"cattlecloud.net/onlylogins/pkg/entropy"
"github.com/hashicorp/go-set/v3"
"github.com/shoenig/go-conceal"
)

Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/users/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down