[edge] Fixes: skip 1xx, enforce auth timeout, resolve upstreams per connect, all Upgrade tokens, strip Remote-*

This commit is contained in:
2026-11-07 15:48:18 +01:00
parent 60aee5d78a
commit a272e476b7
10 changed files with 509 additions and 115 deletions
+25 -8
View File
@@ -40,14 +40,18 @@ pub struct MockAuthelia {
}
fn h(req: &Recorded, name: &str) -> Option<String> {
req.header(name).map(|v| String::from_utf8_lossy(v).into_owned())
req.header(name)
.map(|v| String::from_utf8_lossy(v).into_owned())
}
fn text(status: u16, reason: &str, body: &str) -> Vec<u8> {
response(
status,
reason,
&[("Content-Type", "text/plain; charset=utf-8"), ("X-Sent-Content-Type", "text/plain; charset=utf-8")],
&[
("Content-Type", "text/plain; charset=utf-8"),
("X-Sent-Content-Type", "text/plain; charset=utf-8"),
],
body.as_bytes(),
)
}
@@ -64,9 +68,12 @@ fn decide(req: &Recorded) -> Reply {
if req.method != "GET" || req.path != PATH {
return Reply::Bytes(text(404, "Not Found", "404 page not found"));
}
let (Some(method), Some(proto), Some(host), Some(uri)) =
(h(req, "x-forwarded-method"), h(req, "x-forwarded-proto"), h(req, "x-forwarded-host"), h(req, "x-forwarded-uri"))
else {
let (Some(method), Some(proto), Some(host), Some(uri)) = (
h(req, "x-forwarded-method"),
h(req, "x-forwarded-proto"),
h(req, "x-forwarded-host"),
h(req, "x-forwarded-uri"),
) else {
return Reply::Bytes(text(400, "Bad Request", "missing X-Forwarded headers"));
};
if let Some(auth) = h(req, "authorization") {
@@ -76,13 +83,20 @@ fn decide(req: &Recorded) -> Reply {
response(
401,
"Unauthorized",
&[("WWW-Authenticate", "Basic realm=\"Authorization Required\""), ("X-Sent-Content-Type", "none")],
&[
("WWW-Authenticate", "Basic realm=\"Authorization Required\""),
("X-Sent-Content-Type", "none"),
],
b"401 Unauthorized",
)
});
}
let cookie = h(req, "cookie").unwrap_or_default();
let session = cookie.split(';').filter_map(|c| c.trim().strip_prefix("authelia_session=")).next().unwrap_or("");
let session = cookie
.split(';')
.filter_map(|c| c.trim().strip_prefix("authelia_session="))
.next()
.unwrap_or("");
Reply::Bytes(match session {
"good" => allowed(true),
"nameless" => allowed(false),
@@ -126,7 +140,10 @@ fn decide(req: &Recorded) -> Reply {
impl MockAuthelia {
pub async fn start() -> MockAuthelia {
let server = RawServer::start(decide).await;
MockAuthelia { addr: server.addr, server }
MockAuthelia {
addr: server.addr,
server,
}
}
/// The `[forward_auth] url` for the site file.
+17 -4
View File
@@ -27,7 +27,10 @@ pub fn response(status: u16, reason: &str, headers: &[(&str, &str)], body: &[u8]
for (n, v) in headers {
out.push_str(&format!("{n}: {v}\r\n"));
}
out.push_str(&format!("Content-Length: {}\r\nConnection: close\r\n\r\n", body.len()));
out.push_str(&format!(
"Content-Length: {}\r\nConnection: close\r\n\r\n",
body.len()
));
let mut bytes = out.into_bytes();
bytes.extend_from_slice(body);
bytes
@@ -44,12 +47,17 @@ pub struct RawServer {
impl RawServer {
pub async fn start(handler: impl Fn(&Recorded) -> Reply + Send + Sync + 'static) -> RawServer {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let server = RawServer { addr: listener.local_addr().unwrap(), requests: Arc::default() };
let server = RawServer {
addr: listener.local_addr().unwrap(),
requests: Arc::default(),
};
let handler: Arc<Handler> = Arc::new(handler);
let requests = server.requests.clone();
tokio::spawn(async move {
loop {
let Ok((mut stream, _)) = listener.accept().await else { continue };
let Ok((mut stream, _)) = listener.accept().await else {
continue;
};
let handler = handler.clone();
let requests = requests.clone();
tokio::spawn(async move {
@@ -97,5 +105,10 @@ fn parse(buf: &[u8]) -> Option<Recorded> {
.filter_map(|l| l.split_once(':'))
.map(|(n, v)| (n.trim().to_ascii_lowercase(), v.trim().as_bytes().to_vec()))
.collect();
Some(Recorded { method, path, headers, body: Vec::new() })
Some(Recorded {
method,
path,
headers,
body: Vec::new(),
})
}