Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changelog/proxy-client-ip-headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
mpp: patch
---

Stripped `Forwarded`, `X-Real-IP`, `True-Client-IP`, `CF-Connecting-IP` and `Via` from proxied requests, as `X-Forwarded-*` already is and as mppx does.
49 changes: 49 additions & 0 deletions src/proxy/headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ const PAYMENT_HEADERS: &[&str] = &[
"www-authenticate",
];

/// Client-supplied forwarding headers: a client could claim any address or
/// path through them. `X-Forwarded-*` is stripped by prefix.
const FORWARDING_HEADERS: &[&str] = &[
"cf-connecting-ip",
"forwarded",
"true-client-ip",
"via",
"x-real-ip",
];

/// Identity proofs a client sends the proxy (`Identity.Headers` request
/// headers, Tempo ID's DPoP proof and ID token), which name the user to the
/// proxy and never go upstream.
Expand Down Expand Up @@ -58,6 +68,9 @@ pub fn is_request_header_stripped(name: &str) -> bool {
if IDENTITY_HEADERS.iter().any(|h| eq_ascii_lower(name, h)) {
return true;
}
if FORWARDING_HEADERS.iter().any(|h| eq_ascii_lower(name, h)) {
return true;
}
if eq_ascii_lower(name, "host")
|| eq_ascii_lower(name, "cookie")
|| eq_ascii_lower(name, "accept-encoding")
Expand Down Expand Up @@ -457,6 +470,42 @@ mod tests {
assert!(is_request_header_stripped("x-forwarded-"));
}

#[test]
fn test_scrub_request_drops_client_forwarding_headers() {
let mut headers = vec![
("CF-Connecting-IP".into(), "1.2.3.4".into()),
("Forwarded".into(), "for=1.2.3.4".into()),
("True-Client-IP".into(), "1.2.3.4".into()),
("Via".into(), "1.1 client".into()),
("X-Real-IP".into(), "1.2.3.4".into()),
("X-Public".into(), "value".into()),
];

scrub_request_headers(&mut headers);

assert_eq!(names(&headers), vec!["x-public".to_string()]);
}

#[test]
fn test_client_forwarding_headers_stripped_case_insensitively() {
for name in [
"forwarded",
"FORWARDED",
"x-real-ip",
"X-REAL-IP",
"true-client-ip",
"True-Client-Ip",
"cf-connecting-ip",
"Cf-Connecting-Ip",
"via",
"VIA",
] {
assert!(is_request_header_stripped(name), "{name}");
}
assert!(!is_request_header_stripped("X-Public"));
assert!(!is_request_header_stripped("Viaduct"));
}

#[test]
fn test_scrub_request_drops_hop_by_hop_payment_cookie() {
let mut headers = vec![
Expand Down