first commit

This commit is contained in:
2026-10-02 13:57:32 +02:00
commit 9117f05db9
10 changed files with 2528 additions and 0 deletions
Generated
+972
View File
@@ -0,0 +1,972 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "adler2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "anstream"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
[[package]]
name = "anstyle-parse"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys 0.61.2",
]
[[package]]
name = "anyhow"
version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
[[package]]
name = "base64"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6"
[[package]]
name = "bitflags"
version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
[[package]]
name = "block-buffer"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71"
dependencies = [
"generic-array",
]
[[package]]
name = "cc"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f360145194ee8e21db5ee7f3fcd4fe52210864c75c985dae33218202c8bbe040"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "cfg-if"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
[[package]]
name = "clap"
version = "4.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa8876b300ab35ba921adea3dfd70157a46249b33f95c9084ae5709785478946"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.6.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9c751b79415d4e559e3d1fcf128e09e720eb673a06d26cf6f392d37d75b66e0"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486"
[[package]]
name = "colorchoice"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
[[package]]
name = "cpufeatures"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280"
dependencies = [
"libc",
]
[[package]]
name = "crc32fast"
version = "1.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01a7799fd6b852db0e61728dde9a204c423b44d689dbd432522543614b490e78"
dependencies = [
"cfg-if",
]
[[package]]
name = "crypto-common"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "displaydoc"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "errno"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
dependencies = [
"libc",
"windows-sys 0.61.2",
]
[[package]]
name = "filetime"
version = "0.2.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c287a33c7f0a620c38e641e7f60827713987b3c0f26e8ddc9462cc69cf75759"
dependencies = [
"cfg-if",
"libc",
]
[[package]]
name = "find-msvc-tools"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aedcfb3409746eddb02b9e19ebda1c3394f759a152e48ee875a0844d1b955484"
[[package]]
name = "flate2"
version = "1.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e634e2e0ebac1ee034020da1ca582e17ffe4e0f5e985823721e168928136dcb"
dependencies = [
"crc32fast",
"miniz_oxide",
"zlib-rs",
]
[[package]]
name = "form_urlencoded"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf"
dependencies = [
"percent-encoding",
]
[[package]]
name = "generic-array"
version = "0.14.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "getrandom"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hex"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "icu_collections"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa68d21081c4a05d5a901a1c62add574c77048b6a1c67be3b50ce0b60d4ca513"
dependencies = [
"displaydoc",
"potential_utf",
"utf8_iter",
"yoke",
"zerofrom",
"zerovec",
]
[[package]]
name = "icu_locale_core"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d56e28588da92eee5c3201a6eff33fabdd49b62269c8938d4ff050ce4d900deb"
dependencies = [
"displaydoc",
"litemap",
"tinystr",
"writeable",
"zerovec",
]
[[package]]
name = "icu_normalizer"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12f9cf5f235641ed274641dd81c3f28d870e276763d0797aeeab72317b1c646f"
dependencies = [
"icu_collections",
"icu_normalizer_data",
"icu_properties",
"icu_provider",
"smallvec",
"zerovec",
]
[[package]]
name = "icu_normalizer_data"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1563da1ed3e0b3bf3d74c9b85917ac9c56464d2f57242270c09c9e752f8021a0"
[[package]]
name = "icu_properties"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e7ca276ad3145661a65914e6daf131ca5120cd3dcee8f8f3214b8875184a148"
dependencies = [
"displaydoc",
"icu_collections",
"icu_locale_core",
"icu_properties_data",
"icu_provider",
"zerotrie",
"zerovec",
]
[[package]]
name = "icu_properties_data"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e590f038c1464a96894fd6d10127e90a8be4509f56ff7ecef851b15cee0b7caa"
[[package]]
name = "icu_provider"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d27bbb9d3abbefac45d55f647c9de1d44aafcd1186eb91879afef17c396c3e73"
dependencies = [
"displaydoc",
"icu_locale_core",
"writeable",
"yoke",
"zerofrom",
"zerotrie",
"zerovec",
]
[[package]]
name = "idna"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de"
dependencies = [
"idna_adapter",
"smallvec",
"utf8_iter",
]
[[package]]
name = "idna_adapter"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714"
dependencies = [
"icu_normalizer",
"icu_properties",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "itoa"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
[[package]]
name = "libc"
version = "0.2.189"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2"
[[package]]
name = "linux-raw-sys"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
[[package]]
name = "litemap"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae"
[[package]]
name = "log"
version = "0.4.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6"
[[package]]
name = "memchr"
version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
[[package]]
name = "miniz_oxide"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b63fbc4a50860e98e7b2aa7804ded1db5cbc3aff9193adaff57a6931bf7c4b4c"
dependencies = [
"adler2",
"simd-adler32",
]
[[package]]
name = "once_cell"
version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]]
name = "percent-encoding"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "potential_utf"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661"
dependencies = [
"zerovec",
]
[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ring"
version = "0.17.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7"
dependencies = [
"cc",
"cfg-if",
"getrandom",
"libc",
"untrusted",
"windows-sys 0.52.0",
]
[[package]]
name = "rustix"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "891efababe418670775f199f0d233d84843c227a0949a883ce15b37c78d6629d"
dependencies = [
"bitflags",
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.61.2",
]
[[package]]
name = "rustls"
version = "0.23.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634"
dependencies = [
"log",
"once_cell",
"ring",
"rustls-pki-types",
"rustls-webpki",
"subtle",
"zeroize",
]
[[package]]
name = "rustls-pki-types"
version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96"
dependencies = [
"zeroize",
]
[[package]]
name = "rustls-webpki"
version = "0.103.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2"
dependencies = [
"ring",
"rustls-pki-types",
"untrusted",
]
[[package]]
name = "serde"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
dependencies = [
"serde_core",
]
[[package]]
name = "serde_core"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.229"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.151"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14"
dependencies = [
"itoa",
"memchr",
"serde",
"serde_core",
"zmij",
]
[[package]]
name = "sha2"
version = "0.10.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "shlex"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
[[package]]
name = "simd-adler32"
version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea"
[[package]]
name = "smallvec"
version = "1.16.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9395f0f0eee849a9b707b2f06bb92a6a422090e2123bb2ef8e87a0e61892a8e"
[[package]]
name = "stable_deref_trait"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
[[package]]
name = "strsim"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "syn"
version = "3.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8593e8e72159ed2257d083c7a454a85cbf854f37a0966d8d483aff8c8a3ebcee"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "synstructure"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "tar"
version = "0.4.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f6221d9a6003c78398e3b239969f352578258df48c8eb051caadae0015bc840"
dependencies = [
"filetime",
"libc",
"xattr",
]
[[package]]
name = "tinystr"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1e27c91459209c2986af3dcf603a5a74a4368754ce37414f59acc971167f643"
dependencies = [
"displaydoc",
"zerovec",
]
[[package]]
name = "typenum"
version = "1.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20"
[[package]]
name = "unicode-ident"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d245f478577f809a851594d02313b640fb437e0bb33866753cff937863096954"
[[package]]
name = "untrusted"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
[[package]]
name = "ureq"
version = "2.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02d1a66277ed75f640d608235660df48c8e3c19f3b4edb6a263315626cc3c01d"
dependencies = [
"base64",
"flate2",
"log",
"once_cell",
"rustls",
"rustls-pki-types",
"url",
"webpki-roots 0.26.11",
]
[[package]]
name = "url"
version = "2.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed"
dependencies = [
"form_urlencoded",
"idna",
"percent-encoding",
"serde",
]
[[package]]
name = "utf8_iter"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "webpki-roots"
version = "0.26.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9"
dependencies = [
"webpki-roots 1.0.9",
]
[[package]]
name = "webpki-roots"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dcd9d09a39985f5344844e66b0c530a33843579125f23e21e9f0f220850f22a"
dependencies = [
"rustls-pki-types",
]
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "writeable"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc"
[[package]]
name = "wsbx"
version = "0.1.0"
dependencies = [
"anyhow",
"clap",
"flate2",
"hex",
"serde_json",
"sha2",
"tar",
"ureq",
]
[[package]]
name = "xattr"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32e45ad4206f6d2479085147f02bc2ef834ac85886624a23575ae137c8aa8156"
dependencies = [
"libc",
"rustix",
]
[[package]]
name = "yoke"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5"
dependencies = [
"stable_deref_trait",
"yoke-derive",
"zerofrom",
]
[[package]]
name = "yoke-derive"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec8ebde2db3681e8c9980cc27822030e68752690ddfa9473e739aeb4dbde6d71"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "zerofrom"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272"
dependencies = [
"zerofrom-derive",
]
[[package]]
name = "zerofrom-derive"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f75b4683f6c7f45248d4d64056a24298c6281e0993356d7d1b4a1a962ef10d4a"
dependencies = [
"proc-macro2",
"quote",
"syn",
"synstructure",
]
[[package]]
name = "zeroize"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e13c156562582aa81c60cb29407084cdb54c4164760106ab78e6c5b0858cf64e"
[[package]]
name = "zerotrie"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f"
dependencies = [
"displaydoc",
"yoke",
"zerofrom",
]
[[package]]
name = "zerovec"
version = "0.11.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb0464e17806c1d976d5cba29399c7f08e516e279e2ba493f63123b5fca67dd8"
dependencies = [
"yoke",
"zerofrom",
"zerovec-derive",
]
[[package]]
name = "zerovec-derive"
version = "0.11.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34df6fc39dbd26ddc9c10e6a2984476e13acce22e64e4487636ef494369225da"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "zlib-rs"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b268e58e7c693d7c271f93ffc4ba3b380412554231c85bf61ca7af91042a4112"
[[package]]
name = "zmij"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
+14
View File
@@ -0,0 +1,14 @@
[package]
name = "wsbx"
version = "0.1.0"
edition = "2024"
[dependencies]
anyhow = "1"
clap = { version = "4", features = ["derive"] }
flate2 = "1"
hex = "0.4"
serde_json = "1"
sha2 = "0.10"
tar = "0.4"
ureq = "2"
+95
View File
@@ -0,0 +1,95 @@
# wsbx — tiny microVM sandboxes for macOS
Goal: `docker sandbox`-like UX, but every sandbox is a real Linux microVM.
## Decisions
| Concern | Choice | Why |
|---|---|---|
| Hypervisor | Apple Virtualization.framework via **vfkit** (`brew install vfkit`) | Firecracker needs KVM (Linux only). vfkit is a signed, maintained CLI over VZ — no entitlement/codesign dance for us. |
| Kernel | **Kata Containers** release kernel (`vmlinux.container`, arm64). The latest release is found via the GitHub API, then the tarball is streamed and checked against GitHub's published asset sha256, and only the kernel is kept. `kernel --update` / `--version` / `WSBX_KATA_VERSION` | Trusted upstream, built for VMs (virtio-pci/blk/net/fs/console built in, no initrd needed). Same kernel Apple's `container` uses. Override with `--kernel`. |
| Base OS | Any **OCI image** (Docker Hub, GHCR, …) pulled by wsbx itself (anonymous token auth, sha256-verified blobs) | No Docker needed. |
| Rootfs build | Host flattens layers → one tar (whiteouts applied). Host makes empty ext4 disk with `mke2fs` (brew e2fsprogs) seeded with a static busybox + init. **Guest extracts the tar as root on first boot.** | Correct uid/gid/devices/hardlinks and case-sensitivity — impossible to get right on an APFS host dir. |
| Guest init | `/.wsbx/init` (busybox sh as PID 1): mounts, DHCP (VZ NAT), mounts workspace (virtiofs), runs image entrypoint+cmd as image user, powers off. | No systemd, boots in ~1s. |
| Agents | Presets over Docker's own `docker/sandbox-templates:{claude-code,codex,gemini}` images | Same images Docker Sandboxes use; multi-arch. |
## CLI
```
wsbx kernel # download/cache kernel
wsbx pull IMAGE # pull + flatten image
wsbx run [opts] IMAGE [-- CMD..] # bare VM from any image (default: shell)
wsbx claude|codex|gemini [opts] # agent sandbox for current dir
wsbx ls | wsbx rm NAME...
opts: --name N --cpus N --memory MiB --disk GiB --workspace DIR --env K=V --fresh
```
- Workspace dir is shared via virtio-fs at the **same absolute path** as on the host (like Docker Sandboxes).
- Sandboxes are **persistent** (disk kept per name; default name derived from image+workspace). `--fresh`/`rm` resets.
- Agent API key env vars (`ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, `GEMINI_API_KEY`, …) are forwarded if set.
- Terminal size + `TERM` forwarded.
## Layout
```
~/.wsbx/kernel/vmlinux
~/.wsbx/images/<sha256>/{rootfs.tar,config.json}
~/.wsbx/sandboxes/<name>/{disk.img,meta.json,share/run.sh}
```
## Steps
1. Plan (this file). ✅
2. ✅ OCI pull: parse ref, token auth, index → linux/arm64 manifest, verified blobs, flatten w/ whiteouts.
3. ✅ Kernel fetch via `curl | tar --zstd` (bsdtar streams; no 650 MB file kept).
4. ✅ Disk creation (`mke2fs -d` with busybox+init), init script.
5. ✅ `run` via vfkit; agents presets; ls/rm.
6. ✅ Verified: alpine (net/DNS/TLS/workspace rw/exit codes/interactive pty), ubuntu (apt), claude-code template (runs as `agent`, `claude --version`).
Cold first boot of alpine ≈3.5 s, warm boot ≈0.6 s.
## Findings
- vfkit requires an initrd → empty file works (kernel has everything built in).
- vfkit `virtio-serial,stdio` needs a TTY → non-TTY falls back to `logFilePath=<our stdout>`.
- VZ virtio-fs: any guest uid can write; files land owned by the host user.
- Homebrew e2fsprogs lacks libarchive (`mke2fs -d x.tar` unsupported) → guest-side unpack.
## Iteration 2: pi + host tunnel ✅
- pi: `@earendil-works/pi-coding-agent` pinned to host version (the old `@mariozechner` package stopped at 0.73.1).
- Config: allowlist of `~/.pi/agent` items copied to `share/stage` each start (not the whole dir). The copy is needed because the
host skill symlinks point outside `~/.pi` and would dangle in the guest. `auth.json` is opt-in.
- Tunnel over vsock (vfkit `virtio-vsock,listen`): no host listener on any network interface, per-sandbox, no port clashes.
Guest side is `guest/fwd.go` (static Go binary, built by `build.rs`), and it tells `run.sh` it's ready through a FIFO.
- Verified: 20 MB transfer over IPv4 and IPv6 loopback with matching hash. `wsbx pi -p` answered through the local omlx model on
host 127.0.0.1:8000 (first run 91 s incl. installs, then 3.8 s).
## Iteration 3: de-hardcoding ✅
| Was hardcoded | Now |
|---|---|
| `KATA_VERSION = "4.2.0"`, unverified curl | GitHub releases API (latest or pinned), asset sha256 verified while streaming, `kernel/source.json` records version |
| image tags cached forever | re-resolved after 24 h, falls back to the cache when offline |
| pi package name | read from the host's `pi` install (`package.json` name + version) |
| `/opt/homebrew/opt/e2fsprogs/sbin` | `brew --prefix <formula>` when not on PATH |
| `--cpus 4 --memory 4096` | half the host cores / a quarter of host RAM (≤ 8 GiB) |
| init/dhcp frozen into each disk | disk holds a 3-line stub; the real init is sent through the host share on every start |
Bugs found along the way: the image tar was attached read-write, so two sandboxes from one image couldn't run together.
It's now read-only and only attached until the guest reports it's unpacked. Poweroff raced ext4 lazy-init (`WRITE_ZEROES`
I/O error); root is now remounted read-only first.
Kept on purpose: Kata's in-tarball kernel path, `registry-1.docker.io`, arm64, `hvc0`/`vda`/`eth0` (we
define the device order), vsock ports, 20 GiB sparse disk, the agent image names and API-key env lists.
## Iteration 4: security review fixes ✅
| Finding | Fix | Verified |
|---|---|---|
| Critical: guest symlinks in the persistent share → host file overwrite | share recreated per start (VM stopped, lock held), deleted after exit; outputs read with no-symlink + size cap; `provisioned` moved to meta.json | guest planted `run.sh/init/dhcp/fwd -> victim`, `exit -> /etc/hosts`, plus the killed-wsbx leftover case: victim unchanged |
| High: comma injection into vfkit args | `vf()` rejects `,` `"` and control chars in every interpolated path; workspace overlapping `~/.wsbx` refused | `repo,sharedDir=/Users/niklas` rejected |
| High: automatic API-key forwarding | opt-in `--keys` with hint; `~/.wsbx` 0700; run.sh deleted after exit | key absent in VM without `--keys` |
| Medium: `rm ../..` | name must be a sanitized sandbox name | traversal refused |
| Medium: auth-realm SSRF | https only; public registries use an agent whose resolver refuses non-public IPs | unit test |
| Medium: digest refs not enforced | top-level digest compared; all registry digests format-checked; manifest size capped | pin OK, bogus pin rejected |
| Medium: pi copy follows symlinks | contained to staged roots (incl. `~/.agents/skills`, which pi reads natively), cycle-safe, no special files, 256 MB cap | unit test + `show-me` still arrives |
| (own) | reference/tag/registry validated before any request (`alpine@sha256:../../x` hit the registry as `/manifests/x`), busybox must be a regular file, Kata tag validated | unit tests |
## Out of scope (add when needed)
Port forwarding, network policy / credential-injecting egress proxy, snapshots, x86_64 hosts, zstd layers.
+59
View File
@@ -0,0 +1,59 @@
# wsbx
Docker-Sandboxes-style microVMs on Apple Silicon macOS. Each sandbox is a real Linux VM
(Virtualization.framework via [vfkit]), booted from any OCI image with a Kata Containers kernel.
```sh
brew install vfkit e2fsprogs go # go: builds the static guest tunnel helper
cargo install --path .
wsbx claude # Claude Code in a VM, current dir mounted at the same path
wsbx codex | wsbx gemini # other agents (Docker's sandbox-templates images)
wsbx pi # pi coding agent, same version + config as on the host
wsbx run alpine # bare VM, interactive shell
wsbx run -w . ubuntu -- make test # one-off command, exit code propagated
wsbx run -p 8000 alpine # host's localhost:8000 reachable as localhost:8000 in the VM
wsbx ls / wsbx rm NAME / wsbx pull IMAGE
wsbx kernel [--update | --version TAG] # latest Kata release by default, sha256-verified
```
Defaults for `--cpus`/`--memory` come from the host (half the cores, a quarter of RAM up to 8 GiB). Image tags
are re-resolved after 24 h (cached copy used when offline). `WSBX_KATA_VERSION` pins the kernel release.
Options: `--name --workspace --cpus --memory --disk --env K[=V] --host-port/-p PORT --fresh --pull --kernel`.
**Host tunnel** (`-p PORT`): guest `localhost:PORT` → vsock → vfkit → unix socket → host `localhost:PORT`.
Nothing listens on a host network interface, and only the listed ports are reachable.
**pi**: installs `@earendil-works/pi-coding-agent` at the host's `pi --version` (reinstalls when it changes).
On every start it copies these from `~/.pi/agent` with symlinks resolved: settings, models, keybindings, mcp, AGENTS/CLAUDE/SYSTEM md files,
extensions, skills, prompts, themes. They're linked into the guest's own `~/.pi/agent`, so host edits
win and in-VM edits are discarded. `npm/`, `bin/` (macOS binaries) and sessions stay in the VM. `auth.json` is only
copied with `--pi-auth`. Local `baseUrl`s in `models.json` (127.0.0.1/localhost) are tunneled automatically.
Agent API keys (`ANTHROPIC_API_KEY`, `OPENAI_API_KEY`, …) are only forwarded with `--keys`. Code in the VM can read them.
Sandboxes persist per name (disk under `~/.wsbx/sandboxes`); `--fresh` rebuilds. See `PLAN.md` for design.
[vfkit]: https://github.com/crc-org/vfkit
## Security model
The VM is the boundary. The guest (including a prompt-injected agent or a malicious image) is assumed hostile.
- **Control share** (`/.wsbx/host`): the guest can write to it, so it's recreated empty on every start while the VM is stopped and
deleted after exit. Guest outputs are read only as regular files with a size cap; persistent state lives in the host-only
`meta.json`.
- **vfkit arguments**: paths containing `,` `"` or control characters are refused (vfkit splits options on commas).
Workspaces overlapping `~/.wsbx` are refused.
- **Registry**: references are validated before any request, digest pins are enforced, and every blob/manifest is sha256-verified.
Auth realms must be https. For public registries, connections to loopback/private/link-local addresses are refused
(token realms and redirects included).
- **pi config**: allowlisted items only. Symlinks are followed only into `~/.pi/agent` or `~/.agents/skills`, with no cycles
and no special files, capped at 256 MB.
- **Host tunnel**: only the ports you pass with `-p` (or the local `baseUrl`s in pi's `models.json`), over vsock.
Remaining risks:
- The workspace is read-write. A guest can plant files that run later on the host (`.git/hooks`, `.envrc`,
IDE tasks, package scripts). Review changes before running them on the host.
- Egress is unrestricted NAT. Anything the VM holds (keys passed with `--keys`, API keys in pi's `models.json`, `--pi-auth`)
can be exfiltrated. A host-side credential-injecting egress proxy would fix this; it isn't built yet.
- Image layers are unpacked on the host into one tar, so a decompression bomb can fill the disk.
+18
View File
@@ -0,0 +1,18 @@
// Cross-compile the static guest helper (Go makes linux/arm64 static binaries trivially).
use std::process::Command;
fn main() {
println!("cargo:rerun-if-changed=guest/fwd.go");
let out = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap()).join("wsbx-fwd");
let ok = Command::new("go")
.args(["build", "-trimpath", "-ldflags=-s -w", "-o"])
.arg(&out)
.arg("guest/fwd.go")
.env("GOOS", "linux")
.env("GOARCH", "arm64")
.env("CGO_ENABLED", "0")
.status()
.expect("Go is required to build the guest helper (brew install go)")
.success();
assert!(ok, "go build guest/fwd.go failed");
}
+84
View File
@@ -0,0 +1,84 @@
// wsbx-fwd: guest side of the host tunnel.
// Usage: wsbx-fwd PORT=VSOCKPORT...
// Listens on 127.0.0.1:PORT and [::1]:PORT and pipes each connection to the host (vsock CID 2).
package main
import (
"encoding/binary"
"fmt"
"io"
"net"
"os"
"strconv"
"strings"
"syscall"
"unsafe"
)
const afVsock = 40
func dialHost(port uint32) (*os.File, error) {
fd, err := syscall.Socket(afVsock, syscall.SOCK_STREAM|syscall.SOCK_CLOEXEC, 0)
if err != nil {
return nil, err
}
// struct sockaddr_vm { u16 family; u16 reserved; u32 port; u32 cid; u8 flags; u8 zero[3]; }
var sa [16]byte
binary.LittleEndian.PutUint16(sa[0:], afVsock)
binary.LittleEndian.PutUint32(sa[4:], port)
binary.LittleEndian.PutUint32(sa[8:], 2) // VMADDR_CID_HOST
if _, _, e := syscall.Syscall(syscall.SYS_CONNECT, uintptr(fd), uintptr(unsafe.Pointer(&sa[0])), 16); e != 0 {
syscall.Close(fd)
return nil, e
}
return os.NewFile(uintptr(fd), "vsock"), nil
}
func handle(c *net.TCPConn, port uint32) {
defer c.Close()
f, err := dialHost(port)
if err != nil {
fmt.Fprintln(os.Stderr, "wsbx-fwd: vsock:", err)
return
}
defer f.Close()
done := make(chan struct{})
go func() {
io.Copy(c, f)
c.CloseWrite()
close(done)
}()
io.Copy(f, c)
syscall.Shutdown(int(f.Fd()), syscall.SHUT_WR)
<-done
}
func main() {
for _, spec := range os.Args[1:] {
p, v, _ := strings.Cut(spec, "=")
vport, err := strconv.ParseUint(v, 10, 32)
if err != nil {
fmt.Fprintln(os.Stderr, "wsbx-fwd: bad spec", spec)
os.Exit(2)
}
for _, addr := range []string{"127.0.0.1:" + p, "[::1]:" + p} {
ln, err := net.Listen("tcp", addr)
if err != nil {
fmt.Fprintln(os.Stderr, "wsbx-fwd:", err)
continue
}
go func() {
for {
c, err := ln.Accept()
if err == nil {
go handle(c.(*net.TCPConn), uint32(vport))
}
}
}()
}
}
// Signal readiness (run.sh waits on this) before the agent starts.
fmt.Println("ready")
os.Stdout.Close()
select {}
}
+12
View File
@@ -0,0 +1,12 @@
#!/.wsbx/busybox sh
# udhcpc event script
B=/.wsbx/busybox
case "$1" in
bound|renew)
$B ip addr flush dev "$interface"
$B ip addr add "$ip/${mask:-24}" dev "$interface"
for r in $router; do $B ip route add default via "$r" dev "$interface"; break; done
$B rm -f /etc/resolv.conf
for d in $dns; do echo "nameserver $d"; done > /etc/resolv.conf
;;
esac
+37
View File
@@ -0,0 +1,37 @@
#!/.wsbx/busybox sh
# wsbx guest init (PID 1). The on-disk stub (STUB_INIT) mounts the host share and execs this,
# so every start runs the init shipped with the current wsbx. Uses only the static busybox.
B=/.wsbx/busybox
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
if [ ! -e /.wsbx/provisioned ]; then
echo "wsbx: first boot, unpacking image..."
$B mount -t devtmpfs dev /.wsbx/dev
if $B tar -xf /.wsbx/dev/vdb -C /; then
$B chown 0:0 / /.wsbx && $B touch /.wsbx/provisioned
else
echo "wsbx: WARNING: image unpack reported errors"
fi
$B umount /.wsbx/dev
fi
$B mkdir -p /proc /sys /dev /run /tmp
$B mount -t proc proc /proc
$B mount -t sysfs sys /sys
$B mountpoint -q /dev || $B mount -t devtmpfs dev /dev
$B mkdir -p /dev/pts /dev/shm
$B mount -t devpts -o gid=5,mode=620,ptmxmode=666 devpts /dev/pts
$B mount -t tmpfs -o mode=1777 shm /dev/shm
$B mount -t tmpfs run /run
$B chmod 1777 /tmp
[ -e /.wsbx/provisioned ] && : > /.wsbx/host/provisioned # host stops attaching the image tar
$B ip link set lo up
$B ip link set eth0 up
$B udhcpc -q -f -n -t 10 -i eth0 -s /.wsbx/host/dhcp >/dev/null 2>&1 || echo "wsbx: WARNING: no network"
$B setsid $B cttyhack $B sh /.wsbx/host/run.sh
echo $? > /.wsbx/host/exit
$B sync
$B mount -o remount,ro / 2>/dev/null # stops ext4 lazy-init writes racing the power-off
$B poweroff -f
+775
View File
@@ -0,0 +1,775 @@
//! wsbx — Docker-Sandboxes-style microVMs on macOS (Virtualization.framework via vfkit).
mod oci;
use anyhow::{Context, Result, anyhow, bail};
use clap::{Args, Parser, Subcommand};
use serde_json::{Value, json};
use sha2::{Digest, Sha256};
use std::fs;
use std::io::{IsTerminal, Read};
use std::os::unix::fs::DirBuilderExt;
use std::net::{Shutdown, TcpStream};
use std::os::unix::net::{UnixListener, UnixStream};
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)]
#[command(name = "wsbx", about = "Launch coding agents or bare Linux in microVMs")]
struct Cli {
#[command(subcommand)]
cmd: Cmd,
}
#[derive(Subcommand)]
enum Cmd {
/// Show / download the guest kernel (latest Kata Containers release, sha256-verified)
Kernel {
/// Switch to the latest Kata release if a newer one exists
#[arg(long)]
update: bool,
/// Pin a specific Kata release tag (also: WSBX_KATA_VERSION)
#[arg(long)]
version: Option<String>,
},
/// Pull an OCI image (linux/arm64)
Pull { image: String },
/// Run a bare VM from any OCI image; command defaults to the image's entrypoint/cmd
Run {
#[command(flatten)]
o: Opts,
image: String,
#[arg(last = true)]
cmd: Vec<String>,
},
/// Claude Code sandbox for the workspace (default: current dir)
Claude(Agent),
/// OpenAI Codex sandbox
Codex(Agent),
/// Gemini CLI sandbox
Gemini(Agent),
/// pi coding agent sandbox: same pi version as the host, host config (settings, models,
/// skills, extensions, ...) copied in on each start, local model ports tunneled
Pi {
#[command(flatten)]
a: Agent,
/// Also copy ~/.pi/agent/auth.json (API keys / OAuth tokens) into the VM
#[arg(long)]
pi_auth: bool,
},
/// List sandboxes
Ls,
/// Remove sandboxes
Rm { names: Vec<String> },
}
#[derive(Args)]
struct Agent {
#[command(flatten)]
o: Opts,
/// Forward this agent's API-key env vars (e.g. ANTHROPIC_API_KEY). Code in the VM can read them.
#[arg(long)]
keys: bool,
/// Extra arguments for the agent
#[arg(last = true)]
args: Vec<String>,
}
#[derive(Args, Clone)]
struct Opts {
/// Sandbox name (default: derived from image + workspace)
#[arg(short, long)]
name: Option<String>,
/// Directory shared into the VM at the same path
#[arg(short, long)]
workspace: Option<PathBuf>,
/// vCPUs (default: half the host's cores)
#[arg(long, default_value_t = default_cpus())]
cpus: u32,
/// Memory in MiB (default: a quarter of host RAM, at most 8 GiB)
#[arg(long, default_value_t = default_memory())]
memory: u32,
/// Disk size in GiB (sparse)
#[arg(long, default_value_t = 20)]
disk: u32,
/// Extra env: KEY=VALUE, or KEY to forward from host
#[arg(short, long)]
env: Vec<String>,
/// Throw away existing sandbox disk first
#[arg(long)]
fresh: bool,
/// Re-resolve the image tag from the registry
#[arg(long)]
pull: bool,
/// Use a custom kernel (arm64 Image)
#[arg(long)]
kernel: Option<PathBuf>,
/// Tunnel the host's localhost:PORT to localhost:PORT in the VM (vsock, no network exposure)
#[arg(short = 'p', long = "host-port")]
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> {
let h = std::env::var_os("WSBX_HOME")
.map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(std::env::var_os("HOME").expect("HOME")).join(".wsbx"));
fs::create_dir_all(&h)?;
fs::set_permissions(&h, fs::Permissions::from_mode(0o700))?; // holds run scripts with forwarded env
Ok(h)
}
fn main() -> Result<()> {
let h = home()?;
match Cli::parse().cmd {
Cmd::Kernel { update, version } => {
let k = kernel(&h, update, version)?;
println!("{} (Kata {})", k.display(), installed_kernel(&h).unwrap_or("unknown".into()));
Ok(())
}
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::Claude(a) => 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::Gemini(a) => 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::Ls => ls(&h),
Cmd::Rm { names } => {
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}");
}
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 {
std::thread::available_parallelism().map(|n| n.get() as u32 / 2).unwrap_or(2).max(1)
}
fn default_memory() -> u32 {
let host = Command::new("sysctl").args(["-n", "hw.memsize"]).output().ok();
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
}
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 {
if 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")?;
if let Some(ws) = &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 = fs::File::create(dir.join("lock"))?;
lock.try_lock().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)
};
let cmdline = "console=hvc0 root=/dev/vda rw rootfstype=ext4 init=/.wsbx/init quiet";
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 => Ok(()),
}
}
/// 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()
}
fn ls(h: &Path) -> Result<()> {
println!("{:<28} {:<40} {:>8} WORKSPACE", "NAME", "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);
println!(
"{:<28} {:<40} {:>6}MB {}",
e.file_name().to_string_lossy(),
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);
}
}
+462
View File
@@ -0,0 +1,462 @@
//! 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, SocketAddr, ToSocketAddrs};
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) => match v.to_ipv4_mapped() {
Some(v4) => is_public(v4.into()),
None => !(v.is_loopback() || v.is_unspecified() || v.is_multicast() || v.is_unique_local() || v.is_unicast_link_local()),
},
}
}
/// HTTP agent for a registry. Public registries get an agent that refuses to connect to non-public
/// addresses (covers token realms and blob redirects too); a registry that is itself local
/// (e.g. localhost:5000) was chosen explicitly by the user and keeps the default agent.
fn agent_for(registry: &str) -> ureq::Agent {
let addr = if registry.contains(':') { registry.to_string() } else { format!("{registry}:443") };
let local = addr.to_socket_addrs().map(|mut a| a.any(|a| !is_public(a.ip()))).unwrap_or(false);
if local {
return ureq::agent();
}
ureq::AgentBuilder::new()
.resolver(|addr: &str| -> std::io::Result<Vec<SocketAddr>> {
let ok: Vec<SocketAddr> = 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::<u16>().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<String>,
}
impl Client {
fn get(&mut self, path: &str, accept: &str) -> Result<ureq::Response> {
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<String> {
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<Image> {
validate_ref(&parse_ref(image)).with_context(|| format!("invalid image reference {image:?}"))?;
let refs = home.join("images/refs");
fs::create_dir_all(&refs)?;
let ref_file = refs.join(image.replace(['/', ':', '@'], "_"));
let cached = fs::read_to_string(&ref_file).ok().and_then(|d| load(home, d.trim()).ok());
let age = fs::metadata(&ref_file).and_then(|m| m.modified()).ok().and_then(|t| t.elapsed().ok());
if let Some(img) = cached {
if !refresh && (image.contains('@') || age.is_some_and(|a| a < REF_TTL)) {
return Ok(img);
}
return fetch(home, image, &ref_file).or_else(|e| {
if refresh {
return Err(e);
}
eprintln!("wsbx: could not re-resolve {image} ({e:#}); using cached copy");
Ok(img)
});
}
fetch(home, image, &ref_file)
}
fn fetch(home: &Path, image: &str, ref_file: &Path) -> Result<Image> {
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 = home.join("images").join(digest.replace(':', "-"));
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<Image> {
let dir = home.join("images").join(digest.replace(':', "-"));
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<W: Write>(pub W, pub Sha256);
impl<W: Write> Write for HashWriter<W> {
fn write(&mut self, b: &[u8]) -> std::io::Result<usize> {
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<Box<dyn Read>> {
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<Item = &str> {
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<bool>> = vec![Vec::new(); layers.len()];
let mut seen: HashSet<String> = HashSet::new(); // every path provided by an upper layer
let mut files: HashSet<String> = HashSet::new(); // non-dirs from upper layers
let mut gone: HashSet<String> = HashSet::new(); // whiteouts + opaque dirs (hide lower content)
let mut opaque: HashSet<String> = 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<String> = 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"] {
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"] {
assert!(is_public(ip.parse().unwrap()), "{ip} must be allowed");
}
}
#[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 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"));
}
}