[core] Tests first: share tokens, prefix escapes, repo layout, exact host path, error codes (red)
This commit is contained in:
+89
-43
@@ -11,7 +11,7 @@ use std::net::IpAddr;
|
||||
use serde::Deserialize;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use crate::error::{ValidationError as E, ValidationErrors};
|
||||
use crate::error::{ValidationError as E, ValidationErrors, reason as r};
|
||||
use crate::model::{
|
||||
AUTHELIA_COPY_HEADERS, AccessLog, Acme, AuthMode, ForwardAuth, HeaderRule, HeaderValue,
|
||||
HealthCheck, Host, Hostname, LETSENCRYPT_PRODUCTION, MAX_FILE_BYTES, MAX_UPSTREAMS, Model,
|
||||
@@ -274,11 +274,13 @@ fn check_site(file: &str, raw: RawSite, errors: &mut Vec<E>) -> Option<Site> {
|
||||
errors.push(E::InvalidHeaderName {
|
||||
file: file.to_owned(),
|
||||
name: h.clone(),
|
||||
field: "forward_auth.copy_headers".into(),
|
||||
});
|
||||
} else if is_protected(h) {
|
||||
errors.push(E::ProtectedHeader {
|
||||
file: file.to_owned(),
|
||||
header: h.clone(),
|
||||
field: "forward_auth.copy_headers".into(),
|
||||
});
|
||||
} else if !seen.insert(h.to_ascii_lowercase()) {
|
||||
errors.push(bad("copy_headers lists a header twice"));
|
||||
@@ -397,7 +399,7 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
|
||||
}
|
||||
let mut upstreams: Vec<Upstream> = Vec::new();
|
||||
let mut seen_upstreams = BTreeSet::new();
|
||||
for u in raw.upstreams.iter().take(MAX_UPSTREAMS) {
|
||||
for (index, u) in raw.upstreams.iter().take(MAX_UPSTREAMS).enumerate() {
|
||||
match parse_url(u, false) {
|
||||
Ok((up, _)) => {
|
||||
if seen_upstreams.insert(up.clone()) {
|
||||
@@ -406,6 +408,7 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
|
||||
errors.push(E::DuplicateUpstream {
|
||||
file: f(),
|
||||
url: u.clone(),
|
||||
index,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -413,6 +416,7 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
|
||||
file: f(),
|
||||
url: u.clone(),
|
||||
reason,
|
||||
index: Some(index),
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -421,7 +425,8 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
|
||||
errors.push(E::BadUpstreamUrl {
|
||||
file: f(),
|
||||
url: raw.upstreams.join(", "),
|
||||
reason: "all upstreams of a host must use the same scheme",
|
||||
reason: r::URL_MIXED,
|
||||
index: None,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -450,8 +455,22 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
|
||||
}
|
||||
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);
|
||||
let request_headers = check_rules(
|
||||
file,
|
||||
"request_headers",
|
||||
raw.request_headers,
|
||||
&identity,
|
||||
site,
|
||||
errors,
|
||||
);
|
||||
let response_headers = check_rules(
|
||||
file,
|
||||
"response_headers",
|
||||
raw.response_headers,
|
||||
&[],
|
||||
site,
|
||||
errors,
|
||||
);
|
||||
|
||||
let health_check = raw.health_check.and_then(|hc| {
|
||||
let bad = |reason| E::BadHealthCheck { file: f(), reason };
|
||||
@@ -463,7 +482,7 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
|
||||
.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 '#'",
|
||||
r::HEALTH_PATH,
|
||||
));
|
||||
return None;
|
||||
}
|
||||
@@ -473,7 +492,7 @@ fn check_host(file: &str, raw: RawHost, site: Option<&Site>, errors: &mut Vec<E>
|
||||
interval_secs: i,
|
||||
}),
|
||||
_ => {
|
||||
errors.push(bad("interval_secs must be between 1 and 3600"));
|
||||
errors.push(bad(r::HEALTH_INTERVAL));
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -527,45 +546,63 @@ fn is_protected(name: &str) -> bool {
|
||||
|
||||
fn check_rules(
|
||||
file: &str,
|
||||
list: &str,
|
||||
raw: Vec<RawRule>,
|
||||
request: bool,
|
||||
identity: &[String],
|
||||
site: Option<&Site>,
|
||||
errors: &mut Vec<E>,
|
||||
) -> Vec<HeaderRule> {
|
||||
let request = list == "request_headers";
|
||||
let f = || file.to_owned();
|
||||
let mut out = Vec::new();
|
||||
let mut seen = BTreeSet::new();
|
||||
for r in raw {
|
||||
for (i, r) in raw.into_iter().enumerate() {
|
||||
let at = |part: &str| {
|
||||
if part.is_empty() {
|
||||
format!("{list}[{i}]")
|
||||
} else {
|
||||
format!("{list}[{i}].{part}")
|
||||
}
|
||||
};
|
||||
let header = r.name.clone();
|
||||
if !valid_header_name(&r.name) {
|
||||
errors.push(E::InvalidHeaderName {
|
||||
file: f(),
|
||||
name: r.name,
|
||||
field: at("name"),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
let lower = r.name.to_ascii_lowercase();
|
||||
if is_protected(&r.name) || identity.contains(&lower) {
|
||||
errors.push(E::ProtectedHeader { file: f(), header });
|
||||
errors.push(E::ProtectedHeader {
|
||||
file: f(),
|
||||
header,
|
||||
field: at("name"),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
if !seen.insert(lower) {
|
||||
errors.push(E::DuplicateHeaderRule { file: f(), header });
|
||||
errors.push(E::DuplicateHeaderRule {
|
||||
file: f(),
|
||||
header,
|
||||
field: at("name"),
|
||||
});
|
||||
continue;
|
||||
}
|
||||
let bad = |reason| E::BadHeaderRule {
|
||||
file: f(),
|
||||
header: header.clone(),
|
||||
reason,
|
||||
field: at(if reason == r::RULE_NEITHER { "value" } else { "" }),
|
||||
};
|
||||
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::Remove, _, _) => errors.push(bad(r::RULE_REMOVE_VALUE)),
|
||||
(RawAction::Set, Some(_), Some(_)) => {
|
||||
errors.push(bad("give either value or secret, not both"))
|
||||
errors.push(bad(r::RULE_BOTH))
|
||||
}
|
||||
(RawAction::Set, None, None) => errors.push(bad("set needs a value or a secret")),
|
||||
(RawAction::Set, None, None) => errors.push(bad(r::RULE_NEITHER)),
|
||||
(RawAction::Set, Some(v), None) => match check_header_value(&v) {
|
||||
Ok(()) => out.push(HeaderRule::Set {
|
||||
name: r.name,
|
||||
@@ -575,15 +612,21 @@ fn check_rules(
|
||||
file: f(),
|
||||
header,
|
||||
reason,
|
||||
field: at("value"),
|
||||
}),
|
||||
},
|
||||
(RawAction::Set, None, Some(s)) => {
|
||||
if !request {
|
||||
errors.push(E::SecretInResponseHeader { file: f(), header });
|
||||
errors.push(E::SecretInResponseHeader {
|
||||
file: f(),
|
||||
header,
|
||||
field: at(""),
|
||||
});
|
||||
} else if site.is_some_and(|site| !site.secrets.contains(&SecretName(s.clone()))) {
|
||||
errors.push(E::UndefinedSecret {
|
||||
file: f(),
|
||||
secret: s,
|
||||
field: at("secret"),
|
||||
});
|
||||
} else {
|
||||
out.push(HeaderRule::Set {
|
||||
@@ -608,19 +651,19 @@ fn valid_header_name(n: &str) -> bool {
|
||||
|
||||
fn check_header_value(v: &str) -> Result<(), &'static str> {
|
||||
if v.is_empty() {
|
||||
return Err("an empty value is not allowed; use action = \"remove\"");
|
||||
return Err(r::VALUE_EMPTY);
|
||||
}
|
||||
if v.len() > 4096 {
|
||||
return Err("it is longer than 4096 characters");
|
||||
return Err(r::VALUE_LONG);
|
||||
}
|
||||
if !v.bytes().all(|c| (0x20..0x7f).contains(&c)) {
|
||||
return Err("only visible ASCII characters and spaces are allowed");
|
||||
return Err(r::VALUE_ASCII);
|
||||
}
|
||||
if v.contains(['{', '}']) {
|
||||
return Err("'{' and '}' are not allowed (they are placeholders in Caddy)");
|
||||
return Err(r::VALUE_BRACES);
|
||||
}
|
||||
if v.starts_with(' ') || v.ends_with(' ') {
|
||||
return Err("it must not start or end with a space");
|
||||
return Err(r::VALUE_EDGE_SPACE);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -629,44 +672,47 @@ fn check_header_value(v: &str) -> Result<(), &'static str> {
|
||||
|
||||
pub(crate) fn check_hostname(h: &str) -> Result<(), &'static str> {
|
||||
if h.is_empty() {
|
||||
return Err("it is empty");
|
||||
return Err(r::HOST_EMPTY);
|
||||
}
|
||||
if h.len() > 253 {
|
||||
return Err("it is longer than 253 characters");
|
||||
return Err(r::HOST_TOO_LONG);
|
||||
}
|
||||
if h.parse::<IpAddr>().is_ok() || h.starts_with('[') {
|
||||
return Err("IP addresses are not hostnames");
|
||||
return Err(r::HOST_IP);
|
||||
}
|
||||
if h.contains('*') {
|
||||
return Err("wildcards are not supported");
|
||||
return Err(r::HOST_WILDCARD);
|
||||
}
|
||||
if h.contains(char::is_whitespace) {
|
||||
return Err(r::HOST_SPACES);
|
||||
}
|
||||
if h.ends_with('.') {
|
||||
return Err("it must not end with a dot");
|
||||
return Err(r::HOST_TRAILING_DOT);
|
||||
}
|
||||
if h.bytes().any(|c| c.is_ascii_uppercase()) {
|
||||
return Err("it must be lowercase");
|
||||
return Err(r::HOST_UPPERCASE);
|
||||
}
|
||||
let mut last = "";
|
||||
for label in h.split('.') {
|
||||
if label.is_empty() {
|
||||
return Err("it has an empty label");
|
||||
return Err(r::HOST_EMPTY_LABEL);
|
||||
}
|
||||
if label.len() > 63 {
|
||||
return Err("a label is longer than 63 characters");
|
||||
return Err(r::HOST_LABEL_LONG);
|
||||
}
|
||||
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");
|
||||
return Err(r::HOST_CHARACTERS);
|
||||
}
|
||||
if label.starts_with('-') || label.ends_with('-') {
|
||||
return Err("a label must not start or end with '-'");
|
||||
return Err(r::HOST_LABEL_DASH);
|
||||
}
|
||||
last = label;
|
||||
}
|
||||
if last.bytes().all(|c| c.is_ascii_digit()) {
|
||||
return Err("the last label must not be numeric");
|
||||
return Err(r::HOST_NUMERIC);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -675,42 +721,42 @@ pub(crate) fn check_hostname(h: &str) -> Result<(), &'static str> {
|
||||
/// (for the forward-auth endpoint); otherwise any path other than `/` is rejected.
|
||||
fn parse_url(raw: &str, allow_path: bool) -> Result<(Upstream, String), &'static str> {
|
||||
if !raw.bytes().all(|c| c.is_ascii_graphic()) {
|
||||
return Err("it must be plain ASCII without spaces");
|
||||
return Err(r::URL_ASCII);
|
||||
}
|
||||
let u = url::Url::parse(raw).map_err(|_| "it is not a URL like http://127.0.0.1:8080")?;
|
||||
let u = url::Url::parse(raw).map_err(|_| r::URL_NOT_URL)?;
|
||||
let scheme = match u.scheme() {
|
||||
"http" => Scheme::Http,
|
||||
"https" => Scheme::Https,
|
||||
_ => return Err("only http:// and https:// are supported"),
|
||||
_ => return Err(r::URL_SCHEME),
|
||||
};
|
||||
if !u.username().is_empty() || u.password().is_some() {
|
||||
return Err("credentials in the URL are not allowed");
|
||||
return Err(r::URL_CREDENTIALS);
|
||||
}
|
||||
if u.fragment().is_some() {
|
||||
return Err("a #fragment is not allowed");
|
||||
return Err(r::URL_FRAGMENT);
|
||||
}
|
||||
let host = match u.host() {
|
||||
None => return Err("it has no host"),
|
||||
None => return Err(r::URL_NO_HOST),
|
||||
Some(url::Host::Ipv4(ip)) => UpstreamHost::Ip(IpAddr::V4(ip)),
|
||||
Some(url::Host::Ipv6(ip)) => UpstreamHost::Ip(IpAddr::V6(ip)),
|
||||
Some(url::Host::Domain(d)) => {
|
||||
if check_hostname(d).is_err() {
|
||||
return Err("the host name is not valid");
|
||||
return Err(r::URL_HOST_NAME);
|
||||
}
|
||||
UpstreamHost::Name(d.to_owned())
|
||||
}
|
||||
};
|
||||
let port = u.port_or_known_default().ok_or("it has no port")?;
|
||||
let port = u.port_or_known_default().ok_or(r::URL_NO_PORT)?;
|
||||
if port == 0 {
|
||||
return Err("port 0 is not usable");
|
||||
return Err(r::URL_PORT_ZERO);
|
||||
}
|
||||
let mut path = u.path().to_owned();
|
||||
if !allow_path {
|
||||
if path != "/" && !path.is_empty() {
|
||||
return Err("a path is not allowed; requests keep their own path");
|
||||
return Err(r::URL_PATH);
|
||||
}
|
||||
if u.query().is_some() {
|
||||
return Err("a ?query is not allowed");
|
||||
return Err(r::URL_QUERY);
|
||||
}
|
||||
} else {
|
||||
if let Some(q) = u.query() {
|
||||
@@ -718,7 +764,7 @@ fn parse_url(raw: &str, allow_path: bool) -> Result<(Upstream, String), &'static
|
||||
path.push_str(q);
|
||||
}
|
||||
if path.contains(['{', '}']) {
|
||||
return Err("'{' and '}' are not allowed");
|
||||
return Err(r::URL_BRACES);
|
||||
}
|
||||
}
|
||||
Ok((Upstream { scheme, host, port }, path))
|
||||
|
||||
Reference in New Issue
Block a user