//! Minimal OCI/Docker registry client: anonymous pull of linux/arm64 images, //! sha256-verified blobs, layers flattened into one tar (whiteouts applied). use anyhow::{Context, Result, anyhow, bail}; use serde_json::Value; use sha2::{Digest, Sha256}; use std::collections::HashSet; use std::fs::{self, File}; use std::io::{BufReader, Read, Write}; use std::net::{IpAddr, Ipv4Addr, SocketAddr, ToSocketAddrs}; use std::os::unix::fs::MetadataExt; use std::path::{Path, PathBuf}; const ACCEPT: &str = "application/vnd.oci.image.index.v1+json, application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.oci.image.manifest.v1+json, application/vnd.docker.distribution.manifest.v2+json"; pub struct Image { pub rootfs: PathBuf, pub config: Value, // the "config" object of the image config (Env, User, Cmd, ...) pub digest: String, } struct Ref { registry: String, repo: String, reference: String, } fn parse_ref(s: &str) -> Ref { let (name, reference) = match s.split_once('@') { Some((n, d)) => (n.to_string(), d.to_string()), None => match s.rfind(':') { Some(i) if !s[i..].contains('/') => (s[..i].to_string(), s[i + 1..].to_string()), _ => (s.to_string(), "latest".to_string()), }, }; let first = name.split('/').next().unwrap(); let (registry, repo) = if name.contains('/') && (first.contains('.') || first.contains(':') || first == "localhost") { (first.to_string(), name[first.len() + 1..].to_string()) } else if name.contains('/') { ("registry-1.docker.io".into(), name) } else { ("registry-1.docker.io".into(), format!("library/{name}")) }; let registry = if registry == "docker.io" { "registry-1.docker.io".into() } else { registry }; Ref { registry, repo, reference } } /// Globally routable address? Used to stop a remote registry from steering the host at /// loopback / LAN / link-local services (cloud metadata, local daemons) via auth realms or redirects. pub fn is_public(ip: IpAddr) -> bool { match ip { IpAddr::V4(v) => { let o = v.octets(); !(v.is_private() || v.is_loopback() || v.is_link_local() || v.is_unspecified() || v.is_broadcast() || v.is_multicast() || v.is_documentation() || o[0] == 0 || (o[0] == 100 && o[1] & 0xc0 == 64)) // CGNAT 100.64/10 } IpAddr::V6(v) => { let v4 = |hi: u16, lo: u16| is_public(Ipv4Addr::from((hi as u32) << 16 | lo as u32).into()); match v.segments() { [0, 0, 0, 0, 0, 0xffff, hi, lo] => v4(hi, lo), // IPv4-mapped [0x64, 0xff9b, 0, 0, 0, 0, hi, lo] => v4(hi, lo), // NAT64 well-known prefix [0x2002, hi, lo, ..] => v4(hi, lo), // 6to4 [0x2001, 0xdb8, ..] | [0x2001, 0, ..] => false, // documentation, Teredo s => s[0] & 0xe000 == 0x2000, // global unicast 2000::/3 only } } } } /// Every HTTP agent: a silent server (connect or any single read, TLS handshake included) fails after 30 s instead of hanging. pub fn agent() -> ureq::AgentBuilder { let t = std::time::Duration::from_secs(30); ureq::AgentBuilder::new().timeout_connect(t).timeout_read(t) } /// 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); an explicitly local registry /// (e.g. localhost:5000, 192.168.1.5:5000) was chosen by the user and keeps the default agent. pub fn agent_for(registry: &str) -> ureq::Agent { if explicit_local(registry) { return agent().build(); } agent() .resolver(|addr: &str| -> std::io::Result> { let ok: Vec = addr.to_socket_addrs()?.filter(|a| is_public(a.ip())).collect(); if ok.is_empty() { return Err(std::io::Error::other(format!("refusing non-public address for {addr}"))); } Ok(ok) }) .build() } /// Registry-supplied digests end up in URLs and cache paths: only accept canonical sha256. fn check_digest(d: &str) -> Result<&str> { let hex = d.strip_prefix("sha256:").ok_or_else(|| anyhow!("unsupported digest {d:?}"))?; if hex.len() != 64 || !hex.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b)) { bail!("malformed digest {d:?}"); } Ok(d) } /// Reference parts go into registry URLs: no traversal, only the OCI distribution-spec charset. fn validate_ref(r: &Ref) -> Result<()> { let comp = |c: &str| !c.is_empty() && c != "." && c != ".." && c.bytes().all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"._-".contains(&b)); let (host, port) = r.registry.split_once(':').unwrap_or((&r.registry, "443")); let label = |l: &str| !l.is_empty() && l.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-'); if !host.split('.').all(label) || port.parse::().is_err() { bail!("bad registry {:?}", r.registry); } if !r.repo.split('/').all(comp) { bail!("bad repository {:?}", r.repo); } if r.reference.contains(':') { check_digest(&r.reference)?; } else if r.reference.is_empty() || r.reference.len() > 128 || !r.reference.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b)) || r.reference.starts_with(['.', '-']) { bail!("bad tag {:?}", r.reference); } Ok(()) } struct Client { agent: ureq::Agent, base: String, token: Option, } impl Client { fn get(&mut self, path: &str, accept: &str) -> Result { let url = format!("{}{}", self.base, path); for _ in 0..2 { let mut req = self.agent.get(&url).set("Accept", accept); if let Some(t) = &self.token { req = req.set("Authorization", &format!("Bearer {t}")); } match req.call() { Ok(r) => return Ok(r), Err(ureq::Error::Status(401, r)) if self.token.is_none() => { let h = r.header("www-authenticate").ok_or_else(|| anyhow!("401 without challenge"))?; self.token = Some(fetch_token(&self.agent, h)?); } Err(e) => return Err(anyhow!("GET {url}: {e}")), } } bail!("GET {url}: unauthorized (private images are not supported)") } fn json(&mut self, path: &str, accept: &str) -> Result<(Value, String)> { let mut body = Vec::new(); self.get(path, accept)?.into_reader().take(4 << 20).read_to_end(&mut body)?; // manifests are small let digest = format!("sha256:{}", hex::encode(Sha256::digest(&body))); Ok((serde_json::from_slice(&body)?, digest)) } } fn fetch_token(agent: &ureq::Agent, challenge: &str) -> Result { let rest = challenge.strip_prefix("Bearer ").ok_or_else(|| anyhow!("unsupported auth: {challenge}"))?; let mut realm = String::new(); let mut query = Vec::new(); for part in rest.split("\",") { let (k, v) = part.split_once('=').unwrap_or((part, "")); let v = v.trim_matches('"'); match k.trim() { "realm" => realm = v.to_string(), k => query.push((k.to_string(), v.to_string())), } } if !realm.starts_with("https://") { bail!("refusing non-https auth realm {realm:?}"); } let mut req = agent.get(&realm); for (k, v) in &query { req = req.query(k, v); } let v: Value = serde_json::from_reader(req.call()?.into_reader().take(1 << 20))?; v["token"].as_str().or(v["access_token"].as_str()).map(String::from).ok_or_else(|| anyhow!("no token in auth response")) } /// Tags move; re-resolve a cached tag after this long (digest refs never change). const REF_TTL: std::time::Duration = std::time::Duration::from_secs(24 * 3600); /// Pull (or reuse cached) image, returning its flattened rootfs tar + config. pub fn pull(home: &Path, image: &str, refresh: bool) -> Result { validate_ref(&parse_ref(image)).with_context(|| format!("invalid image reference {image:?}"))?; fs::create_dir_all(home.join("images/refs"))?; let ref_file = ref_path(home, image); let old = fs::read_to_string(&ref_file).ok().map(|d| d.trim().to_string()); let Some(img) = old.as_deref().and_then(|d| load(home, d).ok()) else { return fetch(home, image, &ref_file) }; let age = fs::metadata(&ref_file).and_then(|m| m.modified()).ok().and_then(|t| t.elapsed().ok()); if !refresh && (image.contains('@') || age.is_some_and(|a| a < REF_TTL)) { return Ok(img); } match fetch(home, image, &ref_file) { Ok(new) => { drop_unreferenced(home, &img.digest)?; // tag moved: the old multi-GB tar is dead weight Ok(new) } Err(e) if !refresh => { eprintln!("wsbx: could not re-resolve {image} ({e:#}); using cached copy"); Ok(img) } Err(e) => Err(e), } } /// Flattened name for humans plus a hash of the exact reference, so `a_b:t` and `a/b:t` never share a ref file. /// (Pre-hash ref files are simply ignored: the image is re-resolved and gc drops them.) fn ref_path(home: &Path, image: &str) -> PathBuf { let h = hex::encode(Sha256::digest(image)); home.join("images/refs").join(format!("{}-{}", image.replace(['/', ':', '@'], "_"), &h[..16])) } fn image_dir(home: &Path, digest: &str) -> PathBuf { home.join("images").join(digest.replace(':', "-")) } /// Remove an image unless some ref still points at it. A VM still provisioning from its tar keeps /// its open file descriptor, so unlinking under it is safe. fn drop_unreferenced(home: &Path, digest: &str) -> Result<()> { let used = fs::read_dir(home.join("images/refs"))?.flatten().any(|e| fs::read_to_string(e.path()).is_ok_and(|d| d.trim() == digest)); if !used && check_digest(digest).is_ok() { let _ = fs::remove_dir_all(image_dir(home, digest)); } Ok(()) } /// Delete every cached image not referenced (via its tag) by one of `keep`; returns bytes freed. pub fn gc(home: &Path, keep: &[String]) -> Result { let keep: HashSet = keep.iter().map(|i| ref_path(home, i)).collect(); let mut live = HashSet::new(); for e in fs::read_dir(home.join("images/refs"))?.flatten() { match fs::read_to_string(e.path()) { Ok(d) if keep.contains(&e.path()) => drop(live.insert(image_dir(home, d.trim()))), _ => fs::remove_file(e.path())?, } } let mut freed = 0; for e in fs::read_dir(home.join("images"))?.flatten() { let p = e.path(); if e.file_name() == "refs" || live.contains(&p) { continue; } // ponytail: also removes a .tmp dir of a pull running right now; that pull fails and can be retried freed += fs::read_dir(&p)?.flatten().filter_map(|f| f.metadata().ok()).map(|m| m.blocks() * 512).sum::(); fs::remove_dir_all(p)?; } Ok(freed) } fn fetch(home: &Path, image: &str, ref_file: &Path) -> Result { let r = parse_ref(image); let mut c = Client { agent: agent_for(&r.registry), base: format!("https://{}/v2/{}", r.registry, r.repo), token: None }; eprintln!("wsbx: resolving {image}"); let (mut manifest, mut digest) = c.json(&format!("/manifests/{}", r.reference), ACCEPT)?; if r.reference.contains(':') && digest != check_digest(&r.reference)? { bail!("{image}: registry returned manifest {digest}, not the requested digest"); } if let Some(list) = manifest["manifests"].as_array() { let m = list .iter() .find(|m| m["platform"]["os"] == "linux" && m["platform"]["architecture"] == "arm64") .ok_or_else(|| anyhow!("{image} has no linux/arm64 variant"))?; let d = check_digest(m["digest"].as_str().unwrap_or(""))?.to_string(); let (m2, d2) = c.json(&format!("/manifests/{d}"), ACCEPT)?; if d2 != d { bail!("manifest digest mismatch"); } (manifest, digest) = (m2, d2); } let dir = image_dir(home, &digest); if let Ok(img) = load(home, &digest) { fs::write(ref_file, &digest)?; // also bumps mtime -> TTL restarts return Ok(img); } let tmp = dir.with_extension("tmp"); let _ = fs::remove_dir_all(&tmp); fs::create_dir_all(&tmp)?; let cfg_digest = check_digest(manifest["config"]["digest"].as_str().unwrap_or(""))?; let cfg_path = tmp.join("config.json"); blob(&mut c, cfg_digest, &cfg_path)?; let cfg: Value = serde_json::from_slice(&fs::read(&cfg_path)?)?; if cfg["architecture"] != "arm64" { bail!("{image} is not arm64"); } let layers = manifest["layers"].as_array().ok_or_else(|| anyhow!("bad manifest"))?; let mut paths = Vec::new(); for (i, l) in layers.iter().enumerate() { let d = check_digest(l["digest"].as_str().unwrap_or(""))?; let size = l["size"].as_u64().unwrap_or(0); eprintln!("wsbx: layer {}/{} {} ({} MB)", i + 1, layers.len(), &d[7..19], size / 1_000_000); let p = tmp.join(format!("layer{i}")); blob(&mut c, d, &p)?; paths.push(p); } eprintln!("wsbx: flattening layers"); flatten(&paths, &tmp.join("rootfs.tar"))?; for p in paths { fs::remove_file(p)?; } let _ = fs::remove_dir_all(&dir); fs::rename(&tmp, &dir)?; fs::write(ref_file, &digest)?; load(home, &digest) } fn load(home: &Path, digest: &str) -> Result { let dir = image_dir(home, digest); let cfg: Value = serde_json::from_slice(&fs::read(dir.join("config.json"))?)?; let rootfs = dir.join("rootfs.tar"); if !rootfs.exists() { bail!("incomplete image"); } Ok(Image { rootfs, config: cfg["config"].clone(), digest: digest.to_string() }) } pub struct HashWriter(pub W, pub Sha256); impl Write for HashWriter { fn write(&mut self, b: &[u8]) -> std::io::Result { let n = self.0.write(b)?; self.1.update(&b[..n]); Ok(n) } fn flush(&mut self) -> std::io::Result<()> { self.0.flush() } } fn blob(c: &mut Client, digest: &str, out: &Path) -> Result<()> { let mut w = HashWriter(File::create(out)?, Sha256::new()); std::io::copy(&mut c.get(&format!("/blobs/{digest}"), "*/*")?.into_reader(), &mut w)?; let got = format!("sha256:{}", hex::encode(w.1.finalize())); if got != digest { bail!("digest mismatch for {digest}: got {got}"); } Ok(()) } fn open_layer(p: &Path) -> Result> { let mut f = BufReader::new(File::open(p)?); let mut magic = [0u8; 4]; f.read_exact(&mut magic)?; let f = BufReader::new(File::open(p)?); Ok(match magic { [0x1f, 0x8b, ..] => Box::new(flate2::read::GzDecoder::new(f)), [0x28, 0xb5, 0x2f, 0xfd] => bail!("zstd layers are not supported yet"), _ => Box::new(f), }) } fn norm(p: &Path) -> String { let s = p.to_string_lossy(); s.trim_start_matches("./").trim_start_matches('/').trim_end_matches('/').to_string() } /// Strict ancestors of a path: "a/b/c" -> "a", "a/b". fn ancestors(p: &str) -> impl Iterator { p.match_indices('/').map(move |(i, _)| &p[..i]) } /// Merge layers (bottom first in `layers`) into one tar. /// Pass 1 walks top-down to decide which entries survive; pass 2 writes bottom-up /// so parents are created before children and upper metadata wins. pub fn flatten(layers: &[PathBuf], out: &Path) -> Result<()> { let mut keep: Vec> = vec![Vec::new(); layers.len()]; let mut seen: HashSet = HashSet::new(); // every path provided by an upper layer let mut files: HashSet = HashSet::new(); // non-dirs from upper layers let mut gone: HashSet = HashSet::new(); // whiteouts + opaque dirs (hide lower content) let mut opaque: HashSet = HashSet::new(); for (li, lp) in layers.iter().enumerate().rev() { let (mut new_gone, mut new_opaque) = (Vec::new(), Vec::new()); let mut ar = tar::Archive::new(open_layer(lp)?); for e in ar.entries()? { let e = e?; let p = norm(&e.path()?); let (dir, name) = p.rsplit_once('/').map(|(d, n)| (d.to_string(), n)).unwrap_or((String::new(), p.as_str())); let mut k = false; if name == ".wh..wh..opq" { new_opaque.push(dir); } else if let Some(n) = name.strip_prefix(".wh.") { new_gone.push(if dir.is_empty() { n.to_string() } else { format!("{dir}/{n}") }); } else if !p.is_empty() { let is_dir = e.header().entry_type().is_dir(); let hidden = gone.contains(&p) || ancestors(&p).any(|a| gone.contains(a) || opaque.contains(a) || files.contains(a)) || (seen.contains(&p) && (!is_dir || files.contains(&p))); if !hidden { k = true; seen.insert(p.clone()); if !is_dir { files.insert(p); } } } keep[li].push(k); } gone.extend(new_gone); opaque.extend(new_opaque); } let mut b = tar::Builder::new(std::io::BufWriter::new(File::create(out)?)); for (li, lp) in layers.iter().enumerate() { let mut ar = tar::Archive::new(open_layer(lp)?); for (i, e) in ar.entries()?.enumerate() { let mut e = e?; if !keep[li][i] { continue; } let p = norm(&e.path()?); let src = e.header().clone(); let t = src.entry_type(); let mut h = tar::Header::new_gnu(); h.set_mode(src.mode()?); h.set_uid(src.uid()?); h.set_gid(src.gid()?); h.set_mtime(src.mtime().unwrap_or(0)); h.set_size(0); if t.is_symlink() || t.is_hard_link() { h.set_entry_type(t); let target = e.link_name()?.ok_or_else(|| anyhow!("link without target: {p}"))?; let target = if t.is_hard_link() { PathBuf::from(norm(&target)) } else { target.into_owned() }; b.append_link(&mut h, &p, target)?; } else if t.is_file() || t == tar::EntryType::Continuous { h.set_entry_type(tar::EntryType::Regular); h.set_size(src.size()?); b.append_data(&mut h, &p, &mut e)?; } else if t.is_dir() || t.is_character_special() || t.is_block_special() || t.is_fifo() { h.set_entry_type(t); h.set_device_major(src.device_major().ok().flatten().unwrap_or(0))?; h.set_device_minor(src.device_minor().ok().flatten().unwrap_or(0))?; b.append_data(&mut h, &p, std::io::empty())?; } } } b.into_inner()?.flush()?; Ok(()) } #[cfg(test)] mod tests { use super::*; fn layer(dir: &Path, name: &str, entries: &[(&str, bool)]) -> PathBuf { let p = dir.join(name); let mut b = tar::Builder::new(File::create(&p).unwrap()); for (path, is_dir) in entries { let mut h = tar::Header::new_gnu(); h.set_entry_type(if *is_dir { tar::EntryType::Directory } else { tar::EntryType::Regular }); h.set_mode(0o755); h.set_uid(0); h.set_gid(0); h.set_mtime(0); h.set_size(0); b.append_data(&mut h, path, std::io::empty()).unwrap(); } b.finish().unwrap(); p } #[test] fn whiteouts_and_opaque() { let d = std::env::temp_dir().join(format!("wsbx-test-{}", std::process::id())); fs::create_dir_all(&d).unwrap(); let l0 = layer(&d, "l0", &[("etc", true), ("etc/a", false), ("etc/b", false), ("opt", true), ("opt/x", false), ("f", false)]); let l1 = layer(&d, "l1", &[("etc/.wh.a", false), ("opt/.wh..wh..opq", false), ("opt/y", false), ("f", false)]); let out = d.join("out.tar"); flatten(&[l0, l1], &out).unwrap(); let mut ar = tar::Archive::new(File::open(&out).unwrap()); let got: Vec = ar.entries().unwrap().map(|e| norm(&e.unwrap().path().unwrap())).collect(); assert_eq!(got, ["etc", "etc/b", "opt", "opt/y", "f"]); fs::remove_dir_all(&d).unwrap(); } #[test] fn public_addresses() { for ip in ["127.0.0.1", "10.1.2.3", "172.16.0.1", "192.168.64.1", "169.254.169.254", "100.64.0.1", "0.0.0.0", "::1", "fe80::1", "fd00::1", "::ffff:127.0.0.1", "64:ff9b::7f00:1", "64:ff9b::a00:1", "64:ff9b:1::1", "2002:7f00:1::1", "2002:c0a8:101::", "::7f00:1", "::a00:1", "2001:db8::1", "2001::1", "ff02::1"] { assert!(!is_public(ip.parse().unwrap()), "{ip} must be rejected"); } for ip in ["8.8.8.8", "140.82.121.4", "2606:4700::6810:84e5", "64:ff9b::808:808", "2002:808:808::1", "::ffff:8.8.8.8"] { assert!(is_public(ip.parse().unwrap()), "{ip} must be allowed"); } } #[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"] { assert!(validate_ref(&parse_ref(bad)).is_err(), "{bad}"); } for ok in ["alpine", "ghcr.io/a/b:1.2", "docker/sandbox-templates:claude-code", "localhost:5000/x"] { assert!(validate_ref(&parse_ref(ok)).is_ok(), "{ok}"); } } #[test] fn ref_paths_distinct() { let h = Path::new("/h"); assert_ne!(ref_path(h, "evil_bar:v1"), ref_path(h, "evil/bar:v1")); assert_ne!(ref_path(h, "docker_sandbox-templates_claude-code"), ref_path(h, "docker/sandbox-templates:claude-code")); assert_eq!(ref_path(h, "alpine"), ref_path(h, "alpine")); } #[test] fn digests() { assert!(check_digest(&format!("sha256:{}", "a".repeat(64))).is_ok()); for bad in ["sha256:../../x", "sha256:ABC", "md5:00", ""] { assert!(check_digest(bad).is_err(), "{bad}"); } } #[test] fn refs() { let r = parse_ref("alpine"); assert_eq!((r.registry.as_str(), r.repo.as_str(), r.reference.as_str()), ("registry-1.docker.io", "library/alpine", "latest")); let r = parse_ref("ghcr.io/a/b:1.2"); assert_eq!((r.registry.as_str(), r.repo.as_str(), r.reference.as_str()), ("ghcr.io", "a/b", "1.2")); let r = parse_ref("localhost:5000/x"); assert_eq!((r.registry.as_str(), r.repo.as_str(), r.reference.as_str()), ("localhost:5000", "x", "latest")); } }