388 lines
14 KiB
Rust
388 lines
14 KiB
Rust
// 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).
|
|
|
|
//! Forward-auth (Authelia-compatible): a denial never reaches the upstream, the auth answer
|
|
//! goes back to the client unchanged, and an allowed request carries the identity headers
|
|
//! from the auth server and never the client's own.
|
|
|
|
#![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, Caddy, Pebble): run it in WSL2, see CLAUDE.md"]
|
|
fn needs_linux() {}
|
|
|
|
#[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()
|
|
),
|
|
)],
|
|
..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()
|
|
);
|
|
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}");
|
|
}
|
|
}
|
|
}
|
|
}
|