diff --git a/crates/core/tests/compile.rs b/crates/core/tests/compile.rs index 742ba98..d67500c 100644 --- a/crates/core/tests/compile.rs +++ b/crates/core/tests/compile.rs @@ -217,3 +217,32 @@ fn caddy_output_matches_golden_file() { &compile::caddy(&model(false), &options()).unwrap(), ); } + +#[test] +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}"); + } + 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 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!(deleted.contains(&h), "caddy deletes {h}: {deleted:?}"); + } + 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"); +} diff --git a/crates/core/tests/schema.rs b/crates/core/tests/schema.rs index 4b7a101..5469601 100644 --- a/crates/core/tests/schema.rs +++ b/crates/core/tests/schema.rs @@ -148,11 +148,11 @@ value = "max-age=31536000" #[test] fn hosts_are_sorted_by_hostname_whatever_the_file_order() { let a = SourceFile { - name: "b.toml", + name: "b.test.toml", contents: "hostname = \"b.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n", }; let b = SourceFile { - name: "a.toml", + name: "a.test.toml", contents: "hostname = \"a.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n", }; let m1 = load(&site(), &[a, b]).unwrap(); @@ -175,7 +175,7 @@ fn empty_site_file_is_allowed() { contents: "", }, &[SourceFile { - name: "a.toml", + name: "app.test.toml", contents: MINIMAL, }], ); @@ -236,7 +236,7 @@ fn rejects_unknown_field_in_site_file() { contents: "secrets = []\nsecret_values = { a = \"b\" }\n", }, &[SourceFile { - name: "a.toml", + name: "app.test.toml", contents: MINIMAL, }], ); @@ -334,9 +334,9 @@ fn accepts_good_hostnames() { "x-1.example.org", "0day.example.org", ] { - let r = one(&format!( - "hostname = {good:?}\nupstreams = [\"http://127.0.0.1:1\"]\n" - )); + 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 }]); assert!(r.is_ok(), "{good}: {r:?}"); } } @@ -347,11 +347,11 @@ fn rejects_duplicate_hostnames_across_files() { &site(), &[ SourceFile { - name: "hosts/one.toml", + name: "hosts/app.test.toml", contents: MINIMAL, }, SourceFile { - name: "hosts/two.toml", + name: "other/app.test.toml", contents: MINIMAL, }, ], @@ -361,8 +361,8 @@ fn rejects_duplicate_hostnames_across_files() { errs.errors(), [ValidationError::DuplicateHostname { hostname: "app.test".into(), - first: "hosts/one.toml".into(), - second: "hosts/two.toml".into() + first: "hosts/app.test.toml".into(), + second: "other/app.test.toml".into() }] ); } @@ -373,18 +373,18 @@ fn duplicate_detection_is_independent_of_file_order() { &site(), &[ SourceFile { - name: "hosts/two.toml", + name: "other/app.test.toml", contents: MINIMAL, }, SourceFile { - name: "hosts/one.toml", + name: "hosts/app.test.toml", contents: MINIMAL, }, ], ); let errs = r.unwrap_err(); assert!( - matches!(&errs.errors()[0], ValidationError::DuplicateHostname { first, second, .. } if first == "hosts/one.toml" && second == "hosts/two.toml") + matches!(&errs.errors()[0], ValidationError::DuplicateHostname { first, second, .. } if first == "hosts/app.test.toml" && second == "other/app.test.toml") ); } @@ -394,11 +394,11 @@ fn rejects_duplicate_source_file_names() { &site(), &[ SourceFile { - name: "a.toml", + name: "app.test.toml", contents: MINIMAL, }, SourceFile { - name: "a.toml", + name: "app.test.toml", contents: MINIMAL, }, ], @@ -492,7 +492,7 @@ fn undefined_secret_is_reported_when_site_declares_none() { contents: "", }, &[SourceFile { - name: "a.toml", + name: "app.test.toml", contents: &format!( "{MINIMAL}[[request_headers]]\naction = \"set\"\nname = \"X-K\"\nsecret = \"api_key\"\n" ), @@ -526,7 +526,7 @@ fn rejects_bad_secret_names_in_site_file() { contents: &format!("secrets = [{bad:?}]\n"), }, &[SourceFile { - name: "a.toml", + name: "app.test.toml", contents: MINIMAL, }], ); @@ -672,7 +672,7 @@ fn forward_auth_requires_site_configuration() { contents: "", }, &[SourceFile { - name: "a.toml", + name: "app.test.toml", contents: &format!("{MINIMAL}auth = \"forward-auth\"\n"), }], ); @@ -698,7 +698,7 @@ fn rejects_bad_forward_auth_site_config() { contents: &format!("[forward_auth]\nurl = {bad:?}\n"), }, &[SourceFile { - name: "a.toml", + name: "app.test.toml", contents: MINIMAL, }], ); @@ -737,15 +737,15 @@ fn collects_errors_from_every_file() { &site(), &[ SourceFile { - name: "a.toml", + name: "a.test.toml", contents: "hostname = \"a.test\"\nupstreams = [\"ftp://x\"]\n", }, SourceFile { - name: "b.toml", + name: "B.test.toml", contents: "hostname = \"B.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n", }, SourceFile { - name: "c.toml", + name: "app.test.toml", contents: MINIMAL, }, ], @@ -780,3 +780,93 @@ fn never_panics_on_garbage() { assert!(one(s).is_err(), "{s:?} unexpectedly valid"); } } + +// ---------------------------------------------------------------- QC findings + +#[test] +fn file_name_must_match_the_hostname() { + let r = load(&site(), &[SourceFile { name: "hosts/other.test.toml", contents: MINIMAL }]); + let errs = r.unwrap_err(); + assert_eq!( + errs.errors(), + [ValidationError::FileNameMismatch { + file: "hosts/other.test.toml".into(), + hostname: "app.test".into() + }] + ); +} + +#[test] +fn rejects_wildcards_in_header_names() { + // Caddy treats `*` in header names as a wildcard; edge does not. Never allowed. + for list in ["request_headers", "response_headers"] { + for bad in ["*", "X-*", "*-Id", "X-*-Y"] { + let e = single_error(&rule(list, &format!("action = \"remove\"\nname = {bad:?}"))); + assert!( + matches!(e, ValidationError::InvalidHeaderName { ref name, .. } if name == bad), + "{list} {bad}: {e:?}" + ); + } + } +} + +#[test] +fn rejects_wildcards_in_copy_headers() { + let r = load( + &SourceFile { + 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 }], + ); + 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")); + assert!(matches!(e, ValidationError::BadHealthCheck { .. }), "{e:?}"); +} + +#[test] +fn rejects_oversized_route_files() { + let big = format!("{MINIMAL}# {}\n", "x".repeat(1 << 20)); + 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 }], + ); + 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 = (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 = (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] +fn identity_headers_stay_protected_with_a_custom_copy_list() { + let site = "[forward_auth]\nurl = \"http://127.0.0.1:9091/x\"\ncopy_headers = [\"X-User\"]\n"; + for header in ["Remote-User", "remote-email", "X-User"] { + let host = format!( + "{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 }], + ); + assert!( + r.unwrap_err().errors().iter().any(|e| matches!(e, ValidationError::ProtectedHeader { .. })), + "{header}" + ); + } +}