// 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/. //! 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. #![cfg(unix)] #![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)] mod common; use common::{host_file, start, EdgeSpec}; use ianitor_testkit::authelia::MockAuthelia; use ianitor_testkit::client::{self, Req, WsOutcome}; use ianitor_testkit::upstream::{closed_port, Echo, WsEcho}; 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()); }