| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
use crate::harness::TestHarness; |
| 17 |
use makenotwork::csrf::{ManifestPosture, route_manifest}; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
const REJECTED_BEFORE_CSRF_LAYER: &[&str] = &[ |
| 26 |
|
| 27 |
]; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
#[tokio::test] |
| 42 |
async fn every_auto_route_rejects_authenticated_tokenless_mutation() { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
let (mut h, _admin_id) = TestHarness::with_admin().await; |
| 51 |
h.login("admin", "password123").await; |
| 52 |
|
| 53 |
let manifest = route_manifest(); |
| 54 |
let auto_routes: Vec<_> = manifest |
| 55 |
.iter() |
| 56 |
.filter(|e| e.posture == ManifestPosture::Auto) |
| 57 |
.collect(); |
| 58 |
assert!( |
| 59 |
auto_routes.len() > 50, |
| 60 |
"manifest looks empty/broken: {} auto of {} total routes", |
| 61 |
auto_routes.len(), |
| 62 |
manifest.len() |
| 63 |
); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
let mut ip_counter: u32 = 0; |
| 74 |
let mut next_ip = |h: &mut TestHarness| { |
| 75 |
ip_counter += 1; |
| 76 |
h.client.set_forwarded_ip(&format!( |
| 77 |
"10.50.{}.{}", |
| 78 |
(ip_counter / 256) % 256, |
| 79 |
ip_counter % 256 |
| 80 |
)); |
| 81 |
}; |
| 82 |
|
| 83 |
let mut failures = Vec::new(); |
| 84 |
for entry in &auto_routes { |
| 85 |
if REJECTED_BEFORE_CSRF_LAYER.contains(&entry.path.as_str()) { |
| 86 |
continue; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
let mut outcome: Option<(&str, u16)> = None; |
| 93 |
for method in ["POST", "PUT", "PATCH", "DELETE"] { |
| 94 |
next_ip(&mut h); |
| 95 |
let resp = h |
| 96 |
.client |
| 97 |
.request_with_headers(method, &entry.path, None, &[]) |
| 98 |
.await; |
| 99 |
let code = resp.status.as_u16(); |
| 100 |
if code == 403 { |
| 101 |
outcome = Some((method, code)); |
| 102 |
break; |
| 103 |
} |
| 104 |
if code != 405 { |
| 105 |
|
| 106 |
|
| 107 |
outcome = Some((method, code)); |
| 108 |
break; |
| 109 |
} |
| 110 |
|
| 111 |
} |
| 112 |
match outcome { |
| 113 |
Some((_, 403)) => {} |
| 114 |
Some((method, code)) => failures.push(format!("{} [{method}] -> {code}", entry.path)), |
| 115 |
None => failures.push(format!( |
| 116 |
"{} -> all methods 405 (no mutating method?)", |
| 117 |
entry.path |
| 118 |
)), |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
assert!( |
| 123 |
failures.is_empty(), |
| 124 |
"{} Auto route(s) did NOT reject a tokenless authenticated request (CSRF \ |
| 125 |
layer missing/bypassed). If a route is legitimately refused earlier by \ |
| 126 |
an outer layer, add it to REJECTED_BEFORE_CSRF_LAYER with a reason:\n{}", |
| 127 |
failures.len(), |
| 128 |
failures.join("\n") |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
#[tokio::test] |
| 135 |
async fn csrf_manifest_is_populated_and_optouts_are_justified() { |
| 136 |
let _h = TestHarness::new().await; |
| 137 |
let manifest = route_manifest(); |
| 138 |
|
| 139 |
let count = |p: ManifestPosture| manifest.iter().filter(|e| e.posture == p).count(); |
| 140 |
let (auto, manual, skip) = ( |
| 141 |
count(ManifestPosture::Auto), |
| 142 |
count(ManifestPosture::Manual), |
| 143 |
count(ManifestPosture::Skip), |
| 144 |
); |
| 145 |
eprintln!( |
| 146 |
"CSRF manifest: {auto} auto, {manual} manual, {skip} skip, {} total", |
| 147 |
manifest.len() |
| 148 |
); |
| 149 |
|
| 150 |
assert!( |
| 151 |
manifest.len() > 100, |
| 152 |
"manifest too small: {}", |
| 153 |
manifest.len() |
| 154 |
); |
| 155 |
assert!(auto > 50, "expected many Auto routes, got {auto}"); |
| 156 |
|
| 157 |
for e in &manifest { |
| 158 |
if matches!(e.posture, ManifestPosture::Manual | ManifestPosture::Skip) { |
| 159 |
assert!( |
| 160 |
e.reason.is_some_and(|r| !r.trim().is_empty()), |
| 161 |
"{:?} route {} has no documented CSRF justification", |
| 162 |
e.posture, |
| 163 |
e.path |
| 164 |
); |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
#[tokio::test] |
| 176 |
async fn forgot_password_rejects_preauth_tokenless_post() { |
| 177 |
let mut h = TestHarness::new().await; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
let resp = h |
| 183 |
.client |
| 184 |
.request_with_headers( |
| 185 |
"POST", |
| 186 |
"/forgot-password", |
| 187 |
Some("email=nobody@example.com"), |
| 188 |
&[("Content-Type", "application/x-www-form-urlencoded")], |
| 189 |
) |
| 190 |
.await; |
| 191 |
|
| 192 |
assert_eq!( |
| 193 |
resp.status, 403, |
| 194 |
"CHRONIC A' regressed: /forgot-password accepted a pre-auth tokenless \ |
| 195 |
POST (got {}). The validate_auto !has_user skip must stay removed.", |
| 196 |
resp.status |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
#[tokio::test] |
| 203 |
async fn origin_gate_rejects_cross_site_mutation() { |
| 204 |
let mut h = TestHarness::new().await; |
| 205 |
let resp = h |
| 206 |
.client |
| 207 |
.request_with_headers( |
| 208 |
"POST", |
| 209 |
"/forgot-password", |
| 210 |
Some("email=nobody@example.com"), |
| 211 |
&[ |
| 212 |
("Content-Type", "application/x-www-form-urlencoded"), |
| 213 |
("Sec-Fetch-Site", "cross-site"), |
| 214 |
], |
| 215 |
) |
| 216 |
.await; |
| 217 |
assert_eq!( |
| 218 |
resp.status, 403, |
| 219 |
"origin gate let a Sec-Fetch-Site: cross-site mutation through (got {})", |
| 220 |
resp.status |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
#[tokio::test] |
| 230 |
async fn origin_gate_allows_same_origin_signal() { |
| 231 |
let mut h = TestHarness::new().await; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
let resp = h |
| 238 |
.client |
| 239 |
.request_with_headers( |
| 240 |
"GET", |
| 241 |
"/forgot-password", |
| 242 |
None, |
| 243 |
&[("Sec-Fetch-Site", "same-origin")], |
| 244 |
) |
| 245 |
.await; |
| 246 |
assert!( |
| 247 |
resp.status.is_success(), |
| 248 |
"origin gate or routing blocked a same-origin safe request (got {})", |
| 249 |
resp.status |
| 250 |
); |
| 251 |
} |
| 252 |
|