-
-
Notifications
You must be signed in to change notification settings - Fork 949
/
validator.go
222 lines (190 loc) · 7.37 KB
/
validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package password
import (
"bufio"
"context"
"crypto/sha1" //#nosec G505 -- sha1 is used for k-anonymity
stderrs "errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/arbovm/levenshtein"
"github.com/dgraph-io/ristretto"
"github.com/hashicorp/go-retryablehttp"
"github.com/pkg/errors"
"go.opentelemetry.io/otel/trace"
"github.com/ory/herodot"
"github.com/ory/kratos/driver/config"
"github.com/ory/x/httpx"
"github.com/ory/x/otelx"
)
const hashCacheItemTTL = time.Hour
// Validator implements a validation strategy for passwords. One example is that the password
// has to have at least 6 characters and at least one lower and one uppercase password.
type Validator interface {
// Validate returns nil if the password is passing the validation strategy and an error otherwise. If a validation error
// occurs, a regular error will be returned. If some other type of error occurs (e.g. HTTP request failed), an error
// of type *herodot.DefaultError will be returned.
Validate(ctx context.Context, identifier, password string) error
}
type ValidationProvider interface {
PasswordValidator() Validator
}
var (
_ Validator = new(DefaultPasswordValidator)
ErrNetworkFailure = stderrs.New("unable to check if password has been leaked because an unexpected network error occurred")
ErrUnexpectedStatusCode = stderrs.New("unexpected status code")
ErrTooManyBreaches = stderrs.New("the password has been found in data breaches and must no longer be used")
)
// DefaultPasswordValidator implements Validator. It is based on best
// practices as defined in the following blog posts:
//
// - https://www.troyhunt.com/passwords-evolved-authentication-guidance-for-the-modern-era/
// - https://www.microsoft.com/en-us/research/wp-content/uploads/2016/06/Microsoft_Password_Guidance-1.pdf
//
// Additionally passwords are being checked against Troy Hunt's
// [haveibeenpwnd](https://haveibeenpwned.com/API/v2#SearchingPwnedPasswordsByRange) service to check if the
// password has been breached in a previous data leak using k-anonymity.
type DefaultPasswordValidator struct {
reg validatorDependencies
Client *retryablehttp.Client
hashes *ristretto.Cache
minIdentifierPasswordDist int
maxIdentifierPasswordSubstrThreshold float32
}
type validatorDependencies interface {
config.Provider
}
func NewDefaultPasswordValidatorStrategy(reg validatorDependencies) (*DefaultPasswordValidator, error) {
cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 10 * 10000,
MaxCost: 60 * 10000, // BCrypt hash size is 60 bytes
BufferItems: 64,
IgnoreInternalCost: true,
})
// sanity check - this should never happen unless above configuration variables are invalid
if err != nil {
return nil, errors.Wrap(err, "error while setting up validator cache")
}
return &DefaultPasswordValidator{
Client: httpx.NewResilientClient(
httpx.ResilientClientWithConnectionTimeout(time.Second),
// Tracing still works correctly even though we pass a no-op tracer
// here, because the otelhttp package will preferentially use the
// tracer from the incoming request context over this one.
httpx.ResilientClientWithTracer(trace.NewNoopTracerProvider().Tracer("github.com/ory/kratos/selfservice/strategy/password"))),
reg: reg,
hashes: cache,
minIdentifierPasswordDist: 5, maxIdentifierPasswordSubstrThreshold: 0.5}, nil
}
func b20(src []byte) string {
return fmt.Sprintf("%X", src)
}
// code inspired by https://rosettacode.org/wiki/Longest_Common_Substring#Go
func lcsLength(a, b string) int {
lengths := make([]int, len(a)*len(b))
greatestLength := 0
for i, x := range a {
for j, y := range b {
if x == y {
curr := 1
if i != 0 && j != 0 {
curr = lengths[(i-1)*len(b)+j-1] + 1
}
if curr > greatestLength {
greatestLength = curr
}
lengths[i*len(b)+j] = curr
}
}
}
return greatestLength
}
func (s *DefaultPasswordValidator) fetch(ctx context.Context, hpw []byte, apiDNSName string) (int64, error) {
prefix := fmt.Sprintf("%X", hpw)[0:5]
loc := fmt.Sprintf("https://%s/range/%s", apiDNSName, prefix)
req, err := retryablehttp.NewRequestWithContext(ctx, "GET", loc, nil)
if err != nil {
return 0, err
}
res, err := s.Client.Do(req)
if err != nil {
return 0, errors.Wrapf(ErrNetworkFailure, "%s", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
return 0, errors.Wrapf(ErrUnexpectedStatusCode, "%d", res.StatusCode)
}
var thisCount int64
sc := bufio.NewScanner(res.Body)
for sc.Scan() {
row := sc.Text()
result := strings.Split(strings.TrimSpace(row), ":")
// We assume a count of 1. HIBP API sometimes responds without the
// colon, so we just assume that the leak count is one.
//
// See https://github.com/ory/kratos/issues/2145
count := int64(1)
if len(result) == 2 {
count, err = strconv.ParseInt(strings.ReplaceAll(result[1], ",", ""), 10, 64)
if err != nil {
return 0, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Expected password hash to contain a count formatted as int but got: %s", result[1]))
}
}
s.hashes.SetWithTTL(prefix+result[0], count, 1, hashCacheItemTTL)
if prefix+result[0] == b20(hpw) {
thisCount = count
}
}
if err := sc.Err(); err != nil {
return 0, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("Unable to initialize string scanner: %s", err))
}
s.hashes.SetWithTTL(b20(hpw), thisCount, 1, hashCacheItemTTL)
return thisCount, nil
}
func (s *DefaultPasswordValidator) Validate(ctx context.Context, identifier, password string) error {
return otelx.WithSpan(ctx, "password.DefaultPasswordValidator.Validate", func(ctx context.Context) error {
return s.validate(ctx, identifier, password)
})
}
func (s *DefaultPasswordValidator) validate(ctx context.Context, identifier, password string) error {
passwordPolicyConfig := s.reg.Config().PasswordPolicyConfig(ctx)
if len(password) < int(passwordPolicyConfig.MinPasswordLength) {
return errors.Errorf("password length must be at least %d characters but only got %d", passwordPolicyConfig.MinPasswordLength, len(password))
}
if passwordPolicyConfig.IdentifierSimilarityCheckEnabled && len(identifier) > 0 {
compIdentifier, compPassword := strings.ToLower(identifier), strings.ToLower(password)
dist := levenshtein.Distance(compIdentifier, compPassword)
lcs := float32(lcsLength(compIdentifier, compPassword)) / float32(len(compPassword))
if dist < s.minIdentifierPasswordDist || lcs > s.maxIdentifierPasswordSubstrThreshold {
return errors.Errorf("the password is too similar to the user identifier")
}
}
if !passwordPolicyConfig.HaveIBeenPwnedEnabled {
return nil
}
//#nosec G401 -- sha1 is used for k-anonymity
h := sha1.New()
if _, err := h.Write([]byte(password)); err != nil {
return err
}
hpw := h.Sum(nil)
c, ok := s.hashes.Get(b20(hpw))
if !ok {
var err error
c, err = s.fetch(ctx, hpw, passwordPolicyConfig.HaveIBeenPwnedHost)
if (errors.Is(err, ErrNetworkFailure) || errors.Is(err, ErrUnexpectedStatusCode)) && passwordPolicyConfig.IgnoreNetworkErrors {
return nil
} else if err != nil {
return err
}
}
v, ok := c.(int64)
if ok && v > int64(s.reg.Config().PasswordPolicyConfig(ctx).MaxBreaches) {
return errors.WithStack(ErrTooManyBreaches)
}
return nil
}