| a7aa76ac Andy McCall |
1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#[test] |
| 8 |
fn templates_carry_no_inline_scripts_or_handlers() { |
| 9 |
let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../templates"); |
| 10 |
let handler = |s: &str| { |
| 11 |
s.match_indices(" on").any(|(i, _)| { |
| 12 |
let rest = &s[i + 3..]; |
| 13 |
let name: String = rest.chars().take_while(|c| c.is_ascii_lowercase()).collect(); |
| 14 |
!name.is_empty() && rest[name.len()..].starts_with("=\"") |
| 15 |
}) |
| 16 |
}; |
| 17 |
let mut found = Vec::new(); |
| 18 |
for e in std::fs::read_dir(&dir).unwrap() { |
| 19 |
let path = e.unwrap().path(); |
| 20 |
if path.extension().and_then(|x| x.to_str()) != Some("html") { |
| 21 |
continue; |
| 22 |
} |
| 23 |
let text = std::fs::read_to_string(&path).unwrap(); |
| 24 |
for (n, line) in text.lines().enumerate() { |
| 25 |
let inline_script = line.match_indices("<script").any(|(i, _)| { |
| 26 |
let tag = &line[i..line[i..].find('>').map_or(line.len(), |e| i + e)]; |
| 27 |
!tag.contains("src=") && !tag.contains("application/json") |
| 28 |
}); |
| 29 |
if inline_script || handler(line) { |
| 30 |
found.push(format!("{}:{}: {}", path.file_name().unwrap().to_string_lossy(), n + 1, line.trim())); |
| 31 |
} |
| 32 |
} |
| 33 |
} |
| 34 |
assert!(found.is_empty(), "inline code the CSP would block:\n{}", found.join("\n")); |
| 35 |
} |