[core] Compilers: Pingora runtime config and Caddy JSON, golden files
This commit is contained in:
+352
-76
@@ -8,8 +8,8 @@
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
use ianitor_core::{
|
||||
load, AccessLog, AuthMode, HeaderRule, HeaderValue, SourceFile, TlsMode, ValidationError,
|
||||
ValidationErrors,
|
||||
AccessLog, AuthMode, HeaderRule, HeaderValue, SourceFile, TlsMode, ValidationError,
|
||||
ValidationErrors, load,
|
||||
};
|
||||
|
||||
const SITE: &str = r#"
|
||||
@@ -20,11 +20,20 @@ url = "http://127.0.0.1:9091/api/authz/forward-auth"
|
||||
"#;
|
||||
|
||||
fn site() -> SourceFile<'static> {
|
||||
SourceFile { name: "ianitor.toml", contents: SITE }
|
||||
SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: SITE,
|
||||
}
|
||||
}
|
||||
|
||||
fn one(contents: &str) -> Result<ianitor_core::Model, ValidationErrors> {
|
||||
load(&site(), &[SourceFile { name: "hosts/app.test.toml", contents }])
|
||||
load(
|
||||
&site(),
|
||||
&[SourceFile {
|
||||
name: "hosts/app.test.toml",
|
||||
contents,
|
||||
}],
|
||||
)
|
||||
}
|
||||
|
||||
fn errors(contents: &str) -> Vec<ValidationError> {
|
||||
@@ -68,10 +77,9 @@ fn minimal_host_gets_documented_defaults() {
|
||||
|
||||
#[test]
|
||||
fn full_host_is_accepted() {
|
||||
let model = one(
|
||||
r#"
|
||||
let model = one(r#"
|
||||
hostname = "app.test"
|
||||
upstreams = ["http://127.0.0.1:8080", "https://backend.lan", "http://[::1]:9000/"]
|
||||
upstreams = ["https://127.0.0.1:8443", "https://backend.lan", "https://[::1]:9000/"]
|
||||
tls = "domi"
|
||||
auth = "forward-auth"
|
||||
websockets = false
|
||||
@@ -95,8 +103,7 @@ name = "X-Debug"
|
||||
action = "set"
|
||||
name = "Strict-Transport-Security"
|
||||
value = "max-age=31536000"
|
||||
"#,
|
||||
)
|
||||
"#)
|
||||
.expect("full host is valid");
|
||||
let h = &model.hosts()[0];
|
||||
assert_eq!(h.tls, TlsMode::Domi);
|
||||
@@ -108,12 +115,27 @@ value = "max-age=31536000"
|
||||
assert_eq!(hc.path, "/healthz");
|
||||
assert_eq!(hc.interval_secs, 10);
|
||||
let ups: Vec<String> = h.upstreams.iter().map(|u| u.to_string()).collect();
|
||||
assert_eq!(ups, ["http://127.0.0.1:8080", "https://backend.lan:443", "http://[::1]:9000"]);
|
||||
assert_eq!(
|
||||
ups,
|
||||
[
|
||||
"https://127.0.0.1:8443",
|
||||
"https://backend.lan:443",
|
||||
"https://[::1]:9000"
|
||||
]
|
||||
);
|
||||
assert_eq!(
|
||||
h.request_headers[0],
|
||||
HeaderRule::Set { name: "X-Api-Key".into(), value: HeaderValue::Secret("api_key".into()) }
|
||||
HeaderRule::Set {
|
||||
name: "X-Api-Key".into(),
|
||||
value: HeaderValue::Secret("api_key".into())
|
||||
}
|
||||
);
|
||||
assert_eq!(
|
||||
h.request_headers[1],
|
||||
HeaderRule::Remove {
|
||||
name: "X-Debug".into()
|
||||
}
|
||||
);
|
||||
assert_eq!(h.request_headers[1], HeaderRule::Remove { name: "X-Debug".into() });
|
||||
assert_eq!(
|
||||
h.response_headers[0],
|
||||
HeaderRule::Set {
|
||||
@@ -125,18 +147,38 @@ value = "max-age=31536000"
|
||||
|
||||
#[test]
|
||||
fn hosts_are_sorted_by_hostname_whatever_the_file_order() {
|
||||
let a = SourceFile { name: "b.toml", contents: "hostname = \"b.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n" };
|
||||
let b = SourceFile { name: "a.toml", contents: "hostname = \"a.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n" };
|
||||
let a = SourceFile {
|
||||
name: "b.toml",
|
||||
contents: "hostname = \"b.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
|
||||
};
|
||||
let b = SourceFile {
|
||||
name: "a.toml",
|
||||
contents: "hostname = \"a.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
|
||||
};
|
||||
let m1 = load(&site(), &[a, b]).unwrap();
|
||||
let m2 = load(&site(), &[b, a]).unwrap();
|
||||
let names = |m: &ianitor_core::Model| m.hosts().iter().map(|h| h.hostname.to_string()).collect::<Vec<_>>();
|
||||
let names = |m: &ianitor_core::Model| {
|
||||
m.hosts()
|
||||
.iter()
|
||||
.map(|h| h.hostname.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
assert_eq!(names(&m1), ["a.test", "b.test"]);
|
||||
assert_eq!(m1, m2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_site_file_is_allowed() {
|
||||
let m = load(&SourceFile { name: "ianitor.toml", contents: "" }, &[SourceFile { name: "a.toml", contents: MINIMAL }]);
|
||||
let m = load(
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: "",
|
||||
},
|
||||
&[SourceFile {
|
||||
name: "a.toml",
|
||||
contents: MINIMAL,
|
||||
}],
|
||||
);
|
||||
assert!(m.is_ok(), "{m:?}");
|
||||
}
|
||||
|
||||
@@ -145,20 +187,34 @@ fn empty_site_file_is_allowed() {
|
||||
#[test]
|
||||
fn rejects_unknown_top_level_field() {
|
||||
let e = single_error(&format!("{MINIMAL}colour = \"red\"\n"));
|
||||
assert_eq!(e, ValidationError::UnknownField { file: "hosts/app.test.toml".into(), field: "colour".into() });
|
||||
assert_eq!(
|
||||
e,
|
||||
ValidationError::UnknownField {
|
||||
file: "hosts/app.test.toml".into(),
|
||||
field: "colour".into()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_misspelled_field() {
|
||||
let e = single_error("hostname = \"app.test\"\nupstream = [\"http://127.0.0.1:1\"]\n");
|
||||
// `upstream` is unknown; it is reported before the missing `upstreams`.
|
||||
assert!(matches!(e, ValidationError::UnknownField { ref field, .. } if field == "upstream"), "{e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::UnknownField { ref field, .. } if field == "upstream"),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_unknown_field_in_health_check() {
|
||||
let e = single_error(&format!("{MINIMAL}[health_check]\npath = \"/h\"\ninterval_secs = 5\ntimeout = 3\n"));
|
||||
assert!(matches!(e, ValidationError::UnknownField { ref field, .. } if field == "timeout"), "{e:?}");
|
||||
let e = single_error(&format!(
|
||||
"{MINIMAL}[health_check]\npath = \"/h\"\ninterval_secs = 5\ntimeout = 3\n"
|
||||
));
|
||||
assert!(
|
||||
matches!(e, ValidationError::UnknownField { ref field, .. } if field == "timeout"),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -166,14 +222,23 @@ fn rejects_unknown_field_in_header_rule() {
|
||||
let e = single_error(&format!(
|
||||
"{MINIMAL}[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nvalue = \"1\"\nappend = true\n"
|
||||
));
|
||||
assert!(matches!(e, ValidationError::UnknownField { ref field, .. } if field == "append"), "{e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::UnknownField { ref field, .. } if field == "append"),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_unknown_field_in_site_file() {
|
||||
let r = load(
|
||||
&SourceFile { name: "ianitor.toml", contents: "secrets = []\nsecret_values = { a = \"b\" }\n" },
|
||||
&[SourceFile { name: "a.toml", contents: MINIMAL }],
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: "secrets = []\nsecret_values = { a = \"b\" }\n",
|
||||
},
|
||||
&[SourceFile {
|
||||
name: "a.toml",
|
||||
contents: MINIMAL,
|
||||
}],
|
||||
);
|
||||
let errs = r.unwrap_err();
|
||||
assert!(
|
||||
@@ -184,7 +249,9 @@ fn rejects_unknown_field_in_site_file() {
|
||||
|
||||
#[test]
|
||||
fn rejects_unknown_enum_value() {
|
||||
let e = single_error("hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\ntls = \"selfsigned\"\n");
|
||||
let e = single_error(
|
||||
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:1\"]\ntls = \"selfsigned\"\n",
|
||||
);
|
||||
assert!(matches!(e, ValidationError::InvalidValue { .. }), "{e:?}");
|
||||
}
|
||||
|
||||
@@ -205,13 +272,25 @@ fn rejects_toml_syntax_error() {
|
||||
#[test]
|
||||
fn rejects_missing_hostname() {
|
||||
let e = single_error("upstreams = [\"http://127.0.0.1:8080\"]\n");
|
||||
assert_eq!(e, ValidationError::MissingField { file: "hosts/app.test.toml".into(), field: "hostname".into() });
|
||||
assert_eq!(
|
||||
e,
|
||||
ValidationError::MissingField {
|
||||
file: "hosts/app.test.toml".into(),
|
||||
field: "hostname".into()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_missing_upstreams() {
|
||||
let e = single_error("hostname = \"app.test\"\n");
|
||||
assert_eq!(e, ValidationError::MissingField { file: "hosts/app.test.toml".into(), field: "upstreams".into() });
|
||||
assert_eq!(
|
||||
e,
|
||||
ValidationError::MissingField {
|
||||
file: "hosts/app.test.toml".into(),
|
||||
field: "upstreams".into()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -219,11 +298,26 @@ fn rejects_bad_hostnames() {
|
||||
let long_label = format!("{}.test", "a".repeat(64));
|
||||
let long_name = format!("{}test", "abcdefghi.".repeat(26));
|
||||
for bad in [
|
||||
"", "UPPER.test", "-bad.test", "bad-.test", "a..b", "*.wild.test", "1.2.3.4", "[::1]",
|
||||
"has space.test", "under_score.test", "app.test.", ".app.test", "app.test:443",
|
||||
long_label.as_str(), long_name.as_str(), "ünicode.test",
|
||||
"",
|
||||
"UPPER.test",
|
||||
"-bad.test",
|
||||
"bad-.test",
|
||||
"a..b",
|
||||
"*.wild.test",
|
||||
"1.2.3.4",
|
||||
"[::1]",
|
||||
"has space.test",
|
||||
"under_score.test",
|
||||
"app.test.",
|
||||
".app.test",
|
||||
"app.test:443",
|
||||
long_label.as_str(),
|
||||
long_name.as_str(),
|
||||
"ünicode.test",
|
||||
] {
|
||||
let e = single_error(&format!("hostname = {bad:?}\nupstreams = [\"http://127.0.0.1:1\"]\n"));
|
||||
let e = single_error(&format!(
|
||||
"hostname = {bad:?}\nupstreams = [\"http://127.0.0.1:1\"]\n"
|
||||
));
|
||||
assert!(
|
||||
matches!(e, ValidationError::InvalidHostname { ref hostname, .. } if hostname == bad),
|
||||
"hostname {bad:?} gave {e:?}"
|
||||
@@ -233,8 +327,16 @@ fn rejects_bad_hostnames() {
|
||||
|
||||
#[test]
|
||||
fn accepts_good_hostnames() {
|
||||
for good in ["localhost", "a.b", "app.test", "x-1.example.org", "0day.example.org"] {
|
||||
let r = one(&format!("hostname = {good:?}\nupstreams = [\"http://127.0.0.1:1\"]\n"));
|
||||
for good in [
|
||||
"localhost",
|
||||
"a.b",
|
||||
"app.test",
|
||||
"x-1.example.org",
|
||||
"0day.example.org",
|
||||
] {
|
||||
let r = one(&format!(
|
||||
"hostname = {good:?}\nupstreams = [\"http://127.0.0.1:1\"]\n"
|
||||
));
|
||||
assert!(r.is_ok(), "{good}: {r:?}");
|
||||
}
|
||||
}
|
||||
@@ -244,8 +346,14 @@ fn rejects_duplicate_hostnames_across_files() {
|
||||
let r = load(
|
||||
&site(),
|
||||
&[
|
||||
SourceFile { name: "hosts/one.toml", contents: MINIMAL },
|
||||
SourceFile { name: "hosts/two.toml", contents: MINIMAL },
|
||||
SourceFile {
|
||||
name: "hosts/one.toml",
|
||||
contents: MINIMAL,
|
||||
},
|
||||
SourceFile {
|
||||
name: "hosts/two.toml",
|
||||
contents: MINIMAL,
|
||||
},
|
||||
],
|
||||
);
|
||||
let errs = r.unwrap_err();
|
||||
@@ -264,21 +372,43 @@ fn duplicate_detection_is_independent_of_file_order() {
|
||||
let r = load(
|
||||
&site(),
|
||||
&[
|
||||
SourceFile { name: "hosts/two.toml", contents: MINIMAL },
|
||||
SourceFile { name: "hosts/one.toml", contents: MINIMAL },
|
||||
SourceFile {
|
||||
name: "hosts/two.toml",
|
||||
contents: MINIMAL,
|
||||
},
|
||||
SourceFile {
|
||||
name: "hosts/one.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"));
|
||||
assert!(
|
||||
matches!(&errs.errors()[0], ValidationError::DuplicateHostname { first, second, .. } if first == "hosts/one.toml" && second == "hosts/two.toml")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_duplicate_source_file_names() {
|
||||
let r = load(
|
||||
&site(),
|
||||
&[SourceFile { name: "a.toml", contents: MINIMAL }, SourceFile { name: "a.toml", contents: MINIMAL }],
|
||||
&[
|
||||
SourceFile {
|
||||
name: "a.toml",
|
||||
contents: MINIMAL,
|
||||
},
|
||||
SourceFile {
|
||||
name: "a.toml",
|
||||
contents: MINIMAL,
|
||||
},
|
||||
],
|
||||
);
|
||||
assert!(
|
||||
r.unwrap_err()
|
||||
.errors()
|
||||
.iter()
|
||||
.any(|e| matches!(e, ValidationError::DuplicateFile { .. }))
|
||||
);
|
||||
assert!(r.unwrap_err().errors().iter().any(|e| matches!(e, ValidationError::DuplicateFile { .. })));
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- upstreams
|
||||
@@ -310,6 +440,17 @@ fn rejects_bad_upstream_urls() {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_mixed_upstream_schemes() {
|
||||
// Caddy has one transport per reverse_proxy handler, so a host cannot mix http and https.
|
||||
let e = single_error(
|
||||
"hostname = \"app.test\"
|
||||
upstreams = [\"http://127.0.0.1:1\", \"https://127.0.0.1:2\"]
|
||||
",
|
||||
);
|
||||
assert!(matches!(e, ValidationError::BadUpstreamUrl { .. }), "{e:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_empty_upstream_list() {
|
||||
let e = single_error("hostname = \"app.test\"\nupstreams = []\n");
|
||||
@@ -318,8 +459,13 @@ fn rejects_empty_upstream_list() {
|
||||
|
||||
#[test]
|
||||
fn rejects_duplicate_upstreams() {
|
||||
let e = single_error("hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:80\", \"http://127.0.0.1\"]\n");
|
||||
assert!(matches!(e, ValidationError::DuplicateUpstream { .. }), "{e:?}");
|
||||
let e = single_error(
|
||||
"hostname = \"app.test\"\nupstreams = [\"http://127.0.0.1:80\", \"http://127.0.0.1\"]\n",
|
||||
);
|
||||
assert!(
|
||||
matches!(e, ValidationError::DuplicateUpstream { .. }),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- secrets
|
||||
@@ -329,19 +475,35 @@ fn rejects_undefined_secret_reference() {
|
||||
let e = single_error(&format!(
|
||||
"{MINIMAL}[[request_headers]]\naction = \"set\"\nname = \"X-Api-Key\"\nsecret = \"nope\"\n"
|
||||
));
|
||||
assert_eq!(e, ValidationError::UndefinedSecret { file: "hosts/app.test.toml".into(), secret: "nope".into() });
|
||||
assert_eq!(
|
||||
e,
|
||||
ValidationError::UndefinedSecret {
|
||||
file: "hosts/app.test.toml".into(),
|
||||
secret: "nope".into()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn undefined_secret_is_reported_when_site_declares_none() {
|
||||
let r = load(
|
||||
&SourceFile { name: "ianitor.toml", contents: "" },
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: "",
|
||||
},
|
||||
&[SourceFile {
|
||||
name: "a.toml",
|
||||
contents: &format!("{MINIMAL}[[request_headers]]\naction = \"set\"\nname = \"X-K\"\nsecret = \"api_key\"\n"),
|
||||
contents: &format!(
|
||||
"{MINIMAL}[[request_headers]]\naction = \"set\"\nname = \"X-K\"\nsecret = \"api_key\"\n"
|
||||
),
|
||||
}],
|
||||
);
|
||||
assert!(r.unwrap_err().errors().iter().any(|e| matches!(e, ValidationError::UndefinedSecret { .. })));
|
||||
assert!(
|
||||
r.unwrap_err()
|
||||
.errors()
|
||||
.iter()
|
||||
.any(|e| matches!(e, ValidationError::UndefinedSecret { .. }))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -349,19 +511,30 @@ fn rejects_secret_in_response_header() {
|
||||
let e = single_error(&format!(
|
||||
"{MINIMAL}[[response_headers]]\naction = \"set\"\nname = \"X-Leak\"\nsecret = \"api_key\"\n"
|
||||
));
|
||||
assert!(matches!(e, ValidationError::SecretInResponseHeader { .. }), "{e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::SecretInResponseHeader { .. }),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_bad_secret_names_in_site_file() {
|
||||
for bad in ["../etc/passwd", "a/b", "", "UPPER", "with space", ".hidden"] {
|
||||
let r = load(
|
||||
&SourceFile { name: "ianitor.toml", contents: &format!("secrets = [{bad:?}]\n") },
|
||||
&[SourceFile { name: "a.toml", contents: MINIMAL }],
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: &format!("secrets = [{bad:?}]\n"),
|
||||
},
|
||||
&[SourceFile {
|
||||
name: "a.toml",
|
||||
contents: MINIMAL,
|
||||
}],
|
||||
);
|
||||
let errs = r.unwrap_err();
|
||||
assert!(
|
||||
errs.errors().iter().any(|e| matches!(e, ValidationError::InvalidSecretName { name, .. } if name == bad)),
|
||||
errs.errors().iter().any(
|
||||
|e| matches!(e, ValidationError::InvalidSecretName { name, .. } if name == bad)
|
||||
),
|
||||
"{bad:?}: {errs:?}"
|
||||
);
|
||||
}
|
||||
@@ -376,38 +549,92 @@ fn rule(list: &str, body: &str) -> String {
|
||||
#[test]
|
||||
fn rejects_malformed_header_rules() {
|
||||
let cases = [
|
||||
("set without value", rule("request_headers", "action = \"set\"\nname = \"X-A\"")),
|
||||
("remove with value", rule("request_headers", "action = \"remove\"\nname = \"X-A\"\nvalue = \"1\"")),
|
||||
("value and secret", rule("request_headers", "action = \"set\"\nname = \"X-A\"\nvalue = \"1\"\nsecret = \"api_key\"")),
|
||||
(
|
||||
"set without value",
|
||||
rule("request_headers", "action = \"set\"\nname = \"X-A\""),
|
||||
),
|
||||
(
|
||||
"remove with value",
|
||||
rule(
|
||||
"request_headers",
|
||||
"action = \"remove\"\nname = \"X-A\"\nvalue = \"1\"",
|
||||
),
|
||||
),
|
||||
(
|
||||
"value and secret",
|
||||
rule(
|
||||
"request_headers",
|
||||
"action = \"set\"\nname = \"X-A\"\nvalue = \"1\"\nsecret = \"api_key\"",
|
||||
),
|
||||
),
|
||||
];
|
||||
for (what, input) in cases {
|
||||
let e = single_error(&input);
|
||||
assert!(matches!(e, ValidationError::BadHeaderRule { .. }), "{what}: {e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::BadHeaderRule { .. }),
|
||||
"{what}: {e:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_header_names() {
|
||||
for bad in ["Bad Header", "", "X:Y", "X\nY", "Ä"] {
|
||||
let e = single_error(&rule("request_headers", &format!("action = \"remove\"\nname = {bad:?}")));
|
||||
assert!(matches!(e, ValidationError::InvalidHeaderName { ref name, .. } if name == bad), "{bad:?}: {e:?}");
|
||||
let e = single_error(&rule(
|
||||
"request_headers",
|
||||
&format!("action = \"remove\"\nname = {bad:?}"),
|
||||
));
|
||||
assert!(
|
||||
matches!(e, ValidationError::InvalidHeaderName { ref name, .. } if name == bad),
|
||||
"{bad:?}: {e:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_invalid_header_values() {
|
||||
for bad in ["a\nb", "a\rb", "a\u{0}b", "{http.request.host}", "x}", "\u{7f}", " leading", "ü"] {
|
||||
for bad in [
|
||||
"a\nb",
|
||||
"a\rb",
|
||||
"a\u{0}b",
|
||||
"{http.request.host}",
|
||||
"x}",
|
||||
"\u{7f}",
|
||||
" leading",
|
||||
"ü",
|
||||
] {
|
||||
let value = toml::Value::String(bad.to_owned()).to_string();
|
||||
let e = single_error(&rule("response_headers", &format!("action = \"set\"\nname = \"X-A\"\nvalue = {value}")));
|
||||
assert!(matches!(e, ValidationError::InvalidHeaderValue { .. }), "{bad:?}: {e:?}");
|
||||
let e = single_error(&rule(
|
||||
"response_headers",
|
||||
&format!("action = \"set\"\nname = \"X-A\"\nvalue = {value}"),
|
||||
));
|
||||
assert!(
|
||||
matches!(e, ValidationError::InvalidHeaderValue { .. }),
|
||||
"{bad:?}: {e:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_protected_headers() {
|
||||
for name in ["Host", "content-length", "Transfer-Encoding", "Connection", "Upgrade", "X-Forwarded-For", "x-forwarded-proto", "X-Forwarded-Host"] {
|
||||
let e = single_error(&rule("request_headers", &format!("action = \"remove\"\nname = {name:?}")));
|
||||
assert!(matches!(e, ValidationError::ProtectedHeader { .. }), "{name}: {e:?}");
|
||||
for name in [
|
||||
"Host",
|
||||
"content-length",
|
||||
"Transfer-Encoding",
|
||||
"Connection",
|
||||
"Upgrade",
|
||||
"X-Forwarded-For",
|
||||
"x-forwarded-proto",
|
||||
"X-Forwarded-Host",
|
||||
] {
|
||||
let e = single_error(&rule(
|
||||
"request_headers",
|
||||
&format!("action = \"remove\"\nname = {name:?}"),
|
||||
));
|
||||
assert!(
|
||||
matches!(e, ValidationError::ProtectedHeader { .. }),
|
||||
"{name}: {e:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -417,7 +644,10 @@ fn identity_headers_are_protected_on_forward_auth_hosts() {
|
||||
"{MINIMAL}auth = \"forward-auth\"\n[[request_headers]]\naction = \"set\"\nname = \"remote-user\"\nvalue = \"admin\"\n"
|
||||
);
|
||||
let e = single_error(&input);
|
||||
assert!(matches!(e, ValidationError::ProtectedHeader { .. }), "{e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::ProtectedHeader { .. }),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -426,7 +656,10 @@ fn rejects_duplicate_header_rules_case_insensitively() {
|
||||
"{MINIMAL}[[request_headers]]\naction = \"set\"\nname = \"X-A\"\nvalue = \"1\"\n[[request_headers]]\naction = \"remove\"\nname = \"x-a\"\n"
|
||||
);
|
||||
let e = single_error(&input);
|
||||
assert!(matches!(e, ValidationError::DuplicateHeaderRule { .. }), "{e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::DuplicateHeaderRule { .. }),
|
||||
"{e:?}"
|
||||
);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- auth and health checks
|
||||
@@ -434,21 +667,46 @@ fn rejects_duplicate_header_rules_case_insensitively() {
|
||||
#[test]
|
||||
fn forward_auth_requires_site_configuration() {
|
||||
let r = load(
|
||||
&SourceFile { name: "ianitor.toml", contents: "" },
|
||||
&[SourceFile { name: "a.toml", contents: &format!("{MINIMAL}auth = \"forward-auth\"\n") }],
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: "",
|
||||
},
|
||||
&[SourceFile {
|
||||
name: "a.toml",
|
||||
contents: &format!("{MINIMAL}auth = \"forward-auth\"\n"),
|
||||
}],
|
||||
);
|
||||
assert!(
|
||||
r.unwrap_err()
|
||||
.errors()
|
||||
.iter()
|
||||
.any(|e| matches!(e, ValidationError::ForwardAuthNotConfigured { .. }))
|
||||
);
|
||||
assert!(r.unwrap_err().errors().iter().any(|e| matches!(e, ValidationError::ForwardAuthNotConfigured { .. })));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejects_bad_forward_auth_site_config() {
|
||||
for bad in ["ftp://127.0.0.1/x", "http://u:p@127.0.0.1:9091/api", "http://127.0.0.1:9091/api#x", "nope"] {
|
||||
for bad in [
|
||||
"ftp://127.0.0.1/x",
|
||||
"http://u:p@127.0.0.1:9091/api",
|
||||
"http://127.0.0.1:9091/api#x",
|
||||
"nope",
|
||||
] {
|
||||
let r = load(
|
||||
&SourceFile { name: "ianitor.toml", contents: &format!("[forward_auth]\nurl = {bad:?}\n") },
|
||||
&[SourceFile { name: "a.toml", contents: MINIMAL }],
|
||||
&SourceFile {
|
||||
name: "ianitor.toml",
|
||||
contents: &format!("[forward_auth]\nurl = {bad:?}\n"),
|
||||
},
|
||||
&[SourceFile {
|
||||
name: "a.toml",
|
||||
contents: MINIMAL,
|
||||
}],
|
||||
);
|
||||
assert!(
|
||||
r.unwrap_err().errors().iter().any(|e| matches!(e, ValidationError::BadForwardAuth { .. })),
|
||||
r.unwrap_err()
|
||||
.errors()
|
||||
.iter()
|
||||
.any(|e| matches!(e, ValidationError::BadForwardAuth { .. })),
|
||||
"{bad}"
|
||||
);
|
||||
}
|
||||
@@ -464,7 +722,10 @@ fn rejects_bad_health_checks() {
|
||||
"path = \"/{x}\"\ninterval_secs = 5",
|
||||
] {
|
||||
let e = single_error(&format!("{MINIMAL}[health_check]\n{body}\n"));
|
||||
assert!(matches!(e, ValidationError::BadHealthCheck { .. }), "{body}: {e:?}");
|
||||
assert!(
|
||||
matches!(e, ValidationError::BadHealthCheck { .. }),
|
||||
"{body}: {e:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -475,9 +736,18 @@ fn collects_errors_from_every_file() {
|
||||
let r = load(
|
||||
&site(),
|
||||
&[
|
||||
SourceFile { name: "a.toml", contents: "hostname = \"a.test\"\nupstreams = [\"ftp://x\"]\n" },
|
||||
SourceFile { name: "b.toml", contents: "hostname = \"B.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n" },
|
||||
SourceFile { name: "c.toml", contents: MINIMAL },
|
||||
SourceFile {
|
||||
name: "a.toml",
|
||||
contents: "hostname = \"a.test\"\nupstreams = [\"ftp://x\"]\n",
|
||||
},
|
||||
SourceFile {
|
||||
name: "b.toml",
|
||||
contents: "hostname = \"B.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n",
|
||||
},
|
||||
SourceFile {
|
||||
name: "c.toml",
|
||||
contents: MINIMAL,
|
||||
},
|
||||
],
|
||||
);
|
||||
let errs = r.unwrap_err();
|
||||
@@ -495,8 +765,14 @@ fn error_messages_name_the_file_in_plain_english() {
|
||||
#[test]
|
||||
fn never_panics_on_garbage() {
|
||||
let samples = [
|
||||
"\u{0}\u{1}\u{2}", "[[[[", "hostname = 1", "hostname = [\"a\"]", "= =", "[health_check]\npath = 1",
|
||||
"hostname = \"a.test\"\nupstreams = [1, 2]", "hostname = \"a.test\"\nupstreams = [[]]",
|
||||
"\u{0}\u{1}\u{2}",
|
||||
"[[[[",
|
||||
"hostname = 1",
|
||||
"hostname = [\"a\"]",
|
||||
"= =",
|
||||
"[health_check]\npath = 1",
|
||||
"hostname = \"a.test\"\nupstreams = [1, 2]",
|
||||
"hostname = \"a.test\"\nupstreams = [[]]",
|
||||
"hostname = \"a.test\"\nupstreams = [\"http://127.0.0.1:1\"]\n[[request_headers]]\n",
|
||||
"hostname = \"a.test\"\nupstreams = [\"http://127.0.0.1:1\"]\nrequest_headers = 5",
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user