From 37c58cb5b2a9d38aeece9c66931cc4c2b325c0fd Mon Sep 17 00:00:00 2001 From: Niklas Haiden Date: Thu, 22 Oct 2026 19:52:22 +0200 Subject: [PATCH] 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. --- README.md | 3 ++- src/oci.rs | 25 ++++++++++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f462082..5254ee2 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,8 @@ The VM is the boundary. The guest (including a prompt-injected agent or a malici - **Registry**: references are validated before any request, digest pins are enforced, and every blob/manifest is sha256-verified. The tag cache is keyed by the exact reference (`a_b:t` and `a/b:t` never share an entry). Auth realms must be https. For public registries, connections to loopback/private/link-local addresses are refused - (token realms and redirects included). + (token realms and redirects included, IPv6 forms like NAT64/6to4 too). A registry counts as local only by name: + `localhost` or a private IP literal (`192.168.1.5:5000`). LAN registries addressed by a DNS name are refused; use the IP. - **pi config**: allowlisted items only. Symlinks are followed only into `~/.pi/agent` or `~/.agents/skills`, with no cycles and no special files, capped at 256 MB. - **exec**: sessions run as root before dropping to the user, so the guest helper accepts them only from the host's vsock CID, diff --git a/src/oci.rs b/src/oci.rs index 5b5c416..1f12e09 100644 --- a/src/oci.rs +++ b/src/oci.rs @@ -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"] {