From e30360661e3074d14690cc7bafa3b229c77d4950 Mon Sep 17 00:00:00 2001 From: Precious Okwu Date: Sat, 28 Nov 2026 01:21:07 +0100 Subject: [PATCH] Avoid clobbering Windows docker contexts through WSL symlinks and ignore empty KIRI_UPDATE_URL (#269, #984) --- crates/kiri-win/src/update.rs | 7 ++--- crates/kiri-win/src/wslsync.rs | 47 ++++++++++++++++++++++++++++++---- crates/kiri/src/dockerctx.rs | 29 ++++++++++++++++++++- crates/kiri/src/update.rs | 13 ++++++++-- docs/fix-registry.md | 10 ++++++-- tests/regressions.tsv | 3 ++- 6 files changed, 95 insertions(+), 14 deletions(-) diff --git a/crates/kiri-win/src/update.rs b/crates/kiri-win/src/update.rs index 0cb3aea50..48cf5e6c3 100644 --- a/crates/kiri-win/src/update.rs +++ b/crates/kiri-win/src/update.rs @@ -135,9 +135,10 @@ fn first_line(bytes: &[u8], fallback: &str) -> String { } pub fn latest() -> io::Result { - let url = std::env::var("KIRI_UPDATE_URL").unwrap_or_else(|_| FEED.to_owned()); - allowed_url(&url)?; - let out = curl().args(["-fsSL", "--max-time", "20", "--max-filesize", "65536", "--"]).arg(&url).output()?; + let env_val = std::env::var("KIRI_UPDATE_URL").ok(); + let url = env_val.as_deref().map(str::trim).filter(|u| !u.is_empty()).unwrap_or(FEED); + allowed_url(url)?; + let out = curl().args(["-fsSL", "--max-time", "20", "--max-filesize", "65536", "--"]).arg(url).output()?; if !out.status.success() { return Err(io::Error::other(first_line(&out.stderr, "could not fetch the update feed"))); } diff --git a/crates/kiri-win/src/wslsync.rs b/crates/kiri-win/src/wslsync.rs index 7549fbe2e..87967ecad 100644 --- a/crates/kiri-win/src/wslsync.rs +++ b/crates/kiri-win/src/wslsync.rs @@ -372,10 +372,20 @@ pub fn wsl_enable_script(distro: &str, root: &str) -> String { elif pgrep -x dockerd >/dev/null 2>&1 || pidof dockerd >/dev/null 2>&1; then busy=1; fi; \ fi; \ [ \"$busy\" = 0 ] && ln -sfn {sock_target} \"$vr/docker.sock\"; \ - if [ -n \"$uhome\" ] && [ -d {host_prefix}\"$uhome\" ]; then \ - cdir={host_prefix}\"$uhome/.docker/contexts/meta/{KIRI_CONTEXT_HASH}\"; \ - mkdir -p \"$cdir\" 2>/dev/null && printf %s {} | base64 -d >\"$cdir/meta.json\" 2>/dev/null; \ - [ -n \"$uuid\" ] && [ -n \"$ugid\" ] && chown -R \"$uuid:$ugid\" {host_prefix}\"$uhome/.docker\" 2>/dev/null || true; \ + if [ -n \"$uhome\" ] && [ -d {host_prefix}\"$uhome\" ] && [ ! -L {host_prefix}\"$uhome/.docker\" ]; then \ + dctx={host_prefix}\"$uhome/.docker/contexts\"; \ + if [ -L \"$dctx\" ]; then \ + dt=$(readlink \"$dctx\" 2>/dev/null || true); \ + case \"$dt\" in */mnt/*) \ + tmp=\"$dctx.kiri.$$\"; rm -rf \"$tmp\"; mkdir -p \"$tmp\" && cp -r \"$dctx/.\" \"$tmp/\" 2>/dev/null || true; \ + rm -f \"$dctx\" && mv \"$tmp\" \"$dctx\" ;; \ + esac; \ + fi; \ + if [ ! -L \"$dctx\" ]; then \ + cdir=\"$dctx/meta/{KIRI_CONTEXT_HASH}\"; \ + mkdir -p \"$cdir\" 2>/dev/null && printf %s {} | base64 -d >\"$cdir/meta.json\" 2>/dev/null; \ + [ -n \"$uuid\" ] && [ -n \"$ugid\" ] && chown -R \"$uuid:$ugid\" {host_prefix}\"$uhome/.docker\" 2>/dev/null || true; \ + fi; \ fi; \ echo \"$gid\"", sh_quote(DEFAULT_USER_AWK), @@ -728,7 +738,34 @@ mod tests { assert!(std::fs::symlink_metadata(root.join("var/run/docker.sock")).is_err(), "symlink must be removed"); assert!(!sock_dir.exists(), "socket dir must be removed"); - // 4. When a live dockerd owns /var/run/docker.sock, enable leaves it untouched. + // 4. When Docker Desktop symlinked ~/.docker/contexts -> /mnt/c/Users//.docker/contexts, + // enable materializes a local Linux contexts dir instead of overwriting Windows's npipe kiri context. + let win_ctx = root.join("mnt/c/Users/Alice/.docker/contexts"); + let win_kiri = win_ctx.join("meta").join(KIRI_CONTEXT_HASH); + let win_colima = win_ctx.join("meta/colima-hash"); + std::fs::create_dir_all(&win_kiri).unwrap(); + std::fs::create_dir_all(&win_colima).unwrap(); + let win_meta_json = r#"{"Name":"kiri","Endpoints":{"docker":{"Host":"npipe:////./pipe/kiri"}}}"#; + std::fs::write(win_kiri.join("meta.json"), win_meta_json).unwrap(); + std::fs::write(win_colima.join("meta.json"), r#"{"Name":"colima"}"#).unwrap(); + let linux_ctx = root.join("home/alice/.docker/contexts"); + std::fs::remove_dir_all(&linux_ctx).unwrap(); + std::os::unix::fs::symlink(&win_ctx, &linux_ctx).unwrap(); + let out = Command::new("sh").args(["-c", &enable]).env("PATH", &path).output().unwrap(); + assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr)); + assert_eq!( + std::fs::read_to_string(win_kiri.join("meta.json")).unwrap(), + win_meta_json, + "Windows npipe kiri context must not be overwritten through a /mnt/* symlink" + ); + assert!(!std::fs::symlink_metadata(&linux_ctx).unwrap().file_type().is_symlink()); + assert!(linux_ctx.join("meta/colima-hash/meta.json").is_file(), "existing contexts are preserved"); + let linux_kiri = std::fs::read_to_string(linux_ctx.join("meta").join(KIRI_CONTEXT_HASH).join("meta.json")).unwrap(); + assert!(linux_kiri.contains("/mnt/wsl/kiri/sockets/NixOS/docker.sock"), "{linux_kiri}"); + let out = Command::new("sh").args(["-c", &disable]).env("PATH", &path).output().unwrap(); + assert!(out.status.success()); + + // 5. When a live dockerd owns /var/run/docker.sock, enable leaves it untouched. let own_sock = root.join("var/run/docker.sock"); let _listener = UnixListener::bind(&own_sock).unwrap(); std::fs::write(root.join("var/run/docker.pid"), std::process::id().to_string()).unwrap(); diff --git a/crates/kiri/src/dockerctx.rs b/crates/kiri/src/dockerctx.rs index b77850b7b..379b91227 100644 --- a/crates/kiri/src/dockerctx.rs +++ b/crates/kiri/src/dockerctx.rs @@ -413,7 +413,13 @@ pub fn repair_windows_config(dir: &Path, opts: WindowsRepairOpts<'_>) -> io::Res } let mut out = WindowsRepair::default(); let meta = context_meta(dir, opts.ours); - if opts.force_context_write || !meta.join("meta.json").is_file() { + let meta_file = meta.join("meta.json"); + let unix_host_on_npipe = opts.host.starts_with("npipe:") + && read_json(&meta_file) + .ok() + .and_then(|v| v["Endpoints"]["docker"]["Host"].as_str().map(|h| h.starts_with("unix:"))) + .unwrap_or(false); + if opts.force_context_write || !meta_file.is_file() || unix_host_on_npipe { write_context(&meta, opts.ours, opts.description, opts.host)?; out.context_written = true; } @@ -778,6 +784,27 @@ mod tests { assert_eq!(after["credHelpers"]["ecr"], "ecr-login"); assert_eq!(after["aliases"]["builder"], "buildx"); + // 2b. A Windows kiri context overwritten with a WSL unix:// socket (through a shared + // ~/.docker/contexts symlink) is self-healed back to npipe:// even without force_context_write. + write_context(&context_meta(&docker, "kiri"), "kiri", "Kiri", "unix:///mnt/wsl/kiri/sockets/Ubuntu/docker.sock").unwrap(); + let r_unix = repair_windows_config( + &docker, + WindowsRepairOpts { + ours: "kiri", + description: "Kiri (WSL2)", + host: "npipe:////./pipe/kiri", + plugins_dir: Some(&plugins), + force_context_write: false, + set_current_if_empty: false, + force_use: false, + desktop_installed: false, + }, + ) + .unwrap(); + assert!(r_unix.context_written); + let healed = read_json(&context_meta(&docker, "kiri").join("meta.json")).unwrap(); + assert_eq!(healed["Endpoints"]["docker"]["Host"], "npipe:////./pipe/kiri"); + // 3. Corrupt config.json is never overwritten. std::fs::write(docker.join("config.json"), "{broken").unwrap(); assert_eq!( diff --git a/crates/kiri/src/update.rs b/crates/kiri/src/update.rs index 236c21112..14b25c3df 100644 --- a/crates/kiri/src/update.rs +++ b/crates/kiri/src/update.rs @@ -110,9 +110,13 @@ fn agent() -> ureq::Agent { .into() } +fn feed_url(env_override: Option<&str>) -> &str { + env_override.map(str::trim).filter(|u| !u.is_empty()).unwrap_or(FEED) +} + pub fn latest() -> io::Result { - let url = std::env::var("KIRI_UPDATE_URL").unwrap_or_else(|_| FEED.to_owned()); - latest_from(&url) + let env_val = std::env::var("KIRI_UPDATE_URL").ok(); + latest_from(feed_url(env_val.as_deref())) } fn latest_from(url: &str) -> io::Result { @@ -710,6 +714,11 @@ mod tests { #[test] fn rejects_malformed_json_and_http_500() { + assert_eq!(super::feed_url(None), super::FEED); + assert_eq!(super::feed_url(Some("")), super::FEED); + assert_eq!(super::feed_url(Some(" ")), super::FEED); + assert_eq!(super::feed_url(Some("https://example.com/feed.json")), "https://example.com/feed.json"); + let bad_json = serve_feed("200 OK", "{not json"); let err = latest_from(&bad_json).unwrap_err(); assert_eq!(err.kind(), io::ErrorKind::InvalidData); diff --git a/docs/fix-registry.md b/docs/fix-registry.md index 4aa0938a0..c5659147a 100644 --- a/docs/fix-registry.md +++ b/docs/fix-registry.md @@ -13,15 +13,21 @@ A log of every merged bug fix in Kiri, newest first, together with any later bug ## Fixes +### #269, #984 Windows & WSL: avoid clobbering `%USERPROFILE%\.docker\contexts` through Docker Desktop's WSL symlink, self-heal `unix://` `kiri` context on Windows, and ignore empty `KIRI_UPDATE_URL` (unreleased) +- Fixes: #269 (enabling WSL integration when `~/.docker/contexts` in WSL was symlinked to `/mnt/c/Users//.docker/contexts` overwrote Windows's `npipe:////./pipe/kiri` `kiri` context with a Linux `unix://` socket), #984 (`KIRI_UPDATE_URL=""` from `setx` caused `kiri update --check` to fail with `http: empty string` instead of using the default feed) +- Caused: none known +- Caused by: #1002 +- Check: `crates/kiri-win/src/wslsync.rs`, `crates/kiri/src/dockerctx.rs`, `crates/kiri/src/update.rs` + ### #984 Windows CLI: support `kiri update --check` against the Windows release feed (unreleased) - Fixes: #984 (`kiri.exe update --check` fell through to `docker update --check` with `unknown flag: --check` on Windows) - Caused: none known - Caused by: - - Check: `crates/kiri/src/update.rs`, `crates/kiri/src/main.rs` -### #190, #269, #399, #744 Release gate fixes: virtiofs deleted-file revalidation across `docker run --rm`, offline `kiri domains set/reset`, netns lock takeover after stuck container start, transient `429` manifest retries, WSL `statvfs` `f_bfree` usage, and WSL `kiri` context hash (unreleased) +### #1002 Release gate fixes: virtiofs deleted-file revalidation across `docker run --rm`, offline `kiri domains set/reset`, netns lock takeover after stuck container start, transient `429` manifest retries, WSL `statvfs` `f_bfree` usage, and WSL `kiri` context hash (unreleased) - Fixes: #399 (back-to-back `docker run --rm -v` sees 1 s `ENOENT` recreating a host-deleted file), #744 (`kiri domains set` and `kiri domains reset` failed when the daemon was stopped), #190 (`statvfs_usage` used `f_bavail` instead of `f_bfree`, counting ~55 GB of ext4 reserved blocks on WSL's sparse `ext4.vhdx` as used), #269 (`wslsync.rs` hardcoded the wrong SHA-256 for Docker context `kiri`), `agent-dies-beside-stuck-start` (`claim_host_netns` failed when a dead agent's frozen child still held `@kiri-engine-netns`), `pulls-of-different-images-at-once-leave-a-sound-index` (`Client::manifest` did not retry HTTP `429` / transient registry errors) -- Caused: none known +- Caused: Enabling WSL integration in a distro where Docker Desktop had symlinked `~/.docker/contexts` to `/mnt/c/Users//.docker/contexts` overwrote Windows's `npipe:////./pipe/kiri` `kiri` context with `unix:///mnt/wsl/kiri/sockets//docker.sock` (found during 0.2.3 release-gate restore on Windows) - Caused by: #825, #846, #852, #970 - Check: `agent-dies-beside-stuck-start`, `pulls-of-different-images-at-once-leave-a-sound-index`, `crates/kiri/src/domains_cli.rs`, `crates/kiri-image/src/registry.rs`, `crates/kiri-win/src/wslsync.rs` diff --git a/tests/regressions.tsv b/tests/regressions.tsv index 8e27c6f21..d467962e0 100644 --- a/tests/regressions.tsv +++ b/tests/regressions.tsv @@ -338,7 +338,7 @@ bind-nonroot-parallel #796 concurrent non-root file and directory creation on a bind-nonroot-ro #796 non-root writes to a read-only 0444 bind-mount file must still fail with EACCES, including inside an unprivileged unshare -Ur user namespace crates/kiri/src/daemon/rofs.rs #768 when Kiri's WSL 2 ext4.vhdx or Linux storage filesystem was remounted read-only (emergency_ro), daemon startup, /info and diagnostics lacked actionable fsck / storage recovery steps crates/kiri-init/src/linux/sys.rs #768 on a read-only or emergency_ro storage filesystem, overlayfs silently mounted writable container rootfs read-only instead of refusing with actionable recovery steps -crates/kiri-win/src/wslsync.rs #768 kiri-start and kiri-win reported a generic WSL install failure instead of ext4.vhdx emergency_ro e2fsck recovery steps when Kiri's WSL 2 disk was remounted read-only, and KIRI_CONTEXT_HASH did not match sha256("kiri") (#269) +crates/kiri-win/src/wslsync.rs #768 kiri-start and kiri-win reported a generic WSL install failure instead of ext4.vhdx emergency_ro e2fsck recovery steps when Kiri's WSL 2 disk was remounted read-only, KIRI_CONTEXT_HASH did not match sha256("kiri"), and wsl_enable_script overwrote Windows's npipe kiri context when ~/.docker/contexts in WSL was symlinked into /mnt/ (#269) tls-company-root-pull #303 a registry signed by a root the system trusts failed UnknownIssuer: only the bundled roots were trusted tls-company-root-login #303 docker login to a registry signed by a root the system trusts failed UnknownIssuer tls-unknown-root-refused #303 a root the system does not trust must still be refused @@ -379,3 +379,4 @@ crates/kiri-win/src/refuse.rs #983 Windows pipe gate did not handle `POST /kiri/ crates/kiri/src/domains_cli.rs #744 `kiri domains set` and `kiri domains reset` failed with a daemon-not-running error instead of updating `domains.json` offline when the daemon was stopped crates/kiri-image/src/registry.rs #611 `Client::manifest` did not retry transient registry responses such as HTTP 429 during concurrent image pulls crates/kiri/src/daemon/storage.rs #190 `statvfs_usage` subtracted `f_bavail` instead of `f_bfree`, counting ~55 GB of ext4 root-reserved free blocks on WSL's sparse `ext4.vhdx` as used disk space +crates/kiri/src/dockerctx.rs #269 Windows `repair_windows_config` did not self-heal `%USERPROFILE%\.docker\contexts\meta\\meta.json` when overwritten with a WSL `unix://` socket endpoint