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