| 1 | package home |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/rastrillo/rastrillo/sessions" |
| 5 | "bytes" |
| 6 | "context" |
| 7 | cryptorand "crypto/rand" |
| 8 | "encoding/base64" |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | "github.com/keymaildev/signin" |
| 12 | "io" |
| 13 | "log/slog" |
| 14 | "net/http" |
| 15 | "net/http/httptest" |
| 16 | "net/url" |
| 17 | "path/filepath" |
| 18 | "regexp" |
| 19 | "strings" |
| 20 | "sync" |
| 21 | "sync/atomic" |
| 22 | "testing" |
| 23 | "time" |
| 24 | |
| 25 | "amadan.net/oficina/home/internal/home/sealtest" |
| 26 | "amadan.net/rastrillo/rastrillo/webauthn/authtest" |
| 27 | ) |
| 28 | |
| 29 | func idCodes(t *testing.T, app *testApp, s *http.Cookie) []string { |
| 30 | t.Helper() |
| 31 | r := exchangeRequest(app.App, "POST", Origin+"/passkey/codes", Origin, url.Values{}, s) |
| 32 | if r.Code != 200 { |
| 33 | t.Fatal("codes", r.Code) |
| 34 | } |
| 35 | matches := regexp.MustCompile(`<code>([^<]+)</code>`).FindAllStringSubmatch(r.Body.String(), -1) |
| 36 | var codes []string |
| 37 | for _, m := range matches { |
| 38 | codes = append(codes, m[1]) |
| 39 | } |
| 40 | if len(codes) != 10 { |
| 41 | t.Fatal("wrong code count", len(codes)) |
| 42 | } |
| 43 | return codes |
| 44 | } |
| 45 | func idRecover(app *testApp, code string, cookies ...*http.Cookie) *http.Response { |
| 46 | return exchangeRequest(app.App, "POST", Origin+"/passkey/signin/recovery", Origin, url.Values{"code": {code}}, cookies...).Result() |
| 47 | } |
| 48 | func TestIDRecoveryEntropy(t *testing.T) { |
| 49 | a := newTestApp(t) |
| 50 | s := signInFully(t, a, sealtest.CanaryAddress) |
| 51 | enrol(t, a, s) |
| 52 | seen := map[string]bool{} |
| 53 | entropy := &entropyReader{Reader: cryptorand.Reader} |
| 54 | cryptorand.Reader = entropy |
| 55 | defer func() { cryptorand.Reader = entropy.Reader }() |
| 56 | for _, c := range idCodes(t, a, s) { |
| 57 | raw := strings.ReplaceAll(c, "-", "") |
| 58 | if len(raw) < 16 || seen[raw] { |
| 59 | t.Fatal("less than 80 random bits or repeated code") |
| 60 | } |
| 61 | seen[raw] = true |
| 62 | } |
| 63 | if entropy.n < 100 { |
| 64 | t.Fatal("recovery codes used less than 80 bits each", entropy.n) |
| 65 | } |
| 66 | sealtest.AssertNoPlaintext(t, a.dbPath) |
| 67 | } |
| 68 | |
| 69 | type entropyReader struct { |
| 70 | io.Reader |
| 71 | n int |
| 72 | } |
| 73 | |
| 74 | func (r *entropyReader) Read(b []byte) (int, error) { n, e := r.Reader.Read(b); r.n += n; return n, e } |
| 75 | func TestIDRecoveryAttemptLimitSurvivesReconstruction(t *testing.T) { |
| 76 | a := newTestApp(t) |
| 77 | s := signInFully(t, a, sealtest.CanaryAddress) |
| 78 | enrol(t, a, s) |
| 79 | codes := idCodes(t, a, s) |
| 80 | gated := redeemAfterSignIn(t, a, sealtest.CanaryAddress) |
| 81 | gatedCookies := gated.Result().Cookies() |
| 82 | var wg sync.WaitGroup |
| 83 | for i := 0; i < 16; i++ { |
| 84 | wg.Add(1) |
| 85 | go func() { defer wg.Done(); idRecover(a, "bad-code", gatedCookies...) }() |
| 86 | } |
| 87 | wg.Wait() |
| 88 | app, err := New(a.secrets, a.db, a.logger) |
| 89 | if err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
| 92 | app.flow.Classifier = a.flow.Classifier |
| 93 | a.App = app |
| 94 | // RAM intentionally does not survive reconstruction. Re-prove the mailbox |
| 95 | // so the persistent attempt limit, not missing RAM, must refuse this code. |
| 96 | gated = redeemAfterSignIn(t, a, sealtest.CanaryAddress) |
| 97 | r := exchangeRequest(a.App, "POST", Origin+"/passkey/signin/recovery", Origin, url.Values{"code": {codes[0]}}, gated.Result().Cookies()...) |
| 98 | if sessionCookie(r) != nil { |
| 99 | t.Fatal("parallel failures or restart reset recovery allowance") |
| 100 | } |
| 101 | } |
| 102 | func TestIDRecoveryReplacementRevokesEverything(t *testing.T) { |
| 103 | a := newTestApp(t) |
| 104 | s := signInFully(t, a, sealtest.CanaryAddress) |
| 105 | old := enrol(t, a, s) |
| 106 | codes := idCodes(t, a, s) |
| 107 | gated := redeemAfterSignIn(t, a, sealtest.CanaryAddress) |
| 108 | recovery := exchangeRequest(a.App, "POST", Origin+"/passkey/signin/recovery", Origin, url.Values{"code": {codes[0]}}, gated.Result().Cookies()...) |
| 109 | if sessionCookie(recovery) == nil { |
| 110 | t.Fatal("recovery signin", recovery.Code) |
| 111 | } |
| 112 | cookies := recovery.Result().Cookies() |
| 113 | redeemAfterSignIn(t, a, sealtest.CanaryAddress) // another device still between factors |
| 114 | subject := subjectOf(t, a, s) |
| 115 | a.db.Writer().Exec("INSERT INTO passkey_challenges VALUES(?, ?, 'stepup', ?)", "old-challenge", subject, time.Now().Add(time.Minute).UTC().Format(time.RFC3339)) |
| 116 | a.db.Writer().Exec("INSERT INTO home_id_ceremonies VALUES(?, ?, ?, 'stepup', ?)", "other-challenge", "other-browser", subject, time.Now().Unix()+60) |
| 117 | // Recovery is not a way to add an attacker key while retaining the old one. |
| 118 | if r := idJSON(t, a, "/passkey/register/begin", nil, cookies...); r.Code == 200 { |
| 119 | t.Fatal("recovery bypassed replacement confirmation") |
| 120 | } |
| 121 | page := exchangeRequest(a.App, "GET", Origin+"/passkey/replace", "", nil, cookies...) |
| 122 | if page.Code != 200 || !strings.Contains(page.Body.String(), "Replace all passkeys") { |
| 123 | t.Fatal("missing explicit replacement confirmation", page.Code) |
| 124 | } |
| 125 | begin := idJSON(t, a, "/passkey/replace/begin", nil, cookies...) |
| 126 | challenge := challengeFrom(t, begin) |
| 127 | replacement, _ := authtest.New() |
| 128 | cd, att := replacement.Create(challenge, authtest.Options{RPID: credentialHost, Origin: Origin}) |
| 129 | done := idJSON(t, a, "/passkey/replace/finish", map[string]string{"clientDataJSON": base64.RawURLEncoding.EncodeToString(cd), "attestationObject": base64.RawURLEncoding.EncodeToString(att)}, cookies...) |
| 130 | if done.Code != 200 { |
| 131 | t.Fatal("replace", done.Code) |
| 132 | } |
| 133 | for _, table := range []string{"sessions", "passkey_recovery_codes", "passkey_pending", "home_id_recovery_proofs", "passkey_challenges", "home_id_ceremonies"} { |
| 134 | var n int |
| 135 | if err := a.db.Writer().QueryRow("SELECT count(*) FROM " + table).Scan(&n); err != nil || n != 0 { |
| 136 | t.Fatal("replacement retained authority", table, n, err) |
| 137 | } |
| 138 | } |
| 139 | var n int |
| 140 | a.db.Writer().QueryRow("SELECT count(*) FROM passkey_credentials").Scan(&n) |
| 141 | if n != 1 { |
| 142 | t.Fatal("old passkeys remain", n) |
| 143 | } |
| 144 | b := idJSON(t, a, "/passkey/direct/begin", nil) |
| 145 | c := namedCookie(t, b, "__Host-oficina-ceremony") |
| 146 | if r := idJSON(t, a, "/passkey/direct/finish", assertionBody(old, challengeFrom(t, b), authtest.Options{RPID: credentialHost, Origin: Origin}), c); r.Code == 200 { |
| 147 | t.Fatal("old passkey still works") |
| 148 | } |
| 149 | b = idJSON(t, a, "/passkey/direct/begin", nil) |
| 150 | c = namedCookie(t, b, "__Host-oficina-ceremony") |
| 151 | r := idJSON(t, a, "/passkey/direct/finish", assertionBody(replacement, challengeFrom(t, b), authtest.Options{RPID: credentialHost, Origin: Origin}), c) |
| 152 | if r.Code != 200 { |
| 153 | t.Fatal("replacement cannot sign in", r.Code) |
| 154 | } |
| 155 | if !strings.Contains(r.Body.String(), `"to":"/passkey/codes"`) { |
| 156 | t.Fatal("replacement signin skipped new recovery codes") |
| 157 | } |
| 158 | idCodes(t, a, sessionCookie(r)) |
| 159 | mail, ok := a.mailer.last() |
| 160 | if !ok || mail.To != sealtest.CanaryAddress || mail.Subject != "Your Oficina passkeys were replaced" { |
| 161 | t.Fatal("replacement notice missing") |
| 162 | } |
| 163 | sealtest.AssertNoPlaintext(t, a.dbPath) |
| 164 | } |
| 165 | |
| 166 | type idFailMailer struct{} |
| 167 | |
| 168 | func (idFailMailer) Send(context.Context, string, string, string) error { |
| 169 | return errors.New("SMTP failed for " + sealtest.CanaryAddress) |
| 170 | } |
| 171 | func TestIDSMTPFailureDoesNotLeak(t *testing.T) { |
| 172 | a := newTestApp(t) |
| 173 | var logs bytes.Buffer |
| 174 | a.logger = slog.New(slog.NewTextHandler(&logs, nil)) |
| 175 | a.flow.Mailer = idFailMailer{} |
| 176 | r := signIn(t, a, sealtest.CanaryAddress) |
| 177 | if sessionCookie(r) != nil || strings.Contains(logs.String(), sealtest.CanaryAddress) { |
| 178 | t.Fatal("SMTP failure authenticated or logged address") |
| 179 | } |
| 180 | sealtest.AssertNoPlaintext(t, a.dbPath) |
| 181 | } |
| 182 | |
| 183 | func TestIDRecoveryRequiresFreshMailboxGesture(t *testing.T) { |
| 184 | a := newTestApp(t) |
| 185 | s := signInFully(t, a, sealtest.CanaryAddress) |
| 186 | enrol(t, a, s) |
| 187 | codes := idCodes(t, a, s) |
| 188 | for _, at := range []time.Time{{}, time.Now().Add(-10 * time.Minute), time.Now().Add(time.Hour)} { |
| 189 | req := httptest.NewRequest("GET", Origin+callbackPath, nil) |
| 190 | w := httptest.NewRecorder() |
| 191 | a.completeSignIn(w, req, signin.Identity{Address: sealtest.CanaryAddress, Method: signin.MethodKeymail, At: time.Now(), AuthTime: at}) |
| 192 | r := exchangeRequest(a.App, "POST", Origin+"/passkey/signin/recovery", Origin, url.Values{"code": {codes[0]}}, w.Result().Cookies()...) |
| 193 | if sessionCookie(r) != nil { |
| 194 | t.Fatal("passive or stale Keymail recovered account") |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | func replacementFixture(t *testing.T) (*testApp, []*http.Cookie, map[string]string) { |
| 200 | t.Helper() |
| 201 | a := newTestApp(t) |
| 202 | s := signInFully(t, a, sealtest.CanaryAddress) |
| 203 | enrol(t, a, s) |
| 204 | codes := idCodes(t, a, s) |
| 205 | gated := redeemAfterSignIn(t, a, sealtest.CanaryAddress) |
| 206 | recovered := exchangeRequest(a.App, "POST", Origin+"/passkey/signin/recovery", Origin, url.Values{"code": {codes[0]}}, gated.Result().Cookies()...) |
| 207 | if sessionCookie(recovered) == nil { |
| 208 | t.Fatal("recovery failed", recovered.Code) |
| 209 | } |
| 210 | cookies := recovered.Result().Cookies() |
| 211 | begin := idJSON(t, a, "/passkey/replace/begin", nil, cookies...) |
| 212 | key, _ := authtest.New() |
| 213 | cd, att := key.Create(challengeFrom(t, begin), authtest.Options{RPID: credentialHost, Origin: Origin}) |
| 214 | return a, cookies, map[string]string{"clientDataJSON": base64.RawURLEncoding.EncodeToString(cd), "attestationObject": base64.RawURLEncoding.EncodeToString(att)} |
| 215 | } |
| 216 | func TestIDNoticeFailureSurvivesRestartWithoutAddress(t *testing.T) { |
| 217 | a, cookies, body := replacementFixture(t) |
| 218 | var logs bytes.Buffer |
| 219 | a.logger = slog.New(slog.NewTextHandler(&logs, nil)) |
| 220 | a.secrets.Mailer = idFailMailer{} |
| 221 | done := idJSON(t, a, "/passkey/replace/finish", body, cookies...) |
| 222 | if done.Code != 200 { |
| 223 | t.Fatal("delivery failure rolled back replacement", done.Code) |
| 224 | } |
| 225 | var n int |
| 226 | a.db.Writer().QueryRow("SELECT count(*) FROM sessions").Scan(&n) |
| 227 | if n != 0 { |
| 228 | t.Fatal("delivery failure preserved sessions") |
| 229 | } |
| 230 | if strings.Contains(logs.String(), sealtest.CanaryAddress) { |
| 231 | t.Fatal("notice error leaked address") |
| 232 | } |
| 233 | app, err := New(a.secrets, a.db, a.logger) |
| 234 | if err != nil { |
| 235 | t.Fatal(err) |
| 236 | } |
| 237 | a.App = app |
| 238 | page := exchangeRequest(a.App, "GET", Origin+"/passkey/replaced", "", nil, done.Result().Cookies()...) |
| 239 | if !strings.Contains(page.Body.String(), "could not send") || !strings.Contains(page.Body.String(), "can no longer be sent") || strings.Contains(page.Body.String(), "Send email again") { |
| 240 | t.Fatal("restart hid notice failure or retained retry") |
| 241 | } |
| 242 | sealtest.AssertNoPlaintext(t, a.dbPath) |
| 243 | } |
| 244 | func TestIDReplacementRollback(t *testing.T) { |
| 245 | for _, target := range []struct{ table, operation string }{{"sessions", "DELETE"}, {"passkey_recovery_codes", "DELETE"}, {"passkey_credentials", "DELETE"}, {"home_id_recovery_proofs", "DELETE"}, {"passkey_credentials", "INSERT"}, {"home_id_discoverable", "INSERT"}} { |
| 246 | t.Run(target.table+target.operation, func(t *testing.T) { |
| 247 | a, cookies, body := replacementFixture(t) |
| 248 | before := map[string]int{} |
| 249 | for _, table := range []string{"sessions", "passkey_credentials", "passkey_recovery_codes", "home_id_recovery_proofs"} { |
| 250 | var n int |
| 251 | if err := a.db.Writer().QueryRow("SELECT count(*) FROM " + table).Scan(&n); err != nil { |
| 252 | t.Fatal(err) |
| 253 | } |
| 254 | before[table] = n |
| 255 | } |
| 256 | if _, err := a.db.Writer().Exec("CREATE TRIGGER fail_replacement BEFORE " + target.operation + " ON " + target.table + " BEGIN SELECT RAISE(ABORT,'injected'); END"); err != nil { |
| 257 | t.Fatal(err) |
| 258 | } |
| 259 | r := idJSON(t, a, "/passkey/replace/finish", body, cookies...) |
| 260 | if r.Code == 200 { |
| 261 | t.Fatal("failed write reported success") |
| 262 | } |
| 263 | for table, want := range before { |
| 264 | var n int |
| 265 | a.db.Writer().QueryRow("SELECT count(*) FROM " + table).Scan(&n) |
| 266 | if n != want { |
| 267 | t.Fatal("partial replacement", table, n, want) |
| 268 | } |
| 269 | } |
| 270 | }) |
| 271 | } |
| 272 | } |
| 273 | func TestIDReplacementProofBinding(t *testing.T) { |
| 274 | for _, kind := range []string{"no-proof", "no-session", "expired", "wrong-person", "bad-uv", "replay"} { |
| 275 | t.Run(kind, func(t *testing.T) { |
| 276 | a, cookies, body := replacementFixture(t) |
| 277 | switch kind { |
| 278 | case "no-proof": |
| 279 | cookies = []*http.Cookie{func() *http.Cookie { |
| 280 | for _, c := range cookies { |
| 281 | if c.Name == a.sessions.CookieName() { |
| 282 | return c |
| 283 | } |
| 284 | } |
| 285 | return nil |
| 286 | }()} |
| 287 | case "no-session": |
| 288 | cookies = []*http.Cookie{func() *http.Cookie { |
| 289 | for _, c := range cookies { |
| 290 | if c.Name == recoveryCookie { |
| 291 | return c |
| 292 | } |
| 293 | } |
| 294 | return nil |
| 295 | }()} |
| 296 | case "expired": |
| 297 | a.db.Writer().Exec("UPDATE home_id_recovery_proofs SET expires_at=0") |
| 298 | case "wrong-person": |
| 299 | a.db.Writer().Exec("UPDATE home_id_recovery_proofs SET subject='another-person'") |
| 300 | if r := idJSON(t, a, "/passkey/replace/begin", nil, cookies...); r.Code == 200 { |
| 301 | t.Fatal("proof for another person began replacement") |
| 302 | } |
| 303 | return |
| 304 | case "bad-uv": |
| 305 | b := idJSON(t, a, "/passkey/replace/begin", nil, cookies...) |
| 306 | k, _ := authtest.New() |
| 307 | cd, att := k.Create(challengeFrom(t, b), authtest.Options{RPID: credentialHost, Origin: Origin, Flags: authtest.FlagUserPresent | authtest.FlagAttestedData}) |
| 308 | body = map[string]string{"clientDataJSON": base64.RawURLEncoding.EncodeToString(cd), "attestationObject": base64.RawURLEncoding.EncodeToString(att)} |
| 309 | case "replay": |
| 310 | if r := idJSON(t, a, "/passkey/replace/finish", body, cookies...); r.Code != 200 { |
| 311 | t.Fatal("first replacement failed") |
| 312 | } |
| 313 | } |
| 314 | if r := idJSON(t, a, "/passkey/replace/finish", body, cookies...); r.Code == 200 { |
| 315 | t.Fatal("unbound recovery proof replaced passkeys", kind) |
| 316 | } |
| 317 | }) |
| 318 | } |
| 319 | } |
| 320 | func TestIDRecoveryIPLimitAcrossAccounts(t *testing.T) { |
| 321 | a := newTestApp(t) |
| 322 | for i := 0; i < 6; i++ { |
| 323 | address := fmt.Sprintf("account%d@example.invalid", i) |
| 324 | s := signInFully(t, a, address) |
| 325 | enrol(t, a, s) |
| 326 | codes := idCodes(t, a, s) |
| 327 | g := redeemAfterSignIn(t, a, address) |
| 328 | if i < 5 { |
| 329 | for j := 0; j < 4; j++ { |
| 330 | idRecover(a, "wrong", g.Result().Cookies()...) |
| 331 | } |
| 332 | } else { |
| 333 | r := exchangeRequest(a.App, "POST", Origin+"/passkey/signin/recovery", Origin, url.Values{"code": {codes[0]}}, g.Result().Cookies()...) |
| 334 | if sessionCookie(r) != nil { |
| 335 | t.Fatal("IP budget did not span accounts") |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | func TestIDSigninLimiterUsesBlindedAddress(t *testing.T) { |
| 342 | a := newTestApp(t) |
| 343 | signIn(t, a, sealtest.CanaryAddress) |
| 344 | if strings.Contains(fmt.Sprintf("%#v", a.signinLimiter), sealtest.CanaryAddress) { |
| 345 | t.Fatal("sign-in limiter retains a readable address") |
| 346 | } |
| 347 | } |
| 348 | func TestIDRecoveryPrivacyInBackup(t *testing.T) { |
| 349 | a, cookies, body := replacementFixture(t) |
| 350 | done := idJSON(t, a, "/passkey/replace/finish", body, cookies...) |
| 351 | if done.Code != 200 { |
| 352 | t.Fatal("replacement", done.Code) |
| 353 | } |
| 354 | backup := filepath.Join(t.TempDir(), "backup.sqlite") |
| 355 | if _, err := a.db.Writer().Exec("VACUUM INTO ?", backup); err != nil { |
| 356 | t.Fatal(err) |
| 357 | } |
| 358 | sealtest.AssertNoPlaintext(t, backup) |
| 359 | sealtest.AssertNoPlaintext(t, a.dbPath) |
| 360 | } |
| 361 | func TestIDReplacementNoticeRetry(t *testing.T) { |
| 362 | a, cookies, body := replacementFixture(t) |
| 363 | a.secrets.Mailer = idFailMailer{} |
| 364 | done := idJSON(t, a, "/passkey/replace/finish", body, cookies...) |
| 365 | if done.Code != 200 { |
| 366 | t.Fatal("replacement", done.Code) |
| 367 | } |
| 368 | a.secrets.Mailer = a.mailer |
| 369 | retry := exchangeRequest(a.App, "POST", Origin+"/passkey/notice", Origin, url.Values{}, done.Result().Cookies()...) |
| 370 | if retry.Code != 303 { |
| 371 | t.Fatal("retry", retry.Code) |
| 372 | } |
| 373 | mail, ok := a.mailer.last() |
| 374 | if !ok || mail.To != sealtest.CanaryAddress || mail.Subject != replacementNoticeSubject { |
| 375 | t.Fatal("retry did not use proven mailbox") |
| 376 | } |
| 377 | key := sessions.HashToken(namedCookie(t, done, noticeCookie).Value) |
| 378 | if _, ok := a.mailboxes.get(key); ok { |
| 379 | t.Fatal("successful notice retained address") |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | func TestIDRecoveryCodePersonBindingAndReplay(t *testing.T) { |
| 384 | a := newTestApp(t) |
| 385 | s := signInFully(t, a, sealtest.CanaryAddress) |
| 386 | enrol(t, a, s) |
| 387 | codes := idCodes(t, a, s) |
| 388 | other := signInFully(t, a, "other@example.invalid") |
| 389 | enrol(t, a, other) |
| 390 | wrongGate := redeemAfterSignIn(t, a, "other@example.invalid") |
| 391 | wrong := exchangeRequest(a.App, "POST", Origin+"/passkey/signin/recovery", Origin, url.Values{"code": {codes[0]}}, wrongGate.Result().Cookies()...) |
| 392 | if sessionCookie(wrong) != nil { |
| 393 | t.Fatal("code crossed accounts") |
| 394 | } |
| 395 | gate := redeemAfterSignIn(t, a, sealtest.CanaryAddress) |
| 396 | gateCookies := gate.Result().Cookies() |
| 397 | var successes atomic.Int32 |
| 398 | var wg sync.WaitGroup |
| 399 | for i := 0; i < 12; i++ { |
| 400 | wg.Add(1) |
| 401 | go func() { |
| 402 | defer wg.Done() |
| 403 | r := exchangeRequest(a.App, "POST", Origin+"/passkey/signin/recovery", Origin, url.Values{"code": {codes[0]}}, gateCookies...) |
| 404 | if sessionCookie(r) != nil { |
| 405 | successes.Add(1) |
| 406 | } |
| 407 | }() |
| 408 | } |
| 409 | wg.Wait() |
| 410 | if successes.Load() != 1 { |
| 411 | t.Fatal("recovery proof minted more than once", successes.Load()) |
| 412 | } |
| 413 | gate = redeemAfterSignIn(t, a, sealtest.CanaryAddress) |
| 414 | replay := exchangeRequest(a.App, "POST", Origin+"/passkey/signin/recovery", Origin, url.Values{"code": {codes[0]}}, gate.Result().Cookies()...) |
| 415 | if sessionCookie(replay) != nil { |
| 416 | t.Fatal("used code replayed with new mailbox proof") |
| 417 | } |
| 418 | } |
| 419 | func TestIDRecoveryCannotRegenerateCodesOrSatisfyPasskeyOnlyStepup(t *testing.T) { |
| 420 | a, cookies, _ := replacementFixture(t) |
| 421 | r := exchangeRequest(a.App, "POST", Origin+"/passkey/codes", Origin, url.Values{}, cookies...) |
| 422 | if r.Code == 200 { |
| 423 | t.Fatal("recovery session generated codes before new passkey proof") |
| 424 | } |
| 425 | req := httptest.NewRequest("GET", Origin+"/", nil) |
| 426 | for _, c := range cookies { |
| 427 | req.AddCookie(c) |
| 428 | } |
| 429 | s, ok := a.sessions.From(req) |
| 430 | if !ok || credentialFresh(s, time.Now()) { |
| 431 | t.Fatal("recovery satisfied passkey-only operator stepup") |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | func TestIDMailboxMemoryBoundAndExpiry(t *testing.T) { |
| 436 | m := mailboxMemory{entries: map[string]mailboxProof{}} |
| 437 | for i := 0; i < 1000; i++ { |
| 438 | m.entries[fmt.Sprint(i)] = mailboxProof{"person", sealtest.CanaryAddress, time.Now().Unix() + 300} |
| 439 | } |
| 440 | if m.put("overflow", mailboxProof{"person", sealtest.CanaryAddress, time.Now().Unix() + 300}) { |
| 441 | t.Fatal("mailbox memory exceeded hard bound") |
| 442 | } |
| 443 | m.entries["expired"] = mailboxProof{"person", sealtest.CanaryAddress, time.Now().Unix() - 1} |
| 444 | if _, ok := m.get("expired"); ok { |
| 445 | t.Fatal("expired mailbox proof retained") |
| 446 | } |
| 447 | } |
| 448 | func TestIDRecoveryProofCapacity(t *testing.T) { |
| 449 | a := newTestApp(t) |
| 450 | s := signInFully(t, a, sealtest.CanaryAddress) |
| 451 | enrol(t, a, s) |
| 452 | codes := idCodes(t, a, s) |
| 453 | g := redeemAfterSignIn(t, a, sealtest.CanaryAddress) |
| 454 | if _, err := a.db.Writer().Exec("WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x+1 FROM n WHERE x<10000) INSERT INTO home_id_recovery_proofs SELECT printf('%064x',x),'person','session',? FROM n", time.Now().Unix()+300); err != nil { |
| 455 | t.Fatal(err) |
| 456 | } |
| 457 | r := exchangeRequest(a.App, "POST", Origin+"/passkey/signin/recovery", Origin, url.Values{"code": {codes[0]}}, g.Result().Cookies()...) |
| 458 | if sessionCookie(r) != nil { |
| 459 | t.Fatal("recovery proof capacity exceeded") |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | func TestIDCodesRequireEnrolledPasskey(t *testing.T) { |
| 464 | a := newTestApp(t) |
| 465 | s := signInFully(t, a, sealtest.CanaryAddress) |
| 466 | r := exchangeRequest(a.App, "POST", Origin+"/passkey/codes", Origin, url.Values{}, s) |
| 467 | if r.Code == 200 { |
| 468 | t.Fatal("codes issued before passkey enrollment") |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | func TestIDCredentialPagesAreNoStore(t *testing.T) { |
| 473 | a := newTestApp(t) |
| 474 | s := signInFully(t, a, sealtest.CanaryAddress) |
| 475 | enrol(t, a, s) |
| 476 | for _, path := range []string{"/signin", "/passkey/enrol", "/passkey/codes", "/passkey/replace", "/passkey/replaced"} { |
| 477 | r := exchangeRequest(a.App, "GET", Origin+path, "", nil, s) |
| 478 | if r.Header().Get("Cache-Control") != "no-store" { |
| 479 | t.Fatal("credential page permits storage", path) |
| 480 | } |
| 481 | } |
| 482 | r := exchangeRequest(a.App, "POST", Origin+"/passkey/codes", Origin, url.Values{}, s) |
| 483 | if r.Code != 200 || r.Header().Get("Cache-Control") != "no-store" { |
| 484 | t.Fatal("shown-once codes permit storage") |
| 485 | } |
| 486 | } |
| 487 | |