| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
use axum::{body::Bytes, http::StatusCode}; |
| 18 |
use serde_json::Value; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
const MAX_FIELD_LEN: usize = 512; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
const MAX_REPORTS_PER_BODY: usize = 8; |
| 28 |
|
| 29 |
pub(super) async fn report_csp_violation(body: Bytes) -> StatusCode { |
| 30 |
let Ok(json) = serde_json::from_slice::<Value>(&body) else { |
| 31 |
return StatusCode::NO_CONTENT; |
| 32 |
}; |
| 33 |
|
| 34 |
match &json { |
| 35 |
|
| 36 |
Value::Array(reports) => { |
| 37 |
for report in reports.iter().take(MAX_REPORTS_PER_BODY) { |
| 38 |
if let Some(violation) = report.get("body") { |
| 39 |
log_violation(violation, "report-to"); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
Value::Object(map) => { |
| 45 |
if let Some(violation) = map.get("csp-report") { |
| 46 |
log_violation(violation, "report-uri"); |
| 47 |
} |
| 48 |
} |
| 49 |
_ => {} |
| 50 |
} |
| 51 |
|
| 52 |
StatusCode::NO_CONTENT |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
fn log_violation(violation: &Value, format: &'static str) { |
| 59 |
let field = |names: &[&str]| -> String { |
| 60 |
names |
| 61 |
.iter() |
| 62 |
.find_map(|name| violation.get(*name).and_then(Value::as_str)) |
| 63 |
.map(truncate) |
| 64 |
.unwrap_or_default() |
| 65 |
}; |
| 66 |
|
| 67 |
let directive = field(&[ |
| 68 |
"effectiveDirective", |
| 69 |
"effective-directive", |
| 70 |
"violated-directive", |
| 71 |
]); |
| 72 |
let blocked = field(&["blockedURL", "blocked-uri"]); |
| 73 |
let document = field(&["documentURL", "document-uri"]); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
let disposition = field(&["disposition"]); |
| 78 |
|
| 79 |
tracing::warn!( |
| 80 |
target: "csp_violation", |
| 81 |
format, |
| 82 |
disposition = %disposition, |
| 83 |
directive = %directive, |
| 84 |
blocked = %blocked, |
| 85 |
document = %document, |
| 86 |
"CSP violation reported" |
| 87 |
); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if disposition == "enforce" { |
| 105 |
crate::security_signals::note_csp_violation(&blocked, &directive, &document); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
fn truncate(value: &str) -> String { |
| 110 |
if value.len() <= MAX_FIELD_LEN { |
| 111 |
return value.to_string(); |
| 112 |
} |
| 113 |
let mut end = MAX_FIELD_LEN; |
| 114 |
while !value.is_char_boundary(end) { |
| 115 |
end -= 1; |
| 116 |
} |
| 117 |
format!("{}...", &value[..end]) |
| 118 |
} |
| 119 |
|
| 120 |
#[cfg(test)] |
| 121 |
mod tests { |
| 122 |
use super::*; |
| 123 |
|
| 124 |
#[tokio::test] |
| 125 |
async fn accepts_report_uri_shape() { |
| 126 |
let body = br#"{"csp-report":{"document-uri":"https://makenot.work/","violated-directive":"script-src","blocked-uri":"https://evil.example/x.js"}}"#; |
| 127 |
assert_eq!( |
| 128 |
report_csp_violation(Bytes::from_static(body)).await, |
| 129 |
StatusCode::NO_CONTENT |
| 130 |
); |
| 131 |
} |
| 132 |
|
| 133 |
#[tokio::test] |
| 134 |
async fn accepts_report_to_batch() { |
| 135 |
let body = br#"[{"type":"csp-violation","body":{"documentURL":"https://makenot.work/","effectiveDirective":"style-src","blockedURL":"inline","disposition":"report"}}]"#; |
| 136 |
assert_eq!( |
| 137 |
report_csp_violation(Bytes::from_static(body)).await, |
| 138 |
StatusCode::NO_CONTENT |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
#[tokio::test] |
| 143 |
async fn drops_garbage_without_erroring() { |
| 144 |
assert_eq!( |
| 145 |
report_csp_violation(Bytes::from_static(b"not json at all")).await, |
| 146 |
StatusCode::NO_CONTENT |
| 147 |
); |
| 148 |
assert_eq!( |
| 149 |
report_csp_violation(Bytes::from_static(b"{}")).await, |
| 150 |
StatusCode::NO_CONTENT |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
#[test] |
| 160 |
fn default_filter_actually_records_a_violation() { |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
let capture = crate::test_tracing::Capture::start(); |
| 167 |
|
| 168 |
let body = br#"{"csp-report":{"document-uri":"https://makenot.work/","violated-directive":"style-src","blocked-uri":"inline"}}"#; |
| 169 |
log_violation( |
| 170 |
&serde_json::from_slice::<Value>(body).unwrap()["csp-report"], |
| 171 |
"report-uri", |
| 172 |
); |
| 173 |
|
| 174 |
let logged = capture.logged(); |
| 175 |
assert!( |
| 176 |
logged.contains("CSP violation reported"), |
| 177 |
"the default filter dropped the violation; nothing would reach prod logs. got: {logged:?}" |
| 178 |
); |
| 179 |
assert!( |
| 180 |
logged.contains("style-src"), |
| 181 |
"directive missing: {logged:?}" |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
#[test] |
| 190 |
fn only_enforced_violations_reach_the_alert_path() { |
| 191 |
let enforced = serde_json::json!({ |
| 192 |
"documentURL": "https://makenot.work/", |
| 193 |
"effectiveDirective": "script-src", |
| 194 |
"blockedURL": "https://evil.example/x.js", |
| 195 |
"disposition": "enforce", |
| 196 |
}); |
| 197 |
let report_only = serde_json::json!({ |
| 198 |
"documentURL": "https://makenot.work/", |
| 199 |
"effectiveDirective": "style-src", |
| 200 |
"blockedURL": "inline", |
| 201 |
"disposition": "report", |
| 202 |
}); |
| 203 |
|
| 204 |
let legacy = serde_json::json!({ |
| 205 |
"document-uri": "https://makenot.work/", |
| 206 |
"violated-directive": "style-src", |
| 207 |
"blocked-uri": "inline", |
| 208 |
}); |
| 209 |
|
| 210 |
let disposition = |v: &Value| -> String { |
| 211 |
v.get("disposition") |
| 212 |
.and_then(Value::as_str) |
| 213 |
.unwrap_or_default() |
| 214 |
.to_string() |
| 215 |
}; |
| 216 |
assert_eq!(disposition(&enforced), "enforce"); |
| 217 |
assert_ne!(disposition(&report_only), "enforce"); |
| 218 |
assert_ne!(disposition(&legacy), "enforce"); |
| 219 |
} |
| 220 |
|
| 221 |
#[test] |
| 222 |
fn truncates_long_fields_on_a_char_boundary() { |
| 223 |
let long = "\u{e9}".repeat(MAX_FIELD_LEN); |
| 224 |
let out = truncate(&long); |
| 225 |
assert!(out.ends_with("...")); |
| 226 |
assert!(out.len() < long.len()); |
| 227 |
} |
| 228 |
} |
| 229 |
|