// SPDX-License-Identifier: CPAL-1.0 // Copyright (c) 2026 Marek Novák. Part of Ianitor, under the Common Public Attribution // License 1.0: see LICENSE (its Exhibit A is this file's notice). //! `trusted_proxies` (off by default): behind a proxy on loopback (Caddy beside Ianitor), the //! client named in X-Forwarded-For is the one edge forwards and logs; without the option a //! client-sent X-Forwarded-For is ignored. #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] #[cfg(unix)] mod common; // On Windows this target has nothing to run: it shows as one *ignored* test, never as a // pass (ROADMAP §8.3; run it in WSL2, see CLAUDE.md). #[cfg(not(unix))] #[test] #[ignore = "needs Linux (Pingora runner): run it in WSL2, see CLAUDE.md"] fn needs_linux() {} #[cfg(unix)] mod unix { use crate::common::{EdgeSpec, host_file, start}; use ianitor_testkit::client::{self, Req}; use ianitor_testkit::upstream::Echo; async fn forwarded_for(trusted: Vec, xff: &str) -> (String, String) { let echo = Echo::start("app").await; let edge = start(EdgeSpec { hosts: vec![host_file( "app.test", &format!( "upstreams = [\"{}\"]\naccess_log = \"truncated\"\n", echo.url() ), )], trusted_proxies: trusted, ..Default::default() }); let r = client::https( edge.https, "app.test", edge.roots(), &Req::get("app.test", "/").header("x-forwarded-for", xff), ) .await .unwrap(); assert_eq!(r.status, 200); let seen = echo.proxied(); let got = String::from_utf8(seen[0].header("x-forwarded-for").unwrap().to_vec()).unwrap(); // The access line is written after the response; give it a moment. let mut log = String::new(); for _ in 0..50 { log = edge.access_log(); if log.contains("app.test") { break; } tokio::time::sleep(std::time::Duration::from_millis(50)).await; } (got, log) } #[tokio::test(flavor = "multi_thread")] async fn without_the_option_a_client_sent_forwarded_for_is_ignored() { let (xff, log) = forwarded_for(Vec::new(), "203.0.113.7").await; assert_eq!(xff, "127.0.0.1"); assert!(!log.contains("203.0.113"), "{log}"); } #[tokio::test(flavor = "multi_thread")] async fn a_trusted_proxy_names_the_client_rightmost_first() { // The leftmost entry is what a client could forge; the rightmost untrusted one counts. let (xff, log) = forwarded_for( vec!["127.0.0.1".parse().unwrap()], "198.51.100.9, 203.0.113.7, 127.0.0.1", ) .await; assert_eq!(xff, "203.0.113.7"); assert!(log.contains("203.0.113.0"), "the real /24 is logged: {log}"); assert!(!log.contains("198.51.100"), "{log}"); } }