| 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 |
#[tokio::test] |
| 238 |
async fn origin_gate_allows_same_origin_signal() { |
| 239 |
let mut h = TestHarness::new().await; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
let resp = h |
| 246 |
.client |
| 247 |
.request_with_headers( |
| 248 |
"GET", |
| 249 |
"/forgot-password", |
| 250 |
None, |
| 251 |
&[("Sec-Fetch-Site", "same-origin")], |
| 252 |
) |
| 253 |
.await; |
| 254 |
assert_eq!( |
| 255 |
resp.status, 200, |
| 256 |
"origin gate or routing blocked a same-origin safe request (got {})", |
| 257 |
resp.status |
| 258 |
); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
#[tokio::test] |
| 271 |
async fn described_screens_register_inside_the_csrf_envelope() { |
| 272 |
|
| 273 |
let _h = TestHarness::new().await; |
| 274 |
|
| 275 |
let manifest = route_manifest(); |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
for entry in &manifest { |
| 281 |
if entry.path.starts_with("/library/tabs/") || entry.path.starts_with("/dashboard/tabs/") { |
| 282 |
assert_eq!( |
| 283 |
entry.posture, |
| 284 |
ManifestPosture::Auto, |
| 285 |
"a described mount declared a posture other than Auto: {} ({:?})", |
| 286 |
entry.path, |
| 287 |
entry.reason |
| 288 |
); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
let lib = include_str!("../../src/lib.rs"); |
| 298 |
let mounts = lib |
| 299 |
.split_once("quasi::mounts(state)") |
| 300 |
.expect("build_app mounts the description layer") |
| 301 |
.1; |
| 302 |
let (mounted, _) = mounts |
| 303 |
.split_once(".finalize()") |
| 304 |
.expect("the CSRF tree is finalized after the mounts"); |
| 305 |
assert!( |
| 306 |
mounted.contains("routes.nest_service"), |
| 307 |
"the described mounts must go through CsrfRouter::nest_service and land \ |
| 308 |
before finalize, not on the plain router afterwards: see the CSRF \ |
| 309 |
envelope note in build_app" |
| 310 |
); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
#[tokio::test] |
| 321 |
async fn a_described_write_rejects_an_authenticated_tokenless_mutation() { |
| 322 |
let mut h = TestHarness::build(crate::harness::BuildOptions { |
| 323 |
..Default::default() |
| 324 |
}) |
| 325 |
.await; |
| 326 |
h.signup("creator", "creator@example.com", "password123") |
| 327 |
.await; |
| 328 |
h.login("creator", "password123").await; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
let resp = h |
| 334 |
.client |
| 335 |
.request_with_headers( |
| 336 |
"DELETE", |
| 337 |
"/library/tabs/contacts/revoke/00000000-0000-0000-0000-000000000000", |
| 338 |
None, |
| 339 |
&[], |
| 340 |
) |
| 341 |
.await; |
| 342 |
|
| 343 |
assert_eq!( |
| 344 |
resp.status, 403, |
| 345 |
"a described write accepted a tokenless mutation (got {})", |
| 346 |
resp.status |
| 347 |
); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
#[tokio::test] |
| 369 |
async fn a_described_write_accepts_the_token_the_page_around_it_carries() { |
| 370 |
let mut h = TestHarness::build(crate::harness::BuildOptions { |
| 371 |
..Default::default() |
| 372 |
}) |
| 373 |
.await; |
| 374 |
h.signup("creator", "creator@example.com", "password123") |
| 375 |
.await; |
| 376 |
h.login("creator", "password123").await; |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
let page = h.client.get("/library").await; |
| 381 |
assert_eq!(page.status, 200, "the library page did not render"); |
| 382 |
let token = meta_csrf_token(&page.text) |
| 383 |
.expect("the page around a described screen carries no csrf-token meta"); |
| 384 |
|
| 385 |
let resp = h |
| 386 |
.client |
| 387 |
.request_with_headers( |
| 388 |
"DELETE", |
| 389 |
"/library/tabs/contacts/revoke/00000000-0000-0000-0000-000000000000", |
| 390 |
None, |
| 391 |
&[("X-CSRF-Token", token.as_str()), ("HX-Request", "true")], |
| 392 |
) |
| 393 |
.await; |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
assert_ne!( |
| 398 |
resp.status, 403, |
| 399 |
"a described write refused the token its own document supplied" |
| 400 |
); |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
fn meta_csrf_token(html: &str) -> Option<String> { |
| 405 |
let at = html.split("name=\"csrf-token\"").nth(1)?; |
| 406 |
let at = at.split("content=\"").nth(1)?; |
| 407 |
Some(at.split('"').next()?.to_owned()) |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
#[test] |
| 423 |
fn a_described_screen_never_builds_its_own_shell() { |
| 424 |
for (name, source) in [ |
| 425 |
("ssh_keys", include_str!("../../src/quasi/ssh_keys.rs")), |
| 426 |
( |
| 427 |
"library_contacts", |
| 428 |
include_str!("../../src/quasi/library_contacts.rs"), |
| 429 |
), |
| 430 |
( |
| 431 |
"buyer_contacts", |
| 432 |
include_str!("../../src/quasi/buyer_contacts.rs"), |
| 433 |
), |
| 434 |
( |
| 435 |
"user_analytics", |
| 436 |
include_str!("../../src/quasi/user_analytics.rs"), |
| 437 |
), |
| 438 |
( |
| 439 |
"forum_memberships", |
| 440 |
include_str!("../../src/quasi/forum_memberships.rs"), |
| 441 |
), |
| 442 |
] { |
| 443 |
assert!( |
| 444 |
!source.contains("Shell::under"), |
| 445 |
"{name} builds its own shell: use `viewer.shell()`, which carries \ |
| 446 |
the session token every write on a described document needs" |
| 447 |
); |
| 448 |
} |
| 449 |
} |
| 450 |
|