[core] Schema: reject wildcard header names, cap file size and upstreams, require <hostname>.toml

This commit is contained in:
2026-11-07 15:44:09 +01:00
parent 58d0397875
commit 60aee5d78a
11 changed files with 221 additions and 59 deletions
+41 -10
View File
@@ -223,26 +223,57 @@ fn forward_auth_hosts_strip_client_identity_headers_in_both_engines() {
let m = model(false);
let edge: EdgeConfig = serde_json::from_str(&compile::edge(&m, &options()).unwrap()).unwrap();
let strip = edge.forward_auth.unwrap().strip_headers;
for h in ["Remote-User", "Remote-Groups", "Remote-Email", "Remote-Name"] {
assert!(strip.iter().any(|s| s.eq_ignore_ascii_case(h)), "edge strips {h}");
for h in [
"Remote-User",
"Remote-Groups",
"Remote-Email",
"Remote-Name",
] {
assert!(
strip.iter().any(|s| s.eq_ignore_ascii_case(h)),
"edge strips {h}"
);
}
let caddy = compile::caddy(&m, &options()).unwrap();
let v: serde_json::Value = serde_json::from_str(&caddy).unwrap();
let routes = v["apps"]["http"]["servers"]["ianitor_https"]["routes"].as_array().unwrap();
let wiki = routes.iter().find(|r| r["match"][0]["host"][0] == "wiki.home.test").unwrap();
let routes = v["apps"]["http"]["servers"]["ianitor_https"]["routes"]
.as_array()
.unwrap();
let wiki = routes
.iter()
.find(|r| r["match"][0]["host"][0] == "wiki.home.test")
.unwrap();
let steps = wiki["handle"][0]["routes"].as_array().unwrap();
let first = &steps[0]["handle"][0];
assert_eq!(first["handler"], "headers", "identity headers are deleted before forward_auth");
let deleted: Vec<&str> =
first["request"]["delete"].as_array().unwrap().iter().map(|d| d.as_str().unwrap()).collect();
for h in ["Remote-User", "Remote-Groups", "Remote-Email", "Remote-Name"] {
assert_eq!(
first["handler"], "headers",
"identity headers are deleted before forward_auth"
);
let deleted: Vec<&str> = first["request"]["delete"]
.as_array()
.unwrap()
.iter()
.map(|d| d.as_str().unwrap())
.collect();
for h in [
"Remote-User",
"Remote-Groups",
"Remote-Email",
"Remote-Name",
] {
assert!(deleted.contains(&h), "caddy deletes {h}: {deleted:?}");
}
assert_eq!(steps[1]["handle"][0]["handler"], "reverse_proxy", "then forward_auth");
assert_eq!(
steps[1]["handle"][0]["handler"], "reverse_proxy",
"then forward_auth"
);
}
#[test]
fn websocket_refusal_in_caddy_matches_any_upgrade_token() {
let caddy = compile::caddy(&model(false), &options()).unwrap();
assert!(caddy.contains(r#"(?i)(^|,)\\s*websocket\\s*(,|$)"#), "token-aware Upgrade matcher");
assert!(
caddy.contains(r#"(?i)(^|,)\\s*websocket\\s*(,|$)"#),
"token-aware Upgrade matcher"
);
}
+16 -1
View File
@@ -179,6 +179,21 @@
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "headers",
"request": {
"delete": [
"Remote-User",
"Remote-Groups",
"Remote-Email",
"Remote-Name"
]
}
}
]
},
{
"handle": [
{
@@ -392,7 +407,7 @@
{
"header_regexp": {
"Upgrade": {
"pattern": "(?i)^websocket$"
"pattern": "(?i)(^|,)\\s*websocket\\s*(,|$)"
}
}
}
+6
View File
@@ -22,6 +22,12 @@
"Remote-Groups",
"Remote-Email",
"Remote-Name"
],
"strip_headers": [
"Remote-User",
"Remote-Groups",
"Remote-Email",
"Remote-Name"
]
},
"hosts": [
+71 -20
View File
@@ -336,7 +336,13 @@ fn accepts_good_hostnames() {
] {
let contents = format!("hostname = {good:?}\nupstreams = [\"http://127.0.0.1:1\"]\n");
let name = format!("hosts/{good}.toml");
let r = load(&site(), &[SourceFile { name: &name, contents: &contents }]);
let r = load(
&site(),
&[SourceFile {
name: &name,
contents: &contents,
}],
);
assert!(r.is_ok(), "{good}: {r:?}");
}
}
@@ -785,7 +791,13 @@ fn never_panics_on_garbage() {
#[test]
fn file_name_must_match_the_hostname() {
let r = load(&site(), &[SourceFile { name: "hosts/other.test.toml", contents: MINIMAL }]);
let r = load(
&site(),
&[SourceFile {
name: "hosts/other.test.toml",
contents: MINIMAL,
}],
);
let errs = r.unwrap_err();
assert_eq!(
errs.errors(),
@@ -817,14 +829,24 @@ fn rejects_wildcards_in_copy_headers() {
name: "ianitor.toml",
contents: "[forward_auth]\nurl = \"http://127.0.0.1:9091/x\"\ncopy_headers = [\"Remote-*\"]\n",
},
&[SourceFile { name: "app.test.toml", contents: MINIMAL }],
&[SourceFile {
name: "app.test.toml",
contents: MINIMAL,
}],
);
assert!(
r.unwrap_err()
.errors()
.iter()
.any(|e| matches!(e, ValidationError::InvalidHeaderName { .. }))
);
assert!(r.unwrap_err().errors().iter().any(|e| matches!(e, ValidationError::InvalidHeaderName { .. })));
}
#[test]
fn rejects_fragment_in_health_check_path() {
let e = single_error(&format!("{MINIMAL}[health_check]\npath = \"/h#x\"\ninterval_secs = 5\n"));
let e = single_error(&format!(
"{MINIMAL}[health_check]\npath = \"/h#x\"\ninterval_secs = 5\n"
));
assert!(matches!(e, ValidationError::BadHealthCheck { .. }), "{e:?}");
}
@@ -834,23 +856,43 @@ fn rejects_oversized_route_files() {
let e = single_error(&big);
assert!(matches!(e, ValidationError::FileTooLarge { .. }), "{e:?}");
let r = load(
&SourceFile { name: "ianitor.toml", contents: &big },
&[SourceFile { name: "app.test.toml", contents: MINIMAL }],
&SourceFile {
name: "ianitor.toml",
contents: &big,
},
&[SourceFile {
name: "app.test.toml",
contents: MINIMAL,
}],
);
assert!(r
.unwrap_err()
.errors()
.iter()
.any(|e| matches!(e, ValidationError::FileTooLarge { file, .. } if file == "ianitor.toml")));
assert!(r.unwrap_err().errors().iter().any(
|e| matches!(e, ValidationError::FileTooLarge { file, .. } if file == "ianitor.toml")
));
}
#[test]
fn rejects_too_many_upstreams() {
let ups: Vec<String> = (1..=65).map(|p| format!("\"http://127.0.0.1:{p}\"")).collect();
let e = single_error(&format!("hostname = \"app.test\"\nupstreams = [{}]\n", ups.join(", ")));
assert!(matches!(e, ValidationError::TooManyUpstreams { .. }), "{e:?}");
let ups: Vec<String> = (1..=64).map(|p| format!("\"http://127.0.0.1:{p}\"")).collect();
assert!(one(&format!("hostname = \"app.test\"\nupstreams = [{}]\n", ups.join(", "))).is_ok());
let ups: Vec<String> = (1..=65)
.map(|p| format!("\"http://127.0.0.1:{p}\""))
.collect();
let e = single_error(&format!(
"hostname = \"app.test\"\nupstreams = [{}]\n",
ups.join(", ")
));
assert!(
matches!(e, ValidationError::TooManyUpstreams { .. }),
"{e:?}"
);
let ups: Vec<String> = (1..=64)
.map(|p| format!("\"http://127.0.0.1:{p}\""))
.collect();
assert!(
one(&format!(
"hostname = \"app.test\"\nupstreams = [{}]\n",
ups.join(", ")
))
.is_ok()
);
}
#[test]
@@ -861,11 +903,20 @@ fn identity_headers_stay_protected_with_a_custom_copy_list() {
"{MINIMAL}auth = \"forward-auth\"\n[[request_headers]]\naction = \"set\"\nname = \"{header}\"\nvalue = \"x\"\n"
);
let r = load(
&SourceFile { name: "ianitor.toml", contents: site },
&[SourceFile { name: "hosts/app.test.toml", contents: &host }],
&SourceFile {
name: "ianitor.toml",
contents: site,
},
&[SourceFile {
name: "hosts/app.test.toml",
contents: &host,
}],
);
assert!(
r.unwrap_err().errors().iter().any(|e| matches!(e, ValidationError::ProtectedHeader { .. })),
r.unwrap_err()
.errors()
.iter()
.any(|e| matches!(e, ValidationError::ProtectedHeader { .. })),
"{header}"
);
}