[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
+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.