[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
+53 -25
View File
@@ -14,8 +14,8 @@ use serde::de::DeserializeOwned;
use crate::error::{ValidationError as E, ValidationErrors};
use crate::model::{
AUTHELIA_COPY_HEADERS, AccessLog, Acme, AuthMode, ForwardAuth, HeaderRule, HeaderValue,
HealthCheck, Host, Hostname, LETSENCRYPT_PRODUCTION, Model, Scheme, SecretName, Site, TlsMode,
Upstream, UpstreamHost,
HealthCheck, Host, Hostname, LETSENCRYPT_PRODUCTION, MAX_FILE_BYTES, MAX_UPSTREAMS, Model,
Scheme, SecretName, Site, TlsMode, Upstream, UpstreamHost,
};
/// One route file handed in by the caller (core does no I/O).
@@ -192,6 +192,12 @@ pub fn load(site: &SourceFile<'_>, hosts: &[SourceFile<'_>]) -> Result<Model, Va
fn parse<T: DeserializeOwned>(f: &SourceFile<'_>) -> Result<T, E> {
let file = f.name.to_owned();
if f.contents.len() > MAX_FILE_BYTES {
return Err(E::FileTooLarge {
file,
max: MAX_FILE_BYTES,
});
}
// A plain table parse first tells syntax errors apart from schema errors.
if let Err(e) = toml::from_str::<toml::Table>(f.contents) {
return Err(E::Syntax {
@@ -362,28 +368,45 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
let before = errors.len();
let f = || file.to_owned();
if let Err(reason) = check_hostname(&raw.hostname) {
errors.push(E::InvalidHostname {
match check_hostname(&raw.hostname) {
Err(reason) => errors.push(E::InvalidHostname {
file: f(),
hostname: raw.hostname.clone(),
reason,
});
}),
Ok(()) => {
// One TOML per host, named after it: `hosts/<hostname>.toml`.
let base = file.rsplit(['/', '\\']).next().unwrap_or(file);
if base != format!("{}.toml", raw.hostname) {
errors.push(E::FileNameMismatch {
file: f(),
hostname: raw.hostname.clone(),
});
}
}
}
if raw.upstreams.is_empty() {
errors.push(E::NoUpstreams { file: f() });
}
if raw.upstreams.len() > MAX_UPSTREAMS {
errors.push(E::TooManyUpstreams {
file: f(),
max: MAX_UPSTREAMS,
});
}
let mut upstreams: Vec<Upstream> = Vec::new();
for u in &raw.upstreams {
let mut seen_upstreams = BTreeSet::new();
for u in raw.upstreams.iter().take(MAX_UPSTREAMS) {
match parse_url(u, false) {
Ok((up, _)) => {
if upstreams.contains(&up) {
if seen_upstreams.insert(up.clone()) {
upstreams.push(up);
} else {
errors.push(E::DuplicateUpstream {
file: f(),
url: u.clone(),
});
} else {
upstreams.push(up);
}
}
Err(reason) => errors.push(E::BadUpstreamUrl {
@@ -410,18 +433,22 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
errors.push(E::ForwardAuthNotConfigured { file: f() });
}
// 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(),
_ => Vec::new(),
// Identity headers (Authelia's four, plus any configured copy header) must not be
// overridable by a rule on a forward-auth host.
let identity: Vec<String> = match auth {
AuthMode::ForwardAuth => {
let extra = site
.and_then(|s| s.forward_auth.as_ref())
.map(|fa| fa.copy_headers.clone())
.unwrap_or_default();
AUTHELIA_COPY_HEADERS
.iter()
.map(|h| (*h).to_owned())
.chain(extra)
.map(|h| h.to_ascii_lowercase())
.collect()
}
AuthMode::None => Vec::new(),
};
let request_headers = check_rules(file, raw.request_headers, true, &identity, site, errors);
let response_headers = check_rules(file, raw.response_headers, false, &[], site, errors);
@@ -433,10 +460,10 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
&& hc
.path
.bytes()
.all(|c| c.is_ascii_graphic() && c != b'{' && c != b'}');
.all(|c| c.is_ascii_graphic() && !b"{}#".contains(&c));
if !ok_path {
errors.push(bad(
"path must start with '/' and contain only visible ASCII without '{' or '}'",
"path must start with '/' and contain only visible ASCII without '{', '}' or '#'",
));
return None;
}
@@ -570,12 +597,13 @@ fn check_rules(
out
}
/// RFC 9110 token.
/// RFC 9110 token minus `*`, which is a wildcard in Caddy's header operations but a literal
/// character in edge; allowing it would let the engines disagree.
fn valid_header_name(n: &str) -> bool {
!n.is_empty()
&& n.len() <= 256
&& n.bytes()
.all(|c| c.is_ascii_alphanumeric() || b"!#$%&'*+-.^_`|~".contains(&c))
.all(|c| c.is_ascii_alphanumeric() || b"!#$%&'+-.^_`|~".contains(&c))
}
fn check_header_value(v: &str) -> Result<(), &'static str> {