[core] Unix upstreams: unix:/path parses strictly into a socket upstream; edge proxies and health-checks it in plain HTTP over the socket, Caddy dials unix/<path>

This commit is contained in:
2026-11-10 21:04:24 +01:00
parent 84c1c81906
commit d74079c66d
8 changed files with 183 additions and 10 deletions
+5 -1
View File
@@ -281,7 +281,11 @@ fn host_route(h: &Host, fa: Option<&ForwardAuth>, o: &CompileOptions) -> Value {
fn upstreams(list: &[&Upstream]) -> Value {
Value::Array(
list.iter()
.map(|u| json!({ "dial": u.authority() }))
.map(|u| match u.unix_path() {
// Caddy's network address: `unix/` then the absolute path.
Some(p) => json!({ "dial": format!("unix/{p}") }),
None => json!({ "dial": u.authority() }),
})
.collect(),
)
}
+2
View File
@@ -34,6 +34,7 @@ pub(crate) mod reason {
pub const URL_CREDENTIALS: &str = "credentials in the URL are not allowed";
pub const URL_FRAGMENT: &str = "a #fragment is not allowed";
pub const URL_NO_HOST: &str = "it has no host";
pub const URL_UNIX_PATH: &str = "a socket upstream is unix:/absolute/path: letters, digits, '.', '_', '-' and single '/' only, no '.' or '..' parts, at most 100 characters";
pub const URL_HOST_NAME: &str = "the host name is not valid";
pub const URL_NO_PORT: &str = "it has no port";
pub const URL_PORT_ZERO: &str = "port 0 is not usable";
@@ -275,6 +276,7 @@ impl ValidationError {
x if x == r::URL_PATH => "host.upstream.path",
x if x == r::URL_QUERY => "host.upstream.query",
x if x == r::URL_MIXED => "host.upstream.mixed_schemes",
x if x == r::URL_UNIX_PATH => "host.upstream.unix_path",
_ => "host.upstream.not_url",
},
E::DuplicateUpstream { .. } => "host.upstream.duplicate",
+30 -1
View File
@@ -401,7 +401,11 @@ 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 (index, u) in raw.upstreams.iter().take(MAX_UPSTREAMS).enumerate() {
match parse_url(u, false) {
let parsed = match u.strip_prefix("unix:") {
Some(path) => parse_unix(path).map(|up| (up, String::new())),
None => parse_url(u, false),
};
match parsed {
Ok((up, _)) => {
if seen_upstreams.insert(up.clone()) {
upstreams.push(up);
@@ -786,6 +790,31 @@ fn parse_url(raw: &str, allow_path: bool) -> Result<(Upstream, String), &'static
Ok((Upstream { scheme, host, port }, path))
}
/// The path of a `unix:` upstream. It ends up in JSON strings and in Caddy's dial address
/// (where `{...}` would be a placeholder), so it is plain: absolute, `[A-Za-z0-9._-]` parts
/// joined by single `/`, no `.` or `..` part, at most 100 bytes (sun_path is 108 on Linux).
fn parse_unix(path: &str) -> Result<Upstream, &'static str> {
let ok = path.len() <= 100
&& path.len() > 1
&& path.starts_with('/')
&& path[1..].split('/').all(|part| {
!part.is_empty()
&& part != "."
&& part != ".."
&& part
.bytes()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, b'.' | b'_' | b'-'))
});
if !ok {
return Err(r::URL_UNIX_PATH);
}
Ok(Upstream {
scheme: Scheme::Http,
host: UpstreamHost::Unix(path.to_owned()),
port: 0,
})
}
/// A URL as an error may show it: everything before the last `@` (after the scheme) is
/// replaced by `…`, so a user name or password, even one containing `/`, `?` or `#`, never
/// reaches a message, a log or the API. A URL with no `@` is shown as it is.