[tests] Windows: Linux-only test targets report one explicit skip instead of zero tests
This commit is contained in:
+215
-200
@@ -10,209 +10,224 @@
|
||||
//!
|
||||
//! This is not Let's Encrypt staging: that needs a public domain and DNS.
|
||||
|
||||
#![cfg(unix)]
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
#[cfg(unix)]
|
||||
mod common;
|
||||
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use common::{EdgeSpec, host_file, spawn};
|
||||
use ianitor_testkit::client::{self, Req};
|
||||
use ianitor_testkit::proc::{ChildGuard, TempDir, free_port, local, require_binary, wait_for_port};
|
||||
use ianitor_testkit::tls::{TestCa, roots_from_pem};
|
||||
use ianitor_testkit::upstream::Echo;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn certificate_is_obtained_from_pebble_and_served() {
|
||||
let (Some(pebble), Some(challtestsrv)) = (
|
||||
require_binary("pebble"),
|
||||
require_binary("pebble-challtestsrv"),
|
||||
) else {
|
||||
return;
|
||||
};
|
||||
let dir = TempDir::new("pebble");
|
||||
// Pebble's own HTTPS endpoint needs a certificate; our test CA signs one for 127.0.0.1.
|
||||
let ca = TestCa::new("Pebble Listener CA");
|
||||
let leaf = ca.issue(&["127.0.0.1"]);
|
||||
std::fs::write(dir.join("pebble.crt"), &leaf.cert_pem).unwrap();
|
||||
std::fs::write(dir.join("pebble.key"), &leaf.key_pem).unwrap();
|
||||
std::fs::write(dir.join("listener-ca.pem"), &ca.cert_pem).unwrap();
|
||||
|
||||
let (acme_port, mgmt_port, dns_port, cts_mgmt, tls_port) = (
|
||||
free_port(),
|
||||
free_port(),
|
||||
free_port(),
|
||||
free_port(),
|
||||
free_port(),
|
||||
);
|
||||
|
||||
let edge_http = free_port();
|
||||
let config = serde_json::json!({ "pebble": {
|
||||
"listenAddress": format!("127.0.0.1:{acme_port}"),
|
||||
"managementListenAddress": format!("127.0.0.1:{mgmt_port}"),
|
||||
"certificate": dir.str("pebble.crt"),
|
||||
"privateKey": dir.str("pebble.key"),
|
||||
"httpPort": edge_http,
|
||||
"tlsPort": tls_port,
|
||||
"ocspResponderURL": "",
|
||||
"externalAccountBindingRequired": false,
|
||||
"domainBlocklist": [],
|
||||
"retryAfter": { "authz": 1, "order": 1 },
|
||||
"profiles": { "default": { "description": "test", "validityPeriod": 7_776_000 } }
|
||||
}});
|
||||
std::fs::write(dir.join("pebble.json"), config.to_string()).unwrap();
|
||||
let log = std::fs::File::create(dir.join("pebble.log")).unwrap();
|
||||
|
||||
// Fake DNS: every name resolves to 127.0.0.1, so Pebble validates against edge.
|
||||
let _cts = ChildGuard(
|
||||
Command::new(challtestsrv)
|
||||
.args(["-defaultIPv4", "127.0.0.1", "-defaultIPv6", ""])
|
||||
.args(["-dns01", &format!("127.0.0.1:{dns_port}")])
|
||||
.args(["-management", &format!("127.0.0.1:{cts_mgmt}")])
|
||||
.args(["-http01", "", "-https01", "", "-tlsalpn01", "", "-doh", ""])
|
||||
.stdout(Stdio::from(log.try_clone().unwrap()))
|
||||
.stderr(Stdio::from(log.try_clone().unwrap()))
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
);
|
||||
let _pebble = ChildGuard(
|
||||
Command::new(pebble)
|
||||
.arg("-config")
|
||||
.arg(dir.join("pebble.json"))
|
||||
.args(["-dnsserver", &format!("127.0.0.1:{dns_port}")])
|
||||
.env("PEBBLE_VA_NOSLEEP", "1")
|
||||
.env("PEBBLE_WFE_NONCEREJECT", "0")
|
||||
.env("PEBBLE_AUTHZREUSE", "0")
|
||||
.stdout(Stdio::from(log.try_clone().unwrap()))
|
||||
.stderr(Stdio::from(log))
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
);
|
||||
assert!(
|
||||
wait_for_port(local(acme_port), Duration::from_secs(20)),
|
||||
"pebble did not start"
|
||||
);
|
||||
assert!(wait_for_port(local(mgmt_port), Duration::from_secs(5)));
|
||||
|
||||
// Edge starts after Pebble, so its first ACME round succeeds.
|
||||
let echo_acme = Echo::start("acme-upstream").await;
|
||||
let echo_domi = Echo::start("domi-upstream").await;
|
||||
let site = format!(
|
||||
"[acme]\nemail = \"admin@example.org\"\ndirectory = \"https://127.0.0.1:{acme_port}/dir\"\n"
|
||||
);
|
||||
let mut edge = spawn(EdgeSpec {
|
||||
site: &site,
|
||||
hosts: vec![
|
||||
(
|
||||
"hosts/acme.test.toml".into(),
|
||||
format!(
|
||||
"hostname = \"acme.test\"\ntls = \"letsencrypt\"\nupstreams = [\"{}\"]\n",
|
||||
echo_acme.url()
|
||||
),
|
||||
),
|
||||
host_file(
|
||||
"domi.test",
|
||||
&format!("upstreams = [\"{}\"]\n", echo_domi.url()),
|
||||
),
|
||||
],
|
||||
log_level: "debug",
|
||||
extra_args: vec!["--acme-root".into(), dir.str("listener-ca.pem")],
|
||||
http_port: Some(edge_http),
|
||||
..Default::default()
|
||||
});
|
||||
assert!(
|
||||
wait_for_port(edge.http, Duration::from_secs(20)),
|
||||
"edge HTTP listener:\n{}",
|
||||
edge.stderr()
|
||||
);
|
||||
|
||||
// The root Pebble signs with (generated at Pebble start).
|
||||
let root = client::https(
|
||||
local(mgmt_port),
|
||||
"127.0.0.1",
|
||||
ca.roots(),
|
||||
&Req::get(&format!("127.0.0.1:{mgmt_port}"), "/roots/0"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(root.status, 200);
|
||||
let pebble_roots = roots_from_pem(&root.text());
|
||||
|
||||
let deadline = Instant::now() + Duration::from_secs(90);
|
||||
let served = loop {
|
||||
if let Ok(r) = client::https(
|
||||
edge.https,
|
||||
"acme.test",
|
||||
pebble_roots.clone(),
|
||||
&Req::get("acme.test", "/via-acme"),
|
||||
)
|
||||
.await
|
||||
{
|
||||
break r;
|
||||
}
|
||||
if Instant::now() > deadline || edge.exited().is_some() {
|
||||
panic!(
|
||||
"no ACME certificate served in time.\n--- edge ---\n{}\n--- pebble ---\n{}",
|
||||
edge.stderr(),
|
||||
std::fs::read_to_string(dir.join("pebble.log")).unwrap_or_default()
|
||||
);
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
};
|
||||
assert_eq!(served.status, 200);
|
||||
assert!(served.text().contains("acme-upstream"), "{}", served.text());
|
||||
assert!(
|
||||
edge.stderr().contains("acme.test: certificate issued"),
|
||||
"{}",
|
||||
edge.stderr()
|
||||
);
|
||||
assert!(
|
||||
edge.stderr()
|
||||
.contains("answered HTTP-01 challenge for acme.test"),
|
||||
"the challenge was really validated"
|
||||
);
|
||||
|
||||
// The Domi host keeps working with its own CA next to the ACME host.
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"domi.test",
|
||||
edge.roots(),
|
||||
&Req::get("domi.test", "/"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 200);
|
||||
assert!(r.text().contains("domi-upstream"));
|
||||
|
||||
// Stored with private permissions.
|
||||
let acme_dir = edge.dir.join("state/acme");
|
||||
for f in ["acme.test.key", "acme.test.crt"] {
|
||||
let mode = std::fs::metadata(acme_dir.join(f))
|
||||
.unwrap()
|
||||
.permissions()
|
||||
.mode()
|
||||
& 0o777;
|
||||
assert_eq!(mode, 0o600, "{f} has mode {mode:o}");
|
||||
}
|
||||
let account = std::fs::read_dir(&acme_dir)
|
||||
.unwrap()
|
||||
.map(|e| e.unwrap().path())
|
||||
.find(|p| {
|
||||
p.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.starts_with("account-")
|
||||
})
|
||||
.expect("account file stored");
|
||||
assert_eq!(
|
||||
std::fs::metadata(account).unwrap().permissions().mode() & 0o777,
|
||||
0o600
|
||||
);
|
||||
assert!(
|
||||
!edge.stderr().contains("PRIVATE KEY"),
|
||||
"no key material in the log"
|
||||
#[cfg(not(unix))]
|
||||
#[test]
|
||||
fn skipped_on_windows() {
|
||||
eprintln!(
|
||||
"SKIPPED on Windows: {} needs Linux (Pingora runner, Caddy, Pebble); run it in WSL2, see CLAUDE.md",
|
||||
file!()
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
mod unix {
|
||||
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use crate::common::{EdgeSpec, host_file, spawn};
|
||||
use ianitor_testkit::client::{self, Req};
|
||||
use ianitor_testkit::proc::{
|
||||
ChildGuard, TempDir, free_port, local, require_binary, wait_for_port,
|
||||
};
|
||||
use ianitor_testkit::tls::{TestCa, roots_from_pem};
|
||||
use ianitor_testkit::upstream::Echo;
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn certificate_is_obtained_from_pebble_and_served() {
|
||||
let (Some(pebble), Some(challtestsrv)) = (
|
||||
require_binary("pebble"),
|
||||
require_binary("pebble-challtestsrv"),
|
||||
) else {
|
||||
return;
|
||||
};
|
||||
let dir = TempDir::new("pebble");
|
||||
// Pebble's own HTTPS endpoint needs a certificate; our test CA signs one for 127.0.0.1.
|
||||
let ca = TestCa::new("Pebble Listener CA");
|
||||
let leaf = ca.issue(&["127.0.0.1"]);
|
||||
std::fs::write(dir.join("pebble.crt"), &leaf.cert_pem).unwrap();
|
||||
std::fs::write(dir.join("pebble.key"), &leaf.key_pem).unwrap();
|
||||
std::fs::write(dir.join("listener-ca.pem"), &ca.cert_pem).unwrap();
|
||||
|
||||
let (acme_port, mgmt_port, dns_port, cts_mgmt, tls_port) = (
|
||||
free_port(),
|
||||
free_port(),
|
||||
free_port(),
|
||||
free_port(),
|
||||
free_port(),
|
||||
);
|
||||
|
||||
let edge_http = free_port();
|
||||
let config = serde_json::json!({ "pebble": {
|
||||
"listenAddress": format!("127.0.0.1:{acme_port}"),
|
||||
"managementListenAddress": format!("127.0.0.1:{mgmt_port}"),
|
||||
"certificate": dir.str("pebble.crt"),
|
||||
"privateKey": dir.str("pebble.key"),
|
||||
"httpPort": edge_http,
|
||||
"tlsPort": tls_port,
|
||||
"ocspResponderURL": "",
|
||||
"externalAccountBindingRequired": false,
|
||||
"domainBlocklist": [],
|
||||
"retryAfter": { "authz": 1, "order": 1 },
|
||||
"profiles": { "default": { "description": "test", "validityPeriod": 7_776_000 } }
|
||||
}});
|
||||
std::fs::write(dir.join("pebble.json"), config.to_string()).unwrap();
|
||||
let log = std::fs::File::create(dir.join("pebble.log")).unwrap();
|
||||
|
||||
// Fake DNS: every name resolves to 127.0.0.1, so Pebble validates against edge.
|
||||
let _cts = ChildGuard(
|
||||
Command::new(challtestsrv)
|
||||
.args(["-defaultIPv4", "127.0.0.1", "-defaultIPv6", ""])
|
||||
.args(["-dns01", &format!("127.0.0.1:{dns_port}")])
|
||||
.args(["-management", &format!("127.0.0.1:{cts_mgmt}")])
|
||||
.args(["-http01", "", "-https01", "", "-tlsalpn01", "", "-doh", ""])
|
||||
.stdout(Stdio::from(log.try_clone().unwrap()))
|
||||
.stderr(Stdio::from(log.try_clone().unwrap()))
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
);
|
||||
let _pebble = ChildGuard(
|
||||
Command::new(pebble)
|
||||
.arg("-config")
|
||||
.arg(dir.join("pebble.json"))
|
||||
.args(["-dnsserver", &format!("127.0.0.1:{dns_port}")])
|
||||
.env("PEBBLE_VA_NOSLEEP", "1")
|
||||
.env("PEBBLE_WFE_NONCEREJECT", "0")
|
||||
.env("PEBBLE_AUTHZREUSE", "0")
|
||||
.stdout(Stdio::from(log.try_clone().unwrap()))
|
||||
.stderr(Stdio::from(log))
|
||||
.spawn()
|
||||
.unwrap(),
|
||||
);
|
||||
assert!(
|
||||
wait_for_port(local(acme_port), Duration::from_secs(20)),
|
||||
"pebble did not start"
|
||||
);
|
||||
assert!(wait_for_port(local(mgmt_port), Duration::from_secs(5)));
|
||||
|
||||
// Edge starts after Pebble, so its first ACME round succeeds.
|
||||
let echo_acme = Echo::start("acme-upstream").await;
|
||||
let echo_domi = Echo::start("domi-upstream").await;
|
||||
let site = format!(
|
||||
"[acme]\nemail = \"admin@example.org\"\ndirectory = \"https://127.0.0.1:{acme_port}/dir\"\n"
|
||||
);
|
||||
let mut edge = spawn(EdgeSpec {
|
||||
site: &site,
|
||||
hosts: vec![
|
||||
(
|
||||
"hosts/acme.test.toml".into(),
|
||||
format!(
|
||||
"hostname = \"acme.test\"\ntls = \"letsencrypt\"\nupstreams = [\"{}\"]\n",
|
||||
echo_acme.url()
|
||||
),
|
||||
),
|
||||
host_file(
|
||||
"domi.test",
|
||||
&format!("upstreams = [\"{}\"]\n", echo_domi.url()),
|
||||
),
|
||||
],
|
||||
log_level: "debug",
|
||||
extra_args: vec!["--acme-root".into(), dir.str("listener-ca.pem")],
|
||||
http_port: Some(edge_http),
|
||||
..Default::default()
|
||||
});
|
||||
assert!(
|
||||
wait_for_port(edge.http, Duration::from_secs(20)),
|
||||
"edge HTTP listener:\n{}",
|
||||
edge.stderr()
|
||||
);
|
||||
|
||||
// The root Pebble signs with (generated at Pebble start).
|
||||
let root = client::https(
|
||||
local(mgmt_port),
|
||||
"127.0.0.1",
|
||||
ca.roots(),
|
||||
&Req::get(&format!("127.0.0.1:{mgmt_port}"), "/roots/0"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(root.status, 200);
|
||||
let pebble_roots = roots_from_pem(&root.text());
|
||||
|
||||
let deadline = Instant::now() + Duration::from_secs(90);
|
||||
let served = loop {
|
||||
if let Ok(r) = client::https(
|
||||
edge.https,
|
||||
"acme.test",
|
||||
pebble_roots.clone(),
|
||||
&Req::get("acme.test", "/via-acme"),
|
||||
)
|
||||
.await
|
||||
{
|
||||
break r;
|
||||
}
|
||||
if Instant::now() > deadline || edge.exited().is_some() {
|
||||
panic!(
|
||||
"no ACME certificate served in time.\n--- edge ---\n{}\n--- pebble ---\n{}",
|
||||
edge.stderr(),
|
||||
std::fs::read_to_string(dir.join("pebble.log")).unwrap_or_default()
|
||||
);
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
};
|
||||
assert_eq!(served.status, 200);
|
||||
assert!(served.text().contains("acme-upstream"), "{}", served.text());
|
||||
assert!(
|
||||
edge.stderr().contains("acme.test: certificate issued"),
|
||||
"{}",
|
||||
edge.stderr()
|
||||
);
|
||||
assert!(
|
||||
edge.stderr()
|
||||
.contains("answered HTTP-01 challenge for acme.test"),
|
||||
"the challenge was really validated"
|
||||
);
|
||||
|
||||
// The Domi host keeps working with its own CA next to the ACME host.
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"domi.test",
|
||||
edge.roots(),
|
||||
&Req::get("domi.test", "/"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 200);
|
||||
assert!(r.text().contains("domi-upstream"));
|
||||
|
||||
// Stored with private permissions.
|
||||
let acme_dir = edge.dir.join("state/acme");
|
||||
for f in ["acme.test.key", "acme.test.crt"] {
|
||||
let mode = std::fs::metadata(acme_dir.join(f))
|
||||
.unwrap()
|
||||
.permissions()
|
||||
.mode()
|
||||
& 0o777;
|
||||
assert_eq!(mode, 0o600, "{f} has mode {mode:o}");
|
||||
}
|
||||
let account = std::fs::read_dir(&acme_dir)
|
||||
.unwrap()
|
||||
.map(|e| e.unwrap().path())
|
||||
.find(|p| {
|
||||
p.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
.starts_with("account-")
|
||||
})
|
||||
.expect("account file stored");
|
||||
assert_eq!(
|
||||
std::fs::metadata(account).unwrap().permissions().mode() & 0o777,
|
||||
0o600
|
||||
);
|
||||
assert!(
|
||||
!edge.stderr().contains("PRIVATE KEY"),
|
||||
"no key material in the log"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+343
-330
@@ -6,337 +6,245 @@
|
||||
//! goes back to the client unchanged, and an allowed request carries the identity headers
|
||||
//! from the auth server and never the client's own.
|
||||
|
||||
#![cfg(unix)]
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
#[cfg(unix)]
|
||||
mod common;
|
||||
|
||||
use common::{EdgeSpec, host_file, start};
|
||||
use ianitor_testkit::authelia::MockAuthelia;
|
||||
use ianitor_testkit::client::{self, Req, WsOutcome};
|
||||
use ianitor_testkit::upstream::{Echo, WsEcho, closed_port};
|
||||
|
||||
fn site(auth_url: &str) -> String {
|
||||
format!("[forward_auth]\nurl = \"{auth_url}\"\n")
|
||||
#[cfg(not(unix))]
|
||||
#[test]
|
||||
fn skipped_on_windows() {
|
||||
eprintln!(
|
||||
"SKIPPED on Windows: {} needs Linux (Pingora runner, Caddy, Pebble); run it in WSL2, see CLAUDE.md",
|
||||
file!()
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn denial_never_reaches_the_upstream() {
|
||||
let authelia = MockAuthelia::start().await;
|
||||
let echo = Echo::start("app").await;
|
||||
let ws = WsEcho::start().await;
|
||||
let site = site(&authelia.url());
|
||||
let edge = start(EdgeSpec {
|
||||
site: &site,
|
||||
hosts: vec![
|
||||
host_file(
|
||||
#[cfg(unix)]
|
||||
mod unix {
|
||||
|
||||
use crate::common::{EdgeSpec, host_file, start};
|
||||
use ianitor_testkit::authelia::MockAuthelia;
|
||||
use ianitor_testkit::client::{self, Req, WsOutcome};
|
||||
use ianitor_testkit::upstream::{Echo, WsEcho, closed_port};
|
||||
|
||||
fn site(auth_url: &str) -> String {
|
||||
format!("[forward_auth]\nurl = \"{auth_url}\"\n")
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn denial_never_reaches_the_upstream() {
|
||||
let authelia = MockAuthelia::start().await;
|
||||
let echo = Echo::start("app").await;
|
||||
let ws = WsEcho::start().await;
|
||||
let site = site(&authelia.url());
|
||||
let edge = start(EdgeSpec {
|
||||
site: &site,
|
||||
hosts: vec![
|
||||
host_file(
|
||||
"auth.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\nauth = \"forward-auth\"\n",
|
||||
echo.url()
|
||||
),
|
||||
),
|
||||
host_file(
|
||||
"wsauth.test",
|
||||
&format!("upstreams = [\"{}\"]\nauth = \"forward-auth\"\n", ws.url()),
|
||||
),
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
let roots = edge.roots();
|
||||
let go = |req: Req| {
|
||||
let roots = roots.clone();
|
||||
let addr = edge.https;
|
||||
async move { client::https(addr, "auth.test", roots, &req).await.unwrap() }
|
||||
};
|
||||
|
||||
// No session: browser GET is redirected to the portal, with Authelia's headers and body.
|
||||
let r = go(Req::get("auth.test", "/private?x=1")).await;
|
||||
assert_eq!(r.status, 302);
|
||||
assert_eq!(
|
||||
r.header("location"),
|
||||
Some("https://auth.test/?rd=https%3A%2F%2Fauth.test%2Fprivate%3Fx%3D1")
|
||||
);
|
||||
assert_eq!(
|
||||
r.header("set-cookie"),
|
||||
Some("authelia_flow=1; Path=/; HttpOnly")
|
||||
);
|
||||
assert!(r.text().contains("302 Found"));
|
||||
|
||||
let r = go(Req::new("HEAD", "auth.test", "/")).await;
|
||||
assert_eq!(r.status, 302);
|
||||
|
||||
let r = go(Req::new("POST", "auth.test", "/submit?x=1").body(b"do-not-forward")).await;
|
||||
assert_eq!(r.status, 401);
|
||||
assert_eq!(r.text(), "401 Unauthorized");
|
||||
|
||||
let r = go(Req::get("auth.test", "/").header("authorization", "Bearer wrong")).await;
|
||||
assert_eq!(r.status, 401);
|
||||
assert_eq!(
|
||||
r.header("www-authenticate"),
|
||||
Some("Basic realm=\"Authorization Required\"")
|
||||
);
|
||||
|
||||
let r = go(Req::get("auth.test", "/").header("cookie", "authelia_session=forbidden")).await;
|
||||
assert_eq!(r.status, 403);
|
||||
|
||||
// Spoofed identity and forwarding headers do not help.
|
||||
let r = go(Req::get("auth.test", "/admin")
|
||||
.header("remote-user", "admin")
|
||||
.header("x-forwarded-method", "GET")
|
||||
.header("x-forwarded-uri", "/public")
|
||||
.header("x-forwarded-host", "public.test")
|
||||
.header("x-forwarded-for", "10.0.0.1"))
|
||||
.await;
|
||||
assert_eq!(r.status, 302);
|
||||
assert!(
|
||||
r.header("location").unwrap().contains("auth.test%2Fadmin"),
|
||||
"edge sends its own X-Forwarded-*"
|
||||
);
|
||||
|
||||
// A websocket upgrade without a session is denied before any upgrade happens.
|
||||
match client::websocket(
|
||||
edge.https,
|
||||
"wsauth.test",
|
||||
roots.clone(),
|
||||
"wsauth.test",
|
||||
"/socket",
|
||||
&[],
|
||||
"hi",
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
{
|
||||
WsOutcome::Refused(r) => assert_eq!(r.status, 302),
|
||||
WsOutcome::Upgraded { .. } => panic!("unauthenticated websocket was upgraded"),
|
||||
}
|
||||
|
||||
assert!(
|
||||
echo.proxied().is_empty(),
|
||||
"a denied request reached the upstream: {:?}",
|
||||
echo.proxied()
|
||||
);
|
||||
assert_eq!(
|
||||
ws.connections(),
|
||||
0,
|
||||
"a denied websocket reached the upstream"
|
||||
);
|
||||
|
||||
// What Authelia was told, per its proxy-authorization guide.
|
||||
let asked = authelia.requests();
|
||||
assert!(asked.len() >= 7, "{asked:?}");
|
||||
let post = asked
|
||||
.iter()
|
||||
.find(|r| r.header("x-forwarded-method") == Some(b"POST".as_slice()))
|
||||
.expect("the POST was checked");
|
||||
assert_eq!(post.method, "GET", "the auth subrequest is a GET");
|
||||
assert_eq!(post.path, "/api/authz/forward-auth");
|
||||
assert_eq!(post.header("x-forwarded-proto"), Some(b"https".as_slice()));
|
||||
assert_eq!(
|
||||
post.header("x-forwarded-host"),
|
||||
Some(b"auth.test".as_slice())
|
||||
);
|
||||
assert_eq!(
|
||||
post.header("x-forwarded-uri"),
|
||||
Some(b"/submit?x=1".as_slice())
|
||||
);
|
||||
assert_eq!(
|
||||
post.header("x-forwarded-for"),
|
||||
Some(b"127.0.0.1".as_slice())
|
||||
);
|
||||
assert!(
|
||||
post.body.is_empty(),
|
||||
"the request body is not sent to the auth server"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn allowed_request_carries_identity_from_the_auth_server_only() {
|
||||
let authelia = MockAuthelia::start().await;
|
||||
let echo = Echo::start("app").await;
|
||||
let site = site(&authelia.url());
|
||||
let edge = start(EdgeSpec {
|
||||
site: &site,
|
||||
hosts: vec![host_file(
|
||||
"auth.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\nauth = \"forward-auth\"\n",
|
||||
echo.url()
|
||||
),
|
||||
),
|
||||
host_file(
|
||||
"wsauth.test",
|
||||
&format!("upstreams = [\"{}\"]\nauth = \"forward-auth\"\n", ws.url()),
|
||||
),
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
let roots = edge.roots();
|
||||
let go = |req: Req| {
|
||||
let roots = roots.clone();
|
||||
let addr = edge.https;
|
||||
async move { client::https(addr, "auth.test", roots, &req).await.unwrap() }
|
||||
};
|
||||
)],
|
||||
..Default::default()
|
||||
});
|
||||
let roots = edge.roots();
|
||||
|
||||
// No session: browser GET is redirected to the portal, with Authelia's headers and body.
|
||||
let r = go(Req::get("auth.test", "/private?x=1")).await;
|
||||
assert_eq!(r.status, 302);
|
||||
assert_eq!(
|
||||
r.header("location"),
|
||||
Some("https://auth.test/?rd=https%3A%2F%2Fauth.test%2Fprivate%3Fx%3D1")
|
||||
);
|
||||
assert_eq!(
|
||||
r.header("set-cookie"),
|
||||
Some("authelia_flow=1; Path=/; HttpOnly")
|
||||
);
|
||||
assert!(r.text().contains("302 Found"));
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"auth.test",
|
||||
roots.clone(),
|
||||
&Req::new("POST", "auth.test", "/submit")
|
||||
.header("cookie", "authelia_session=good")
|
||||
.header("remote-user", "mallory")
|
||||
.header("remote-groups", "root")
|
||||
.body(b"form=data"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 200, "{}", r.text());
|
||||
let seen = echo.proxied();
|
||||
assert_eq!(seen.len(), 1);
|
||||
let up = &seen[0];
|
||||
assert_eq!(up.header("remote-user"), Some(b"alice".as_slice()));
|
||||
assert_eq!(up.header("remote-groups"), Some(b"admins,dev".as_slice()));
|
||||
assert_eq!(
|
||||
up.header("remote-email"),
|
||||
Some(b"alice@example.org".as_slice())
|
||||
);
|
||||
assert_eq!(up.header("remote-name"), Some(b"Alice Example".as_slice()));
|
||||
assert_eq!(up.body, b"form=data", "the body still reaches the upstream");
|
||||
assert_eq!(
|
||||
up.headers
|
||||
.iter()
|
||||
.filter(|(n, _)| n == "remote-user")
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
|
||||
let r = go(Req::new("HEAD", "auth.test", "/")).await;
|
||||
assert_eq!(r.status, 302);
|
||||
// Allowed without identity: the client's own Remote-* headers are removed, not passed on.
|
||||
echo.clear();
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"auth.test",
|
||||
roots.clone(),
|
||||
&Req::get("auth.test", "/")
|
||||
.header("cookie", "authelia_session=nameless")
|
||||
.header("remote-user", "mallory"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 200);
|
||||
assert_eq!(echo.proxied()[0].header("remote-user"), None);
|
||||
|
||||
let r = go(Req::new("POST", "auth.test", "/submit?x=1").body(b"do-not-forward")).await;
|
||||
assert_eq!(r.status, 401);
|
||||
assert_eq!(r.text(), "401 Unauthorized");
|
||||
|
||||
let r = go(Req::get("auth.test", "/").header("authorization", "Bearer wrong")).await;
|
||||
assert_eq!(r.status, 401);
|
||||
assert_eq!(
|
||||
r.header("www-authenticate"),
|
||||
Some("Basic realm=\"Authorization Required\"")
|
||||
);
|
||||
|
||||
let r = go(Req::get("auth.test", "/").header("cookie", "authelia_session=forbidden")).await;
|
||||
assert_eq!(r.status, 403);
|
||||
|
||||
// Spoofed identity and forwarding headers do not help.
|
||||
let r = go(Req::get("auth.test", "/admin")
|
||||
.header("remote-user", "admin")
|
||||
.header("x-forwarded-method", "GET")
|
||||
.header("x-forwarded-uri", "/public")
|
||||
.header("x-forwarded-host", "public.test")
|
||||
.header("x-forwarded-for", "10.0.0.1"))
|
||||
.await;
|
||||
assert_eq!(r.status, 302);
|
||||
assert!(
|
||||
r.header("location").unwrap().contains("auth.test%2Fadmin"),
|
||||
"edge sends its own X-Forwarded-*"
|
||||
);
|
||||
|
||||
// A websocket upgrade without a session is denied before any upgrade happens.
|
||||
match client::websocket(
|
||||
edge.https,
|
||||
"wsauth.test",
|
||||
roots.clone(),
|
||||
"wsauth.test",
|
||||
"/socket",
|
||||
&[],
|
||||
"hi",
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
{
|
||||
WsOutcome::Refused(r) => assert_eq!(r.status, 302),
|
||||
WsOutcome::Upgraded { .. } => panic!("unauthenticated websocket was upgraded"),
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"auth.test",
|
||||
roots,
|
||||
&Req::get("auth.test", "/").header("authorization", "Bearer good"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 200);
|
||||
}
|
||||
|
||||
assert!(
|
||||
echo.proxied().is_empty(),
|
||||
"a denied request reached the upstream: {:?}",
|
||||
echo.proxied()
|
||||
);
|
||||
assert_eq!(
|
||||
ws.connections(),
|
||||
0,
|
||||
"a denied websocket reached the upstream"
|
||||
);
|
||||
|
||||
// What Authelia was told, per its proxy-authorization guide.
|
||||
let asked = authelia.requests();
|
||||
assert!(asked.len() >= 7, "{asked:?}");
|
||||
let post = asked
|
||||
.iter()
|
||||
.find(|r| r.header("x-forwarded-method") == Some(b"POST".as_slice()))
|
||||
.expect("the POST was checked");
|
||||
assert_eq!(post.method, "GET", "the auth subrequest is a GET");
|
||||
assert_eq!(post.path, "/api/authz/forward-auth");
|
||||
assert_eq!(post.header("x-forwarded-proto"), Some(b"https".as_slice()));
|
||||
assert_eq!(
|
||||
post.header("x-forwarded-host"),
|
||||
Some(b"auth.test".as_slice())
|
||||
);
|
||||
assert_eq!(
|
||||
post.header("x-forwarded-uri"),
|
||||
Some(b"/submit?x=1".as_slice())
|
||||
);
|
||||
assert_eq!(
|
||||
post.header("x-forwarded-for"),
|
||||
Some(b"127.0.0.1".as_slice())
|
||||
);
|
||||
assert!(
|
||||
post.body.is_empty(),
|
||||
"the request body is not sent to the auth server"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn allowed_request_carries_identity_from_the_auth_server_only() {
|
||||
let authelia = MockAuthelia::start().await;
|
||||
let echo = Echo::start("app").await;
|
||||
let site = site(&authelia.url());
|
||||
let edge = start(EdgeSpec {
|
||||
site: &site,
|
||||
hosts: vec![host_file(
|
||||
"auth.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\nauth = \"forward-auth\"\n",
|
||||
echo.url()
|
||||
),
|
||||
)],
|
||||
..Default::default()
|
||||
});
|
||||
let roots = edge.roots();
|
||||
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"auth.test",
|
||||
roots.clone(),
|
||||
&Req::new("POST", "auth.test", "/submit")
|
||||
.header("cookie", "authelia_session=good")
|
||||
.header("remote-user", "mallory")
|
||||
.header("remote-groups", "root")
|
||||
.body(b"form=data"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 200, "{}", r.text());
|
||||
let seen = echo.proxied();
|
||||
assert_eq!(seen.len(), 1);
|
||||
let up = &seen[0];
|
||||
assert_eq!(up.header("remote-user"), Some(b"alice".as_slice()));
|
||||
assert_eq!(up.header("remote-groups"), Some(b"admins,dev".as_slice()));
|
||||
assert_eq!(
|
||||
up.header("remote-email"),
|
||||
Some(b"alice@example.org".as_slice())
|
||||
);
|
||||
assert_eq!(up.header("remote-name"), Some(b"Alice Example".as_slice()));
|
||||
assert_eq!(up.body, b"form=data", "the body still reaches the upstream");
|
||||
assert_eq!(
|
||||
up.headers
|
||||
.iter()
|
||||
.filter(|(n, _)| n == "remote-user")
|
||||
.count(),
|
||||
1
|
||||
);
|
||||
|
||||
// Allowed without identity: the client's own Remote-* headers are removed, not passed on.
|
||||
echo.clear();
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"auth.test",
|
||||
roots.clone(),
|
||||
&Req::get("auth.test", "/")
|
||||
.header("cookie", "authelia_session=nameless")
|
||||
.header("remote-user", "mallory"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 200);
|
||||
assert_eq!(echo.proxied()[0].header("remote-user"), None);
|
||||
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"auth.test",
|
||||
roots,
|
||||
&Req::get("auth.test", "/").header("authorization", "Bearer good"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 200);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn unreachable_auth_server_fails_closed() {
|
||||
let echo = Echo::start("app").await;
|
||||
let site = site(&format!(
|
||||
"http://127.0.0.1:{}/api/authz/forward-auth",
|
||||
closed_port()
|
||||
));
|
||||
let edge = start(EdgeSpec {
|
||||
site: &site,
|
||||
hosts: vec![host_file(
|
||||
"auth.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\nauth = \"forward-auth\"\n",
|
||||
echo.url()
|
||||
),
|
||||
)],
|
||||
..Default::default()
|
||||
});
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"auth.test",
|
||||
edge.roots(),
|
||||
&Req::get("auth.test", "/").header("cookie", "authelia_session=good"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 502);
|
||||
assert!(echo.proxied().is_empty());
|
||||
}
|
||||
|
||||
/// QC B2a, B2b, m3: misbehaving auth servers. Edge fails closed and never crashes or hangs.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn misbehaving_auth_answers_fail_closed() {
|
||||
let authelia = MockAuthelia::start().await;
|
||||
let echo = Echo::start("app").await;
|
||||
let site = site(&authelia.url());
|
||||
let edge = start(EdgeSpec {
|
||||
site: &site,
|
||||
hosts: vec![host_file(
|
||||
"auth.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\nauth = \"forward-auth\"\n",
|
||||
echo.url()
|
||||
),
|
||||
)],
|
||||
..Default::default()
|
||||
});
|
||||
let go = |session: &'static str| {
|
||||
let roots = edge.roots();
|
||||
let addr = edge.https;
|
||||
async move {
|
||||
let cookie = format!("authelia_session={session}");
|
||||
client::https(
|
||||
addr,
|
||||
"auth.test",
|
||||
roots,
|
||||
&Req::get("auth.test", "/").header("cookie", &cookie),
|
||||
)
|
||||
.await
|
||||
}
|
||||
};
|
||||
|
||||
// 100 Continue before the real answer is skipped, like any HTTP client does.
|
||||
let r = go("continue-ok")
|
||||
.await
|
||||
.expect("edge answers after a 1xx from the auth server");
|
||||
assert_eq!(r.status, 200);
|
||||
assert_eq!(echo.proxied().len(), 1);
|
||||
assert_eq!(
|
||||
echo.proxied()[0].header("remote-user"),
|
||||
Some(b"alice".as_slice())
|
||||
);
|
||||
echo.clear();
|
||||
|
||||
// 101 is a final answer but not an allow: denied with 502.
|
||||
assert_eq!(go("switch").await.unwrap().status, 502);
|
||||
// Oversized or truncated auth answers: 502, never forwarded.
|
||||
assert_eq!(go("big-ok").await.unwrap().status, 502);
|
||||
assert_eq!(go("big-deny").await.unwrap().status, 502);
|
||||
assert_eq!(go("truncated-ok").await.unwrap().status, 502);
|
||||
assert!(echo.proxied().is_empty(), "{:?}", echo.proxied());
|
||||
|
||||
// An auth server that never answers: 502 once the 10 s auth timeout passes.
|
||||
let t = std::time::Instant::now();
|
||||
let r = go("hang")
|
||||
.await
|
||||
.expect("edge answers a hanging auth server");
|
||||
assert_eq!(r.status, 502);
|
||||
assert!(
|
||||
t.elapsed() < std::time::Duration::from_secs(14),
|
||||
"took {:?}",
|
||||
t.elapsed()
|
||||
);
|
||||
assert!(echo.proxied().is_empty());
|
||||
|
||||
// Still serving afterwards.
|
||||
assert_eq!(go("good").await.unwrap().status, 200);
|
||||
}
|
||||
|
||||
/// QC m5: client-sent Remote-* headers never reach a forward-auth upstream, whatever
|
||||
/// copy_headers says.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn remote_headers_are_stripped_even_with_a_custom_copy_list() {
|
||||
let authelia = MockAuthelia::start().await;
|
||||
let echo = Echo::start("app").await;
|
||||
for copy in ["[]", "[\"Remote-User\"]"] {
|
||||
echo.clear();
|
||||
let site = format!(
|
||||
"[forward_auth]\nurl = \"{}\"\ncopy_headers = {copy}\n",
|
||||
authelia.url()
|
||||
);
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn unreachable_auth_server_fails_closed() {
|
||||
let echo = Echo::start("app").await;
|
||||
let site = site(&format!(
|
||||
"http://127.0.0.1:{}/api/authz/forward-auth",
|
||||
closed_port()
|
||||
));
|
||||
let edge = start(EdgeSpec {
|
||||
site: &site,
|
||||
hosts: vec![host_file(
|
||||
@@ -352,25 +260,130 @@ async fn remote_headers_are_stripped_even_with_a_custom_copy_list() {
|
||||
edge.https,
|
||||
"auth.test",
|
||||
edge.roots(),
|
||||
&Req::get("auth.test", "/")
|
||||
.header("cookie", "authelia_session=good")
|
||||
.header("remote-user", "mallory")
|
||||
.header("remote-groups", "root")
|
||||
.header("remote-email", "m@evil.test")
|
||||
.header("remote-name", "Mallory"),
|
||||
&Req::get("auth.test", "/").header("cookie", "authelia_session=good"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 502);
|
||||
assert!(echo.proxied().is_empty());
|
||||
}
|
||||
|
||||
/// QC B2a, B2b, m3: misbehaving auth servers. Edge fails closed and never crashes or hangs.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn misbehaving_auth_answers_fail_closed() {
|
||||
let authelia = MockAuthelia::start().await;
|
||||
let echo = Echo::start("app").await;
|
||||
let site = site(&authelia.url());
|
||||
let edge = start(EdgeSpec {
|
||||
site: &site,
|
||||
hosts: vec![host_file(
|
||||
"auth.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\nauth = \"forward-auth\"\n",
|
||||
echo.url()
|
||||
),
|
||||
)],
|
||||
..Default::default()
|
||||
});
|
||||
let go = |session: &'static str| {
|
||||
let roots = edge.roots();
|
||||
let addr = edge.https;
|
||||
async move {
|
||||
let cookie = format!("authelia_session={session}");
|
||||
client::https(
|
||||
addr,
|
||||
"auth.test",
|
||||
roots,
|
||||
&Req::get("auth.test", "/").header("cookie", &cookie),
|
||||
)
|
||||
.await
|
||||
}
|
||||
};
|
||||
|
||||
// 100 Continue before the real answer is skipped, like any HTTP client does.
|
||||
let r = go("continue-ok")
|
||||
.await
|
||||
.expect("edge answers after a 1xx from the auth server");
|
||||
assert_eq!(r.status, 200);
|
||||
let up = &echo.proxied()[0];
|
||||
let user = up.header("remote-user");
|
||||
if copy == "[]" {
|
||||
assert_eq!(user, None, "copy_headers = []");
|
||||
} else {
|
||||
assert_eq!(user, Some(b"alice".as_slice()));
|
||||
}
|
||||
for h in ["remote-groups", "remote-email", "remote-name"] {
|
||||
assert_eq!(up.header(h), None, "{h} with copy_headers = {copy}");
|
||||
assert_eq!(echo.proxied().len(), 1);
|
||||
assert_eq!(
|
||||
echo.proxied()[0].header("remote-user"),
|
||||
Some(b"alice".as_slice())
|
||||
);
|
||||
echo.clear();
|
||||
|
||||
// 101 is a final answer but not an allow: denied with 502.
|
||||
assert_eq!(go("switch").await.unwrap().status, 502);
|
||||
// Oversized or truncated auth answers: 502, never forwarded.
|
||||
assert_eq!(go("big-ok").await.unwrap().status, 502);
|
||||
assert_eq!(go("big-deny").await.unwrap().status, 502);
|
||||
assert_eq!(go("truncated-ok").await.unwrap().status, 502);
|
||||
assert!(echo.proxied().is_empty(), "{:?}", echo.proxied());
|
||||
|
||||
// An auth server that never answers: 502 once the 10 s auth timeout passes.
|
||||
let t = std::time::Instant::now();
|
||||
let r = go("hang")
|
||||
.await
|
||||
.expect("edge answers a hanging auth server");
|
||||
assert_eq!(r.status, 502);
|
||||
assert!(
|
||||
t.elapsed() < std::time::Duration::from_secs(14),
|
||||
"took {:?}",
|
||||
t.elapsed()
|
||||
);
|
||||
assert!(echo.proxied().is_empty());
|
||||
|
||||
// Still serving afterwards.
|
||||
assert_eq!(go("good").await.unwrap().status, 200);
|
||||
}
|
||||
|
||||
/// QC m5: client-sent Remote-* headers never reach a forward-auth upstream, whatever
|
||||
/// copy_headers says.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn remote_headers_are_stripped_even_with_a_custom_copy_list() {
|
||||
let authelia = MockAuthelia::start().await;
|
||||
let echo = Echo::start("app").await;
|
||||
for copy in ["[]", "[\"Remote-User\"]"] {
|
||||
echo.clear();
|
||||
let site = format!(
|
||||
"[forward_auth]\nurl = \"{}\"\ncopy_headers = {copy}\n",
|
||||
authelia.url()
|
||||
);
|
||||
let edge = start(EdgeSpec {
|
||||
site: &site,
|
||||
hosts: vec![host_file(
|
||||
"auth.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\nauth = \"forward-auth\"\n",
|
||||
echo.url()
|
||||
),
|
||||
)],
|
||||
..Default::default()
|
||||
});
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"auth.test",
|
||||
edge.roots(),
|
||||
&Req::get("auth.test", "/")
|
||||
.header("cookie", "authelia_session=good")
|
||||
.header("remote-user", "mallory")
|
||||
.header("remote-groups", "root")
|
||||
.header("remote-email", "m@evil.test")
|
||||
.header("remote-name", "Mallory"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 200);
|
||||
let up = &echo.proxied()[0];
|
||||
let user = up.header("remote-user");
|
||||
if copy == "[]" {
|
||||
assert_eq!(user, None, "copy_headers = []");
|
||||
} else {
|
||||
assert_eq!(user, Some(b"alice".as_slice()));
|
||||
}
|
||||
for h in ["remote-groups", "remote-email", "remote-name"] {
|
||||
assert_eq!(up.header(h), None, "{h} with copy_headers = {copy}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+245
-232
@@ -5,256 +5,269 @@
|
||||
//! An injected secret reaches the upstream and never appears in a response, a log line, the
|
||||
//! access log or the compiled config, even with the most verbose log level.
|
||||
|
||||
#![cfg(unix)]
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
#[cfg(unix)]
|
||||
mod common;
|
||||
|
||||
use common::{EdgeSpec, host_file, spawn, start};
|
||||
use ianitor_testkit::client::{self, Req, Resp, WsOutcome};
|
||||
use ianitor_testkit::upstream::{Echo, WsEcho, closed_port};
|
||||
|
||||
fn unique_secret() -> String {
|
||||
let nanos = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos();
|
||||
format!("sekret-{}-{nanos:x}-tail", std::process::id())
|
||||
#[cfg(not(unix))]
|
||||
#[test]
|
||||
fn skipped_on_windows() {
|
||||
eprintln!(
|
||||
"SKIPPED on Windows: {} needs Linux (Pingora runner, Caddy, Pebble); run it in WSL2, see CLAUDE.md",
|
||||
file!()
|
||||
);
|
||||
}
|
||||
|
||||
fn assert_clean(what: &str, haystack: &[u8], secret: &str) {
|
||||
let hay = String::from_utf8_lossy(haystack);
|
||||
assert!(!hay.contains(secret), "secret leaked into {what}:\n{hay}");
|
||||
}
|
||||
#[cfg(unix)]
|
||||
mod unix {
|
||||
|
||||
fn assert_resp_clean(what: &str, r: &Resp, secret: &str) {
|
||||
for (n, v) in &r.headers {
|
||||
assert_clean(
|
||||
&format!("{what}: response header {n}"),
|
||||
v.as_bytes(),
|
||||
secret,
|
||||
);
|
||||
use crate::common::{EdgeSpec, host_file, spawn, start};
|
||||
use ianitor_testkit::client::{self, Req, Resp, WsOutcome};
|
||||
use ianitor_testkit::upstream::{Echo, WsEcho, closed_port};
|
||||
|
||||
fn unique_secret() -> String {
|
||||
let nanos = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_nanos();
|
||||
format!("sekret-{}-{nanos:x}-tail", std::process::id())
|
||||
}
|
||||
assert_clean(&format!("{what}: response body"), &r.body, secret);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn injected_secret_never_appears_in_a_response_or_log() {
|
||||
let secret = unique_secret();
|
||||
let echo = Echo::start("app").await;
|
||||
let ws = WsEcho::start().await;
|
||||
let rule =
|
||||
"[[request_headers]]\naction = \"set\"\nname = \"X-Api-Key\"\nsecret = \"api_key\"\n";
|
||||
let edge = start(EdgeSpec {
|
||||
site: "secrets = [\"api_key\"]\n",
|
||||
hosts: vec![
|
||||
host_file(
|
||||
fn assert_clean(what: &str, haystack: &[u8], secret: &str) {
|
||||
let hay = String::from_utf8_lossy(haystack);
|
||||
assert!(!hay.contains(secret), "secret leaked into {what}:\n{hay}");
|
||||
}
|
||||
|
||||
fn assert_resp_clean(what: &str, r: &Resp, secret: &str) {
|
||||
for (n, v) in &r.headers {
|
||||
assert_clean(
|
||||
&format!("{what}: response header {n}"),
|
||||
v.as_bytes(),
|
||||
secret,
|
||||
);
|
||||
}
|
||||
assert_clean(&format!("{what}: response body"), &r.body, secret);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn injected_secret_never_appears_in_a_response_or_log() {
|
||||
let secret = unique_secret();
|
||||
let echo = Echo::start("app").await;
|
||||
let ws = WsEcho::start().await;
|
||||
let rule =
|
||||
"[[request_headers]]\naction = \"set\"\nname = \"X-Api-Key\"\nsecret = \"api_key\"\n";
|
||||
let edge = start(EdgeSpec {
|
||||
site: "secrets = [\"api_key\"]\n",
|
||||
hosts: vec![
|
||||
host_file(
|
||||
"app.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\naccess_log = \"truncated\"\n{rule}",
|
||||
echo.url()
|
||||
),
|
||||
),
|
||||
host_file(
|
||||
"down.test",
|
||||
&format!(
|
||||
"upstreams = [\"http://127.0.0.1:{}\"]\naccess_log = \"truncated\"\n{rule}",
|
||||
closed_port()
|
||||
),
|
||||
),
|
||||
host_file(
|
||||
"ws.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\naccess_log = \"truncated\"\n{rule}",
|
||||
ws.url()
|
||||
),
|
||||
),
|
||||
],
|
||||
// systemd credentials usually end with a newline; it is not part of the value.
|
||||
secrets: vec![("api_key".into(), format!("{secret}\n").into_bytes())],
|
||||
log_level: "trace",
|
||||
..Default::default()
|
||||
});
|
||||
let roots = edge.roots();
|
||||
let go = |sni: &'static str, req: Req| {
|
||||
let roots = roots.clone();
|
||||
let addr = edge.https;
|
||||
async move { client::https(addr, sni, roots, &req).await.unwrap() }
|
||||
};
|
||||
|
||||
let mut responses = Vec::new();
|
||||
responses.push((
|
||||
"plain GET",
|
||||
go("app.test", Req::get("app.test", "/hello?token=abc")).await,
|
||||
));
|
||||
responses.push((
|
||||
"POST",
|
||||
go(
|
||||
"app.test",
|
||||
Req::new("POST", "app.test", "/submit").body(b"payload"),
|
||||
)
|
||||
.await,
|
||||
));
|
||||
responses.push((
|
||||
"client tries to set the header",
|
||||
go(
|
||||
"app.test",
|
||||
Req::get("app.test", "/").header("x-api-key", "attacker"),
|
||||
)
|
||||
.await,
|
||||
));
|
||||
responses.push((
|
||||
"upstream error status",
|
||||
go("app.test", Req::get("app.test", "/status/500")).await,
|
||||
));
|
||||
responses.push((
|
||||
"upstream reflects the secret in a header",
|
||||
go("app.test", Req::get("app.test", "/reflect?h=x-api-key")).await,
|
||||
));
|
||||
responses.push((
|
||||
"upstream down",
|
||||
go("down.test", Req::get("down.test", "/")).await,
|
||||
));
|
||||
responses.push((
|
||||
"unknown host",
|
||||
go("app.test", Req::get("nope.test", "/")).await,
|
||||
));
|
||||
let plain = client::http(edge.http, &Req::get("app.test", "/x?y=1"))
|
||||
.await
|
||||
.unwrap();
|
||||
responses.push(("plain HTTP redirect", plain));
|
||||
|
||||
for (what, r) in &responses {
|
||||
assert_resp_clean(what, r, &secret);
|
||||
}
|
||||
assert_eq!(responses[0].1.status, 200);
|
||||
assert_eq!(responses[4].1.status, 200);
|
||||
assert!(
|
||||
responses[4].1.header("x-reflected").is_none(),
|
||||
"edge strips a response header carrying a secret"
|
||||
);
|
||||
assert_eq!(responses[5].1.status, 502);
|
||||
|
||||
match client::websocket(
|
||||
edge.https,
|
||||
"ws.test",
|
||||
roots.clone(),
|
||||
"ws.test",
|
||||
"/socket",
|
||||
&[],
|
||||
"ping",
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
{
|
||||
WsOutcome::Upgraded { status, echo, .. } => {
|
||||
assert_eq!(status, 101);
|
||||
assert_eq!(echo, "ping");
|
||||
}
|
||||
WsOutcome::Refused(r) => panic!("websocket refused: {r:?}"),
|
||||
}
|
||||
|
||||
// The secret did reach the upstream, without the trailing newline, replacing the client's value.
|
||||
let seen = echo.proxied();
|
||||
assert!(seen.len() >= 5, "{seen:?}");
|
||||
for r in &seen {
|
||||
assert_eq!(
|
||||
r.header("x-api-key"),
|
||||
Some(secret.as_bytes()),
|
||||
"upstream request {}",
|
||||
r.path
|
||||
);
|
||||
}
|
||||
|
||||
// Give the access log a moment to flush, then check every output channel.
|
||||
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
|
||||
let access = edge.access_log();
|
||||
assert!(
|
||||
access.lines().count() >= 7,
|
||||
"access log has the requests:\n{access}"
|
||||
);
|
||||
assert!(
|
||||
access.contains("\"127.0.0.0\""),
|
||||
"IPv4 truncated to /24:\n{access}"
|
||||
);
|
||||
assert!(
|
||||
!access.contains("127.0.0.1"),
|
||||
"full client IP must not be logged:\n{access}"
|
||||
);
|
||||
assert!(
|
||||
!access.contains("token=abc"),
|
||||
"query strings are not logged:\n{access}"
|
||||
);
|
||||
let stderr = edge.stderr();
|
||||
assert!(
|
||||
stderr.len() > 100,
|
||||
"trace logging produced output:\n{stderr}"
|
||||
);
|
||||
assert_clean("stderr (trace level)", stderr.as_bytes(), &secret);
|
||||
assert_clean("access log", access.as_bytes(), &secret);
|
||||
assert_clean("compiled edge config", edge.config_text.as_bytes(), &secret);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn missing_secret_stops_startup_without_leaking_others() {
|
||||
let secret = unique_secret();
|
||||
let echo = Echo::start("app").await;
|
||||
let mut edge = spawn(EdgeSpec {
|
||||
site: "secrets = [\"present\", \"absent\"]\n",
|
||||
hosts: vec![host_file(
|
||||
"app.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\naccess_log = \"truncated\"\n{rule}",
|
||||
"upstreams = [\"{}\"]\n[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nsecret = \"present\"\n\
|
||||
[[request_headers]]\naction = \"set\"\nname = \"X-B\"\nsecret = \"absent\"\n",
|
||||
echo.url()
|
||||
),
|
||||
),
|
||||
host_file(
|
||||
"down.test",
|
||||
&format!(
|
||||
"upstreams = [\"http://127.0.0.1:{}\"]\naccess_log = \"truncated\"\n{rule}",
|
||||
closed_port()
|
||||
),
|
||||
),
|
||||
host_file(
|
||||
"ws.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\naccess_log = \"truncated\"\n{rule}",
|
||||
ws.url()
|
||||
),
|
||||
),
|
||||
],
|
||||
// systemd credentials usually end with a newline; it is not part of the value.
|
||||
secrets: vec![("api_key".into(), format!("{secret}\n").into_bytes())],
|
||||
log_level: "trace",
|
||||
..Default::default()
|
||||
});
|
||||
let roots = edge.roots();
|
||||
let go = |sni: &'static str, req: Req| {
|
||||
let roots = roots.clone();
|
||||
let addr = edge.https;
|
||||
async move { client::https(addr, sni, roots, &req).await.unwrap() }
|
||||
};
|
||||
|
||||
let mut responses = Vec::new();
|
||||
responses.push((
|
||||
"plain GET",
|
||||
go("app.test", Req::get("app.test", "/hello?token=abc")).await,
|
||||
));
|
||||
responses.push((
|
||||
"POST",
|
||||
go(
|
||||
"app.test",
|
||||
Req::new("POST", "app.test", "/submit").body(b"payload"),
|
||||
)
|
||||
.await,
|
||||
));
|
||||
responses.push((
|
||||
"client tries to set the header",
|
||||
go(
|
||||
"app.test",
|
||||
Req::get("app.test", "/").header("x-api-key", "attacker"),
|
||||
)
|
||||
.await,
|
||||
));
|
||||
responses.push((
|
||||
"upstream error status",
|
||||
go("app.test", Req::get("app.test", "/status/500")).await,
|
||||
));
|
||||
responses.push((
|
||||
"upstream reflects the secret in a header",
|
||||
go("app.test", Req::get("app.test", "/reflect?h=x-api-key")).await,
|
||||
));
|
||||
responses.push((
|
||||
"upstream down",
|
||||
go("down.test", Req::get("down.test", "/")).await,
|
||||
));
|
||||
responses.push((
|
||||
"unknown host",
|
||||
go("app.test", Req::get("nope.test", "/")).await,
|
||||
));
|
||||
let plain = client::http(edge.http, &Req::get("app.test", "/x?y=1"))
|
||||
.await
|
||||
.unwrap();
|
||||
responses.push(("plain HTTP redirect", plain));
|
||||
|
||||
for (what, r) in &responses {
|
||||
assert_resp_clean(what, r, &secret);
|
||||
}
|
||||
assert_eq!(responses[0].1.status, 200);
|
||||
assert_eq!(responses[4].1.status, 200);
|
||||
assert!(
|
||||
responses[4].1.header("x-reflected").is_none(),
|
||||
"edge strips a response header carrying a secret"
|
||||
);
|
||||
assert_eq!(responses[5].1.status, 502);
|
||||
|
||||
match client::websocket(
|
||||
edge.https,
|
||||
"ws.test",
|
||||
roots.clone(),
|
||||
"ws.test",
|
||||
"/socket",
|
||||
&[],
|
||||
"ping",
|
||||
)
|
||||
.await
|
||||
.unwrap()
|
||||
{
|
||||
WsOutcome::Upgraded { status, echo, .. } => {
|
||||
assert_eq!(status, 101);
|
||||
assert_eq!(echo, "ping");
|
||||
}
|
||||
WsOutcome::Refused(r) => panic!("websocket refused: {r:?}"),
|
||||
)],
|
||||
secrets: vec![("present".into(), secret.clone().into_bytes())],
|
||||
log_level: "trace",
|
||||
..Default::default()
|
||||
});
|
||||
let status = wait_exit(&mut edge);
|
||||
assert!(!status.success(), "edge must refuse to start");
|
||||
let err = edge.stderr();
|
||||
assert!(err.contains("absent"), "names the missing secret:\n{err}");
|
||||
assert_clean("stderr", err.as_bytes(), &secret);
|
||||
}
|
||||
|
||||
// The secret did reach the upstream, without the trailing newline, replacing the client's value.
|
||||
let seen = echo.proxied();
|
||||
assert!(seen.len() >= 5, "{seen:?}");
|
||||
for r in &seen {
|
||||
assert_eq!(
|
||||
r.header("x-api-key"),
|
||||
Some(secret.as_bytes()),
|
||||
"upstream request {}",
|
||||
r.path
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn malformed_secret_is_rejected_without_echoing_it() {
|
||||
let secret = unique_secret();
|
||||
let echo = Echo::start("app").await;
|
||||
let mut edge = spawn(EdgeSpec {
|
||||
site: "secrets = [\"api_key\"]\n",
|
||||
hosts: vec![host_file(
|
||||
"app.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\n[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nsecret = \"api_key\"\n",
|
||||
echo.url()
|
||||
),
|
||||
)],
|
||||
// A newline inside the value would split the header: refused.
|
||||
secrets: vec![(
|
||||
"api_key".into(),
|
||||
format!("{secret}\ninjected: yes\n").into_bytes(),
|
||||
)],
|
||||
log_level: "trace",
|
||||
..Default::default()
|
||||
});
|
||||
let status = wait_exit(&mut edge);
|
||||
assert!(
|
||||
!status.success(),
|
||||
"edge must refuse a secret that is not a valid header value"
|
||||
);
|
||||
assert_clean("stderr", edge.stderr().as_bytes(), &secret);
|
||||
}
|
||||
|
||||
// Give the access log a moment to flush, then check every output channel.
|
||||
tokio::time::sleep(std::time::Duration::from_millis(300)).await;
|
||||
let access = edge.access_log();
|
||||
assert!(
|
||||
access.lines().count() >= 7,
|
||||
"access log has the requests:\n{access}"
|
||||
);
|
||||
assert!(
|
||||
access.contains("\"127.0.0.0\""),
|
||||
"IPv4 truncated to /24:\n{access}"
|
||||
);
|
||||
assert!(
|
||||
!access.contains("127.0.0.1"),
|
||||
"full client IP must not be logged:\n{access}"
|
||||
);
|
||||
assert!(
|
||||
!access.contains("token=abc"),
|
||||
"query strings are not logged:\n{access}"
|
||||
);
|
||||
let stderr = edge.stderr();
|
||||
assert!(
|
||||
stderr.len() > 100,
|
||||
"trace logging produced output:\n{stderr}"
|
||||
);
|
||||
assert_clean("stderr (trace level)", stderr.as_bytes(), &secret);
|
||||
assert_clean("access log", access.as_bytes(), &secret);
|
||||
assert_clean("compiled edge config", edge.config_text.as_bytes(), &secret);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn missing_secret_stops_startup_without_leaking_others() {
|
||||
let secret = unique_secret();
|
||||
let echo = Echo::start("app").await;
|
||||
let mut edge = spawn(EdgeSpec {
|
||||
site: "secrets = [\"present\", \"absent\"]\n",
|
||||
hosts: vec![host_file(
|
||||
"app.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\n[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nsecret = \"present\"\n\
|
||||
[[request_headers]]\naction = \"set\"\nname = \"X-B\"\nsecret = \"absent\"\n",
|
||||
echo.url()
|
||||
),
|
||||
)],
|
||||
secrets: vec![("present".into(), secret.clone().into_bytes())],
|
||||
log_level: "trace",
|
||||
..Default::default()
|
||||
});
|
||||
let status = wait_exit(&mut edge);
|
||||
assert!(!status.success(), "edge must refuse to start");
|
||||
let err = edge.stderr();
|
||||
assert!(err.contains("absent"), "names the missing secret:\n{err}");
|
||||
assert_clean("stderr", err.as_bytes(), &secret);
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn malformed_secret_is_rejected_without_echoing_it() {
|
||||
let secret = unique_secret();
|
||||
let echo = Echo::start("app").await;
|
||||
let mut edge = spawn(EdgeSpec {
|
||||
site: "secrets = [\"api_key\"]\n",
|
||||
hosts: vec![host_file(
|
||||
"app.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\n[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nsecret = \"api_key\"\n",
|
||||
echo.url()
|
||||
),
|
||||
)],
|
||||
// A newline inside the value would split the header: refused.
|
||||
secrets: vec![(
|
||||
"api_key".into(),
|
||||
format!("{secret}\ninjected: yes\n").into_bytes(),
|
||||
)],
|
||||
log_level: "trace",
|
||||
..Default::default()
|
||||
});
|
||||
let status = wait_exit(&mut edge);
|
||||
assert!(
|
||||
!status.success(),
|
||||
"edge must refuse a secret that is not a valid header value"
|
||||
);
|
||||
assert_clean("stderr", edge.stderr().as_bytes(), &secret);
|
||||
}
|
||||
|
||||
fn wait_exit(edge: &mut common::Edge) -> std::process::ExitStatus {
|
||||
for _ in 0..200 {
|
||||
if let Some(s) = edge.exited() {
|
||||
return s;
|
||||
fn wait_exit(edge: &mut crate::common::Edge) -> std::process::ExitStatus {
|
||||
for _ in 0..200 {
|
||||
if let Some(s) = edge.exited() {
|
||||
return s;
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis(50));
|
||||
panic!("edge kept running:\n{}", edge.stderr());
|
||||
}
|
||||
panic!("edge kept running:\n{}", edge.stderr());
|
||||
}
|
||||
|
||||
+177
-164
@@ -5,184 +5,197 @@
|
||||
//! Upstream handling: name resolution at connect time, health checks that survive odd
|
||||
//! answers, websocket refusal on every Upgrade token, absolute-form requests.
|
||||
|
||||
#![cfg(unix)]
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
#[cfg(unix)]
|
||||
mod common;
|
||||
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use common::{EdgeSpec, host_file, start};
|
||||
use ianitor_testkit::client::{self, Req};
|
||||
use ianitor_testkit::raw::{RawServer, Reply, response};
|
||||
use ianitor_testkit::upstream::Echo;
|
||||
|
||||
/// QC M1: `localhost` may resolve to ::1 first while the service listens on 127.0.0.1 only.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn upstream_names_are_resolved_at_connect_time_trying_every_address() {
|
||||
let echo = Echo::start("app").await;
|
||||
let edge = start(EdgeSpec {
|
||||
hosts: vec![
|
||||
host_file(
|
||||
"local.test",
|
||||
&format!("upstreams = [\"http://localhost:{}\"]\n", echo.addr.port()),
|
||||
),
|
||||
host_file(
|
||||
"gone.test",
|
||||
"upstreams = [\"http://no-such-host.invalid:8080\"]\n",
|
||||
),
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"local.test",
|
||||
edge.roots(),
|
||||
&Req::get("local.test", "/"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 200, "{}", edge.stderr());
|
||||
// An upstream that does not resolve is a 502 for that host, not a startup failure.
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"gone.test",
|
||||
edge.roots(),
|
||||
&Req::get("gone.test", "/"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 502);
|
||||
#[cfg(not(unix))]
|
||||
#[test]
|
||||
fn skipped_on_windows() {
|
||||
eprintln!(
|
||||
"SKIPPED on Windows: {} needs Linux (Pingora runner, Caddy, Pebble); run it in WSL2, see CLAUDE.md",
|
||||
file!()
|
||||
);
|
||||
}
|
||||
|
||||
/// QC B2a: a health endpoint that sends a 1xx must not kill health checking.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn health_checks_survive_informational_answers() {
|
||||
let started = Instant::now();
|
||||
let up = RawServer::start(move |req| {
|
||||
if req.path == "/healthz" && started.elapsed() < Duration::from_secs(3) {
|
||||
let mut b = b"HTTP/1.1 102 Processing\r\n\r\n".to_vec();
|
||||
b.extend(response(200, "OK", &[], b"ok"));
|
||||
Reply::Bytes(b)
|
||||
} else {
|
||||
Reply::Bytes(response(500, "Internal Server Error", &[], b"broken"))
|
||||
}
|
||||
})
|
||||
.await;
|
||||
let edge = start(EdgeSpec {
|
||||
hosts: vec![host_file(
|
||||
#[cfg(unix)]
|
||||
mod unix {
|
||||
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use crate::common::{EdgeSpec, host_file, start};
|
||||
use ianitor_testkit::client::{self, Req};
|
||||
use ianitor_testkit::raw::{RawServer, Reply, response};
|
||||
use ianitor_testkit::upstream::Echo;
|
||||
|
||||
/// QC M1: `localhost` may resolve to ::1 first while the service listens on 127.0.0.1 only.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn upstream_names_are_resolved_at_connect_time_trying_every_address() {
|
||||
let echo = Echo::start("app").await;
|
||||
let edge = start(EdgeSpec {
|
||||
hosts: vec![
|
||||
host_file(
|
||||
"local.test",
|
||||
&format!("upstreams = [\"http://localhost:{}\"]\n", echo.addr.port()),
|
||||
),
|
||||
host_file(
|
||||
"gone.test",
|
||||
"upstreams = [\"http://no-such-host.invalid:8080\"]\n",
|
||||
),
|
||||
],
|
||||
..Default::default()
|
||||
});
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"local.test",
|
||||
edge.roots(),
|
||||
&Req::get("local.test", "/"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 200, "{}", edge.stderr());
|
||||
// An upstream that does not resolve is a 502 for that host, not a startup failure.
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"gone.test",
|
||||
edge.roots(),
|
||||
&Req::get("gone.test", "/"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 502);
|
||||
}
|
||||
|
||||
/// QC B2a: a health endpoint that sends a 1xx must not kill health checking.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn health_checks_survive_informational_answers() {
|
||||
let started = Instant::now();
|
||||
let up = RawServer::start(move |req| {
|
||||
if req.path == "/healthz" && started.elapsed() < Duration::from_secs(3) {
|
||||
let mut b = b"HTTP/1.1 102 Processing\r\n\r\n".to_vec();
|
||||
b.extend(response(200, "OK", &[], b"ok"));
|
||||
Reply::Bytes(b)
|
||||
} else {
|
||||
Reply::Bytes(response(500, "Internal Server Error", &[], b"broken"))
|
||||
}
|
||||
})
|
||||
.await;
|
||||
let edge = start(EdgeSpec {
|
||||
hosts: vec![host_file(
|
||||
"hc.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\n[health_check]\npath = \"/healthz\"\ninterval_secs = 1\n",
|
||||
up.url()
|
||||
),
|
||||
)],
|
||||
..Default::default()
|
||||
});
|
||||
tokio::time::sleep(Duration::from_millis(5500)).await;
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"hc.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]\n[health_check]\npath = \"/healthz\"\ninterval_secs = 1\n",
|
||||
up.url()
|
||||
),
|
||||
)],
|
||||
..Default::default()
|
||||
});
|
||||
tokio::time::sleep(Duration::from_millis(5500)).await;
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"hc.test",
|
||||
edge.roots(),
|
||||
&Req::get("hc.test", "/"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
r.status,
|
||||
503,
|
||||
"health checks kept running and marked the upstream down\n{}",
|
||||
edge.stderr()
|
||||
);
|
||||
assert!(
|
||||
up.requests()
|
||||
.iter()
|
||||
.filter(|r| r.path == "/healthz")
|
||||
.count()
|
||||
>= 4
|
||||
);
|
||||
}
|
||||
edge.roots(),
|
||||
&Req::get("hc.test", "/"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
r.status,
|
||||
503,
|
||||
"health checks kept running and marked the upstream down\n{}",
|
||||
edge.stderr()
|
||||
);
|
||||
assert!(
|
||||
up.requests()
|
||||
.iter()
|
||||
.filter(|r| r.path == "/healthz")
|
||||
.count()
|
||||
>= 4
|
||||
);
|
||||
}
|
||||
|
||||
/// QC m4: every Upgrade header and every comma-separated token counts.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn websocket_refusal_checks_every_upgrade_token() {
|
||||
let echo = Echo::start("app").await;
|
||||
let edge = start(EdgeSpec {
|
||||
hosts: vec![host_file(
|
||||
"nows.test",
|
||||
&format!("upstreams = [\"{}\"]\nwebsockets = false\n", echo.url()),
|
||||
)],
|
||||
..Default::default()
|
||||
});
|
||||
let cases = [
|
||||
Req::get("nows.test", "/")
|
||||
.header("connection", "upgrade")
|
||||
.header("upgrade", "h2c")
|
||||
.header("upgrade", "websocket"),
|
||||
Req::get("nows.test", "/")
|
||||
.header("connection", "upgrade")
|
||||
.header("upgrade", "websocket, h2c"),
|
||||
Req::get("nows.test", "/")
|
||||
.header("connection", "upgrade")
|
||||
.header("upgrade", "h2c , WebSocket"),
|
||||
];
|
||||
for req in &cases {
|
||||
let r = client::https(edge.https, "nows.test", edge.roots(), req)
|
||||
/// QC m4: every Upgrade header and every comma-separated token counts.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn websocket_refusal_checks_every_upgrade_token() {
|
||||
let echo = Echo::start("app").await;
|
||||
let edge = start(EdgeSpec {
|
||||
hosts: vec![host_file(
|
||||
"nows.test",
|
||||
&format!("upstreams = [\"{}\"]\nwebsockets = false\n", echo.url()),
|
||||
)],
|
||||
..Default::default()
|
||||
});
|
||||
let cases = [
|
||||
Req::get("nows.test", "/")
|
||||
.header("connection", "upgrade")
|
||||
.header("upgrade", "h2c")
|
||||
.header("upgrade", "websocket"),
|
||||
Req::get("nows.test", "/")
|
||||
.header("connection", "upgrade")
|
||||
.header("upgrade", "websocket, h2c"),
|
||||
Req::get("nows.test", "/")
|
||||
.header("connection", "upgrade")
|
||||
.header("upgrade", "h2c , WebSocket"),
|
||||
];
|
||||
for req in &cases {
|
||||
let r = client::https(edge.https, "nows.test", edge.roots(), req)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 403, "{req:?}");
|
||||
}
|
||||
assert!(echo.proxied().is_empty());
|
||||
}
|
||||
|
||||
/// QC nit: absolute-form request targets are answered, never dropped. Pingora refuses a target
|
||||
/// whose authority differs from Host (400, on both listeners); a consistent one is routed and
|
||||
/// sent upstream in origin form.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn absolute_form_requests_are_answered() {
|
||||
let echo = Echo::start("app").await;
|
||||
let edge = start(EdgeSpec {
|
||||
hosts: vec![host_file(
|
||||
"plain.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]
|
||||
",
|
||||
echo.url()
|
||||
),
|
||||
)],
|
||||
..Default::default()
|
||||
});
|
||||
let r = client::http(edge.http, &Req::get("plain.test", "http://evil.test/x"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 403, "{req:?}");
|
||||
}
|
||||
assert!(echo.proxied().is_empty());
|
||||
}
|
||||
|
||||
/// QC nit: absolute-form request targets are answered, never dropped. Pingora refuses a target
|
||||
/// whose authority differs from Host (400, on both listeners); a consistent one is routed and
|
||||
/// sent upstream in origin form.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn absolute_form_requests_are_answered() {
|
||||
let echo = Echo::start("app").await;
|
||||
let edge = start(EdgeSpec {
|
||||
hosts: vec![host_file(
|
||||
assert_eq!(r.status, 400, "plain HTTP, Host differs from the target");
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"plain.test",
|
||||
&format!(
|
||||
"upstreams = [\"{}\"]
|
||||
",
|
||||
echo.url()
|
||||
),
|
||||
)],
|
||||
..Default::default()
|
||||
});
|
||||
let r = client::http(edge.http, &Req::get("plain.test", "http://evil.test/x"))
|
||||
edge.roots(),
|
||||
&Req::get("plain.test", "https://evil.test/x"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 400, "plain HTTP, Host differs from the target");
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"plain.test",
|
||||
edge.roots(),
|
||||
&Req::get("plain.test", "https://evil.test/x"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 400, "HTTPS, Host differs from the target");
|
||||
assert!(echo.proxied().is_empty());
|
||||
assert_eq!(r.status, 400, "HTTPS, Host differs from the target");
|
||||
assert!(echo.proxied().is_empty());
|
||||
|
||||
let r = client::http(edge.http, &Req::get("plain.test", "http://plain.test/x?y"))
|
||||
let r = client::http(edge.http, &Req::get("plain.test", "http://plain.test/x?y"))
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 308);
|
||||
assert_eq!(
|
||||
r.header("location"),
|
||||
Some(format!("https://plain.test:{}/x?y", edge.https.port()).as_str())
|
||||
);
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"plain.test",
|
||||
edge.roots(),
|
||||
&Req::get("plain.test", "https://plain.test/abs?q=1"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 308);
|
||||
assert_eq!(
|
||||
r.header("location"),
|
||||
Some(format!("https://plain.test:{}/x?y", edge.https.port()).as_str())
|
||||
);
|
||||
let r = client::https(
|
||||
edge.https,
|
||||
"plain.test",
|
||||
edge.roots(),
|
||||
&Req::get("plain.test", "https://plain.test/abs?q=1"),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(r.status, 200);
|
||||
assert!(r.text().contains("\"path\":\"/abs\""), "{}", r.text());
|
||||
assert_eq!(r.status, 200);
|
||||
assert!(r.text().contains("\"path\":\"/abs\""), "{}", r.text());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user