| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
const APP_UPSTREAMS: &[&str] = &[":3000", ":3400"]; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
const CADDYFILE: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/deploy/Caddyfile")); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
fn top_level_blocks(src: &str) -> Vec<(String, String)> { |
| 40 |
let cleaned: String = src |
| 41 |
.lines() |
| 42 |
.map(|l| match l.find('#') { |
| 43 |
Some(i) => &l[..i], |
| 44 |
None => l, |
| 45 |
}) |
| 46 |
.collect::<Vec<_>>() |
| 47 |
.join("\n"); |
| 48 |
|
| 49 |
let chars: Vec<char> = cleaned.chars().collect(); |
| 50 |
let mut blocks = Vec::new(); |
| 51 |
let mut depth: i32 = 0; |
| 52 |
let mut seg_start = 0usize; |
| 53 |
let mut body_start = 0usize; |
| 54 |
let mut header = String::new(); |
| 55 |
|
| 56 |
for (i, &c) in chars.iter().enumerate() { |
| 57 |
match c { |
| 58 |
'{' => { |
| 59 |
if depth == 0 { |
| 60 |
header = chars[seg_start..i] |
| 61 |
.iter() |
| 62 |
.collect::<String>() |
| 63 |
.trim() |
| 64 |
.to_string(); |
| 65 |
body_start = i + 1; |
| 66 |
} |
| 67 |
depth += 1; |
| 68 |
} |
| 69 |
'}' => { |
| 70 |
depth -= 1; |
| 71 |
if depth == 0 { |
| 72 |
let body: String = chars[body_start..i].iter().collect(); |
| 73 |
blocks.push((header.clone(), body)); |
| 74 |
seg_start = i + 1; |
| 75 |
} |
| 76 |
} |
| 77 |
_ => {} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
blocks |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
fn proxies_app(body: &str) -> bool { |
| 89 |
body.lines().any(|line| { |
| 90 |
line.contains("reverse_proxy") && APP_UPSTREAMS.iter().any(|up| line.contains(up)) |
| 91 |
}) |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
fn has_safe_ip_posture(body: &str) -> bool { |
| 96 |
let imports_mtls = body.contains("import cloudflare_tls"); |
| 97 |
let sets_cf_ip = body.lines().any(|line| { |
| 98 |
let toks: Vec<&str> = line.split_whitespace().collect(); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
toks.len() >= 3 |
| 103 |
&& toks[0] == "header_up" |
| 104 |
&& toks[1].eq_ignore_ascii_case("CF-Connecting-IP") |
| 105 |
}); |
| 106 |
imports_mtls || sets_cf_ip |
| 107 |
} |
| 108 |
|
| 109 |
#[cfg(test)] |
| 110 |
mod tests { |
| 111 |
use super::*; |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn every_app_proxy_block_declares_safe_ip_posture() { |
| 115 |
let blocks = top_level_blocks(CADDYFILE); |
| 116 |
let mut proxying = 0usize; |
| 117 |
|
| 118 |
for (header, body) in &blocks { |
| 119 |
if !proxies_app(body) { |
| 120 |
continue; |
| 121 |
} |
| 122 |
proxying += 1; |
| 123 |
assert!( |
| 124 |
has_safe_ip_posture(body), |
| 125 |
"Caddy block `{}` reverse-proxies an app ({APP_UPSTREAMS:?}) but neither \ |
| 126 |
`import cloudflare_tls` (mTLS) nor sets `CF-Connecting-IP` via `header_up`. \ |
| 127 |
It would trust a client-forged source IP, defeating rate limits, lockouts, \ |
| 128 |
and audit-log IP attribution. Add one of the two postures (see \ |
| 129 |
src/deploy_lint.rs).", |
| 130 |
if header.is_empty() { |
| 131 |
"<catch-all>" |
| 132 |
} else { |
| 133 |
header |
| 134 |
} |
| 135 |
); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
assert!( |
| 143 |
proxying >= APP_UPSTREAMS.len(), |
| 144 |
"deploy lint found {proxying} Caddy blocks proxying {APP_UPSTREAMS:?}, expected at \ |
| 145 |
least {}; the parser or the Caddyfile layout changed, fix the lint, do not delete it.", |
| 146 |
APP_UPSTREAMS.len() |
| 147 |
); |
| 148 |
} |
| 149 |
|
| 150 |
#[test] |
| 151 |
fn parser_splits_blocks_and_skips_snippets_and_globals() { |
| 152 |
let src = "{\n\tglobal\n}\n\n(snippet) {\n\timport nothing\n}\n\nexample.com {\n\treverse_proxy localhost:3000\n}\n"; |
| 153 |
let blocks = top_level_blocks(src); |
| 154 |
let headers: Vec<&str> = blocks.iter().map(|(h, _)| h.as_str()).collect(); |
| 155 |
assert!( |
| 156 |
headers.contains(&""), |
| 157 |
"global options block (empty header) parsed" |
| 158 |
); |
| 159 |
assert!(headers.contains(&"(snippet)")); |
| 160 |
assert!(headers.contains(&"example.com")); |
| 161 |
} |
| 162 |
|
| 163 |
#[test] |
| 164 |
fn proxies_app_ignores_non_proxy_mentions() { |
| 165 |
|
| 166 |
|
| 167 |
assert!(!proxies_app( |
| 168 |
"on_demand_tls {\n\task http://localhost:3000/api/domains/caddy-ask\n}\n" |
| 169 |
)); |
| 170 |
assert!(proxies_app("reverse_proxy localhost:3000\n")); |
| 171 |
assert!(proxies_app( |
| 172 |
"reverse_proxy localhost:3000 {\n\theader_up X 1\n}\n" |
| 173 |
)); |
| 174 |
|
| 175 |
assert!(proxies_app("reverse_proxy 127.0.0.1:3000\n")); |
| 176 |
|
| 177 |
|
| 178 |
assert!(proxies_app("reverse_proxy localhost:3400\n")); |
| 179 |
|
| 180 |
assert!(!proxies_app( |
| 181 |
"reverse_proxy https://fsn1.your-objectstorage.com\n" |
| 182 |
)); |
| 183 |
} |
| 184 |
|
| 185 |
#[test] |
| 186 |
fn posture_accepts_mtls_import() { |
| 187 |
assert!(has_safe_ip_posture( |
| 188 |
"import cloudflare_tls\nreverse_proxy localhost:3000\n" |
| 189 |
)); |
| 190 |
} |
| 191 |
|
| 192 |
#[test] |
| 193 |
fn posture_accepts_header_up_set() { |
| 194 |
assert!(has_safe_ip_posture( |
| 195 |
"reverse_proxy localhost:3000 {\n\theader_up CF-Connecting-IP {http.request.remote.host}\n}\n" |
| 196 |
)); |
| 197 |
} |
| 198 |
|
| 199 |
#[test] |
| 200 |
fn posture_rejects_bare_proxy() { |
| 201 |
assert!(!has_safe_ip_posture("reverse_proxy localhost:3000\n")); |
| 202 |
} |
| 203 |
|
| 204 |
#[test] |
| 205 |
fn posture_rejects_delete_only_header() { |
| 206 |
|
| 207 |
|
| 208 |
assert!(!has_safe_ip_posture( |
| 209 |
"reverse_proxy localhost:3000 {\n\theader_up -CF-Connecting-IP\n}\n" |
| 210 |
)); |
| 211 |
} |
| 212 |
} |
| 213 |
|