| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
use axum::http::HeaderValue; |
| 5 |
use axum::http::StatusCode; |
| 6 |
use axum::http::header::HeaderMap; |
| 7 |
use axum::response::{IntoResponse, Response}; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
pub static HTTP_CLIENT: std::sync::LazyLock<reqwest::Client> = std::sync::LazyLock::new(|| { |
| 16 |
reqwest::Client::builder() |
| 17 |
.timeout(std::time::Duration::from_secs(10)) |
| 18 |
.build() |
| 19 |
.expect("build shared reqwest client") |
| 20 |
}); |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
pub fn extract_client_ip(headers: &HeaderMap) -> Option<String> { |
| 40 |
let ip = headers |
| 41 |
.get("cf-connecting-ip") |
| 42 |
.and_then(|v| v.to_str().ok()) |
| 43 |
.and_then(|s| s.split(',').next()) |
| 44 |
.map(|s| s.trim().to_string()) |
| 45 |
.filter(|s| !s.is_empty()); |
| 46 |
if ip.is_none() { |
| 47 |
static MISSING_COUNT: std::sync::atomic::AtomicUsize = |
| 48 |
std::sync::atomic::AtomicUsize::new(0); |
| 49 |
static WARNED: std::sync::OnceLock<()> = std::sync::OnceLock::new(); |
| 50 |
let n = MISSING_COUNT.fetch_add(1, std::sync::atomic::Ordering::Relaxed) + 1; |
| 51 |
if n >= 100 { |
| 52 |
WARNED.get_or_init(|| { |
| 53 |
tracing::warn!( |
| 54 |
missing_count = n, |
| 55 |
"cf-connecting-ip header missing on 100+ requests — rate-limits and \ |
| 56 |
sandbox caps will key on None. Verify Cloudflare proxy is in front \ |
| 57 |
of the origin (dev/test environments hit this naturally and can ignore)." |
| 58 |
); |
| 59 |
}); |
| 60 |
} |
| 61 |
} |
| 62 |
ip |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
pub fn ip_advisory_lock_key(ip: &str) -> i64 { |
| 73 |
use sha2::{Digest, Sha256}; |
| 74 |
let mut h = Sha256::new(); |
| 75 |
h.update(b"sandbox_ip_cap\0"); |
| 76 |
h.update(ip.as_bytes()); |
| 77 |
let digest = h.finalize(); |
| 78 |
|
| 79 |
|
| 80 |
i64::from_be_bytes(digest[..8].try_into().expect("sha256 yields >= 8 bytes")) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
pub fn is_htmx_request(headers: &HeaderMap) -> bool { |
| 85 |
headers.get("HX-Request").is_some() |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
pub fn check_etag(headers: &HeaderMap, generation: i64) -> Option<Response> { |
| 91 |
let etag = format!("\"g{generation}\""); |
| 92 |
if let Some(if_none_match) = headers.get(axum::http::header::IF_NONE_MATCH) |
| 93 |
&& if_none_match.as_bytes() == etag.as_bytes() |
| 94 |
{ |
| 95 |
return Some( |
| 96 |
( |
| 97 |
StatusCode::NOT_MODIFIED, |
| 98 |
[( |
| 99 |
axum::http::header::ETAG, |
| 100 |
HeaderValue::try_from(&etag) |
| 101 |
.unwrap_or_else(|_| HeaderValue::from_static("invalid")), |
| 102 |
)], |
| 103 |
) |
| 104 |
.into_response(), |
| 105 |
); |
| 106 |
} |
| 107 |
None |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
pub fn with_etag(generation: i64, body: impl IntoResponse) -> Response { |
| 113 |
let etag = format!("\"g{generation}\""); |
| 114 |
( |
| 115 |
[ |
| 116 |
(axum::http::header::ETAG, etag), |
| 117 |
( |
| 118 |
axum::http::header::CACHE_CONTROL, |
| 119 |
"private, no-cache".to_string(), |
| 120 |
), |
| 121 |
], |
| 122 |
body, |
| 123 |
) |
| 124 |
.into_response() |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
pub fn htmx_toast_response( |
| 131 |
message: &str, |
| 132 |
toast_type: &str, |
| 133 |
) -> ( |
| 134 |
[(&'static str, HeaderValue); 1], |
| 135 |
axum::response::Html<String>, |
| 136 |
) { |
| 137 |
( |
| 138 |
[("HX-Trigger", hx_toast(message, toast_type))], |
| 139 |
axum::response::Html(String::new()), |
| 140 |
) |
| 141 |
} |
| 142 |
|
| 143 |
pub fn hx_toast(message: &str, toast_type: &str) -> HeaderValue { |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
let message: String = message.chars().filter(|c| !c.is_control()).collect(); |
| 150 |
let toast_type: String = toast_type.chars().filter(|c| !c.is_control()).collect(); |
| 151 |
let json = serde_json::json!({ |
| 152 |
"showToast": { |
| 153 |
"message": message, |
| 154 |
"type": toast_type |
| 155 |
} |
| 156 |
}) |
| 157 |
.to_string(); |
| 158 |
HeaderValue::from_str(&json).unwrap_or_else(|e| { |
| 159 |
tracing::warn!(error = %e, "hx_toast produced invalid header value"); |
| 160 |
HeaderValue::from_static("") |
| 161 |
}) |
| 162 |
} |
| 163 |
|
| 164 |
#[cfg(test)] |
| 165 |
mod tests { |
| 166 |
use super::*; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
#[test] |
| 171 |
fn htmx_request_detected() { |
| 172 |
let mut headers = HeaderMap::new(); |
| 173 |
headers.insert("HX-Request", HeaderValue::from_static("true")); |
| 174 |
assert!(is_htmx_request(&headers)); |
| 175 |
} |
| 176 |
|
| 177 |
#[test] |
| 178 |
fn non_htmx_request() { |
| 179 |
let headers = HeaderMap::new(); |
| 180 |
assert!(!is_htmx_request(&headers)); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
#[test] |
| 186 |
fn hx_toast_produces_valid_json() { |
| 187 |
let val = hx_toast("Item deleted", "success"); |
| 188 |
let s = val.to_str().unwrap(); |
| 189 |
assert!(s.contains("showToast")); |
| 190 |
assert!(s.contains("Item deleted")); |
| 191 |
let parsed: serde_json::Value = serde_json::from_str(s).unwrap(); |
| 192 |
assert_eq!(parsed["showToast"]["message"], "Item deleted"); |
| 193 |
assert_eq!(parsed["showToast"]["type"], "success"); |
| 194 |
} |
| 195 |
|
| 196 |
#[test] |
| 197 |
fn hx_toast_error_type() { |
| 198 |
let val = hx_toast("Something failed", "error"); |
| 199 |
let s = val.to_str().unwrap(); |
| 200 |
let parsed: serde_json::Value = serde_json::from_str(s).unwrap(); |
| 201 |
assert_eq!(parsed["showToast"]["type"], "error"); |
| 202 |
} |
| 203 |
|
| 204 |
#[test] |
| 205 |
fn hx_toast_with_quotes() { |
| 206 |
let val = hx_toast("Say \"hello\"", "info"); |
| 207 |
let s = val.to_str().unwrap(); |
| 208 |
let parsed: serde_json::Value = serde_json::from_str(s).unwrap(); |
| 209 |
assert_eq!(parsed["showToast"]["message"], "Say \"hello\""); |
| 210 |
} |
| 211 |
|
| 212 |
#[test] |
| 213 |
fn adversarial_hx_toast_json_injection() { |
| 214 |
let val = hx_toast("\"},{\"malicious\":\"true", "error"); |
| 215 |
let s = val.to_str().unwrap(); |
| 216 |
let parsed: serde_json::Value = serde_json::from_str(s).unwrap(); |
| 217 |
assert_eq!(parsed["showToast"]["message"], "\"},{\"malicious\":\"true"); |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
#[test] |
| 223 |
fn extract_client_ip_cf_preferred() { |
| 224 |
let mut headers = HeaderMap::new(); |
| 225 |
headers.insert("cf-connecting-ip", HeaderValue::from_static("1.2.3.4")); |
| 226 |
headers.insert("x-forwarded-for", HeaderValue::from_static("5.6.7.8")); |
| 227 |
assert_eq!(extract_client_ip(&headers).as_deref(), Some("1.2.3.4")); |
| 228 |
} |
| 229 |
|
| 230 |
#[test] |
| 231 |
fn extract_client_ip_ignores_xff_when_cf_missing() { |
| 232 |
|
| 233 |
let mut headers = HeaderMap::new(); |
| 234 |
headers.insert( |
| 235 |
"x-forwarded-for", |
| 236 |
HeaderValue::from_static("5.6.7.8, 9.10.11.12"), |
| 237 |
); |
| 238 |
assert_eq!(extract_client_ip(&headers), None); |
| 239 |
} |
| 240 |
|
| 241 |
#[test] |
| 242 |
fn extract_client_ip_ignores_xff_even_when_cf_present() { |
| 243 |
|
| 244 |
let mut headers = HeaderMap::new(); |
| 245 |
headers.insert("cf-connecting-ip", HeaderValue::from_static("1.2.3.4")); |
| 246 |
headers.insert("x-forwarded-for", HeaderValue::from_static("5.6.7.8")); |
| 247 |
assert_eq!(extract_client_ip(&headers).as_deref(), Some("1.2.3.4")); |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn extract_client_ip_missing() { |
| 252 |
let headers = HeaderMap::new(); |
| 253 |
assert_eq!(extract_client_ip(&headers), None); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
#[test] |
| 259 |
fn ip_advisory_lock_key_deterministic() { |
| 260 |
assert_eq!( |
| 261 |
ip_advisory_lock_key("1.2.3.4"), |
| 262 |
ip_advisory_lock_key("1.2.3.4") |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
#[test] |
| 267 |
fn ip_advisory_lock_key_different_ips() { |
| 268 |
assert_ne!( |
| 269 |
ip_advisory_lock_key("1.2.3.4"), |
| 270 |
ip_advisory_lock_key("5.6.7.8") |
| 271 |
); |
| 272 |
} |
| 273 |
} |
| 274 |
|