feat: split up code at logical boundaries

This commit is contained in:
2026-10-02 17:40:28 +02:00
parent 9706e9aaf2
commit a1bca2e188
10 changed files with 761 additions and 679 deletions
+4
View File
@@ -1,2 +1,6 @@
/target /target
/.llm-wiki/ /.llm-wiki/
#Ignore vscode AI rules
.github/instructions/codacy.instructions.md
+110
View File
@@ -0,0 +1,110 @@
//! Coding-agent sandboxes (Docker sandbox-templates images) and pi config mirroring.
use crate::vm::{Guest, run};
use crate::util::{base, sq};
use crate::Agent;
use anyhow::{Context, Result};
use serde_json::Value;
use std::fs;
use std::path::{Path, PathBuf};
/// Fallback when pi isn't installed on the host (otherwise the host's package + version are mirrored).
const PI_PKG: &str = "@earendil-works/pi-coding-agent";
/// User-level pi config worth sharing. Never npm/, bin/ (macOS binaries), sessions/, caches.
const PI_CONFIG: &[&str] = &[
"settings.json", "keybindings.json", "models.json", "mcp.json", "AGENTS.override.md", "AGENTS.md", "AGENTS.MD",
"CLAUDE.md", "CLAUDE.MD", "SYSTEM.md", "APPEND_SYSTEM.md", "extensions", "skills", "prompts", "themes",
];
// No official Docker template for pi: the generic `shell` template has node + npm; pi is installed
// (pinned to the host's version) onto the persistent sandbox disk.
pub fn pi(h: &Path, mut a: Agent, auth: bool) -> Result<()> {
let user_home = PathBuf::from(std::env::var_os("HOME").expect("HOME"));
let agent_dir = std::env::var_os("PI_CODING_AGENT_DIR").map(PathBuf::from).unwrap_or_else(|| user_home.join(".pi/agent"));
let agents_skills = user_home.join(".agents/skills"); // Agent Skills location pi also reads
let mut items = PI_CONFIG.to_vec();
if auth {
items.push("auth.json");
}
let mut stage: Vec<(PathBuf, String)> = items.iter().map(|i| (agent_dir.join(i), format!("pi/{i}"))).collect();
stage.push((agents_skills.clone(), "agents-skills".into()));
stage.retain(|(p, _)| p.exists());
let roots = [&agent_dir, &agents_skills].iter().filter_map(|p| fs::canonicalize(p).ok()).collect();
if let Ok(m) = fs::read(agent_dir.join("models.json")) {
let m: Value = serde_json::from_slice(&m).context("models.json")?;
for p in m["providers"].as_object().into_iter().flat_map(|p| p.values()) {
if let Some(port) = p["baseUrl"].as_str().and_then(local_port) {
a.o.host_ports.push(port);
}
}
}
let sh = match host_npm_pkg("pi") {
Some((pkg, v)) => format!(
"[ \"$(pi --version 2>/dev/null)\" = {v} ] || npm install -g --force {}@{v} >&2 || exit 1; exec pi \"$@\"",
sq(&pkg)
),
None => format!("command -v pi >/dev/null || npm install -g --force {PI_PKG} >&2 || exit 1; exec pi \"$@\""),
};
// Link each staged item into the guest's own ~/.pi/agent (which keeps its linux npm/, bin/, sessions/).
let setup = r#"PA="$HOME/.pi/agent"; S=/.wsbx/host/stage; $B mkdir -p "$PA" "$HOME/.agents"
for l in "$PA"/* "$PA"/.[!.]* "$HOME/.agents/skills"; do [ -L "$l" ] && $B rm -f "$l"; done
for f in "$S"/pi/*; do [ -e "$f" ] && $B rm -rf "$PA/${f##*/}" && $B ln -s "$f" "$PA/${f##*/}"; done
[ -e "$S/agents-skills" ] && $B rm -rf "$HOME/.agents/skills" && $B ln -s "$S/agents-skills" "$HOME/.agents/skills"
[ -z "$WSBX_USER" ] || $B chown "$WSBX_USER" "$HOME/.pi" "$PA" "$HOME/.agents"
"#;
let cmd = ["bash", "-c", &sh, "pi"].map(String::from).to_vec();
let keys = ["ANTHROPIC_API_KEY", "OPENAI_API_KEY", "GEMINI_API_KEY", "OPENROUTER_API_KEY", "GROQ_API_KEY", "XAI_API_KEY", "MISTRAL_API_KEY", "CEREBRAS_API_KEY", "ZAI_API_KEY"];
agent(h, a, "shell", "pi", cmd, &keys, Guest { stage, roots, setup: setup.into() })
}
/// npm package (name, version) behind a host command: follow the bin symlink up to its package.json.
fn host_npm_pkg(cmd: &str) -> Option<(String, String)> {
let bin = std::env::split_paths(&std::env::var_os("PATH")?).map(|d| d.join(cmd)).find(|p| p.is_file())?;
let real = fs::canonicalize(bin).ok()?;
let pj = real.ancestors().map(|d| d.join("package.json")).find(|p| p.is_file())?;
let v: Value = serde_json::from_slice(&fs::read(pj).ok()?).ok()?;
let (name, ver) = (v["name"].as_str()?, v["version"].as_str()?);
ver.chars().all(|c| c.is_ascii_alphanumeric() || ".-+".contains(c)).then(|| (name.into(), ver.into()))
}
/// Port of a URL pointing at the host's loopback, e.g. "http://127.0.0.1:8000/v1" -> 8000.
fn local_port(url: &str) -> Option<u16> {
let (scheme, rest) = url.split_once("://")?;
let auth = rest.split('/').next()?;
let (host, port) = match auth.rsplit_once(':') {
Some((h, p)) if !p.ends_with(']') => (h, p.parse().ok()?),
_ => (auth, if scheme == "https" { 443 } else { 80 }),
};
["127.0.0.1", "localhost", "[::1]", "0.0.0.0"].contains(&host).then_some(port)
}
pub fn agent(h: &Path, a: Agent, template: &str, short: &str, cmd: Vec<String>, keys: &[&str], g: Guest) -> Result<()> {
let mut o = a.o;
if o.workspace.is_none() {
o.workspace = Some(std::env::current_dir()?);
}
if o.name.is_none() {
o.name = Some(format!("{short}-{}", base(o.workspace.as_ref().unwrap())));
}
let set: Vec<String> = keys.iter().filter(|k| std::env::var_os(k).is_some()).map(|k| k.to_string()).collect();
if a.keys {
o.env.extend(set);
} else if !set.is_empty() {
eprintln!("wsbx: not forwarding {} (use --keys; code in the VM, e.g. a prompt-injected agent, could read it)", set.join(", "));
}
// Docker's sandbox templates: Cmd is the agent (e.g. `claude --dangerously-skip-permissions`) unless overridden
run(h, o, &format!("docker/sandbox-templates:{template}"), cmd, &a.args, g)
}
#[cfg(test)]
mod tests {
#[test]
fn local_port() {
use super::local_port as lp;
assert_eq!(lp("http://127.0.0.1:8000/v1"), Some(8000));
assert_eq!(lp("http://localhost:11434"), Some(11434));
assert_eq!(lp("http://[::1]:1234/v1"), Some(1234));
assert_eq!(lp("http://localhost/v1"), Some(80));
assert_eq!(lp("http://192.168.1.169:8080/v1"), None);
assert_eq!(lp("https://api.openai.com/v1"), None);
}
}
+61
View File
@@ -0,0 +1,61 @@
//! Sandbox root disk: empty ext4 seeded with a static busybox + stub init.
use crate::oci;
use crate::util::need;
use anyhow::{Result, bail};
use std::fs;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::process::Command;
/// Baked into each disk once; everything else comes fresh from the host share on every start.
const STUB_INIT: &str = "#!/.wsbx/busybox sh
/.wsbx/busybox mount -t virtiofs wsbx /.wsbx/host && exec /.wsbx/busybox sh /.wsbx/host/init
echo 'wsbx: cannot mount host share'; exec /.wsbx/busybox sh
";
/// Static busybox from the official busybox image; runs PID 1 + helpers in the guest.
fn busybox(h: &Path) -> Result<PathBuf> {
let p = h.join("busybox");
if p.exists() {
return Ok(p);
}
let img = oci::pull(h, "busybox:musl", false)?;
let tmp = h.join("busybox.tmp");
fs::create_dir_all(&tmp)?;
let ok = Command::new("tar").arg("-xf").arg(&img.rootfs).arg("-C").arg(&tmp).arg("bin").status()?.success();
if !ok {
bail!("failed to extract busybox");
}
// Must be a real file: a symlink would make us copy an arbitrary host file into guest disks.
if !fs::symlink_metadata(tmp.join("bin/busybox"))?.is_file() {
bail!("busybox image: bin/busybox is not a regular file");
}
fs::rename(tmp.join("bin/busybox"), &p)?;
fs::remove_dir_all(tmp)?;
Ok(p)
}
pub fn create_disk(h: &Path, disk: &Path, gib: u32) -> Result<()> {
let mke2fs = need("mke2fs", "e2fsprogs")?;
let seed = disk.with_extension("seed");
let _ = fs::remove_dir_all(&seed);
for d in [".wsbx/dev", ".wsbx/host"] {
fs::create_dir_all(seed.join(d))?;
}
fs::copy(busybox(h)?, seed.join(".wsbx/busybox"))?;
fs::write(seed.join(".wsbx/init"), STUB_INIT)?;
for f in ["busybox", "init"] {
fs::set_permissions(seed.join(".wsbx").join(f), fs::Permissions::from_mode(0o755))?;
}
let out = Command::new(mke2fs)
.args(["-q", "-F", "-t", "ext4", "-E", "root_owner=0:0", "-d"])
.arg(&seed)
.arg(disk)
.arg(format!("{gib}G"))
.output()?;
fs::remove_dir_all(&seed)?;
if !out.status.success() {
bail!("mke2fs failed: {}", String::from_utf8_lossy(&out.stderr));
}
Ok(())
}
+84
View File
@@ -0,0 +1,84 @@
//! Guest kernel: Kata Containers release kernel, downloaded and sha256-verified.
use crate::oci;
use anyhow::{Context, Result, anyhow, bail};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
const KATA_REPO: &str = "kata-containers/kata-containers";
/// Where Kata's release tarball keeps the guest kernel (VM-ready arm64 Image, all virtio built in).
const KATA_KERNEL: &str = "opt/kata/share/kata-containers/vmlinux.container";
pub fn installed_kernel(h: &Path) -> Option<String> {
let v: Value = serde_json::from_slice(&fs::read(h.join("kernel/source.json")).ok()?).ok()?;
v["version"].as_str().map(String::from)
}
/// Guest kernel from the latest (or pinned) Kata Containers release. Never re-downloads implicitly:
/// runs use whatever is installed; `wsbx kernel --update` moves to a newer release.
pub fn kernel(h: &Path, update: bool, version: Option<String>) -> Result<PathBuf> {
let k = h.join("kernel/vmlinux");
let installed = installed_kernel(h);
let pin = version.or_else(|| std::env::var("WSBX_KATA_VERSION").ok());
if k.exists() && !update && (pin.is_none() || pin == installed) {
return Ok(k);
}
if let Some(v) = &pin
&& (v.is_empty() || !v.chars().all(|c| c.is_ascii_alphanumeric() || "._-".contains(c)))
{
bail!("invalid Kata version {v:?}");
}
let api = match &pin {
Some(v) => format!("https://api.github.com/repos/{KATA_REPO}/releases/tags/{v}"),
None => format!("https://api.github.com/repos/{KATA_REPO}/releases/latest"),
};
let rel: Value = serde_json::from_reader(ureq::get(&api).call().with_context(|| format!("GET {api}"))?.into_reader())?;
let tag = rel["tag_name"].as_str().ok_or_else(|| anyhow!("no tag in {api}"))?;
if k.exists() && installed.as_deref() == Some(tag) {
eprintln!("wsbx: kernel already at Kata {tag}");
return Ok(k);
}
let asset = rel["assets"]
.as_array()
.into_iter()
.flatten()
.find(|a| a["name"].as_str().is_some_and(|n| n.starts_with("kata-static-") && n.ends_with("-arm64.tar.zst")))
.ok_or_else(|| anyhow!("Kata {tag} has no kata-static arm64 asset"))?;
let url = asset["browser_download_url"].as_str().ok_or_else(|| anyhow!("asset without url"))?;
eprintln!(
"wsbx: fetching kernel from Kata Containers {tag} ({} MB, streamed; only the kernel is kept)",
asset["size"].as_u64().unwrap_or(0) / 1_000_000
);
let tmp = h.join("kernel.tmp");
let _ = fs::remove_dir_all(&tmp);
fs::create_dir_all(&tmp)?;
// Stream download -> sha256 + bsdtar (zstd); nothing but the kernel touches the disk.
let mut tar = Command::new("tar")
.args(["-x", "--zstd", "-f", "-", "-C"])
.arg(&tmp)
.arg(format!("{}*", KATA_KERNEL.trim_end_matches(".container")))
.stdin(Stdio::piped())
.spawn()?;
let mut w = oci::HashWriter(tar.stdin.take().unwrap(), Sha256::new());
std::io::copy(&mut ureq::get(url).call()?.into_reader(), &mut w)?;
let oci::HashWriter(stdin, hasher) = w;
drop(stdin);
if !tar.wait()?.success() {
bail!("kernel extraction failed");
}
let got = format!("sha256:{}", hex::encode(hasher.finalize()));
match asset["digest"].as_str() {
Some(want) if want != got => bail!("kernel download checksum mismatch: want {want}, got {got}"),
Some(_) => {}
None => eprintln!("wsbx: warning: release publishes no digest; download not verified"),
}
let src = fs::canonicalize(tmp.join(KATA_KERNEL))?;
fs::create_dir_all(k.parent().unwrap())?;
fs::rename(src, &k)?;
let _ = fs::remove_file(h.join("kernel/SOURCE")); // pre-source.json format
fs::write(h.join("kernel/source.json"), serde_json::to_vec_pretty(&json!({"version": tag, "url": url, "sha256": got}))?)?;
fs::remove_dir_all(&tmp)?;
Ok(k)
}
+23 -679
View File
@@ -1,30 +1,21 @@
//! wsbx — Docker-Sandboxes-style microVMs on macOS (Virtualization.framework via vfkit). //! wsbx — Docker-Sandboxes-style microVMs on macOS (Virtualization.framework via vfkit).
mod agents;
mod disk;
mod kernel;
mod oci; mod oci;
mod sandbox;
mod stage;
mod tunnel;
mod util;
mod vm;
use anyhow::{Context, Result, anyhow, bail}; use anyhow::Result;
use clap::{Args, Parser, Subcommand}; use clap::{Args, Parser, Subcommand};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use std::fs; use std::fs;
use std::io::{IsTerminal, Read}; use std::os::unix::fs::PermissionsExt;
use std::os::unix::fs::DirBuilderExt; use std::path::PathBuf;
use std::net::{Shutdown, TcpStream}; use std::process::Command;
use std::os::unix::net::{UnixListener, UnixStream}; use vm::Guest;
use std::os::unix::fs::{MetadataExt, PermissionsExt};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
const KATA_REPO: &str = "kata-containers/kata-containers";
/// Where Kata's release tarball keeps the guest kernel (VM-ready arm64 Image, all virtio built in).
const KATA_KERNEL: &str = "opt/kata/share/kata-containers/vmlinux.container";
const INIT: &str = include_str!("init.sh");
/// Baked into each disk once; everything else comes fresh from the host share on every start.
const STUB_INIT: &str = "#!/.wsbx/busybox sh
/.wsbx/busybox mount -t virtiofs wsbx /.wsbx/host && exec /.wsbx/busybox sh /.wsbx/host/init
echo 'wsbx: cannot mount host share'; exec /.wsbx/busybox sh
";
const DHCP: &str = include_str!("dhcp.sh");
const FWD: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/wsbx-fwd"));
#[derive(Parser)] #[derive(Parser)]
#[command(name = "wsbx", about = "Launch coding agents or bare Linux in microVMs")] #[command(name = "wsbx", about = "Launch coding agents or bare Linux in microVMs")]
@@ -130,17 +121,6 @@ struct Opts {
host_ports: Vec<u16>, host_ports: Vec<u16>,
} }
/// Agent-specific guest preparation.
#[derive(Default)]
struct Guest {
/// (host path, name under /.wsbx/host/stage) copied before each start, symlinks resolved
stage: Vec<(PathBuf, String)>,
/// Symlinks inside staged trees may only resolve into these (canonical) roots
roots: Vec<PathBuf>,
/// Shell run as root after HOME/env are set, before dropping to the image user
setup: String,
}
fn home() -> Result<PathBuf> { fn home() -> Result<PathBuf> {
let h = std::env::var_os("WSBX_HOME") let h = std::env::var_os("WSBX_HOME")
.map(PathBuf::from) .map(PathBuf::from)
@@ -149,170 +129,24 @@ fn home() -> Result<PathBuf> {
fs::set_permissions(&h, fs::Permissions::from_mode(0o700))?; // holds run scripts with forwarded env fs::set_permissions(&h, fs::Permissions::from_mode(0o700))?; // holds run scripts with forwarded env
Ok(h) Ok(h)
} }
fn main() -> Result<()> { fn main() -> Result<()> {
let h = home()?; let h = home()?;
match Cli::parse().cmd { match Cli::parse().cmd {
Cmd::Kernel { update, version } => { Cmd::Kernel { update, version } => {
let k = kernel(&h, update, version)?; let k = kernel::kernel(&h, update, version)?;
println!("{} (Kata {})", k.display(), installed_kernel(&h).unwrap_or("unknown".into())); println!("{} (Kata {})", k.display(), kernel::installed_kernel(&h).unwrap_or("unknown".into()));
Ok(()) Ok(())
} }
Cmd::Pull { image } => oci::pull(&h, &image, true).map(|i| println!("{} {}", image, i.digest)), Cmd::Pull { image } => oci::pull(&h, &image, true).map(|i| println!("{} {}", image, i.digest)),
Cmd::Run { o, image, cmd } => run(&h, o, &image, cmd, &[], Guest::default()), Cmd::Run { o, image, cmd } => vm::run(&h, o, &image, cmd, &[], Guest::default()),
Cmd::Claude(a) => agent(&h, a, "claude-code", "claude", vec![], &["ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"], Guest::default()), Cmd::Claude(a) => agents::agent(&h, a, "claude-code", "claude", vec![], &["ANTHROPIC_API_KEY", "CLAUDE_CODE_OAUTH_TOKEN"], Guest::default()),
Cmd::Codex(a) => agent(&h, a, "codex", "codex", vec![], &["OPENAI_API_KEY"], Guest::default()), Cmd::Codex(a) => agents::agent(&h, a, "codex", "codex", vec![], &["OPENAI_API_KEY"], Guest::default()),
Cmd::Gemini(a) => agent(&h, a, "gemini", "gemini", vec![], &["GEMINI_API_KEY", "GOOGLE_API_KEY"], Guest::default()), Cmd::Gemini(a) => agents::agent(&h, a, "gemini", "gemini", vec![], &["GEMINI_API_KEY", "GOOGLE_API_KEY"], Guest::default()),
Cmd::Pi { a, pi_auth } => pi(&h, a, pi_auth), Cmd::Pi { a, pi_auth } => agents::pi(&h, a, pi_auth),
Cmd::Ls => ls(&h), Cmd::Ls => sandbox::ls(&h),
Cmd::Gc => { Cmd::Gc => sandbox::gc(&h),
let used: Vec<String> = fs::read_dir(h.join("sandboxes")) Cmd::Rm { names, disk } => sandbox::rm(&h, names, disk),
.into_iter()
.flatten()
.flatten()
.filter_map(|e| serde_json::from_slice::<Value>(&fs::read(e.path().join("meta.json")).ok()?).ok())
.filter_map(|m| m["image"].as_str().map(String::from))
.collect();
println!("freed {} MB", oci::gc(&h, &used)? / 1_000_000);
Ok(())
} }
Cmd::Rm { names, disk } => {
for n in names {
if n.is_empty() || sanitize(&n) != n {
bail!("invalid sandbox name {n:?}");
}
let d = h.join("sandboxes").join(&n);
if !d.join("meta.json").exists() {
bail!("no sandbox {n}");
}
// Held across the delete so the sandbox can't be started meanwhile.
let _lock = lock_stopped(&d).with_context(|| format!("sandbox {n} is running"))?;
if disk {
match fs::remove_file(d.join("disk.img")) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()),
_ => println!("removed disk of {n}"),
}
} else {
fs::remove_dir_all(d)?;
println!("removed {n}");
}
}
Ok(())
}
}
}
/// Fallback when pi isn't installed on the host (otherwise the host's package + version are mirrored).
const PI_PKG: &str = "@earendil-works/pi-coding-agent";
/// User-level pi config worth sharing. Never npm/, bin/ (macOS binaries), sessions/, caches.
const PI_CONFIG: &[&str] = &[
"settings.json", "keybindings.json", "models.json", "mcp.json", "AGENTS.override.md", "AGENTS.md", "AGENTS.MD",
"CLAUDE.md", "CLAUDE.MD", "SYSTEM.md", "APPEND_SYSTEM.md", "extensions", "skills", "prompts", "themes",
];
// No official Docker template for pi: the generic `shell` template has node + npm; pi is installed
// (pinned to the host's version) onto the persistent sandbox disk.
fn pi(h: &Path, mut a: Agent, auth: bool) -> Result<()> {
let user_home = PathBuf::from(std::env::var_os("HOME").expect("HOME"));
let agent_dir = std::env::var_os("PI_CODING_AGENT_DIR").map(PathBuf::from).unwrap_or_else(|| user_home.join(".pi/agent"));
let agents_skills = user_home.join(".agents/skills"); // Agent Skills location pi also reads
let mut items = PI_CONFIG.to_vec();
if auth {
items.push("auth.json");
}
let mut stage: Vec<(PathBuf, String)> = items.iter().map(|i| (agent_dir.join(i), format!("pi/{i}"))).collect();
stage.push((agents_skills.clone(), "agents-skills".into()));
stage.retain(|(p, _)| p.exists());
let roots = [&agent_dir, &agents_skills].iter().filter_map(|p| fs::canonicalize(p).ok()).collect();
if let Ok(m) = fs::read(agent_dir.join("models.json")) {
let m: Value = serde_json::from_slice(&m).context("models.json")?;
for p in m["providers"].as_object().into_iter().flat_map(|p| p.values()) {
if let Some(port) = p["baseUrl"].as_str().and_then(local_port) {
a.o.host_ports.push(port);
}
}
}
let sh = match host_npm_pkg("pi") {
Some((pkg, v)) => format!(
"[ \"$(pi --version 2>/dev/null)\" = {v} ] || npm install -g --force {}@{v} >&2 || exit 1; exec pi \"$@\"",
sq(&pkg)
),
None => format!("command -v pi >/dev/null || npm install -g --force {PI_PKG} >&2 || exit 1; exec pi \"$@\""),
};
// Link each staged item into the guest's own ~/.pi/agent (which keeps its linux npm/, bin/, sessions/).
let setup = r#"PA="$HOME/.pi/agent"; S=/.wsbx/host/stage; $B mkdir -p "$PA" "$HOME/.agents"
for l in "$PA"/* "$PA"/.[!.]* "$HOME/.agents/skills"; do [ -L "$l" ] && $B rm -f "$l"; done
for f in "$S"/pi/*; do [ -e "$f" ] && $B rm -rf "$PA/${f##*/}" && $B ln -s "$f" "$PA/${f##*/}"; done
[ -e "$S/agents-skills" ] && $B rm -rf "$HOME/.agents/skills" && $B ln -s "$S/agents-skills" "$HOME/.agents/skills"
[ -z "$WSBX_USER" ] || $B chown "$WSBX_USER" "$HOME/.pi" "$PA" "$HOME/.agents"
"#;
let cmd = ["bash", "-c", &sh, "pi"].map(String::from).to_vec();
let keys = ["ANTHROPIC_API_KEY", "OPENAI_API_KEY", "GEMINI_API_KEY", "OPENROUTER_API_KEY", "GROQ_API_KEY", "XAI_API_KEY", "MISTRAL_API_KEY", "CEREBRAS_API_KEY", "ZAI_API_KEY"];
agent(h, a, "shell", "pi", cmd, &keys, Guest { stage, roots, setup: setup.into() })
}
/// npm package (name, version) behind a host command: follow the bin symlink up to its package.json.
fn host_npm_pkg(cmd: &str) -> Option<(String, String)> {
let bin = std::env::split_paths(&std::env::var_os("PATH")?).map(|d| d.join(cmd)).find(|p| p.is_file())?;
let real = fs::canonicalize(bin).ok()?;
let pj = real.ancestors().map(|d| d.join("package.json")).find(|p| p.is_file())?;
let v: Value = serde_json::from_slice(&fs::read(pj).ok()?).ok()?;
let (name, ver) = (v["name"].as_str()?, v["version"].as_str()?);
ver.chars().all(|c| c.is_ascii_alphanumeric() || ".-+".contains(c)).then(|| (name.into(), ver.into()))
}
/// Port of a URL pointing at the host's loopback, e.g. "http://127.0.0.1:8000/v1" -> 8000.
fn local_port(url: &str) -> Option<u16> {
let (scheme, rest) = url.split_once("://")?;
let auth = rest.split('/').next()?;
let (host, port) = match auth.rsplit_once(':') {
Some((h, p)) if !p.ends_with(']') => (h, p.parse().ok()?),
_ => (auth, if scheme == "https" { 443 } else { 80 }),
};
["127.0.0.1", "localhost", "[::1]", "0.0.0.0"].contains(&host).then_some(port)
}
fn agent(h: &Path, a: Agent, template: &str, short: &str, cmd: Vec<String>, keys: &[&str], g: Guest) -> Result<()> {
let mut o = a.o;
if o.workspace.is_none() {
o.workspace = Some(std::env::current_dir()?);
}
if o.name.is_none() {
o.name = Some(format!("{short}-{}", base(o.workspace.as_ref().unwrap())));
}
let set: Vec<String> = keys.iter().filter(|k| std::env::var_os(k).is_some()).map(|k| k.to_string()).collect();
if a.keys {
o.env.extend(set);
} else if !set.is_empty() {
eprintln!("wsbx: not forwarding {} (use --keys; code in the VM, e.g. a prompt-injected agent, could read it)", set.join(", "));
}
// Docker's sandbox templates: Cmd is the agent (e.g. `claude --dangerously-skip-permissions`) unless overridden
run(h, o, &format!("docker/sandbox-templates:{template}"), cmd, &a.args, g)
}
fn base(p: &Path) -> String {
p.file_name().map(|s| s.to_string_lossy().into_owned()).unwrap_or_else(|| "root".into())
}
fn sanitize(s: &str) -> String {
s.chars().map(|c| if c.is_ascii_alphanumeric() || c == '-' { c.to_ascii_lowercase() } else { '-' }).collect()
}
fn sq(s: &str) -> String {
format!("'{}'", s.replace('\'', r"'\''"))
}
/// Find a tool on PATH, else in its Homebrew keg (keg-only formulae like e2fsprogs aren't linked).
fn need(tool: &str, formula: &str) -> Result<PathBuf> {
let on_path = std::env::var_os("PATH").map(|p| std::env::split_paths(&p).collect::<Vec<_>>()).unwrap_or_default();
let find = |dirs: &[PathBuf]| dirs.iter().map(|d| d.join(tool)).find(|p| p.is_file());
find(&on_path)
.or_else(|| {
let o = Command::new("brew").args(["--prefix", formula]).output().ok()?;
let keg = PathBuf::from(String::from_utf8_lossy(&o.stdout).trim());
find(&[keg.join("sbin"), keg.join("bin")])
})
.ok_or_else(|| anyhow!("{tool} not found — install it with: brew install {formula}"))
} }
fn default_cpus() -> u32 { fn default_cpus() -> u32 {
@@ -324,493 +158,3 @@ fn default_memory() -> u32 {
let bytes: u64 = host.and_then(|o| String::from_utf8_lossy(&o.stdout).trim().parse().ok()).unwrap_or(8 << 30); let bytes: u64 = host.and_then(|o| String::from_utf8_lossy(&o.stdout).trim().parse().ok()).unwrap_or(8 << 30);
((bytes >> 20) / 4).clamp(1024, 8192) as u32 ((bytes >> 20) / 4).clamp(1024, 8192) as u32
} }
fn installed_kernel(h: &Path) -> Option<String> {
let v: Value = serde_json::from_slice(&fs::read(h.join("kernel/source.json")).ok()?).ok()?;
v["version"].as_str().map(String::from)
}
/// Guest kernel from the latest (or pinned) Kata Containers release. Never re-downloads implicitly:
/// runs use whatever is installed; `wsbx kernel --update` moves to a newer release.
fn kernel(h: &Path, update: bool, version: Option<String>) -> Result<PathBuf> {
let k = h.join("kernel/vmlinux");
let installed = installed_kernel(h);
let pin = version.or_else(|| std::env::var("WSBX_KATA_VERSION").ok());
if k.exists() && !update && (pin.is_none() || pin == installed) {
return Ok(k);
}
if let Some(v) = &pin
&& (v.is_empty() || !v.chars().all(|c| c.is_ascii_alphanumeric() || "._-".contains(c)))
{
bail!("invalid Kata version {v:?}");
}
let api = match &pin {
Some(v) => format!("https://api.github.com/repos/{KATA_REPO}/releases/tags/{v}"),
None => format!("https://api.github.com/repos/{KATA_REPO}/releases/latest"),
};
let rel: Value = serde_json::from_reader(ureq::get(&api).call().with_context(|| format!("GET {api}"))?.into_reader())?;
let tag = rel["tag_name"].as_str().ok_or_else(|| anyhow!("no tag in {api}"))?;
if k.exists() && installed.as_deref() == Some(tag) {
eprintln!("wsbx: kernel already at Kata {tag}");
return Ok(k);
}
let asset = rel["assets"]
.as_array()
.into_iter()
.flatten()
.find(|a| a["name"].as_str().is_some_and(|n| n.starts_with("kata-static-") && n.ends_with("-arm64.tar.zst")))
.ok_or_else(|| anyhow!("Kata {tag} has no kata-static arm64 asset"))?;
let url = asset["browser_download_url"].as_str().ok_or_else(|| anyhow!("asset without url"))?;
eprintln!(
"wsbx: fetching kernel from Kata Containers {tag} ({} MB, streamed; only the kernel is kept)",
asset["size"].as_u64().unwrap_or(0) / 1_000_000
);
let tmp = h.join("kernel.tmp");
let _ = fs::remove_dir_all(&tmp);
fs::create_dir_all(&tmp)?;
// Stream download -> sha256 + bsdtar (zstd); nothing but the kernel touches the disk.
let mut tar = Command::new("tar")
.args(["-x", "--zstd", "-f", "-", "-C"])
.arg(&tmp)
.arg(format!("{}*", KATA_KERNEL.trim_end_matches(".container")))
.stdin(Stdio::piped())
.spawn()?;
let mut w = oci::HashWriter(tar.stdin.take().unwrap(), Sha256::new());
std::io::copy(&mut ureq::get(url).call()?.into_reader(), &mut w)?;
let oci::HashWriter(stdin, hasher) = w;
drop(stdin);
if !tar.wait()?.success() {
bail!("kernel extraction failed");
}
let got = format!("sha256:{}", hex::encode(hasher.finalize()));
match asset["digest"].as_str() {
Some(want) if want != got => bail!("kernel download checksum mismatch: want {want}, got {got}"),
Some(_) => {}
None => eprintln!("wsbx: warning: release publishes no digest; download not verified"),
}
let src = fs::canonicalize(tmp.join(KATA_KERNEL))?;
fs::create_dir_all(k.parent().unwrap())?;
fs::rename(src, &k)?;
let _ = fs::remove_file(h.join("kernel/SOURCE")); // pre-source.json format
fs::write(h.join("kernel/source.json"), serde_json::to_vec_pretty(&json!({"version": tag, "url": url, "sha256": got}))?)?;
fs::remove_dir_all(&tmp)?;
Ok(k)
}
/// Static busybox from the official busybox image; runs PID 1 + helpers in the guest.
fn busybox(h: &Path) -> Result<PathBuf> {
let p = h.join("busybox");
if p.exists() {
return Ok(p);
}
let img = oci::pull(h, "busybox:musl", false)?;
let tmp = h.join("busybox.tmp");
fs::create_dir_all(&tmp)?;
let ok = Command::new("tar").arg("-xf").arg(&img.rootfs).arg("-C").arg(&tmp).arg("bin").status()?.success();
if !ok {
bail!("failed to extract busybox");
}
// Must be a real file: a symlink would make us copy an arbitrary host file into guest disks.
if !fs::symlink_metadata(tmp.join("bin/busybox"))?.is_file() {
bail!("busybox image: bin/busybox is not a regular file");
}
fs::rename(tmp.join("bin/busybox"), &p)?;
fs::remove_dir_all(tmp)?;
Ok(p)
}
fn create_disk(h: &Path, disk: &Path, gib: u32) -> Result<()> {
let mke2fs = need("mke2fs", "e2fsprogs")?;
let seed = disk.with_extension("seed");
let _ = fs::remove_dir_all(&seed);
for d in [".wsbx/dev", ".wsbx/host"] {
fs::create_dir_all(seed.join(d))?;
}
fs::copy(busybox(h)?, seed.join(".wsbx/busybox"))?;
fs::write(seed.join(".wsbx/init"), STUB_INIT)?;
for f in ["busybox", "init"] {
fs::set_permissions(seed.join(".wsbx").join(f), fs::Permissions::from_mode(0o755))?;
}
let out = Command::new(mke2fs)
.args(["-q", "-F", "-t", "ext4", "-E", "root_owner=0:0", "-d"])
.arg(&seed)
.arg(disk)
.arg(format!("{gib}G"))
.output()?;
fs::remove_dir_all(&seed)?;
if !out.status.success() {
bail!("mke2fs failed: {}", String::from_utf8_lossy(&out.stderr));
}
Ok(())
}
fn strs(v: &Value) -> Vec<String> {
v.as_array().map(|a| a.iter().filter_map(|s| s.as_str().map(String::from)).collect()).unwrap_or_default()
}
fn run(h: &Path, o: Opts, image: &str, cmd: Vec<String>, extra: &[String], g: Guest) -> Result<()> {
let vfkit = need("vfkit", "vfkit")?;
let kernel = match &o.kernel {
Some(k) => k.clone(),
None => kernel(h, false, None)?,
};
let ws = o.workspace.as_ref().map(fs::canonicalize).transpose().context("workspace")?;
vf(h)?; // everything vfkit gets lives under h, plus kernel + workspace: refuse bad paths before creating anything
vf(&kernel)?;
if let Some(ws) = &ws {
vf(ws)?;
let hc = fs::canonicalize(h)?;
if ws.starts_with(&hc) || hc.starts_with(ws) {
bail!("workspace {} overlaps the wsbx state dir {} (disks, kernel, control files)", ws.display(), hc.display());
}
}
let img = oci::pull(h, image, o.pull)?;
let short = image.rsplit('/').next().unwrap().split([':', '@']).next().unwrap();
let name = sanitize(&o.name.clone().unwrap_or_else(|| format!("{short}-{}", ws.as_deref().map(base).unwrap_or("vm".into()))));
let dir = h.join("sandboxes").join(&name);
fs::create_dir_all(&dir)?;
let _lock = lock_stopped(&dir).map_err(|_| anyhow!("sandbox {name} is already running"))?;
// The control share is writable by the guest (virtio-fs has no host-enforced read-only).
// Recreate it empty on every start -- while this sandbox's VM is stopped and we hold the lock --
// so nothing a previous guest left there (e.g. `run.sh -> ~/.zshrc`) is ever written through.
// Persistent state lives in meta.json, which the guest cannot reach.
let share = dir.join("share");
match fs::remove_dir_all(&share) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()),
_ => {}
}
std::fs::DirBuilder::new().mode(0o700).create(&share)?;
let disk = dir.join("disk.img");
let meta_path = dir.join("meta.json");
if o.fresh {
let _ = fs::remove_file(&disk);
}
let mut meta: Value = fs::read(&meta_path).ok().and_then(|m| serde_json::from_slice(&m).ok()).unwrap_or(Value::Null);
if disk.exists() && meta["digest"] != img.digest.as_str() {
eprintln!("wsbx: note: {name} was created from an older image; use --fresh to rebuild");
}
if !disk.exists() {
eprintln!("wsbx: creating sandbox {name}");
create_disk(h, &disk, o.disk)?;
meta = json!({"image": image, "digest": img.digest, "workspace": ws, "created": now(), "provisioned": false});
fs::write(&meta_path, serde_json::to_vec_pretty(&meta)?)?;
}
// Build the guest-side run script (executed as root by init, then drops to the image user).
let c = &img.config;
let mut argv = strs(&c["Entrypoint"]);
argv.extend(if cmd.is_empty() { strs(&c["Cmd"]) } else { cmd });
argv.extend(extra.iter().cloned());
if argv.is_empty() {
argv.push("/bin/sh".into());
}
let user = c["User"].as_str().unwrap_or("").to_string();
let wd = ws
.as_ref()
.map(|p| p.to_string_lossy().into_owned())
.or_else(|| c["WorkingDir"].as_str().filter(|s| !s.is_empty()).map(String::from))
.unwrap_or("/".into());
let mut s = String::from("B=/.wsbx/busybox\n");
s += &format!("$B date -s @{} >/dev/null\n", now());
s += &format!("$B hostname {name}; echo {name} > /etc/hostname; $B grep -q ' {name}$' /etc/hosts 2>/dev/null || echo '127.0.1.1 {name}' >> /etc/hosts\n");
if let Some((rows @ 1.., cols @ 1..)) = term_size() {
s += &format!("$B stty rows {rows} cols {cols}\n");
}
if let Some(ws) = &ws {
let w = sq(&ws.to_string_lossy());
s += &format!("$B mkdir -p {w} && $B mount -t virtiofs ws {w}\n");
}
let u = user.split(':').next().unwrap_or("");
s += &format!("export HOME=$($B awk -F: -v u={} '$1==u||$3==u{{print $6;exit}}' /etc/passwd)\n", sq(if u.is_empty() { "root" } else { u }));
s += "[ -n \"$HOME\" ] || export HOME=/root\n";
s += &format!("export TERM={} TINI_SUBREAPER=1\n", sq(&std::env::var("TERM").unwrap_or("xterm-256color".into())));
let mut env = strs(&c["Env"]);
for e in &o.env {
match e.split_once('=') {
Some(_) => env.push(e.clone()),
None => env.push(format!("{e}={}", std::env::var(e).with_context(|| format!("env {e} not set on host"))?)),
}
}
for e in env {
if let Some((k, v)) = e.split_once('=') {
s += &format!("export {k}={}\n", sq(v));
}
}
s += &format!("export WSBX_USER={}\n", sq(&user));
let stage = share.join("stage");
fs::create_dir(&stage)?;
let (mut budget, mut seen) = (STAGE_LIMIT, std::collections::HashSet::new());
for (src, to) in &g.stage {
let dst = stage.join(to);
fs::create_dir_all(dst.parent().unwrap())?;
copy_tree(src, &dst, &g.roots, &mut budget, &mut seen)?;
}
s += &g.setup;
// Host tunnel: guest wsbx-fwd listens on localhost:PORT -> vsock -> vfkit -> unix socket -> host localhost:PORT
let mut ports = o.host_ports.clone();
ports.sort();
ports.dedup();
let mut vsock = Vec::new();
if !ports.is_empty() {
let fwd = share.join("fwd");
fs::write(&fwd, FWD)?;
fs::set_permissions(&fwd, fs::Permissions::from_mode(0o755))?;
let mut specs = Vec::new();
for (i, &p) in ports.iter().enumerate() {
let vport = 10000 + i as u32;
let sock = dir.join(format!("p{p}.sock"));
tunnel(&sock, p)?;
vsock.push(format!("virtio-vsock,port={vport},socketURL={},listen", vf(&sock)?));
specs.push(format!("{p}={vport}"));
}
s += "$B mkfifo /run/wsbx-fwd\n";
s += &format!("/.wsbx/host/fwd {} >/run/wsbx-fwd 2>/.wsbx/host/fwd.log &\n", specs.join(" "));
s += "read _ </run/wsbx-fwd\n";
eprintln!("wsbx: host localhost:{{{}}} tunneled into the VM", ports.iter().map(u16::to_string).collect::<Vec<_>>().join(","));
}
s += &format!("cd {} 2>/dev/null || cd /\n", sq(&wd));
let argv: Vec<String> = argv.iter().map(|a| sq(a)).collect();
if u.is_empty() || u == "root" || u == "0" {
s += &format!("exec {}\n", argv.join(" "));
} else {
s += &format!("exec $B chpst -u {} {}\n", sq(&user), argv.join(" "));
}
fs::write(share.join("run.sh"), s)?;
for (f, body) in [("init", INIT), ("dhcp", DHCP)] {
fs::write(share.join(f), body)?;
fs::set_permissions(share.join(f), fs::Permissions::from_mode(0o755))?;
}
let _ = fs::remove_file(share.join("exit"));
let initrd = h.join("empty.initrd"); // vfkit insists on an initrd; an empty one is fine
if !initrd.exists() {
fs::write(&initrd, "")?;
}
let mac = {
let x = name.bytes().fold(0xcbf29ce484222325u64, |a, b| (a ^ b as u64).wrapping_mul(0x100000001b3));
format!("02:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", x as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8, (x >> 32) as u8)
};
// panic=1 reboots, which VZ turns into a restart; init powers off on any second boot (see init.sh).
let cmdline = "console=hvc0 root=/dev/vda rw rootfstype=ext4 init=/.wsbx/init quiet panic=1";
let tty = std::io::stdin().is_terminal();
let mut v = Command::new(vfkit);
v.args(["--cpus", &o.cpus.to_string(), "--memory", &o.memory.to_string(), "--log-level", "error"])
.arg("--bootloader")
.arg(format!("linux,kernel={},initrd={},cmdline=\"{cmdline}\"", vf(&kernel)?, vf(&initrd)?))
.arg("--device")
.arg(format!("virtio-blk,path={}", vf(&disk)?))
.arg("--device")
.arg(format!("virtio-net,nat,mac={mac}"))
.arg("--device")
.arg(format!("virtio-fs,sharedDir={},mountTag=wsbx", vf(&share)?))
.args(["--device", "virtio-rng", "--device"])
// vfkit's stdio console needs a TTY; otherwise stream output only (no stdin)
.arg(if tty { "virtio-serial,stdio".into() } else { format!("virtio-serial,logFilePath={}", vf(&out_path())?) })
.stderr(fs::File::create(dir.join("vfkit.log"))?);
if let Some(ws) = &ws {
v.arg("--device").arg(format!("virtio-fs,sharedDir={},mountTag=ws", vf(ws)?));
}
// Image tar is only needed until the guest has unpacked it; read-only so sandboxes can share it.
let provisioned = meta["provisioned"].as_bool().unwrap_or(false);
if !provisioned {
v.arg("--device").arg(format!("virtio-blk,path={},readonly", vf(&img.rootfs)?));
}
for d in vsock {
v.arg("--device").arg(d);
}
let saved = stty(&["-g"]);
if saved.is_some() {
stty(&["raw", "-echo"]);
}
let status = v.stdin(Stdio::inherit()).status();
if let Some(s) = saved {
stty(&[s.trim()]);
}
let status = status?;
// The VM is gone now, so the guest can't race these reads; still never follow its symlinks.
let code = guest_output(&share, "exit").and_then(|s| s.trim().parse().ok());
if !provisioned && guest_output(&share, "provisioned").is_some() && meta.is_object() {
meta["provisioned"] = json!(true);
fs::write(&meta_path, serde_json::to_vec_pretty(&meta)?)?;
}
let _ = fs::remove_dir_all(&share); // run.sh contains forwarded env
match code {
Some(c) => std::process::exit(c),
None if !status.success() => bail!("vm failed, see {}", dir.join("vfkit.log").display()),
None => bail!("VM stopped without an exit status (kernel panic, reboot or poweroff inside the VM)"),
}
}
/// Small result file written by the guest; regular files only (no symlinks), size-capped.
fn guest_output(share: &Path, name: &str) -> Option<String> {
let p = share.join(name);
fs::symlink_metadata(&p).ok().filter(|m| m.is_file())?;
let mut s = String::new();
fs::File::open(p).ok()?.take(64).read_to_string(&mut s).ok()?;
Some(s)
}
/// vfkit splits device options on ',' and the bootloader cmdline is quoted: a path containing
/// those (e.g. a workspace named `repo,sharedDir=/Users/me`) would inject options. Refuse them.
fn vf(p: &Path) -> Result<String> {
let s = p.to_str().ok_or_else(|| anyhow!("non-UTF-8 path {}", p.display()))?;
if s.chars().any(|c| c == ',' || c == '"' || c.is_control()) {
bail!("unsupported character in path {s:?} (',', '\"' and control characters would inject vfkit options)");
}
Ok(s.to_string())
}
/// Where our stdout points (vfkit only accepts a path for non-TTY console output).
fn out_path() -> PathBuf {
fs::read_link("/dev/fd/1").unwrap_or("/dev/stdout".into())
}
/// Host end of a tunnel: vfkit dials this unix socket for each guest vsock connection.
fn tunnel(sock: &Path, port: u16) -> Result<()> {
if sock.as_os_str().len() > 100 {
bail!("socket path too long: {} (set WSBX_HOME to something shorter)", sock.display());
}
let _ = fs::remove_file(sock);
let l = UnixListener::bind(sock)?;
std::thread::spawn(move || {
for u in l.incoming().flatten() {
std::thread::spawn(move || {
if let Ok(t) = TcpStream::connect(("localhost", port)) {
pipe(u, t);
}
});
}
});
Ok(())
}
fn pipe(mut u: UnixStream, mut t: TcpStream) {
let (Ok(mut u2), Ok(mut t2)) = (u.try_clone(), t.try_clone()) else { return };
let up = std::thread::spawn(move || {
let _ = std::io::copy(&mut u2, &mut t2);
let _ = t2.shutdown(Shutdown::Write);
});
let _ = std::io::copy(&mut t, &mut u);
let _ = u.shutdown(Shutdown::Write);
let _ = up.join();
}
const STAGE_LIMIT: u64 = 256 << 20;
/// Copy config into the stage, resolving symlinks (they'd dangle in the VM) but only when they
/// resolve inside `roots`; no cycles, regular files and dirs only (a FIFO would hang fs::copy).
fn copy_tree(src: &Path, dst: &Path, roots: &[PathBuf], budget: &mut u64, seen: &mut std::collections::HashSet<PathBuf>) -> Result<()> {
let Ok(real) = fs::canonicalize(src) else { return Ok(()) }; // dangling symlink
if !roots.iter().any(|r| real.starts_with(r)) {
eprintln!("wsbx: not sharing {} -> {} (outside the shared config)", src.display(), real.display());
return Ok(());
}
let m = fs::metadata(&real)?;
if m.is_dir() {
if !seen.insert(real.clone()) {
return Ok(()); // symlink cycle or already copied
}
fs::create_dir_all(dst)?;
for e in fs::read_dir(&real)? {
let e = e?;
copy_tree(&e.path(), &dst.join(e.file_name()), roots, budget, seen)?;
}
} else if m.is_file() {
*budget = budget.checked_sub(m.len()).ok_or_else(|| anyhow!("shared config exceeds {} MB", STAGE_LIMIT >> 20))?;
fs::copy(&real, dst)?;
}
Ok(())
}
fn stty(args: &[&str]) -> Option<String> {
let out = Command::new("stty").args(args).stdin(Stdio::inherit()).stderr(Stdio::null()).output().ok()?;
out.status.success().then(|| String::from_utf8_lossy(&out.stdout).into_owned())
}
fn term_size() -> Option<(u32, u32)> {
let s = stty(&["size"])?;
let mut it = s.split_whitespace().map(|x| x.parse().ok());
Some((it.next()??, it.next()??))
}
fn now() -> u64 {
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs()
}
/// The lock `run` holds for the VM's lifetime; Err if the sandbox is running.
fn lock_stopped(dir: &Path) -> Result<fs::File> {
let f = fs::File::create(dir.join("lock"))?;
f.try_lock().map_err(|_| anyhow!("locked by a running VM"))?;
Ok(f)
}
fn ls(h: &Path) -> Result<()> {
println!("{:<28} {:<8} {:<40} {:>8} WORKSPACE", "NAME", "STATUS", "IMAGE", "DISK");
let Ok(rd) = fs::read_dir(h.join("sandboxes")) else { return Ok(()) };
let mut rows: Vec<_> = rd.flatten().collect();
rows.sort_by_key(|e| e.file_name());
for e in rows {
let Ok(m) = fs::read(e.path().join("meta.json")) else { continue };
let m: Value = serde_json::from_slice(&m)?;
let used = fs::metadata(e.path().join("disk.img")).map(|m| m.blocks() * 512).unwrap_or(0);
let status = if lock_stopped(&e.path()).is_err() { "running" } else { "stopped" };
println!(
"{:<28} {:<8} {:<40} {:>6}MB {}",
e.file_name().to_string_lossy(),
status,
m["image"].as_str().unwrap_or("?"),
used / 1_000_000,
m["workspace"].as_str().unwrap_or("-")
);
}
Ok(())
}
#[cfg(test)]
mod tests {
use std::path::Path;
#[test]
fn vfkit_injection_rejected() {
assert!(super::vf(Path::new("/Users/me/repo")).is_ok());
for bad in ["/shared/repo/,sharedDir=/Users/me", "/a\"b", "/a\nb"] {
assert!(super::vf(Path::new(bad)).is_err(), "{bad}");
}
}
#[test]
fn copy_tree_contains_symlinks() {
use std::os::unix::fs::symlink;
let t = std::env::temp_dir().join(format!("wsbx-ct-{}", std::process::id()));
let (root, out, secret) = (t.join("root"), t.join("out"), t.join("secret"));
std::fs::create_dir_all(root.join("skills/a")).unwrap();
std::fs::create_dir_all(&secret).unwrap();
std::fs::write(root.join("skills/a/SKILL.md"), "ok").unwrap();
std::fs::write(secret.join("id_rsa"), "key").unwrap();
symlink(&secret, root.join("skills/leak")).unwrap(); // escapes the root
symlink(root.join("skills"), root.join("skills/a/loop")).unwrap(); // cycle
let roots = vec![std::fs::canonicalize(&root).unwrap()];
let (mut budget, mut seen) = (super::STAGE_LIMIT, Default::default());
super::copy_tree(&root.join("skills"), &out, &roots, &mut budget, &mut seen).unwrap();
assert_eq!(std::fs::read_to_string(out.join("a/SKILL.md")).unwrap(), "ok");
assert!(!out.join("leak").exists());
std::fs::remove_dir_all(&t).unwrap();
}
#[test]
fn local_port() {
use super::local_port as lp;
assert_eq!(lp("http://127.0.0.1:8000/v1"), Some(8000));
assert_eq!(lp("http://localhost:11434"), Some(11434));
assert_eq!(lp("http://[::1]:1234/v1"), Some(1234));
assert_eq!(lp("http://localhost/v1"), Some(80));
assert_eq!(lp("http://192.168.1.169:8080/v1"), None);
assert_eq!(lp("https://api.openai.com/v1"), None);
}
}
+74
View File
@@ -0,0 +1,74 @@
//! Sandbox bookkeeping: listing, removal, image gc.
use crate::oci;
use crate::util::sanitize;
use anyhow::{Context, Result, anyhow, bail};
use serde_json::Value;
use std::fs;
use std::os::unix::fs::MetadataExt;
use std::path::Path;
/// The lock `run` holds for the VM's lifetime; Err if the sandbox is running.
pub fn lock_stopped(dir: &Path) -> Result<fs::File> {
let f = fs::File::create(dir.join("lock"))?;
f.try_lock().map_err(|_| anyhow!("locked by a running VM"))?;
Ok(f)
}
pub fn ls(h: &Path) -> Result<()> {
println!("{:<28} {:<8} {:<40} {:>8} WORKSPACE", "NAME", "STATUS", "IMAGE", "DISK");
let Ok(rd) = fs::read_dir(h.join("sandboxes")) else { return Ok(()) };
let mut rows: Vec<_> = rd.flatten().collect();
rows.sort_by_key(|e| e.file_name());
for e in rows {
let Ok(m) = fs::read(e.path().join("meta.json")) else { continue };
let m: Value = serde_json::from_slice(&m)?;
let used = fs::metadata(e.path().join("disk.img")).map(|m| m.blocks() * 512).unwrap_or(0);
let status = if lock_stopped(&e.path()).is_err() { "running" } else { "stopped" };
println!(
"{:<28} {:<8} {:<40} {:>6}MB {}",
e.file_name().to_string_lossy(),
status,
m["image"].as_str().unwrap_or("?"),
used / 1_000_000,
m["workspace"].as_str().unwrap_or("-")
);
}
Ok(())
}
pub fn rm(h: &Path, names: Vec<String>, disk: bool) -> Result<()> {
for n in names {
if n.is_empty() || sanitize(&n) != n {
bail!("invalid sandbox name {n:?}");
}
let d = h.join("sandboxes").join(&n);
if !d.join("meta.json").exists() {
bail!("no sandbox {n}");
}
// Held across the delete so the sandbox can't be started meanwhile.
let _lock = lock_stopped(&d).with_context(|| format!("sandbox {n} is running"))?;
if disk {
match fs::remove_file(d.join("disk.img")) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()),
_ => println!("removed disk of {n}"),
}
} else {
fs::remove_dir_all(d)?;
println!("removed {n}");
}
}
Ok(())
}
/// Delete cached images no sandbox was created from.
pub fn gc(h: &Path) -> Result<()> {
let used: Vec<String> = fs::read_dir(h.join("sandboxes"))
.into_iter()
.flatten()
.flatten()
.filter_map(|e| serde_json::from_slice::<Value>(&fs::read(e.path().join("meta.json")).ok()?).ok())
.filter_map(|m| m["image"].as_str().map(String::from))
.collect();
println!("freed {} MB", oci::gc(h, &used)? / 1_000_000);
Ok(())
}
+53
View File
@@ -0,0 +1,53 @@
//! Copying host config into the guest share without following symlinks out of it.
use anyhow::{Result, anyhow};
use std::fs;
use std::path::{Path, PathBuf};
pub const STAGE_LIMIT: u64 = 256 << 20;
/// Copy config into the stage, resolving symlinks (they'd dangle in the VM) but only when they
/// resolve inside `roots`; no cycles, regular files and dirs only (a FIFO would hang fs::copy).
pub fn copy_tree(src: &Path, dst: &Path, roots: &[PathBuf], budget: &mut u64, seen: &mut std::collections::HashSet<PathBuf>) -> Result<()> {
let Ok(real) = fs::canonicalize(src) else { return Ok(()) }; // dangling symlink
if !roots.iter().any(|r| real.starts_with(r)) {
eprintln!("wsbx: not sharing {} -> {} (outside the shared config)", src.display(), real.display());
return Ok(());
}
let m = fs::metadata(&real)?;
if m.is_dir() {
if !seen.insert(real.clone()) {
return Ok(()); // symlink cycle or already copied
}
fs::create_dir_all(dst)?;
for e in fs::read_dir(&real)? {
let e = e?;
copy_tree(&e.path(), &dst.join(e.file_name()), roots, budget, seen)?;
}
} else if m.is_file() {
*budget = budget.checked_sub(m.len()).ok_or_else(|| anyhow!("shared config exceeds {} MB", STAGE_LIMIT >> 20))?;
fs::copy(&real, dst)?;
}
Ok(())
}
#[cfg(test)]
mod tests {
#[test]
fn copy_tree_contains_symlinks() {
use std::os::unix::fs::symlink;
let t = std::env::temp_dir().join(format!("wsbx-ct-{}", std::process::id()));
let (root, out, secret) = (t.join("root"), t.join("out"), t.join("secret"));
std::fs::create_dir_all(root.join("skills/a")).unwrap();
std::fs::create_dir_all(&secret).unwrap();
std::fs::write(root.join("skills/a/SKILL.md"), "ok").unwrap();
std::fs::write(secret.join("id_rsa"), "key").unwrap();
symlink(&secret, root.join("skills/leak")).unwrap(); // escapes the root
symlink(root.join("skills"), root.join("skills/a/loop")).unwrap(); // cycle
let roots = vec![std::fs::canonicalize(&root).unwrap()];
let (mut budget, mut seen) = (super::STAGE_LIMIT, Default::default());
super::copy_tree(&root.join("skills"), &out, &roots, &mut budget, &mut seen).unwrap();
assert_eq!(std::fs::read_to_string(out.join("a/SKILL.md")).unwrap(), "ok");
assert!(!out.join("leak").exists());
std::fs::remove_dir_all(&t).unwrap();
}
}
+36
View File
@@ -0,0 +1,36 @@
//! Host side of `-p PORT`: vfkit vsock unix socket -> host localhost:PORT.
use anyhow::{Result, bail};
use std::fs;
use std::net::{Shutdown, TcpStream};
use std::os::unix::net::{UnixListener, UnixStream};
use std::path::Path;
/// Host end of a tunnel: vfkit dials this unix socket for each guest vsock connection.
pub fn tunnel(sock: &Path, port: u16) -> Result<()> {
if sock.as_os_str().len() > 100 {
bail!("socket path too long: {} (set WSBX_HOME to something shorter)", sock.display());
}
let _ = fs::remove_file(sock);
let l = UnixListener::bind(sock)?;
std::thread::spawn(move || {
for u in l.incoming().flatten() {
std::thread::spawn(move || {
if let Ok(t) = TcpStream::connect(("localhost", port)) {
pipe(u, t);
}
});
}
});
Ok(())
}
fn pipe(mut u: UnixStream, mut t: TcpStream) {
let (Ok(mut u2), Ok(mut t2)) = (u.try_clone(), t.try_clone()) else { return };
let up = std::thread::spawn(move || {
let _ = std::io::copy(&mut u2, &mut t2);
let _ = t2.shutdown(Shutdown::Write);
});
let _ = std::io::copy(&mut t, &mut u);
let _ = u.shutdown(Shutdown::Write);
let _ = up.join();
}
+38
View File
@@ -0,0 +1,38 @@
//! Small helpers shared across modules.
use anyhow::{Result, anyhow};
use serde_json::Value;
use std::path::{Path, PathBuf};
use std::process::Command;
pub fn base(p: &Path) -> String {
p.file_name().map(|s| s.to_string_lossy().into_owned()).unwrap_or_else(|| "root".into())
}
pub fn sanitize(s: &str) -> String {
s.chars().map(|c| if c.is_ascii_alphanumeric() || c == '-' { c.to_ascii_lowercase() } else { '-' }).collect()
}
pub fn sq(s: &str) -> String {
format!("'{}'", s.replace('\'', r"'\''"))
}
/// Find a tool on PATH, else in its Homebrew keg (keg-only formulae like e2fsprogs aren't linked).
pub fn need(tool: &str, formula: &str) -> Result<PathBuf> {
let on_path = std::env::var_os("PATH").map(|p| std::env::split_paths(&p).collect::<Vec<_>>()).unwrap_or_default();
let find = |dirs: &[PathBuf]| dirs.iter().map(|d| d.join(tool)).find(|p| p.is_file());
find(&on_path)
.or_else(|| {
let o = Command::new("brew").args(["--prefix", formula]).output().ok()?;
let keg = PathBuf::from(String::from_utf8_lossy(&o.stdout).trim());
find(&[keg.join("sbin"), keg.join("bin")])
})
.ok_or_else(|| anyhow!("{tool} not found — install it with: brew install {formula}"))
}
pub fn strs(v: &Value) -> Vec<String> {
v.as_array().map(|a| a.iter().filter_map(|s| s.as_str().map(String::from)).collect()).unwrap_or_default()
}
pub fn now() -> u64 {
std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs()
}
+278
View File
@@ -0,0 +1,278 @@
//! Booting a sandbox: run script, vfkit invocation, guest results.
use crate::disk::create_disk;
use crate::kernel::kernel;
use crate::oci;
use crate::sandbox::lock_stopped;
use crate::stage::{STAGE_LIMIT, copy_tree};
use crate::tunnel::tunnel;
use crate::util::{base, need, now, sanitize, sq, strs};
use crate::Opts;
use anyhow::{Context, Result, anyhow, bail};
use serde_json::{Value, json};
use std::fs;
use std::io::{IsTerminal, Read};
use std::os::unix::fs::{DirBuilderExt, PermissionsExt};
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
const INIT: &str = include_str!("init.sh");
const DHCP: &str = include_str!("dhcp.sh");
const FWD: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/wsbx-fwd"));
/// Agent-specific guest preparation.
#[derive(Default)]
pub struct Guest {
/// (host path, name under /.wsbx/host/stage) copied before each start, symlinks resolved
pub stage: Vec<(PathBuf, String)>,
/// Symlinks inside staged trees may only resolve into these (canonical) roots
pub roots: Vec<PathBuf>,
/// Shell run as root after HOME/env are set, before dropping to the image user
pub setup: String,
}
pub fn run(h: &Path, o: Opts, image: &str, cmd: Vec<String>, extra: &[String], g: Guest) -> Result<()> {
let vfkit = need("vfkit", "vfkit")?;
let kernel = match &o.kernel {
Some(k) => k.clone(),
None => kernel(h, false, None)?,
};
let ws = o.workspace.as_ref().map(fs::canonicalize).transpose().context("workspace")?;
vf(h)?; // everything vfkit gets lives under h, plus kernel + workspace: refuse bad paths before creating anything
vf(&kernel)?;
if let Some(ws) = &ws {
vf(ws)?;
let hc = fs::canonicalize(h)?;
if ws.starts_with(&hc) || hc.starts_with(ws) {
bail!("workspace {} overlaps the wsbx state dir {} (disks, kernel, control files)", ws.display(), hc.display());
}
}
let img = oci::pull(h, image, o.pull)?;
let short = image.rsplit('/').next().unwrap().split([':', '@']).next().unwrap();
let name = sanitize(&o.name.clone().unwrap_or_else(|| format!("{short}-{}", ws.as_deref().map(base).unwrap_or("vm".into()))));
let dir = h.join("sandboxes").join(&name);
fs::create_dir_all(&dir)?;
let _lock = lock_stopped(&dir).map_err(|_| anyhow!("sandbox {name} is already running"))?;
// The control share is writable by the guest (virtio-fs has no host-enforced read-only).
// Recreate it empty on every start -- while this sandbox's VM is stopped and we hold the lock --
// so nothing a previous guest left there (e.g. `run.sh -> ~/.zshrc`) is ever written through.
// Persistent state lives in meta.json, which the guest cannot reach.
let share = dir.join("share");
match fs::remove_dir_all(&share) {
Err(e) if e.kind() != std::io::ErrorKind::NotFound => return Err(e.into()),
_ => {}
}
std::fs::DirBuilder::new().mode(0o700).create(&share)?;
let disk = dir.join("disk.img");
let meta_path = dir.join("meta.json");
if o.fresh {
let _ = fs::remove_file(&disk);
}
let mut meta: Value = fs::read(&meta_path).ok().and_then(|m| serde_json::from_slice(&m).ok()).unwrap_or(Value::Null);
if disk.exists() && meta["digest"] != img.digest.as_str() {
eprintln!("wsbx: note: {name} was created from an older image; use --fresh to rebuild");
}
if !disk.exists() {
eprintln!("wsbx: creating sandbox {name}");
create_disk(h, &disk, o.disk)?;
meta = json!({"image": image, "digest": img.digest, "workspace": ws, "created": now(), "provisioned": false});
fs::write(&meta_path, serde_json::to_vec_pretty(&meta)?)?;
}
// Build the guest-side run script (executed as root by init, then drops to the image user).
let c = &img.config;
let mut argv = strs(&c["Entrypoint"]);
argv.extend(if cmd.is_empty() { strs(&c["Cmd"]) } else { cmd });
argv.extend(extra.iter().cloned());
if argv.is_empty() {
argv.push("/bin/sh".into());
}
let user = c["User"].as_str().unwrap_or("").to_string();
let wd = ws
.as_ref()
.map(|p| p.to_string_lossy().into_owned())
.or_else(|| c["WorkingDir"].as_str().filter(|s| !s.is_empty()).map(String::from))
.unwrap_or("/".into());
let mut s = String::from("B=/.wsbx/busybox\n");
s += &format!("$B date -s @{} >/dev/null\n", now());
s += &format!("$B hostname {name}; echo {name} > /etc/hostname; $B grep -q ' {name}$' /etc/hosts 2>/dev/null || echo '127.0.1.1 {name}' >> /etc/hosts\n");
if let Some((rows @ 1.., cols @ 1..)) = term_size() {
s += &format!("$B stty rows {rows} cols {cols}\n");
}
if let Some(ws) = &ws {
let w = sq(&ws.to_string_lossy());
s += &format!("$B mkdir -p {w} && $B mount -t virtiofs ws {w}\n");
}
let u = user.split(':').next().unwrap_or("");
s += &format!("export HOME=$($B awk -F: -v u={} '$1==u||$3==u{{print $6;exit}}' /etc/passwd)\n", sq(if u.is_empty() { "root" } else { u }));
s += "[ -n \"$HOME\" ] || export HOME=/root\n";
s += &format!("export TERM={} TINI_SUBREAPER=1\n", sq(&std::env::var("TERM").unwrap_or("xterm-256color".into())));
let mut env = strs(&c["Env"]);
for e in &o.env {
match e.split_once('=') {
Some(_) => env.push(e.clone()),
None => env.push(format!("{e}={}", std::env::var(e).with_context(|| format!("env {e} not set on host"))?)),
}
}
for e in env {
if let Some((k, v)) = e.split_once('=') {
s += &format!("export {k}={}\n", sq(v));
}
}
s += &format!("export WSBX_USER={}\n", sq(&user));
let stage = share.join("stage");
fs::create_dir(&stage)?;
let (mut budget, mut seen) = (STAGE_LIMIT, std::collections::HashSet::new());
for (src, to) in &g.stage {
let dst = stage.join(to);
fs::create_dir_all(dst.parent().unwrap())?;
copy_tree(src, &dst, &g.roots, &mut budget, &mut seen)?;
}
s += &g.setup;
// Host tunnel: guest wsbx-fwd listens on localhost:PORT -> vsock -> vfkit -> unix socket -> host localhost:PORT
let mut ports = o.host_ports.clone();
ports.sort();
ports.dedup();
let mut vsock = Vec::new();
if !ports.is_empty() {
let fwd = share.join("fwd");
fs::write(&fwd, FWD)?;
fs::set_permissions(&fwd, fs::Permissions::from_mode(0o755))?;
let mut specs = Vec::new();
for (i, &p) in ports.iter().enumerate() {
let vport = 10000 + i as u32;
let sock = dir.join(format!("p{p}.sock"));
tunnel(&sock, p)?;
vsock.push(format!("virtio-vsock,port={vport},socketURL={},listen", vf(&sock)?));
specs.push(format!("{p}={vport}"));
}
s += "$B mkfifo /run/wsbx-fwd\n";
s += &format!("/.wsbx/host/fwd {} >/run/wsbx-fwd 2>/.wsbx/host/fwd.log &\n", specs.join(" "));
s += "read _ </run/wsbx-fwd\n";
eprintln!("wsbx: host localhost:{{{}}} tunneled into the VM", ports.iter().map(u16::to_string).collect::<Vec<_>>().join(","));
}
s += &format!("cd {} 2>/dev/null || cd /\n", sq(&wd));
let argv: Vec<String> = argv.iter().map(|a| sq(a)).collect();
if u.is_empty() || u == "root" || u == "0" {
s += &format!("exec {}\n", argv.join(" "));
} else {
s += &format!("exec $B chpst -u {} {}\n", sq(&user), argv.join(" "));
}
fs::write(share.join("run.sh"), s)?;
for (f, body) in [("init", INIT), ("dhcp", DHCP)] {
fs::write(share.join(f), body)?;
fs::set_permissions(share.join(f), fs::Permissions::from_mode(0o755))?;
}
let _ = fs::remove_file(share.join("exit"));
let initrd = h.join("empty.initrd"); // vfkit insists on an initrd; an empty one is fine
if !initrd.exists() {
fs::write(&initrd, "")?;
}
let mac = {
let x = name.bytes().fold(0xcbf29ce484222325u64, |a, b| (a ^ b as u64).wrapping_mul(0x100000001b3));
format!("02:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}", x as u8, (x >> 8) as u8, (x >> 16) as u8, (x >> 24) as u8, (x >> 32) as u8)
};
// panic=1 reboots, which VZ turns into a restart; init powers off on any second boot (see init.sh).
let cmdline = "console=hvc0 root=/dev/vda rw rootfstype=ext4 init=/.wsbx/init quiet panic=1";
let tty = std::io::stdin().is_terminal();
let mut v = Command::new(vfkit);
v.args(["--cpus", &o.cpus.to_string(), "--memory", &o.memory.to_string(), "--log-level", "error"])
.arg("--bootloader")
.arg(format!("linux,kernel={},initrd={},cmdline=\"{cmdline}\"", vf(&kernel)?, vf(&initrd)?))
.arg("--device")
.arg(format!("virtio-blk,path={}", vf(&disk)?))
.arg("--device")
.arg(format!("virtio-net,nat,mac={mac}"))
.arg("--device")
.arg(format!("virtio-fs,sharedDir={},mountTag=wsbx", vf(&share)?))
.args(["--device", "virtio-rng", "--device"])
// vfkit's stdio console needs a TTY; otherwise stream output only (no stdin)
.arg(if tty { "virtio-serial,stdio".into() } else { format!("virtio-serial,logFilePath={}", vf(&out_path())?) })
.stderr(fs::File::create(dir.join("vfkit.log"))?);
if let Some(ws) = &ws {
v.arg("--device").arg(format!("virtio-fs,sharedDir={},mountTag=ws", vf(ws)?));
}
// Image tar is only needed until the guest has unpacked it; read-only so sandboxes can share it.
let provisioned = meta["provisioned"].as_bool().unwrap_or(false);
if !provisioned {
v.arg("--device").arg(format!("virtio-blk,path={},readonly", vf(&img.rootfs)?));
}
for d in vsock {
v.arg("--device").arg(d);
}
let saved = stty(&["-g"]);
if saved.is_some() {
stty(&["raw", "-echo"]);
}
let status = v.stdin(Stdio::inherit()).status();
if let Some(s) = saved {
stty(&[s.trim()]);
}
let status = status?;
// The VM is gone now, so the guest can't race these reads; still never follow its symlinks.
let code = guest_output(&share, "exit").and_then(|s| s.trim().parse().ok());
if !provisioned && guest_output(&share, "provisioned").is_some() && meta.is_object() {
meta["provisioned"] = json!(true);
fs::write(&meta_path, serde_json::to_vec_pretty(&meta)?)?;
}
let _ = fs::remove_dir_all(&share); // run.sh contains forwarded env
match code {
Some(c) => std::process::exit(c),
None if !status.success() => bail!("vm failed, see {}", dir.join("vfkit.log").display()),
None => bail!("VM stopped without an exit status (kernel panic, reboot or poweroff inside the VM)"),
}
}
/// Small result file written by the guest; regular files only (no symlinks), size-capped.
fn guest_output(share: &Path, name: &str) -> Option<String> {
let p = share.join(name);
fs::symlink_metadata(&p).ok().filter(|m| m.is_file())?;
let mut s = String::new();
fs::File::open(p).ok()?.take(64).read_to_string(&mut s).ok()?;
Some(s)
}
/// vfkit splits device options on ',' and the bootloader cmdline is quoted: a path containing
/// those (e.g. a workspace named `repo,sharedDir=/Users/me`) would inject options. Refuse them.
fn vf(p: &Path) -> Result<String> {
let s = p.to_str().ok_or_else(|| anyhow!("non-UTF-8 path {}", p.display()))?;
if s.chars().any(|c| c == ',' || c == '"' || c.is_control()) {
bail!("unsupported character in path {s:?} (',', '\"' and control characters would inject vfkit options)");
}
Ok(s.to_string())
}
/// Where our stdout points (vfkit only accepts a path for non-TTY console output).
fn out_path() -> PathBuf {
fs::read_link("/dev/fd/1").unwrap_or("/dev/stdout".into())
}
fn stty(args: &[&str]) -> Option<String> {
let out = Command::new("stty").args(args).stdin(Stdio::inherit()).stderr(Stdio::null()).output().ok()?;
out.status.success().then(|| String::from_utf8_lossy(&out.stdout).into_owned())
}
fn term_size() -> Option<(u32, u32)> {
let s = stty(&["size"])?;
let mut it = s.split_whitespace().map(|x| x.parse().ok());
Some((it.next()??, it.next()??))
}
#[cfg(test)]
mod tests {
use std::path::Path;
#[test]
fn vfkit_injection_rejected() {
assert!(super::vf(Path::new("/Users/me/repo")).is_ok());
for bad in ["/shared/repo/,sharedDir=/Users/me", "/a\"b", "/a\nb"] {
assert!(super::vf(Path::new(bad)).is_err(), "{bad}");
}
}
}