[edge] Tests first: QC repros for 1xx, auth timeout, name resolution, Upgrade tokens, Remote-* stripping (red)

This commit is contained in:
2026-11-07 15:36:00 +01:00
parent fd4b4a7775
commit d42c178a13
6 changed files with 414 additions and 134 deletions
+93
View File
@@ -255,3 +255,96 @@ async fn unreachable_auth_server_fails_closed() {
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());
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}");
}
}
}
+103
View File
@@ -0,0 +1,103 @@
// 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/.
//! 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)]
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);
}
/// 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", 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).await.unwrap();
assert_eq!(r.status, 403, "{req:?}");
}
assert!(echo.proxied().is_empty());
}
/// QC nit: absolute-form request targets are answered and routed by the URI's host
/// (RFC 9112 §3.2.2), as Go/Caddy do.
#[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 = [\"{}\"]\n", echo.url()))],
..Default::default()
});
let r = client::http(edge.http, &Req::get("plain.test", "http://evil.test/x")).await.unwrap();
assert_eq!(r.status, 404, "plain HTTP");
let r = client::https(edge.https, "plain.test", edge.roots(), &Req::get("plain.test", "https://evil.test/x"))
.await
.unwrap();
assert_eq!(r.status, 404, "HTTPS");
let r = client::http(edge.http, &Req::get("evil.test", "http://plain.test/x?y")).await.unwrap();
assert_eq!(r.status, 308);
assert!(echo.proxied().is_empty());
}