fix(#4): decide 'local registry' by name, not by its DNS answer
A registry name resolving to loopback (e.g. *.127.0.0.1.nip.io) got the unfiltered agent for the whole pull. Only localhost or a non-public IP literal is local now; DNS-named registries always use the filtered resolver.
This commit is contained in:
+20
-5
@@ -73,13 +73,18 @@ pub fn is_public(ip: IpAddr) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
/// Registry the user named as local: `localhost` or a non-public IP literal. Decided by name only --
|
||||
/// a DNS name pointing at loopback (e.g. *.127.0.0.1.nip.io) is controlled by whoever owns the zone.
|
||||
fn explicit_local(registry: &str) -> bool {
|
||||
let host = registry.split_once(':').map_or(registry, |(h, _)| h);
|
||||
host == "localhost" || host.parse().is_ok_and(|ip| !is_public(ip))
|
||||
}
|
||||
|
||||
/// HTTP agent for a registry. Public registries get an agent that refuses to connect to non-public
|
||||
/// addresses (covers token realms and blob redirects too); a registry that is itself local
|
||||
/// (e.g. localhost:5000) was chosen explicitly by the user and keeps the default agent.
|
||||
/// addresses (covers token realms and blob redirects too); an explicitly local registry
|
||||
/// (e.g. localhost:5000, 192.168.1.5:5000) was chosen by the user and keeps the default agent.
|
||||
fn agent_for(registry: &str) -> ureq::Agent {
|
||||
let addr = if registry.contains(':') { registry.to_string() } else { format!("{registry}:443") };
|
||||
let local = addr.to_socket_addrs().map(|mut a| a.any(|a| !is_public(a.ip()))).unwrap_or(false);
|
||||
if local {
|
||||
if explicit_local(registry) {
|
||||
return ureq::agent();
|
||||
}
|
||||
ureq::AgentBuilder::new()
|
||||
@@ -486,6 +491,16 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_registries() {
|
||||
for l in ["localhost:5000", "localhost", "127.0.0.1:5000", "192.168.1.5:5000"] {
|
||||
assert!(explicit_local(l), "{l}");
|
||||
}
|
||||
for r in ["evil.127.0.0.1.nip.io:8443", "localhost.evil.com", "ghcr.io", "8.8.8.8:443", "registry-1.docker.io"] {
|
||||
assert!(!explicit_local(r), "{r}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_refs() {
|
||||
for bad in ["alpine@sha256:../../x", "../x", "a/../b", "alpine:../x", "Alpine", "a//b"] {
|
||||
|
||||
Reference in New Issue
Block a user