| 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 |
|
| 93 |
|
| 94 |
|
| 95 |
if makenotwork::quasi::PATHS.contains(&entry.path.as_str()) { |
| 96 |
continue; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
let mut outcome: Option<(&str, u16)> = None; |
| 103 |
for method in ["POST", "PUT", "PATCH", "DELETE"] { |
| 104 |
next_ip(&mut h); |
| 105 |
let resp = h |
| 106 |
.client |
| 107 |
.request_with_headers(method, &entry.path, None, &[]) |
| 108 |
.await; |
| 109 |
let code = resp.status.as_u16(); |
| 110 |
if code == 403 { |
| 111 |
outcome = Some((method, code)); |
| 112 |
break; |
| 113 |
} |
| 114 |
if code != 405 { |
| 115 |
|
| 116 |
|
| 117 |
outcome = Some((method, code)); |
| 118 |
break; |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
match outcome { |
| 123 |
Some((_, 403)) => {} |
| 124 |
Some((method, code)) => failures.push(format!("{} [{method}] -> {code}", entry.path)), |
| 125 |
None => failures.push(format!( |
| 126 |
"{} -> all methods 405 (no mutating method?)", |
| 127 |
entry.path |
| 128 |
)), |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
assert!( |
| 133 |
failures.is_empty(), |
| 134 |
"{} Auto route(s) did NOT reject a tokenless authenticated request (CSRF \ |
| 135 |
layer missing/bypassed). If a route is legitimately refused earlier by \ |
| 136 |
an outer layer, add it to REJECTED_BEFORE_CSRF_LAYER with a reason:\n{}", |
| 137 |
failures.len(), |
| 138 |
failures.join("\n") |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
#[tokio::test] |
| 145 |
async fn csrf_manifest_is_populated_and_optouts_are_justified() { |
| 146 |
let _h = TestHarness::new().await; |
| 147 |
let manifest = route_manifest(); |
| 148 |
|
| 149 |
let count = |p: ManifestPosture| manifest.iter().filter(|e| e.posture == p).count(); |
| 150 |
let (auto, manual, skip) = ( |
| 151 |
count(ManifestPosture::Auto), |
| 152 |
count(ManifestPosture::Manual), |
| 153 |
count(ManifestPosture::Skip), |
| 154 |
); |
| 155 |
eprintln!( |
| 156 |
"CSRF manifest: {auto} auto, {manual} manual, {skip} skip, {} total", |
| 157 |
manifest.len() |
| 158 |
); |
| 159 |
|
| 160 |
assert!( |
| 161 |
manifest.len() > 100, |
| 162 |
"manifest too small: {}", |
| 163 |
manifest.len() |
| 164 |
); |
| 165 |
assert!(auto > 50, "expected many Auto routes, got {auto}"); |
| 166 |
|
| 167 |
for e in &manifest { |
| 168 |
if matches!(e.posture, ManifestPosture::Manual | ManifestPosture::Skip) { |
| 169 |
assert!( |
| 170 |
e.reason.is_some_and(|r| !r.trim().is_empty()), |
| 171 |
"{:?} route {} has no documented CSRF justification", |
| 172 |
e.posture, |
| 173 |
e.path |
| 174 |
); |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
#[tokio::test] |
| 186 |
async fn forgot_password_rejects_preauth_tokenless_post() { |
| 187 |
let mut h = TestHarness::new().await; |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
let resp = h |
| 193 |
.client |
| 194 |
.request_with_headers( |
| 195 |
"POST", |
| 196 |
"/forgot-password", |
| 197 |
Some("email=nobody@example.com"), |
| 198 |
&[("Content-Type", "application/x-www-form-urlencoded")], |
| 199 |
) |
| 200 |
.await; |
| 201 |
|
| 202 |
assert_eq!( |
| 203 |
resp.status, 403, |
| 204 |
"CHRONIC A' regressed: /forgot-password accepted a pre-auth tokenless \ |
| 205 |
POST (got {}). The validate_auto !has_user skip must stay removed.", |
| 206 |
resp.status |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
#[tokio::test] |
| 213 |
async fn origin_gate_rejects_cross_site_mutation() { |
| 214 |
let mut h = TestHarness::new().await; |
| 215 |
let resp = h |
| 216 |
.client |
| 217 |
.request_with_headers( |
| 218 |
"POST", |
| 219 |
"/forgot-password", |
| 220 |
Some("email=nobody@example.com"), |
| 221 |
&[ |
| 222 |
("Content-Type", "application/x-www-form-urlencoded"), |
| 223 |
("Sec-Fetch-Site", "cross-site"), |
| 224 |
], |
| 225 |
) |
| 226 |
.await; |
| 227 |
assert_eq!( |
| 228 |
resp.status, 403, |
| 229 |
"origin gate let a Sec-Fetch-Site: cross-site mutation through (got {})", |
| 230 |
resp.status |
| 231 |
); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
#[tokio::test] |
| 240 |
async fn origin_gate_allows_same_origin_signal() { |
| 241 |
let mut h = TestHarness::new().await; |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
let resp = h |
| 248 |
.client |
| 249 |
.request_with_headers( |
| 250 |
"GET", |
| 251 |
"/forgot-password", |
| 252 |
None, |
| 253 |
&[("Sec-Fetch-Site", "same-origin")], |
| 254 |
) |
| 255 |
.await; |
| 256 |
assert_eq!( |
| 257 |
resp.status, 200, |
| 258 |
"origin gate or routing blocked a same-origin safe request (got {})", |
| 259 |
resp.status |
| 260 |
); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
#[tokio::test] |
| 273 |
async fn described_screens_register_inside_the_csrf_envelope() { |
| 274 |
|
| 275 |
let _h = TestHarness::new().await; |
| 276 |
|
| 277 |
let manifest = route_manifest(); |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
for entry in &manifest { |
| 283 |
if entry.path.starts_with("/library/tabs/") || entry.path.starts_with("/dashboard/tabs/") { |
| 284 |
assert_eq!( |
| 285 |
entry.posture, |
| 286 |
ManifestPosture::Auto, |
| 287 |
"a described mount declared a posture other than Auto: {} ({:?})", |
| 288 |
entry.path, |
| 289 |
entry.reason |
| 290 |
); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
let lib = include_str!("../../src/lib.rs"); |
| 300 |
let mounts = lib |
| 301 |
.split_once("quasi::mounts(state)") |
| 302 |
.expect("build_app mounts the description layer") |
| 303 |
.1; |
| 304 |
let (mounted, _) = mounts |
| 305 |
.split_once(".finalize()") |
| 306 |
.expect("the CSRF tree is finalized after the mounts"); |
| 307 |
assert!( |
| 308 |
mounted.contains("routes.nest_service"), |
| 309 |
"the described mounts must go through CsrfRouter::nest_service and land \ |
| 310 |
before finalize, not on the plain router afterwards: see the CSRF \ |
| 311 |
envelope note in build_app" |
| 312 |
); |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
#[tokio::test] |
| 323 |
async fn a_described_write_rejects_an_authenticated_tokenless_mutation() { |
| 324 |
let mut h = TestHarness::build(crate::harness::BuildOptions { |
| 325 |
quasi_screens: makenotwork::config::QuasiScreens::parse("library_contacts"), |
| 326 |
..Default::default() |
| 327 |
}) |
| 328 |
.await; |
| 329 |
h.signup("creator", "creator@example.com", "password123") |
| 330 |
.await; |
| 331 |
h.login("creator", "password123").await; |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
let resp = h |
| 337 |
.client |
| 338 |
.request_with_headers( |
| 339 |
"DELETE", |
| 340 |
"/library/tabs/contacts/revoke/00000000-0000-0000-0000-000000000000", |
| 341 |
None, |
| 342 |
&[], |
| 343 |
) |
| 344 |
.await; |
| 345 |
|
| 346 |
assert_eq!( |
| 347 |
resp.status, 403, |
| 348 |
"a described write accepted a tokenless mutation (got {})", |
| 349 |
resp.status |
| 350 |
); |
| 351 |
} |
| 352 |
|