oxedyne/fe2o3/fe2o3_net/tests/webauthn_challenge.rs
4.2 KiB, 1 run
created by r1870400018:72159, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! `webauthn::challenge`: stateless challenges, each accepted once. |
| 2 | //! |
| 3 | //! [Written with AI entirely](https://need2know.ai/entirely-ai/code)\ |
| 4 | //! Anthropic Claude |
| 5 | |
| 6 | mod webauthn { |
| 7 | |
| 8 | use oxedyne_fe2o3_net::{ |
| 9 | acme::jose::{ |
| 10 | base64url_decode, |
| 11 | base64url_encode, |
| 12 | }, |
| 13 | webauthn::challenge::{ |
| 14 | Challenges, |
| 15 | Purpose, |
| 16 | TTL_SECS, |
| 17 | }, |
| 18 | }; |
| 19 | |
| 20 | use oxedyne_fe2o3_core::prelude::*; |
| 21 | |
| 22 | const T0: u64 = 1_800_000_000; |
| 23 | |
| 24 | #[test] |
| 25 | fn a_challenge_checks_once() -> Outcome<()> { |
| 26 | let ch = Challenges::new(); |
| 27 | let (c, cookie) = ch.mint(Purpose::Get, T0); |
| 28 | assert_eq!(res!(ch.used_len()), 0, "a mint records nothing"); |
| 29 | res!(ch.check(&cookie, &c, Purpose::Get, T0 + 5)); |
| 30 | assert_eq!(res!(ch.used_len()), 1); |
| 31 | assert!(ch.check(&cookie, &c, Purpose::Get, T0 + 6).is_err(), "a replay"); |
| 32 | assert!(ch.check(&cookie, &c, Purpose::Get, T0 + TTL_SECS - 1).is_err(), "a late replay"); |
| 33 | // A second challenge, minted alike, is its own. |
| 34 | let (c2, cookie2) = ch.mint(Purpose::Create, T0); |
| 35 | assert_ne!(c, c2); |
| 36 | res!(ch.check(&cookie2, &c2, Purpose::Create, T0 + 1)); |
| 37 | Ok(()) |
| 38 | } |
| 39 | |
| 40 | #[test] |
| 41 | fn bad_challenges_are_refused() -> Outcome<()> { |
| 42 | let ch = Challenges::new(); |
| 43 | let (c, cookie) = ch.mint(Purpose::Create, T0); |
| 44 | |
| 45 | // Expired. |
| 46 | assert!(ch.check(&cookie, &c, Purpose::Create, T0 + TTL_SECS).is_err()); |
| 47 | // For the other ceremony. |
| 48 | assert!(ch.check(&cookie, &c, Purpose::Get, T0 + 1).is_err()); |
| 49 | // The browser signed another challenge. |
| 50 | let mut other = c; |
| 51 | other[0] ^= 1; |
| 52 | assert!(ch.check(&cookie, &other, Purpose::Create, T0 + 1).is_err()); |
| 53 | // Tampered: each field flipped in turn, the expiry pushed out, the purpose switched. |
| 54 | let b = res!(base64url_decode(&cookie)); |
| 55 | for i in [0usize, 1, 8, 9, 40, 41, 72] { |
| 56 | let mut t = b.clone(); |
| 57 | t[i] ^= 1; |
| 58 | assert!(ch.check(&base64url_encode(&t), &c, Purpose::Create, T0 + 1).is_err(), |
| 59 | "byte {} flipped", i); |
| 60 | } |
| 61 | let mut late = b.clone(); |
| 62 | late[1..9].copy_from_slice(&(T0 + 10_000).to_be_bytes()); |
| 63 | assert!(ch.check(&base64url_encode(&late), &c, Purpose::Create, T0 + 1).is_err()); |
| 64 | let mut get = b.clone(); |
| 65 | get[0] = b'g'; |
| 66 | assert!(ch.check(&base64url_encode(&get), &c, Purpose::Get, T0 + 1).is_err()); |
| 67 | // Junk and the wrong length. |
| 68 | assert!(ch.check("not a cookie!", &c, Purpose::Create, T0 + 1).is_err()); |
| 69 | assert!(ch.check(&base64url_encode(&b[..40]), &c, Purpose::Create, T0 + 1).is_err()); |
| 70 | // Minted by another process, or before a restart. |
| 71 | let foreign = Challenges::new(); |
| 72 | let (fc, fcookie) = foreign.mint(Purpose::Create, T0); |
| 73 | assert!(ch.check(&fcookie, &fc, Purpose::Create, T0 + 1).is_err()); |
| 74 | |
| 75 | // None of those refusals spent the real one. |
| 76 | assert_eq!(res!(ch.used_len()), 0); |
| 77 | res!(ch.check(&cookie, &c, Purpose::Create, T0 + 1)); |
| 78 | Ok(()) |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn ten_thousand_mints_hold_no_state() -> Outcome<()> { |
| 83 | let ch = Challenges::new().with_max_used(100); |
| 84 | let mut minted = Vec::with_capacity(10_000); |
| 85 | for i in 0..10_000u64 { |
| 86 | minted.push(ch.mint(Purpose::Get, T0 + i / 1_000)); |
| 87 | } |
| 88 | assert_eq!(res!(ch.used_len()), 0, "a mint keeps nothing"); |
| 89 | // The earliest is still checkable: no flood of mints evicts it. |
| 90 | res!(ch.check(&minted[0].1, &minted[0].0, Purpose::Get, T0 + 10)); |
| 91 | // Checks fill the used-set to its bound and no further. |
| 92 | let mut refused = 0usize; |
| 93 | for (c, cookie) in minted.iter().skip(1).take(200) { |
| 94 | if ch.check(cookie, c, Purpose::Get, T0 + 10).is_err() { |
| 95 | refused += 1; |
| 96 | } |
| 97 | } |
| 98 | assert_eq!(res!(ch.used_len()), 100); |
| 99 | assert_eq!(refused, 101); |
| 100 | // And it empties as the window passes. |
| 101 | let (c, cookie) = ch.mint(Purpose::Get, T0 + 10 + TTL_SECS + 1); |
| 102 | res!(ch.check(&cookie, &c, Purpose::Get, T0 + 10 + TTL_SECS + 2)); |
| 103 | assert_eq!(res!(ch.used_len()), 1); |
| 104 | Ok(()) |
| 105 | } |
| 106 | } |