[core] Compilers: Pingora runtime config and Caddy JSON, golden files

This commit is contained in:
2026-11-07 14:39:34 +01:00
parent ff087a14dd
commit a5823ba33d
11 changed files with 1794 additions and 148 deletions
+148 -40
View File
@@ -151,7 +151,9 @@ pub fn load(site: &SourceFile<'_>, hosts: &[SourceFile<'_>]) -> Result<Model, Va
let mut checked = Vec::new();
for f in files {
if !seen_files.insert(f.name) {
errors.push(E::DuplicateFile { file: f.name.to_owned() });
errors.push(E::DuplicateFile {
file: f.name.to_owned(),
});
continue;
}
match parse::<RawHost>(f) {
@@ -178,7 +180,10 @@ pub fn load(site: &SourceFile<'_>, hosts: &[SourceFile<'_>]) -> Result<Model, Va
}
match site_model {
Some(site) if errors.is_empty() => Ok(Model { site, hosts: by_name.into_values().collect() }),
Some(site) if errors.is_empty() => Ok(Model {
site,
hosts: by_name.into_values().collect(),
}),
_ => Err(ValidationErrors(errors)),
}
}
@@ -215,7 +220,10 @@ fn classify(file: String, message: &str) -> E {
} else if let Some(field) = quoted("missing field `") {
E::MissingField { file, field }
} else {
E::InvalidValue { file, message: message.to_owned() }
E::InvalidValue {
file,
message: message.to_owned(),
}
}
}
@@ -228,12 +236,18 @@ fn check_site(file: &str, raw: RawSite, errors: &mut Vec<E>) -> Option<Site> {
if valid_secret_name(&name) {
secrets.insert(SecretName(name));
} else {
errors.push(E::InvalidSecretName { file: file.to_owned(), name });
errors.push(E::InvalidSecretName {
file: file.to_owned(),
name,
});
}
}
let forward_auth = raw.forward_auth.and_then(|fa| {
let bad = |reason| E::BadForwardAuth { file: file.to_owned(), reason };
let bad = |reason| E::BadForwardAuth {
file: file.to_owned(),
reason,
};
let (upstream, path_and_query) = match parse_url(&fa.url, true) {
Ok(v) => v,
Err(reason) => {
@@ -243,39 +257,67 @@ fn check_site(file: &str, raw: RawSite, errors: &mut Vec<E>) -> Option<Site> {
};
let copy_headers: Vec<String> = match fa.copy_headers {
Some(h) => h,
None => AUTHELIA_COPY_HEADERS.iter().map(|s| (*s).to_owned()).collect(),
None => AUTHELIA_COPY_HEADERS
.iter()
.map(|s| (*s).to_owned())
.collect(),
};
let mut seen = BTreeSet::new();
for h in &copy_headers {
if !valid_header_name(h) {
errors.push(E::InvalidHeaderName { file: file.to_owned(), name: h.clone() });
errors.push(E::InvalidHeaderName {
file: file.to_owned(),
name: h.clone(),
});
} else if is_protected(h) {
errors.push(E::ProtectedHeader { file: file.to_owned(), header: h.clone() });
errors.push(E::ProtectedHeader {
file: file.to_owned(),
header: h.clone(),
});
} else if !seen.insert(h.to_ascii_lowercase()) {
errors.push(bad("copy_headers lists a header twice"));
}
}
Some(ForwardAuth { upstream, path_and_query, copy_headers })
Some(ForwardAuth {
upstream,
path_and_query,
copy_headers,
})
});
let acme = match raw.acme {
None => Acme { email: None, directory: LETSENCRYPT_PRODUCTION.to_owned() },
None => Acme {
email: None,
directory: LETSENCRYPT_PRODUCTION.to_owned(),
},
Some(a) => {
let bad = |reason| E::BadAcme { file: file.to_owned(), reason };
if let Some(email) = &a.email {
if !valid_email(email) {
errors.push(bad("email is not a plain address like admin@example.org"));
}
let bad = |reason| E::BadAcme {
file: file.to_owned(),
reason,
};
if a.email.as_deref().is_some_and(|e| !valid_email(e)) {
errors.push(bad("email is not a plain address like admin@example.org"));
}
let directory = a.directory.unwrap_or_else(|| LETSENCRYPT_PRODUCTION.to_owned());
let directory = a
.directory
.unwrap_or_else(|| LETSENCRYPT_PRODUCTION.to_owned());
if !valid_acme_directory(&directory) {
errors.push(bad("directory must be an https:// URL without credentials or fragment"));
errors.push(bad(
"directory must be an https:// URL without credentials or fragment",
));
}
Acme {
email: a.email,
directory,
}
Acme { email: a.email, directory }
}
};
(errors.len() == before).then_some(Site { secrets, forward_auth, acme })
(errors.len() == before).then_some(Site {
secrets,
forward_auth,
acme,
})
}
fn valid_secret_name(n: &str) -> bool {
@@ -283,15 +325,20 @@ fn valid_secret_name(n: &str) -> bool {
!b.is_empty()
&& b.len() <= 64
&& (b[0].is_ascii_lowercase() || b[0].is_ascii_digit())
&& b.iter().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || *c == b'_' || *c == b'-')
&& b.iter()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || *c == b'_' || *c == b'-')
}
fn valid_email(e: &str) -> bool {
let Some((local, domain)) = e.split_once('@') else { return false };
let Some((local, domain)) = e.split_once('@') else {
return false;
};
!local.is_empty()
&& e.len() <= 254
&& check_hostname(domain).is_ok()
&& local.bytes().all(|c| c.is_ascii_graphic() && !b"{}@<>\"\\,;:[]()".contains(&c))
&& local
.bytes()
.all(|c| c.is_ascii_graphic() && !b"{}@<>\"\\,;:[]()".contains(&c))
}
fn valid_acme_directory(d: &str) -> bool {
@@ -302,7 +349,8 @@ fn valid_acme_directory(d: &str) -> bool {
&& u.password().is_none()
&& u.fragment().is_none()
&& u.host().is_some()
&& d.bytes().all(|c| c.is_ascii_graphic() && c != b'{' && c != b'}')
&& d.bytes()
.all(|c| c.is_ascii_graphic() && c != b'{' && c != b'}')
}
Err(_) => false,
}
@@ -315,7 +363,11 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
let f = || file.to_owned();
if let Err(reason) = check_hostname(&raw.hostname) {
errors.push(E::InvalidHostname { file: f(), hostname: raw.hostname.clone(), reason });
errors.push(E::InvalidHostname {
file: f(),
hostname: raw.hostname.clone(),
reason,
});
}
if raw.upstreams.is_empty() {
@@ -326,15 +378,30 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
match parse_url(u, false) {
Ok((up, _)) => {
if upstreams.contains(&up) {
errors.push(E::DuplicateUpstream { file: f(), url: u.clone() });
errors.push(E::DuplicateUpstream {
file: f(),
url: u.clone(),
});
} else {
upstreams.push(up);
}
}
Err(reason) => errors.push(E::BadUpstreamUrl { file: f(), url: u.clone(), reason }),
Err(reason) => errors.push(E::BadUpstreamUrl {
file: f(),
url: u.clone(),
reason,
}),
}
}
if upstreams.windows(2).any(|w| w[0].scheme != w[1].scheme) {
errors.push(E::BadUpstreamUrl {
file: f(),
url: raw.upstreams.join(", "),
reason: "all upstreams of a host must use the same scheme",
});
}
let auth = match raw.auth {
RawAuth::None => AuthMode::None,
RawAuth::ForwardAuth => AuthMode::ForwardAuth,
@@ -345,8 +412,15 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
// Identity headers set by forward-auth must not be overridable by a rule.
let identity: Vec<String> = match (auth, site.and_then(|s| s.forward_auth.as_ref())) {
(AuthMode::ForwardAuth, Some(fa)) => fa.copy_headers.iter().map(|h| h.to_ascii_lowercase()).collect(),
(AuthMode::ForwardAuth, None) => AUTHELIA_COPY_HEADERS.iter().map(|h| h.to_ascii_lowercase()).collect(),
(AuthMode::ForwardAuth, Some(fa)) => fa
.copy_headers
.iter()
.map(|h| h.to_ascii_lowercase())
.collect(),
(AuthMode::ForwardAuth, None) => AUTHELIA_COPY_HEADERS
.iter()
.map(|h| h.to_ascii_lowercase())
.collect(),
_ => Vec::new(),
};
let request_headers = check_rules(file, raw.request_headers, true, &identity, site, errors);
@@ -356,13 +430,21 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
let bad = |reason| E::BadHealthCheck { file: f(), reason };
let ok_path = hc.path.starts_with('/')
&& hc.path.len() <= 1024
&& hc.path.bytes().all(|c| c.is_ascii_graphic() && c != b'{' && c != b'}');
&& hc
.path
.bytes()
.all(|c| c.is_ascii_graphic() && c != b'{' && c != b'}');
if !ok_path {
errors.push(bad("path must start with '/' and contain only visible ASCII without '{' or '}'"));
errors.push(bad(
"path must start with '/' and contain only visible ASCII without '{' or '}'",
));
return None;
}
match u32::try_from(hc.interval_secs) {
Ok(i) if (1..=3600).contains(&i) => Some(HealthCheck { path: hc.path, interval_secs: i }),
Ok(i) if (1..=3600).contains(&i) => Some(HealthCheck {
path: hc.path,
interval_secs: i,
}),
_ => {
errors.push(bad("interval_secs must be between 1 and 3600"));
None
@@ -430,7 +512,10 @@ fn check_rules(
for r in raw {
let header = r.name.clone();
if !valid_header_name(&r.name) {
errors.push(E::InvalidHeaderName { file: f(), name: r.name });
errors.push(E::InvalidHeaderName {
file: f(),
name: r.name,
});
continue;
}
let lower = r.name.to_ascii_lowercase();
@@ -442,23 +527,42 @@ fn check_rules(
errors.push(E::DuplicateHeaderRule { file: f(), header });
continue;
}
let bad = |reason| E::BadHeaderRule { file: f(), header: header.clone(), reason };
let bad = |reason| E::BadHeaderRule {
file: f(),
header: header.clone(),
reason,
};
match (r.action, r.value, r.secret) {
(RawAction::Remove, None, None) => out.push(HeaderRule::Remove { name: r.name }),
(RawAction::Remove, _, _) => errors.push(bad("remove takes no value or secret")),
(RawAction::Set, Some(_), Some(_)) => errors.push(bad("give either value or secret, not both")),
(RawAction::Set, Some(_), Some(_)) => {
errors.push(bad("give either value or secret, not both"))
}
(RawAction::Set, None, None) => errors.push(bad("set needs a value or a secret")),
(RawAction::Set, Some(v), None) => match check_header_value(&v) {
Ok(()) => out.push(HeaderRule::Set { name: r.name, value: HeaderValue::Literal(v) }),
Err(reason) => errors.push(E::InvalidHeaderValue { file: f(), header, reason }),
Ok(()) => out.push(HeaderRule::Set {
name: r.name,
value: HeaderValue::Literal(v),
}),
Err(reason) => errors.push(E::InvalidHeaderValue {
file: f(),
header,
reason,
}),
},
(RawAction::Set, None, Some(s)) => {
if !request {
errors.push(E::SecretInResponseHeader { file: f(), header });
} else if site.is_some_and(|site| !site.secrets.contains(&SecretName(s.clone()))) {
errors.push(E::UndefinedSecret { file: f(), secret: s });
errors.push(E::UndefinedSecret {
file: f(),
secret: s,
});
} else {
out.push(HeaderRule::Set { name: r.name, value: HeaderValue::Secret(SecretName(s)) });
out.push(HeaderRule::Set {
name: r.name,
value: HeaderValue::Secret(SecretName(s)),
});
}
}
}
@@ -470,7 +574,8 @@ fn check_rules(
fn valid_header_name(n: &str) -> bool {
!n.is_empty()
&& n.len() <= 256
&& n.bytes().all(|c| c.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&c))
&& n.bytes()
.all(|c| c.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&c))
}
fn check_header_value(v: &str) -> Result<(), &'static str> {
@@ -521,7 +626,10 @@ pub(crate) fn check_hostname(h: &str) -> Result<(), &'static str> {
if label.len() > 63 {
return Err("a label is longer than 63 characters");
}
if !label.bytes().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'-') {
if !label
.bytes()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == b'-')
{
return Err("only a-z, 0-9, '-' and '.' are allowed");
}
if label.starts_with('-') || label.ends_with('-') {