| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | use std::{ |
| 28 | fs, |
| 29 | path::{Path, PathBuf}, |
| 30 | }; |
| 31 | |
| 32 | use super::csrf::EXEMPT; |
| 33 | |
| 34 | |
| 35 | fn web_dir() -> PathBuf { |
| 36 | PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("src/infrastructure/web") |
| 37 | } |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | fn rust_files_under(dir: &Path, out: &mut Vec<PathBuf>) { |
| 45 | for entry in fs::read_dir(dir).expect("the directory is readable") { |
| 46 | let path = entry.expect("a directory entry").path(); |
| 47 | if path.is_dir() { |
| 48 | rust_files_under(&path, out); |
| 49 | } else if path.extension().is_some_and(|ext| ext == "rs") { |
| 50 | out.push(path); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | struct Handler { |
| 57 | file: String, |
| 58 | route: String, |
| 59 | body: String, |
| 60 | } |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | fn handlers() -> Vec<Handler> { |
| 68 | let mut found = Vec::new(); |
| 69 | |
| 70 | let root = web_dir(); |
| 71 | let mut files = Vec::new(); |
| 72 | rust_files_under(&root, &mut files); |
| 73 | files.sort(); |
| 74 | |
| 75 | for path in files { |
| 76 | |
| 77 | |
| 78 | let file = path |
| 79 | .strip_prefix(&root) |
| 80 | .expect("under the web directory") |
| 81 | .to_string_lossy() |
| 82 | .into_owned(); |
| 83 | let source = fs::read_to_string(&path).expect("the file is readable"); |
| 84 | let lines: Vec<&str> = source.lines().collect(); |
| 85 | |
| 86 | for (start, line) in lines.iter().enumerate() { |
| 87 | let Some(route) = post_route(line) else { |
| 88 | continue; |
| 89 | }; |
| 90 | |
| 91 | let end = lines[start..] |
| 92 | .iter() |
| 93 | .position(|line| *line == "}") |
| 94 | .map_or(lines.len(), |offset| start + offset); |
| 95 | |
| 96 | found.push(Handler { |
| 97 | file: file.clone(), |
| 98 | route, |
| 99 | body: lines[start..=end.min(lines.len() - 1)].join("\n"), |
| 100 | }); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | found |
| 105 | } |
| 106 | |
| 107 | |
| 108 | fn post_route(line: &str) -> Option<String> { |
| 109 | let rest = line |
| 110 | .strip_prefix("#[page(POST ") |
| 111 | .or_else(|| line.strip_prefix("#[route(POST "))?; |
| 112 | |
| 113 | Some(rest.trim_start_matches('"').split('"').next()?.to_owned()) |
| 114 | } |
| 115 | |
| 116 | |
| 117 | fn exempt(route: &str) -> bool { |
| 118 | EXEMPT.iter().any(|(exempt, _)| *exempt == route) |
| 119 | } |
| 120 | |
| 121 | #[test] |
| 122 | fn every_post_route_is_scanned() { |
| 123 | |
| 124 | |
| 125 | |
| 126 | let handlers = handlers(); |
| 127 | |
| 128 | assert!( |
| 129 | handlers.len() >= 30, |
| 130 | "found only {} POST routes, which means the scanner stopped matching", |
| 131 | handlers.len() |
| 132 | ); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn every_post_handler_checks_the_csrf_token() { |
| 137 | for handler in handlers() { |
| 138 | if exempt(&handler.route) { |
| 139 | assert!( |
| 140 | !handler.body.contains("csrf_verified"), |
| 141 | "{} answers {} and is on the exemption list, yet checks the token — \ |
| 142 | remove it from `csrf::EXEMPT`", |
| 143 | handler.file, |
| 144 | handler.route, |
| 145 | ); |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | assert!( |
| 150 | handler.body.contains("csrf_verified"), |
| 151 | "{} answers POST {} without calling `csrf_verified` — add the check, and \ |
| 152 | `csrf_field()` to the form it serves. If it genuinely cannot carry a \ |
| 153 | token, add it to `csrf::EXEMPT` with the reason.", |
| 154 | handler.file, |
| 155 | handler.route, |
| 156 | ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | #[test] |
| 161 | fn no_post_handler_redirects_with_a_307() { |
| 162 | for handler in handlers() { |
| 163 | assert!( |
| 164 | !handler.body.contains("redirect("), |
| 165 | "{} answers POST {} with `redirect(`, which is a 307 — the browser \ |
| 166 | re-POSTs the form to the target. Use `see_other` in a `#[route]`, or \ |
| 167 | `StatusCode::SEE_OTHER` with `context::location` inside `view!`.", |
| 168 | handler.file, |
| 169 | handler.route, |
| 170 | ); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | #[test] |
| 175 | fn every_exempt_route_exists() { |
| 176 | |
| 177 | |
| 178 | |
| 179 | let routes: Vec<String> = handlers().into_iter().map(|found| found.route).collect(); |
| 180 | |
| 181 | for (route, _) in EXEMPT { |
| 182 | assert!( |
| 183 | routes.iter().any(|found| found == route), |
| 184 | "`csrf::EXEMPT` names POST {route}, which no handler answers" |
| 185 | ); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | #[test] |
| 190 | fn the_scan_descends_into_subdirectories() { |
| 191 | |
| 192 | let dir = tempfile::tempdir().expect("a temp dir"); |
| 193 | let nested = dir.path().join("pulls"); |
| 194 | fs::create_dir(&nested).expect("mkdir"); |
| 195 | fs::write(nested.join("writes.rs"), "// a file\n").expect("write"); |
| 196 | fs::write(dir.path().join("top.rs"), "// a file\n").expect("write"); |
| 197 | fs::write(dir.path().join("notes.md"), "not rust\n").expect("write"); |
| 198 | |
| 199 | let mut files = Vec::new(); |
| 200 | rust_files_under(dir.path(), &mut files); |
| 201 | files.sort(); |
| 202 | |
| 203 | let names: Vec<String> = files |
| 204 | .iter() |
| 205 | .map(|p| { |
| 206 | p.strip_prefix(dir.path()) |
| 207 | .unwrap() |
| 208 | .to_string_lossy() |
| 209 | .into_owned() |
| 210 | }) |
| 211 | .collect(); |
| 212 | assert_eq!( |
| 213 | names, |
| 214 | vec!["pulls/writes.rs".to_owned(), "top.rs".to_owned()] |
| 215 | ); |
| 216 | } |