117 lines
4.4 KiB
Rust
117 lines
4.4 KiB
Rust
// 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 compatible with Authelia's ForwardAuth implementation
|
|
//! (`GET /api/authz/forward-auth`) and with Caddy's `forward_auth` directive:
|
|
//!
|
|
//! - The subrequest is a `GET` to the configured path, carrying the client's request headers
|
|
//! (minus hop-by-hop and framing headers, never a body) plus `X-Forwarded-Method`,
|
|
//! `X-Forwarded-Proto`, `X-Forwarded-Host`, `X-Forwarded-Uri` and `X-Forwarded-For`, which
|
|
//! edge always sets itself (client-supplied values are replaced).
|
|
//! - A 2xx answer allows the request: each configured identity header is removed from the
|
|
//! client request and, when the auth answer has a non-empty value, set from it.
|
|
//! - Any other answer is returned to the client as it is (status, headers, body), and the
|
|
//! request never reaches the upstream.
|
|
//! - If the auth server cannot be reached, does not answer completely within 10 s, answers
|
|
//! with more than 1 MiB of body, a broken body or `101`, the request fails closed with 502.
|
|
//! Informational answers (100, 102, 103) before the real answer are skipped.
|
|
//! - Client-sent identity headers (Authelia's four plus the copy headers) are removed first,
|
|
//! so neither the auth server nor the upstream ever sees them.
|
|
|
|
use std::time::Duration;
|
|
|
|
use bytes::Bytes;
|
|
use http::header::{self, HeaderName, HeaderValue};
|
|
use pingora::connectors::http::Connector;
|
|
use pingora::http::{RequestHeader, ResponseHeader};
|
|
use pingora::proxy::Session;
|
|
use pingora::{Error, ErrorType, Result};
|
|
|
|
use crate::fetch::fetch;
|
|
use crate::runtime::ForwardAuthRt;
|
|
|
|
const MAX_AUTH_BODY: usize = 1 << 20;
|
|
const AUTH_TIMEOUT: Duration = Duration::from_secs(10);
|
|
|
|
pub enum Outcome {
|
|
Allow(Vec<(HeaderName, Option<HeaderValue>)>),
|
|
Deny(Box<ResponseHeader>, Bytes),
|
|
}
|
|
|
|
/// Request headers never copied to a subrequest or a passed-through response.
|
|
pub fn hop_by_hop(name: &HeaderName) -> bool {
|
|
matches!(
|
|
name.as_str(),
|
|
"connection"
|
|
| "keep-alive"
|
|
| "proxy-connection"
|
|
| "te"
|
|
| "trailer"
|
|
| "transfer-encoding"
|
|
| "upgrade"
|
|
| "content-length"
|
|
| "proxy-authenticate"
|
|
| "proxy-authorization"
|
|
)
|
|
}
|
|
|
|
fn forwarded(name: &HeaderName) -> bool {
|
|
name.as_str().starts_with("x-forwarded-")
|
|
}
|
|
|
|
pub async fn check(
|
|
connector: &Connector,
|
|
fa: &ForwardAuthRt,
|
|
session: &Session,
|
|
host_header: &str,
|
|
client_ip: Option<String>,
|
|
) -> Result<Outcome> {
|
|
let req = session.req_header();
|
|
let mut sub = RequestHeader::build("GET", fa.path_and_query.as_bytes(), None)?;
|
|
for (name, value) in req.headers.iter() {
|
|
if !hop_by_hop(name) && !forwarded(name) && name != header::HOST {
|
|
sub.append_header(name.clone(), value.clone())?;
|
|
}
|
|
}
|
|
let uri = req.uri.path_and_query().map(|p| p.as_str()).unwrap_or("/");
|
|
sub.insert_header(header::HOST, host_header)?;
|
|
sub.insert_header("X-Forwarded-Method", req.method.as_str())?;
|
|
sub.insert_header("X-Forwarded-Proto", "https")?;
|
|
sub.insert_header("X-Forwarded-Host", host_header)?;
|
|
sub.insert_header("X-Forwarded-Uri", uri)?;
|
|
if let Some(ip) = client_ip {
|
|
sub.insert_header("X-Forwarded-For", ip)?;
|
|
}
|
|
|
|
let got = fetch(connector, &fa.upstream, sub, MAX_AUTH_BODY, AUTH_TIMEOUT).await?;
|
|
let status = got.header.status.as_u16();
|
|
if status < 200 {
|
|
// 101 (or any 1xx left over) is not an answer we can pass on or treat as an allow.
|
|
return Error::e_explain(
|
|
ErrorType::InvalidHTTPHeader,
|
|
format!("auth server answered {status}"),
|
|
);
|
|
}
|
|
if (200..300).contains(&status) {
|
|
let copy = fa
|
|
.copy_headers
|
|
.iter()
|
|
.map(|n| {
|
|
let v = got.header.headers.get(n).filter(|v| !v.is_empty()).cloned();
|
|
(n.clone(), v)
|
|
})
|
|
.collect();
|
|
return Ok(Outcome::Allow(copy));
|
|
}
|
|
|
|
let mut resp = ResponseHeader::build(got.header.status, None)?;
|
|
for (name, value) in got.header.headers.iter() {
|
|
if !hop_by_hop(name) {
|
|
resp.append_header(name.clone(), value.clone())?;
|
|
}
|
|
}
|
|
resp.insert_header(header::CONTENT_LENGTH, got.body.len().to_string())?;
|
|
Ok(Outcome::Deny(Box::new(resp), got.body))
|
|
}
|