Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Require minimum length of 8 characters password #2009

Merged
merged 9 commits into from
Dec 6, 2021
17 changes: 7 additions & 10 deletions selfservice/strategy/password/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ package password
import (
"bufio"
"context"
"time"

"github.com/hashicorp/go-retryablehttp"

"github.com/ory/kratos/driver/config"

/* #nosec G505 sha1 is used for k-anonymity */
"crypto/sha1"
Expand All @@ -16,15 +11,17 @@ import (
"strconv"
"strings"
"sync"
"time"

"github.com/arbovm/levenshtein"

"github.com/ory/x/httpx"

"github.com/hashicorp/go-retryablehttp"
"github.com/pkg/errors"

"github.com/ory/herodot"
"github.com/ory/x/httpx"
"github.com/ory/x/stringsx"

"github.com/ory/kratos/driver/config"
)

// Validator implements a validation strategy for passwords. One example is that the password
Expand Down Expand Up @@ -145,8 +142,8 @@ func (s *DefaultPasswordValidator) fetch(hpw []byte, apiDNSName string) error {
}

func (s *DefaultPasswordValidator) Validate(ctx context.Context, identifier, password string) error {
if len(password) < 6 {
return errors.Errorf("password length must be at least 6 characters but only got %d", len(password))
if len(password) < 8 {
return errors.Errorf("password length must be at least 8 characters but only got %d", len(password))
}

compIdentifier, compPassword := strings.ToLower(identifier), strings.ToLower(password)
Expand Down
17 changes: 10 additions & 7 deletions selfservice/strategy/password/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
"testing"
"time"

"github.com/ory/x/httpx"

"github.com/stretchr/testify/require"

"github.com/ory/x/httpx"

"github.com/ory/kratos/driver/config"
"github.com/ory/kratos/internal"
"github.com/ory/kratos/selfservice/strategy/password"
Expand All @@ -42,6 +42,7 @@ func TestDefaultPasswordValidationStrategy(t *testing.T) {
{pw: "password", pass: false},
{pw: "1234567890", pass: false},
{pw: "qwertyui", pass: false},
{pw: "l3f9to", pass: false},
{pw: "l3f9toh1uaf81n21", pass: true},
{pw: "l3f9toh1uaf81n21", id: "l3f9toh1uaf81n21", pass: false},
{pw: "l3f9toh1", pass: true},
Expand All @@ -56,19 +57,21 @@ func TestDefaultPasswordValidationStrategy(t *testing.T) {
{id: "[email protected]", pw: "h3ll0@example", pass: false},
{pw: "[email protected]", id: "hello@exam", pass: false},
{id: "abcd", pw: "9d3c8a1b", pass: true},
{id: "a", pw: "kjOkla", pass: true},
{id: "a", pw: "kjOklafe", pass: true},
{id: "ab", pw: "0000ab0000", pass: true},
// longest common substring with long password
{id: "d4f6090b-5a84", pw: "d4f6090b-5a84-2184-4404-8d1b-8da3eb00ebbe", pass: true},
{id: "asdflasdflasdf", pw: "asdflasdflpiuhefnciluaksdzuföfhg", pass: true},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
c := tc
t.Parallel()
aeneasr marked this conversation as resolved.
Show resolved Hide resolved
err := s.Validate(context.Background(), tc.id, tc.pw)
if tc.pass {
require.NoError(t, err, "err: %+v, id: %s, pw: %s", err, tc.id, tc.pw)

err := s.Validate(context.Background(), c.id, c.pw)
if c.pass {
require.NoError(t, err, "err: %+v, id: %s, pw: %s", err, c.id, c.pw)
} else {
require.Error(t, err, "id: %s, pw: %s", tc.id, tc.pw)
require.Error(t, err, "id: %s, pw: %s", c.id, c.pw)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ describe('Registration failures with email profile', () => {
.type(identity)
.should('have.value', identity)
cy.get('input[name="password"]')
.type('123456')
.should('have.value', '123456')
.type('12345678')
.should('have.value', '12345678')

cy.shouldHaveCsrfError({ app })
})
Expand All @@ -56,8 +56,8 @@ describe('Registration failures with email profile', () => {
.type(identity)
.should('have.value', identity)
cy.get('input[name="password"]')
.type('123456')
.should('have.value', '123456')
.type('12345678')
.should('have.value', '12345678')

cy.submitPasswordForm()
cy.get('*[data-testid^="ui/message"]').should(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ context('Settings failures with email profile', () => {
})

it('fails if password policy is violated', () => {
cy.get('input[name="password"]').clear().type('123456')
cy.get('input[name="password"]').clear().type('12345678')
cy.get('button[value="password"]').click()
cy.get('*[data-testid^="ui/message"]').should(
'contain.text',
Expand Down Expand Up @@ -282,7 +282,7 @@ context('Settings failures with email profile', () => {

describe('global errors', () => {
it('fails when CSRF is incorrect', () => {
cy.get(appPrefix(app) + 'input[name="password"]').type('123456')
cy.get(appPrefix(app) + 'input[name="password"]').type('12345678')
cy.shouldHaveCsrfError({ app })
})

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/cypress/integration/profiles/mfa/totp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ context('2FA lookup secrets', () => {

it('should fail to set up totp if verify code is wrong', () => {
cy.visit(settings)
cy.get('input[name="totp_code"]').type('123456')
cy.get('input[name="totp_code"]').type('12345678')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this would not fail with this change, but doesn't matter.

cy.get('*[name="method"][value="totp"]').click()
cy.get('[data-testid="ui/message/4000008"]').should(
'contain.text',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ context('Mobile Profile', () => {
describe('show errors when invalid signup data is used', () => {
it('should show an error when the password has leaked before', () => {
cy.get('input[data-testid="traits.email"]').type(email)
cy.get('input[data-testid="password"]').type('123456')
cy.get('input[data-testid="password"]').type('12345678')
cy.get('input[data-testid="traits.website"]').type(website)
cy.get('div[data-testid="submit-form"]').click()

Expand Down