forge
Explore
Log in

Don't you dare go Hollow

hezarfen_backend

Clone
★ 0

0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits

auth_enumeration.rs158 lines, 5.3 KiBHistoryRawDownload
1//! `POST /auth/register` must not tell an unauthenticated caller whether a
2//! username exists — not by status, not by body, not by how long it takes.
3
4mod common;
5
6use std::time::Instant;
7
8use axum::http::StatusCode;
9use common::{app_and_db, items, send, set_role};
10use serde_json::json;
11
12#[tokio::test]
13async fn duplicate_register_is_indistinguishable_from_a_fresh_one() {
14 let (app, db) = app_and_db().await;
15 let taken = json!({ "school": "019732e3-7b00-7000-8000-00000000dead", "username": "ada", "password": "secret1" });
16
17 let first = send(&app, "POST", "/auth/register", None, Some(taken)).await;
18 assert_eq!(first.status, StatusCode::CREATED);
19
20 // Same username again, different password: still 201, same body shape.
21 let dup_creds = json!({ "school": "019732e3-7b00-7000-8000-00000000dead", "username": "ada", "password": "attacker" });
22 let started = Instant::now();
23 let dup = send(
24 &app,
25 "POST",
26 "/auth/register",
27 None,
28 Some(dup_creds),
29 )
30 .await;
31 let dup_elapsed = started.elapsed();
32
33 let fresh = send(
34 &app,
35 "POST",
36 "/auth/register",
37 None,
38 Some(json!({ "school": "019732e3-7b00-7000-8000-00000000dead", "username": "grace", "password": "secret1" })),
39 )
40 .await;
41
42 assert_eq!(dup.status, fresh.status, "status leaks existence");
43 assert_eq!(dup.status, StatusCode::CREATED);
44
45 // Structurally identical: same keys, and every value either the echoed
46 // username or the same default.
47 let dup_obj = dup.body.as_object().expect("dup body object");
48 let fresh_obj = fresh.body.as_object().expect("fresh body object");
49 assert_eq!(
50 dup_obj.keys().collect::<Vec<_>>(),
51 fresh_obj.keys().collect::<Vec<_>>(),
52 "field set leaks existence"
53 );
54 for (key, value) in dup_obj {
55 if key == "username" {
56 continue;
57 }
58 assert_eq!(value, &fresh_obj[key], "field {key} leaks existence");
59 }
60 assert_eq!(dup_obj["username"], "ada");
61 assert_eq!(dup_obj["role"], "student");
62 // No id at all: the taken path has no row to name, so returning one would
63 // hand the client an id that resolves to nothing.
64 assert!(
65 !dup_obj.contains_key("id"),
66 "register must not return an id: {dup_obj:?}"
67 );
68 assert!(dup.cookie.is_none() && fresh.cookie.is_none());
69
70 // The argon2 work is what equalises timing, so pin that the taken path pays
71 // it. A floor, not a comparison between the two paths — a gap assertion
72 // would be flaky, whereas this can only fail if hashing got skipped
73 // (argon2's default cost is ~33ms; 8ms leaves room for a loaded CI box).
74 assert!(
75 dup_elapsed.as_millis() >= 8,
76 "taken-username register returned in {dup_elapsed:?} — it skipped argon2, \
77 which re-opens the timing oracle"
78 );
79
80 // The original account is untouched: its password still works and the
81 // attacker's does not.
82 let login = send(
83 &app,
84 "POST",
85 "/auth/login",
86 None,
87 Some(json!({ "username": "ada", "password": "secret1" })),
88 )
89 .await;
90 assert_eq!(
91 login.status,
92 StatusCode::OK,
93 "original password stopped working"
94 );
95 let hijack = send(
96 &app,
97 "POST",
98 "/auth/login",
99 None,
100 Some(json!({ "username": "ada", "password": "attacker" })),
101 )
102 .await;
103 assert_eq!(
104 hijack.status,
105 StatusCode::UNAUTHORIZED,
106 "the duplicate register overwrote the password"
107 );
108
109 // And no second row was written: exactly two users exist (ada, grace).
110 set_role(&db, "ada", "admin").await;
111 let cookie = login.cookie.expect("session cookie");
112 let list = send(&app, "GET", "/users", Some(&cookie), None).await;
113 assert_eq!(list.status, StatusCode::OK);
114 let names: Vec<&str> = items(&list.body)
115 .iter()
116 .map(|u| u["username"].as_str().expect("username"))
117 .collect();
118 assert_eq!(
119 names.iter().filter(|n| **n == "ada").count(),
120 1,
121 "duplicate register created a second row: {names:?}"
122 );
123 assert_eq!(names.len(), 2, "unexpected user rows: {names:?}");
124}
125
126/// The invariant the response type exists to hold: for one and the same
127/// username, the taken reply and the fresh reply are the *identical* JSON
128/// value — not merely the same key set. Any per-path field (an id, a profile
129/// echo, a flag) reintroduced into one arm and not the other fails here.
130#[tokio::test]
131async fn the_taken_body_equals_the_fresh_body_for_the_same_username() {
132 let (app, _db) = app_and_db().await;
133
134 let fresh = send(
135 &app,
136 "POST",
137 "/auth/register",
138 None,
139 Some(json!({ "school": "019732e3-7b00-7000-8000-00000000dead", "username": "ada", "password": "secret1" })),
140 )
141 .await;
142 let taken = send(
143 &app,
144 "POST",
145 "/auth/register",
146 None,
147 Some(json!({ "school": "019732e3-7b00-7000-8000-00000000dead", "username": "ada", "password": "another1" })),
148 )
149 .await;
150
151 assert_eq!(fresh.status, StatusCode::CREATED);
152 assert_eq!(taken.status, StatusCode::CREATED);
153 assert_eq!(
154 taken.body, fresh.body,
155 "the two register paths returned different bodies — that is an \
156 enumeration oracle"
157 );
158}