This commit is contained in:
Mateusz Gruszczyński committed 2026-10-04 11:15:58 +02:00
1 parent 87e571c170
commit fa7abc9e53
308 files changed
+49746 -50237

No files matched your search

+121
View File
@@ -0,0 +1,121 @@
mod tests {
use super::*;
fn settings() -> Settings {
Settings {
enabled: true,
url: "http://127.0.0.1:9300".into(),
login_url: "https://auth.example/login".into(),
allow_private: true,
..Default::default()
}
}
fn compiled() -> Compiled {
settings()
.compile(
&ValidationPolicy {
proxy_addr: "127.0.0.1:8080".parse().unwrap(),
admin_addr: "127.0.0.1:9090".parse().unwrap(),
tls_addr: None,
cert_dir: None,
data_dir: None,
},
&[],
)
.unwrap()
.unwrap()
}
#[test]
fn origin_adds_nginx_endpoint() {
assert_eq!(
compiled().uri.to_string(),
"http://127.0.0.1:9300/api/auth/nginx"
);
}
#[test]
fn rejects_transport_header_injection_and_unsafe_urls() {
for name in [
"host",
"authorization",
"cookie",
"content-length",
"x-forwarded-for",
"x-real-ip",
] {
let mut s = settings();
s.response_headers = vec![name.into()];
assert!(s.validate().is_err(), "{name}");
}
let mut s = settings();
s.url = "http://user:secret@host/".into();
assert!(s.validate().is_err());
s = settings();
s.login_url = "http://auth.example/login".into();
assert!(s.validate().is_err());
}
#[test]
fn redirect_preserves_original_query_as_one_encoded_parameter() {
let c = compiled();
let location = c
.redirect(None, "https://app.example/a?x=1&next=%2Fa", true)
.unwrap();
let url = Url::parse(location.to_str().unwrap()).unwrap();
assert_eq!(
url.query_pairs()
.find(|(k, _)| k == "redirect_uri")
.unwrap()
.1,
"https://app.example/a?x=1&next=%2Fa"
);
}
#[test]
fn provider_redirects_are_restricted_to_configured_login_origin() {
let c = compiled();
for value in [
"https://evil.example/",
"//evil.example/",
"javascript:alert(1)",
"https://auth.example@evil.example/",
] {
assert!(c
.redirect(Some(&value.parse().unwrap()), "https://app.example/", false)
.is_none());
}
assert_eq!(
c.redirect(Some(&"/unauthorized".parse().unwrap()), "", false)
.unwrap(),
"https://auth.example/unauthorized"
);
}
#[test]
fn identity_removes_unselected_remote_headers_too() {
let c = compiled();
let mut h = HeaderMap::new();
for name in [
"remote-user",
"remote-email",
"remote-groups",
"remote-name",
] {
h.insert(name, "forged".parse().unwrap());
}
c.strip_identity(&mut h);
assert!(h.is_empty());
}
#[test]
fn same_port_different_remote_auth_is_allowed_but_self_loop_is_not() {
let mut s = settings();
s.url = "http://127.0.0.1:8080".into();
assert!(s
.compile(
&ValidationPolicy {
proxy_addr: "127.0.0.1:8080".parse().unwrap(),
admin_addr: "127.0.0.1:9090".parse().unwrap(),
tls_addr: None,
cert_dir: None,
data_dir: None,
},
&[]
)
.is_err());
}
}