[control] QC minors: device install links open a page and download on POST (never listed), approve and install failures show on the request and the CLI waits, PBKDF2 floor, private pki dirs or an error, trusted_proxies for a proxy in front, a placeholder keeps the device allow list from being empty (Caddy refuses that)
This commit is contained in:
@@ -75,11 +75,35 @@ pub fn hostname(host_header: &str) -> String {
|
||||
name.to_ascii_lowercase()
|
||||
}
|
||||
|
||||
pub fn client_ip(session: &Session) -> Option<IpAddr> {
|
||||
session
|
||||
/// The client's address: the peer, or, when the peer is a trusted proxy, the rightmost
|
||||
/// `X-Forwarded-For` entry that is not itself trusted (the same right-to-left rule as Caddy's
|
||||
/// `trusted_proxies_strict`). Anything a client could forge to the left is ignored.
|
||||
pub fn client_ip(session: &Session, trusted: &[IpAddr]) -> Option<IpAddr> {
|
||||
let peer = session
|
||||
.client_addr()
|
||||
.and_then(|a| a.as_inet())
|
||||
.map(|a| a.ip())
|
||||
.map(|a| a.ip())?;
|
||||
if !trusted.contains(&peer) {
|
||||
return Some(peer);
|
||||
}
|
||||
let mut last = peer;
|
||||
for v in session
|
||||
.req_header()
|
||||
.headers
|
||||
.get_all("x-forwarded-for")
|
||||
.iter()
|
||||
.rev()
|
||||
{
|
||||
let Ok(s) = v.to_str() else { return Some(last) };
|
||||
for part in s.rsplit(',') {
|
||||
match part.trim().parse::<IpAddr>() {
|
||||
Ok(ip) if trusted.contains(&ip) => last = ip,
|
||||
Ok(ip) => return Some(ip),
|
||||
Err(_) => return Some(last),
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(last)
|
||||
}
|
||||
|
||||
/// A response with an empty body, like Caddy's `static_response` and error responses.
|
||||
@@ -157,7 +181,7 @@ impl ProxyHttp for EdgeProxy {
|
||||
if !host.cfg.share_links {
|
||||
share::strip_any_token(session)?;
|
||||
} else {
|
||||
let ip = client_ip(session);
|
||||
let ip = client_ip(session, &rt.trusted_proxies);
|
||||
let revs = self.shared.revocations();
|
||||
match share::handle(
|
||||
session,
|
||||
@@ -198,7 +222,7 @@ impl ProxyHttp for EdgeProxy {
|
||||
// A valid share link stands in for the sign-in on its prefix.
|
||||
return self.after_auth(session, &host).await;
|
||||
}
|
||||
let ip = client_ip(session).map(|i| i.to_string());
|
||||
let ip = client_ip(session, &rt.trusted_proxies).map(|i| i.to_string());
|
||||
let outcome = match forward_auth::check(
|
||||
&self.connector,
|
||||
fa,
|
||||
@@ -322,7 +346,12 @@ impl ProxyHttp for EdgeProxy {
|
||||
}
|
||||
req.insert_header(header::HOST, ctx.host_header.as_str())?;
|
||||
// Forwarding headers are always edge's own; client-supplied values are replaced.
|
||||
match client_ip(session) {
|
||||
let trusted = ctx
|
||||
.rt
|
||||
.as_ref()
|
||||
.map(|r| r.trusted_proxies.as_slice())
|
||||
.unwrap_or(&[]);
|
||||
match client_ip(session, trusted) {
|
||||
Some(ip) => req.insert_header("X-Forwarded-For", ip.to_string())?,
|
||||
None => {
|
||||
req.remove_header("X-Forwarded-For");
|
||||
@@ -415,7 +444,13 @@ impl ProxyHttp for EdgeProxy {
|
||||
method: req.method.as_str(),
|
||||
path: req.uri.path(),
|
||||
status: session.response_written().map_or(0, |r| r.status.as_u16()),
|
||||
remote_ip: client_ip(session).map(|i| truncate_ip(i).to_string()),
|
||||
remote_ip: client_ip(
|
||||
session,
|
||||
ctx.rt
|
||||
.as_ref()
|
||||
.map_or(&[], |r| r.trusted_proxies.as_slice()),
|
||||
)
|
||||
.map(|i| truncate_ip(i).to_string()),
|
||||
duration_ms: u64::try_from(ctx.start.elapsed().as_millis()).unwrap_or(u64::MAX),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -123,6 +123,8 @@ pub struct Runtime {
|
||||
pub share_key: Option<Arc<ShareKey>>,
|
||||
/// Loaded when any host has `device_certs`.
|
||||
pub device_trust: Option<Arc<crate::tls::DeviceTrust>>,
|
||||
/// Peers whose X-Forwarded-For names the client.
|
||||
pub trusted_proxies: Vec<std::net::IpAddr>,
|
||||
/// SHA-256 (hex) of the config file bytes this runtime was built from; empty when it was
|
||||
/// built from a parsed config.
|
||||
pub sha256: String,
|
||||
@@ -470,9 +472,20 @@ pub fn build(cfg: EdgeConfig, credentials_dir: Option<&Path>) -> Result<Runtime,
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let trusted_proxies = cfg
|
||||
.trusted_proxies
|
||||
.iter()
|
||||
.map(|s| {
|
||||
s.parse().map_err(|_| EdgeError::Config {
|
||||
path: PathBuf::from("<config>"),
|
||||
message: format!("trusted_proxies: {s:?} is not an IP address"),
|
||||
})
|
||||
})
|
||||
.collect::<Result<Vec<std::net::IpAddr>, _>>()?;
|
||||
|
||||
Ok(Runtime {
|
||||
cfg,
|
||||
trusted_proxies,
|
||||
hosts,
|
||||
forward_auth,
|
||||
access_log,
|
||||
|
||||
@@ -110,6 +110,8 @@ pub struct EdgeSpec<'a> {
|
||||
pub http_port: Option<u16>,
|
||||
/// Extra files written into the edge directory before it starts (path relative to it).
|
||||
pub files: Vec<(String, Vec<u8>)>,
|
||||
/// Peers whose X-Forwarded-For edge believes.
|
||||
pub trusted_proxies: Vec<std::net::IpAddr>,
|
||||
}
|
||||
|
||||
impl Default for EdgeSpec<'_> {
|
||||
@@ -122,6 +124,7 @@ impl Default for EdgeSpec<'_> {
|
||||
extra_args: Vec::new(),
|
||||
http_port: None,
|
||||
files: Vec::new(),
|
||||
trusted_proxies: Vec::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -191,6 +194,7 @@ pub fn spawn(spec: EdgeSpec<'_>) -> Edge {
|
||||
caddy_credentials_dir: "/run/credentials/caddy-fallback.service".into(),
|
||||
caddy_admin: None,
|
||||
caddy_admin_socket: None,
|
||||
trusted_proxies: spec.trusted_proxies.clone(),
|
||||
};
|
||||
let config_text = compile_with(&dir, &ca, &opts, spec.site, &spec.hosts);
|
||||
std::fs::write(dir.join("edge.json"), &config_text).unwrap();
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
//! `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;
|
||||
|
||||
#[cfg(not(unix))]
|
||||
#[test]
|
||||
fn skipped_on_windows() {
|
||||
eprintln!(
|
||||
"SKIPPED on Windows: {} needs Linux (Pingora runner); run it in WSL2, see CLAUDE.md",
|
||||
file!()
|
||||
);
|
||||
}
|
||||
|
||||
#[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<std::net::IpAddr>, 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}");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user