From 19f1563b7ef7e135268a43e70db73fcac885cf46 Mon Sep 17 00:00:00 2001 From: robobun Date: Thu, 1 Oct 2026 13:28:44 +0000 Subject: [PATCH 1/6] bunx: root the package cache in a per-user directory A package bunx installs can ship a native addon whose library search path (DT_RPATH, DT_RUNPATH, LC_RPATH) climbs out of the package with `..`. The dynamic loader resolves those paths and loads what it finds. The cache sat one component below the shared temp directory, so the climb reached the temp directory itself, where any other local user can create the directory the path names. Root the cache at `/.bunx-` (mode 0700), the same directory `bun install` resolves from BUN_INSTALL_CACHE_DIR, BUN_INSTALL, XDG_CACHE_HOME or HOME. Every directory above it belongs to the user or to root, so the climb is safe at any depth. When no install cache resolves, or it cannot be created, the root is `/.bunx-`, also 0700. The ownership checks now start at that root instead of at the first component below the temp directory, and `bun pm cache rm` clears and counts both roots plus the per-package directories an older bun wrote straight into the temp directory. Co-authored-by: Jarred Sumner --- .../PackageManagerDirectories.rs | 39 +-- src/runtime/cli/bunx_command.rs | 222 +++++++++++++++--- src/runtime/cli/package_manager_command.rs | 61 ++++- test/cli/install/bun-pm.test.ts | 51 ++++ test/cli/install/bunx.test.ts | 219 +++++++++++++++-- 5 files changed, 516 insertions(+), 76 deletions(-) diff --git a/src/install/PackageManager/PackageManagerDirectories.rs b/src/install/PackageManager/PackageManagerDirectories.rs index 6dae4163a5c5..e46535aa3cf6 100644 --- a/src/install/PackageManager/PackageManagerDirectories.rs +++ b/src/install/PackageManager/PackageManagerDirectories.rs @@ -374,47 +374,56 @@ unsafe fn ensure_cache_directory(this: *mut PackageManager) -> Dir { pub struct CacheDir { pub path: Vec, + /// True when nothing named a cache directory and `path` is + /// `node_modules/.bun-cache` under the working directory. + /// + /// `bunx` roots its package cache in this directory and needs one that + /// only the current user can write. The working directory is not one, so + /// `bunx` uses its own per-user root in the temp directory instead. + pub is_cwd_fallback: bool, +} + +impl CacheDir { + fn configured(path: Vec) -> CacheDir { + CacheDir { + path, + is_cwd_fallback: false, + } + } } pub fn fetch_cache_directory_path(env: &mut DotEnvLoader, options: Option<&Options>) -> CacheDir { if let Some(dir) = env.get(b"BUN_INSTALL_CACHE_DIR") { - return CacheDir { - path: FileSystem::instance().abs(&[dir]).to_vec(), - }; + return CacheDir::configured(FileSystem::instance().abs(&[dir]).to_vec()); } if let Some(opts) = options { if !opts.cache_directory.is_empty() { - return CacheDir { - path: FileSystem::instance().abs(&[opts.cache_directory]).to_vec(), - }; + return CacheDir::configured( + FileSystem::instance().abs(&[opts.cache_directory]).to_vec(), + ); } } if let Some(dir) = env.get(b"BUN_INSTALL") { let parts: [&[u8]; 3] = [dir, b"install/", b"cache/"]; - return CacheDir { - path: FileSystem::instance().abs(&parts).to_vec(), - }; + return CacheDir::configured(FileSystem::instance().abs(&parts).to_vec()); } if let Some(dir) = env_var::XDG_CACHE_HOME.get() { let parts: [&[u8]; 4] = [dir, b".bun/", b"install/", b"cache/"]; - return CacheDir { - path: FileSystem::instance().abs(&parts).to_vec(), - }; + return CacheDir::configured(FileSystem::instance().abs(&parts).to_vec()); } if let Some(dir) = env_var::HOME.get() { let parts: [&[u8]; 4] = [dir, b".bun/", b"install/", b"cache/"]; - return CacheDir { - path: FileSystem::instance().abs(&parts).to_vec(), - }; + return CacheDir::configured(FileSystem::instance().abs(&parts).to_vec()); } let fallback_parts: [&[u8]; 1] = [b"node_modules/.bun-cache"]; CacheDir { path: FileSystem::instance().abs(&fallback_parts).to_vec(), + is_cwd_fallback: true, } } diff --git a/src/runtime/cli/bunx_command.rs b/src/runtime/cli/bunx_command.rs index 42af649bb36a..54f326d4aab3 100644 --- a/src/runtime/cli/bunx_command.rs +++ b/src/runtime/cli/bunx_command.rs @@ -213,6 +213,35 @@ pub(crate) enum GetBinNameError { NeedToInstall, } +/// Name of the directory bunx roots its package cache in, inside the install +/// cache directory or the temp directory. `bun pm cache rm` clears it, so both +/// spell it from here. +pub(crate) fn cache_root_name() -> Vec { + #[cfg(unix)] + // SAFETY: getuid(2) is always-successful with no preconditions. + let uid = unsafe { libc::getuid() }; + #[cfg(windows)] + let uid = bun_sys::windows::user_unique_id(); + let mut name = Vec::new(); + write!(&mut name, ".bunx-{uid}").expect("unreachable"); + name +} + +/// The directory the bunx package cache tree is rooted in. One directory per +/// package sits directly below it. +struct CacheRoot { + /// Absolute path of the root. + path: Vec, + /// Length of the directory the root was created in. bunx owns everything + /// below it and nothing above it, which is where the ownership checks + /// stop. + parent_len: usize, + /// True when the package directory carries the `bunx--` prefix + /// because the root holds more than the bunx cache (Windows, where the + /// temp directory is already per-user). + prefix_package_with_uid: bool, +} + impl BunxCommand { /// Adds `create-` to the string, but also handles scoped packages correctly. /// Always clones the string in the process. @@ -268,8 +297,7 @@ impl BunxCommand { const NANOSECONDS_CACHE_VALID: i128 = (Self::SECONDS_CACHE_VALID as i128) * 1_000_000_000; /// `bin` keys (and the `name` fallback) in package.json are command - /// names, not paths. The bunx cache lives in a world-writable temp dir, - /// so a crafted package.json there could yield a key like + /// names, not paths. A published package.json could yield a key like /// `../../../../tmp/x` or `/tmp/x`; `bun_which::which` resolves /// slash-containing names against the cwd, escaping `node_modules/.bin` /// and skipping the cache-ownership check before execution. Reject @@ -528,8 +556,9 @@ impl BunxCommand { /// Refuse to execute a binary resolved from inside the bunx cache unless /// it is owned by the current user. /// - /// The bunx cache lives under the world-writable temp dir at a predictable - /// path. Another local user could pre-create that path. Bun's bin linker + /// The cache root is a private per-user directory, but bunx also reaches + /// it through `$PATH` and through paths built from `package.json`, so the + /// check runs on every candidate as defense in depth. Bun's bin linker /// creates `.bin/` entries as *symlinks* on Unix /// (`Linker::create_symlink`), so a regular-file-only check would mark every /// legitimate cache hit as untrusted and reinstall on every invocation. @@ -538,8 +567,8 @@ impl BunxCommand { /// regular file so an attacker-planted, uid-matching link can't redirect /// execution outside the cache. /// - /// On non-Unix targets there is no comparable shared world-writable temp - /// dir / uid model, so the check is a no-op there. + /// On non-Unix targets there is no comparable uid model, so the check is a + /// no-op there. #[cfg(unix)] fn is_trusted_cached_binary(destination: &ZStr, uid: libc::uid_t) -> bool { let lstat_ok = |st: &bun_sys::Stat| { @@ -566,10 +595,14 @@ impl BunxCommand { true } + /// Check every directory bunx owns on the way to `cache_root`: the + /// per-user cache root itself and each component below it. + /// `root_parent_len` is the length of the directory the root was created + /// in, which bunx does not own and does not judge here. #[cfg(unix)] - fn is_trusted_cache_root(cache_root: &[u8], temp_dir_len: usize, uid: libc::uid_t) -> bool { + fn is_trusted_cache_root(cache_root: &[u8], root_parent_len: usize, uid: libc::uid_t) -> bool { let mut buf = bun_paths::path_buffer_pool::get(); - if cache_root.len() >= buf.len() || temp_dir_len >= cache_root.len() { + if cache_root.len() >= buf.len() || root_parent_len >= cache_root.len() { return false; } buf[..cache_root.len()].copy_from_slice(cache_root); @@ -578,7 +611,7 @@ impl BunxCommand { && st.st_uid == uid && (st.st_mode & (libc::S_IWGRP | libc::S_IWOTH)) == 0 }; - let mut start = temp_dir_len + 1; + let mut start = root_parent_len + 1; loop { let end = match strings::index_of_char_pos(cache_root, bun_paths::SEP, start) { Some(i) => i, @@ -607,15 +640,19 @@ impl BunxCommand { #[cfg(not(unix))] #[inline(always)] - fn is_trusted_cache_root(_cache_root: &[u8], _temp_dir_len: usize, _uid: u32) -> bool { + fn is_trusted_cache_root(_cache_root: &[u8], _root_parent_len: usize, _uid: u32) -> bool { true } + /// The same check as `is_trusted_cache_root`, bound to the directory bunx + /// actually opened: the leaf must be the same inode as the path it was + /// opened by, so a rename between the open and the install cannot redirect + /// it. #[cfg(unix)] fn is_trusted_opened_cache_dir( dir: Fd, cache_dir: &[u8], - temp_dir_len: usize, + root_parent_len: usize, uid: libc::uid_t, ) -> bool { let dir_ok = |st: &bun_sys::Stat| { @@ -646,7 +683,7 @@ impl BunxCommand { } is_leaf = false; match strings::last_index_of_char(&cache_dir[..end], bun_paths::SEP) { - Some(idx) if idx > temp_dir_len => end = idx, + Some(idx) if idx > root_parent_len => end = idx, _ => return true, } } @@ -657,12 +694,108 @@ impl BunxCommand { fn is_trusted_opened_cache_dir( _dir: Fd, _cache_dir: &[u8], - _temp_dir_len: usize, + _root_parent_len: usize, _uid: u32, ) -> bool { true } + /// Make `root` usable as the cache root. Reports false only when it does + /// not exist and cannot be created, which is what sends bunx to the next + /// candidate root. A directory that exists is left for + /// `is_trusted_cache_root` to judge, so a root owned by somebody else is + /// reported to the user instead of being quietly worked around. + /// + /// Only the root itself is private. Its parents are the install cache + /// directory, which `bun install` creates with the default mode. + #[cfg(unix)] + fn ensure_cache_root(root: &[u8]) -> bool { + let mut buf = bun_paths::path_buffer_pool::get(); + if root.len() >= buf.len() { + return false; + } + buf[..root.len()].copy_from_slice(root); + buf[root.len()] = 0; + if bun_sys::lstat(ZStr::from_buf(&buf[..], root.len())).is_ok() { + return true; + } + let parent = match bun_paths::dirname(root) { + Some(parent) => parent, + None => return false, + }; + if Fd::cwd().make_path(parent).is_err() { + return false; + } + bun_sys::mkdirat(Fd::cwd(), ZStr::from_buf(&buf[..], root.len()), 0o700).is_ok() + } + + /// Pick the directory the bunx package cache tree is rooted in. + /// + /// A package bunx installs can carry a native addon whose library search + /// path climbs out of the package with `..`. The dynamic loader resolves + /// those paths, so the root has to sit in a tree where every directory + /// such a climb can reach belongs to the current user or to root. + /// + /// The install cache (`BUN_INSTALL_CACHE_DIR`, `BUN_INSTALL`, + /// `XDG_CACHE_HOME`, `HOME`) is that tree. When nothing names one, or the + /// root cannot be created there, bunx uses a private directory in the temp + /// directory instead: a climb that stays inside it is still safe, and it + /// is the only writable location left. + #[cfg(unix)] + fn user_cache_root( + env: &mut bun_dotenv::Loader, + temp_dir: &'static [u8], + ) -> crate::Result { + let root_name = cache_root_name(); + let join = |base: &[u8]| -> Vec { + let base = strings::without_trailing_slash(base); + let mut root = Vec::with_capacity(base.len() + root_name.len() + 1); + root.extend_from_slice(base); + root.push(bun_paths::SEP); + root.extend_from_slice(&root_name); + root + }; + + let cache_dir = bun_install::package_manager::fetch_cache_directory_path(env, None); + if !cache_dir.is_cwd_fallback { + let root = join(&cache_dir.path); + if Self::ensure_cache_root(&root) { + return Ok(CacheRoot { + parent_len: root.len() - root_name.len() - 1, + path: root, + prefix_package_with_uid: false, + }); + } + bun_output::scoped_log!(bunx, "install cache root unusable: {}", BStr::new(&root[..])); + } + + // A failure to create this one is reported by the install below, which + // needs the directory anyway. + let root = join(temp_dir); + Self::ensure_cache_root(&root); + Ok(CacheRoot { + parent_len: root.len() - root_name.len() - 1, + path: root, + prefix_package_with_uid: false, + }) + } + + /// Windows has no shared temp directory: `GetTempPath` is per-user, so the + /// layout stays as it was. + #[cfg(not(unix))] + fn user_cache_root( + _env: &mut bun_dotenv::Loader, + temp_dir: &'static [u8], + ) -> crate::Result { + let root = strings::without_trailing_slash(temp_dir).to_vec(); + let parent_len = root.len(); + Ok(CacheRoot { + path: root, + parent_len, + prefix_package_with_uid: true, + }) + } + fn exit_with_usage() -> ! { crate::cli::command::tag_print_help(Command::Tag::BunxCommand, false); Global::exit(1); @@ -931,17 +1064,26 @@ impl BunxCommand { // The bunx cache path is at the following location // - // /bunx--/node_modules/.bin/ + // //node_modules/.bin/ + // + // where is a directory only the current user can write, + // chosen by `user_cache_root`. On Windows it is the temp directory, + // which is already per-user, and the package component keeps its + // `bunx--` prefix. // // Reasoning: - // - Prefix with "bunx" to identify the bunx cache, make it easier to "rm -r" - // - Suffix would not work because scoped packages have a "/" in them, and - // before Bun 1.1 this was practically impossible to clear the cache manually. - // It was easier to just remove the entire temp directory. - // - Use the uid to prevent conflicts between users. If the paths were the same - // across users, you run into permission conflicts - // - If you set permission to 777, you run into a potential attack vector - // where a user can replace the directory with malicious code. + // - A package in this tree can carry a native addon whose library + // search path (DT_RPATH, DT_RUNPATH, LC_RPATH) climbs out of the + // package with `..`. The dynamic loader resolves those paths and + // loads whatever it finds. So every directory such a climb can reach + // has to belong to the current user or to root, which is why the + // tree no longer sits one component below the shared temp directory. + // - Name the root after the uid to prevent conflicts between users. If + // the paths were the same across users, you run into permission + // conflicts. + // - Keep the "bunx" string in the path to identify the cache and make + // it easier to "rm -r". A suffix would not work because scoped + // packages have a "/" in them. // // If this format changes, please update cache clearing code in package_manager_command.rs #[cfg(unix)] @@ -950,16 +1092,27 @@ impl BunxCommand { #[cfg(windows)] let uid = bun_sys::windows::user_unique_id(); + let cache_root = Self::user_cache_root(env_loader, temp_dir)?; + let bunx_cache_dir_buf: Vec = { + let mut v = Vec::with_capacity(cache_root.path.len() + package_fmt.len() + 1); + v.extend_from_slice(&cache_root.path); + v.push(bun_paths::SEP); + if cache_root.prefix_package_with_uid { + write!(&mut v, "bunx-{uid}-").map_err(|_| crate::Error::Alloc(AllocError))?; + } + v.extend_from_slice(&package_fmt); + v + }; + let bunx_cache_dir: &[u8] = &bunx_cache_dir_buf; + path = { let mut v = Vec::new(); let path_is_nonzero = !path.is_empty(); write!( &mut v, - "{tmp}{sep}bunx-{uid}-{pkg}{sep}node_modules{sep}.bin", - tmp = BStr::new(temp_dir), + "{cache}{sep}node_modules{sep}.bin", + cache = BStr::new(bunx_cache_dir), sep = bun_paths::SEP as char, - uid = uid, - pkg = BStr::new(&package_fmt), ) .map_err(|_| crate::Error::Alloc(bun_alloc::AllocError))?; if path_is_nonzero { @@ -972,9 +1125,6 @@ impl BunxCommand { env_loader.map.put(b"PATH", &path)?; // SAFETY: `Transpiler::init` always sets `fs` to the process singleton. let fs = unsafe { &mut *this_transpiler.fs }; - let uid_digits = bun_core::fmt::digit_count(uid); - let bunx_cache_dir: &[u8] = - &path[0..temp_dir.len() + b"/bunx--".len() + package_fmt.len() + uid_digits]; bun_output::scoped_log!(bunx, "bunx_cache_dir: {}", BStr::new(bunx_cache_dir)); @@ -1002,7 +1152,7 @@ impl BunxCommand { unsafe { core::slice::from_raw_parts(absolute_in_cache_dir_buf.as_ptr(), written) } }; - if !Self::is_trusted_cache_root(bunx_cache_dir, temp_dir.len(), uid) { + if !Self::is_trusted_cache_root(bunx_cache_dir, cache_root.parent_len, uid) { Output::err_generic( "refusing to use bunx cache directory {} because it or a parent directory is not a directory owned by the current user. Remove it and try again.", format_args!("{}", BStr::new(bunx_cache_dir)), @@ -1235,8 +1385,7 @@ impl BunxCommand { // Same hardening as the first cache probe: this path // resolves the package's *real* bin name (which may // differ from the package name), so it is just as - // reachable for a binary planted by another local user - // in the world-writable bunx cache. + // reachable for a binary planted in the cache. if strings::has_prefix(out, bunx_cache_dir) && !Self::is_trusted_cached_binary(destination, uid) { @@ -1296,11 +1445,13 @@ impl BunxCommand { Global::exit(1); } + // The root carries 0700 from `user_cache_root`; `make_open_path` only + // creates the package directory below it. let bunx_install_dir = Fd::cwd().make_open_path(bunx_cache_dir)?; if !Self::is_trusted_opened_cache_dir( bunx_install_dir.fd, bunx_cache_dir, - temp_dir.len(), + cache_root.parent_len, uid, ) { Output::err_generic( @@ -1489,9 +1640,8 @@ impl BunxCommand { ) { let out: &[u8] = destination.as_bytes(); // The install we just ran should have created this symlink as the - // current user, but the cache lives in a world-writable temp dir; an - // attacker can race the install and plant a uid-mismatched entry. - // Bail out to the generic error rather than execute it. + // current user. Bail out to the generic error rather than execute + // an entry whose uid does not match. if Self::is_trusted_cached_binary(destination, uid) { let stored = fs.dirname_store.append_slice(out)?; Run::run_binary( diff --git a/src/runtime/cli/package_manager_command.rs b/src/runtime/cli/package_manager_command.rs index 13c0d44ff4db..c0213b29699a 100644 --- a/src/runtime/cli/package_manager_command.rs +++ b/src/runtime/cli/package_manager_command.rs @@ -440,6 +440,26 @@ Learn more about these at https://bun.com/docs/cli/pm.\n"; let mut process_env = bun_dotenv::Loader::init(); process_env.load_process()?; let cache_dir = fetch_cache_directory_path(&mut process_env, None); + + // `bunx` roots its package cache at `/.bunx-`, + // or at `/.bunx-` when no cache directory + // resolves. Both are counted here; the first is inside the + // install cache tree deleted below, so count it first. + let mut deleted: usize = 0; + let bunx_root_name = crate::cli::bunx_command::cache_root_name(); + if !cache_dir.is_cwd_fallback { + let mut root = strings::without_trailing_slash(&cache_dir.path).to_vec(); + root.push(Path::SEP); + root.extend_from_slice(&bunx_root_name); + if let Ok(root_dir) = Dir::open(&root) { + let mut root_iter = bun_sys::iterate_dir(root_dir.fd()); + while let Ok(Some(_)) = root_iter.next() { + deleted += 1; + } + root_dir.close(); + } + } + let mut rm_buf = bun_paths::path_buffer_pool::get(); let rm_dir = match Dir::cwd().make_open_path(&cache_dir.path, Default::default()) { Ok(d) => d, @@ -485,7 +505,10 @@ Learn more about these at https://bun.com/docs/cli/pm.\n"; }; let mut iter = bun_sys::iterate_dir(tmp_dir.fd()); - // This is to match 'bunx_command.BunxCommand.exec's logic + // This is to match 'bunx_command.BunxCommand.exec's logic: + // the per-package directories it writes directly in the + // temp directory (Windows, and caches written by an older + // bun). let mut prefix: Vec = Vec::new(); #[cfg(unix)] { @@ -499,7 +522,6 @@ Learn more about these at https://bun.com/docs/cli/pm.\n"; .expect("unreachable"); } - let mut deleted: usize = 0; loop { let entry = match iter.next() { Ok(Some(e)) => e, @@ -515,20 +537,37 @@ Learn more about these at https://bun.com/docs/cli/pm.\n"; } }; let name = entry.name.slice_u8(); - if name.starts_with(prefix.as_slice()) { - if let Err(err) = tmp_dir.delete_tree(name) { - Output::err(err, "Could not delete {s}", (bstr::BStr::new(name),)); - had_err = true; - continue; + let is_cache_root = strings::eql(name, &bunx_root_name); + if !is_cache_root && !name.starts_with(prefix.as_slice()) { + continue; + } + // One package per entry for the flat layout, one per + // entry *inside* the cache root for the nested one. + let count = if is_cache_root { + let mut packages: usize = 0; + if let Ok(root_dir) = tmp_dir.open_at(name) { + let mut root_iter = bun_sys::iterate_dir(root_dir.fd()); + while let Ok(Some(_)) = root_iter.next() { + packages += 1; + } + root_dir.close(); } - - deleted += 1; + packages + } else { + 1 + }; + if let Err(err) = tmp_dir.delete_tree(name) { + Output::err(err, "Could not delete {s}", (bstr::BStr::new(name),)); + had_err = true; + continue; } - } - bun_core::prettyln!("Cleared {} cached 'bunx' packages", deleted); + deleted += count; + } } + bun_core::prettyln!("Cleared {} cached 'bunx' packages", deleted); + Global::exit(if had_err { 1 } else { 0 }); } diff --git a/test/cli/install/bun-pm.test.ts b/test/cli/install/bun-pm.test.ts index fcde7ada6f33..22ba52415350 100644 --- a/test/cli/install/bun-pm.test.ts +++ b/test/cli/install/bun-pm.test.ts @@ -1138,3 +1138,54 @@ test("bun pm cache rm does not create the directory named by a project-local .en expect(stderr).not.toContain("error"); expect(exitCode).toBe(0); }); + +// `bunx` roots its package cache at `/.bunx-`, or at +// `/.bunx-` when no install cache resolves. `bun pm cache rm` +// reports and clears the packages in both, plus the per-package directories an +// older bun wrote straight into the temp directory. +test.skipIf(process.platform === "win32")("bun pm cache rm clears and counts every bunx cache layout", async () => { + const uid = process.getuid!(); + using dir = tempDir("pm-cache-rm-bunx", { + "package.json": JSON.stringify({ name: "cache-rm-bunx", version: "1.0.0" }), + // Two packages in the per-user root under the install cache. + [`bun-install/install/cache/.bunx-${uid}/uglify-js@3.14.1/node_modules/.bin/keep`]: "bin", + [`bun-install/install/cache/.bunx-${uid}/cowsay@latest/node_modules/.bin/keep`]: "bin", + // One in the per-user root in the temp directory (the fallback layout). + [`tmp/.bunx-${uid}/prettier@latest/node_modules/.bin/keep`]: "bin", + // One written by an older bun, straight into the temp directory. + [`tmp/bunx-${uid}-eslint@latest/node_modules/.bin/keep`]: "bin", + // Another user's directory in the same temp directory is left alone. + [`tmp/bunx-${uid + 1}-someone-else@latest/keep`]: "theirs", + }); + const dirStr = String(dir); + const installCache = join(dirStr, "bun-install", "install", "cache"); + const tmpDir = join(dirStr, "tmp"); + + const spawnEnv: NodeJS.Dict = { + ...env, + BUN_INSTALL: join(dirStr, "bun-install"), + TMPDIR: tmpDir, + TEMP: tmpDir, + BUN_TMPDIR: tmpDir, + HOME: dirStr, + }; + delete spawnEnv.BUN_INSTALL_CACHE_DIR; + + await using proc = spawn({ + cmd: [bunExe(), "pm", "cache", "rm"], + cwd: dirStr, + stdout: "pipe", + stderr: "pipe", + env: spawnEnv, + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + + expect(stdout).toInclude("Cleared 4 cached 'bunx' packages"); + expect(stderr).not.toContain("error"); + expect(exitCode).toBe(0); + + expect(await exists(join(installCache, `.bunx-${uid}`))).toBeFalse(); + expect(await exists(join(tmpDir, `.bunx-${uid}`))).toBeFalse(); + expect(await exists(join(tmpDir, `bunx-${uid}-eslint@latest`))).toBeFalse(); + expect(await exists(join(tmpDir, `bunx-${uid + 1}-someone-else@latest`, "keep"))).toBeTrue(); +}); diff --git a/test/cli/install/bunx.test.ts b/test/cli/install/bunx.test.ts index ec0a9fb5a508..69f7418dd708 100644 --- a/test/cli/install/bunx.test.ts +++ b/test/cli/install/bunx.test.ts @@ -1,10 +1,10 @@ import { spawn } from "bun"; import { afterAll, beforeAll, beforeEach, describe, expect, it, setDefaultTimeout } from "bun:test"; import { mkdir, rm, writeFile } from "fs/promises"; -import { bunEnv, bunExe, isWindows, readdirSorted, tmpdirSync } from "harness"; -import { chmodSync, copyFileSync, readdirSync, symlinkSync } from "node:fs"; +import { bunEnv, bunExe, isWindows, readdirSorted, tempDir, tmpdirSync } from "harness"; +import { chmodSync, copyFileSync, readdirSync, statSync, symlinkSync } from "node:fs"; import { tmpdir } from "os"; -import { delimiter, join, resolve } from "path"; +import { delimiter, dirname, join, resolve } from "path"; import { dummyAfterAll, dummyBeforeAll, dummyBeforeEach, dummyRegistry, getPort, setHandler } from "./dummy.registry"; setDefaultTimeout(1000 * 60 * 5); @@ -45,7 +45,7 @@ beforeAll(async () => { const tmp = isWindows ? tmpdir() : "/tmp"; const waiting: Promise[] = []; readdirSync(tmp).forEach(file => { - if (file.startsWith("bunx-") || file.startsWith("bun-x.test")) { + if (file.includes("bunx-") || file.startsWith("bun-x.test")) { waiting.push(rm(join(tmp, file), { recursive: true, force: true })); } }); @@ -1258,20 +1258,19 @@ it.skipIf(!isWindows)("should not crash on corrupted .bunx file with missing quo expect(stderr).not.toContain("reached unreachable code"); }); -// The bunx cache root lives at a predictable path inside the shared temp dir -// ($TMPDIR/bunx--@). bunx must refuse to reuse a -// pre-existing cache root that is not a private directory owned by the +// The bunx cache root lives at a predictable path inside the per-user install +// cache (/.bunx-/@). bunx must refuse to +// reuse a pre-existing cache root that is not a private directory owned by the // current user, because the owner of that directory can replace any of the // cached package's module files after install. The check happens before any // network or filesystem access inside the cache, so this test is fully -// offline. The check is Unix-only (no uid/world-writable-tmp model on -// Windows). +// offline. The check is Unix-only (no uid model on Windows). it.concurrent.skipIf(isWindows)( "refuses to reuse a bunx cache directory that other local users can modify", async () => { const { x_dir, env } = setup(); const pkg = "bunx-cache-root-fixture"; - const cacheRoot = join(env.TMPDIR, `bunx-${process.getuid!()}-${pkg}@latest`); + const cacheRoot = join(env.BUN_INSTALL_CACHE_DIR, `.bunx-${process.getuid!()}`, `${pkg}@latest`); const run = () => { const subprocess = spawn({ @@ -1300,9 +1299,8 @@ it.concurrent.skipIf(isWindows)( } // The same cache root made writable by group/other -- the state a - // pre-created directory in the shared temp dir must be in for another - // user's install to populate it -- must be refused before bunx reads or - // writes anything inside it. + // pre-created directory must be in for another user's install to populate + // it -- must be refused before bunx reads or writes anything inside it. chmodSync(cacheRoot, 0o777); { const [err, out, exitCode] = await run(); @@ -1333,7 +1331,7 @@ it.concurrent.skipIf(isWindows)( const { x_dir, env } = setup(); const scope = "bunx-cache-scope-fixture"; const pkg = "bunx-cache-root-fixture"; - const scopeDir = join(env.TMPDIR, `bunx-${process.getuid!()}-@${scope}`); + const scopeDir = join(env.BUN_INSTALL_CACHE_DIR, `.bunx-${process.getuid!()}`, `@${scope}`); const run = () => { const subprocess = spawn({ @@ -1380,3 +1378,196 @@ it.concurrent.skipIf(isWindows)( } }, ); + +// A native addon carries its own library search paths (DT_RPATH, DT_RUNPATH, +// LC_RPATH). They are relative to the addon's own directory and may climb out +// of the package with `..`, which the dynamic loader resolves and searches. So +// the bunx cache has to sit in a tree whose directories belong to the current +// user: under the shared temp directory, a climb lands where any local user +// can put a library, and that library is loaded into the process of the user +// who ran bunx. Here `$TMPDIR` stands in for the shared temp directory. +const cc = isWindows ? null : (Bun.which("cc") ?? Bun.which("clang") ?? Bun.which("gcc")); + +it.concurrent.skipIf(isWindows || !cc)( + "does not load a library that another local user put in the temp directory", + async () => { + const { x_dir, env } = setup(); + // The default resolution: no cache directory is named, so bunx keys the + // cache off $HOME. `setup` points the install cache at a temp directory, + // which is not where a real user's cache lives. + delete env.BUN_INSTALL_CACHE_DIR; + delete env.BUN_INSTALL; + delete env.XDG_CACHE_HOME; + env.HOME = tmpdirSync(); + + const pkg = "bunx-rpath-fixture"; + const uid = process.getuid!(); + const marker = "BUNX_RPATH_PLANTED_LIB_LOADED"; + // `@img/sharp-linux-x64` climbs five levels. Cover one to eight. + const climbs = [1, 2, 3, 4, 5, 6, 7, 8]; + + using buildDir = tempDir("bunx-rpath-build", { + "package/package.json": JSON.stringify({ name: pkg, version: "1.0.0", bin: { [pkg]: "cli.js" } }), + "package/cli.js": `#!/usr/bin/env bun +let answer = null; +let error = null; +try { + answer = require("./lib/addon.node").answer; +} catch (e) { + error = e.code ?? String(e); +} +console.log(JSON.stringify({ ran: true, answer, error })); +`, + "package/foo.c": `int foo(void) { return 777; }\n`, + "package/addon.c": `typedef struct napi_env__* napi_env; +typedef struct napi_value__* napi_value; +int napi_create_int32(napi_env, int, napi_value*); +int napi_set_named_property(napi_env, napi_value, const char*, napi_value); +int foo(void); +napi_value napi_register_module_v1(napi_env env, napi_value exports) { + napi_value v; + napi_create_int32(env, foo(), &v); + napi_set_named_property(env, exports, "answer", v); + return exports; +} +`, + // What the other local user puts in the temp directory. + "planted.c": `#include +__attribute__((constructor)) static void init(void) { fprintf(stderr, "${marker}\\n"); } +int foo(void) { return 666; } +`, + }); + const build = String(buildDir); + const pkgRoot = join(build, "package"); + await mkdir(join(pkgRoot, "lib"), { recursive: true }); + + const runCc = async (cwd: string, args: string[]) => { + await using proc = spawn({ cmd: [cc!, ...args], cwd, env: bunEnv, stdout: "pipe", stderr: "pipe" }); + const [stdout, stderr, code] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(`${stdout}${stderr}`.trim()).toBe(""); + expect(code).toBe(0); + }; + + // The addon needs libfoo.so and looks for it `..`-wards. The package does + // not ship the library, so only a search path decides what gets loaded. + await runCc(pkgRoot, ["-shared", "-fPIC", "foo.c", "-o", "libfoo.so", "-Wl,-soname,libfoo.so"]); + await runCc(pkgRoot, [ + "-shared", + "-fPIC", + "addon.c", + "-o", + "lib/addon.node", + "-L.", + "-lfoo", + ...climbs.map(n => "-Wl,-rpath,$ORIGIN/" + "../".repeat(n) + "x/lib"), + ]); + await rm(join(pkgRoot, "libfoo.so")); + + const tgzDir = tmpdirSync(); + await Bun.$`tar -czf ${join(tgzDir, `${pkg}-1.0.0.tgz`)} -C ${build} package`.quiet(); + + await mkdir(join(env.TMPDIR, "x", "lib"), { recursive: true }); + await runCc(build, [ + "-shared", + "-fPIC", + "planted.c", + "-o", + join(env.TMPDIR, "x", "lib", "libfoo.so"), + "-Wl,-soname,libfoo.so", + ]); + + using registry = Bun.serve({ + port: 0, + async fetch(req) { + const path = decodeURIComponent(new URL(req.url).pathname); + if (path === `/${pkg}`) { + return Response.json({ + name: pkg, + "dist-tags": { latest: "1.0.0" }, + versions: { + "1.0.0": { + name: pkg, + version: "1.0.0", + bin: { [pkg]: "cli.js" }, + dist: { tarball: `http://localhost:${registry.port}/${pkg}/-/${pkg}-1.0.0.tgz` }, + }, + }, + }); + } + if (path === `/${pkg}/-/${pkg}-1.0.0.tgz`) return new Response(Bun.file(join(tgzDir, `${pkg}-1.0.0.tgz`))); + return new Response("not found", { status: 404 }); + }, + }); + + await using proc = spawn({ + cmd: [bunExe(), "x", pkg], + cwd: x_dir, + stdout: "pipe", + stdin: "ignore", + stderr: "pipe", + env: { + ...env, + npm_config_registry: `http://localhost:${registry.port}/`, + // `cli.js` starts with `#!/usr/bin/env bun`. + PATH: `${dirname(bunExe())}${delimiter}${env.PATH}`, + }, + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + + expect(stderr).not.toContain(marker); + // The addon ran and found no libfoo.so anywhere, rather than the require + // being skipped. + expect(JSON.parse(stdout.trim().split("\n").at(-1)!)).toEqual({ + ran: true, + answer: null, + error: "ERR_DLOPEN_FAILED", + }); + expect(exitCode).toBe(0); + + // The tree is under the per-user cache root, and nothing bunx owns is left + // in the temp directory. + const cacheRoot = join(env.HOME, ".bun", "install", "cache", `.bunx-${uid}`); + expect(statSync(cacheRoot).mode & 0o777).toBe(0o700); + expect(statSync(join(cacheRoot, `${pkg}@latest`, "node_modules", ".bin")).isDirectory()).toBe(true); + expect(readdirSync(env.TMPDIR).filter(entry => entry.includes("bunx-"))).toEqual([]); + // The install cache directory itself keeps the mode bun install gives it. + expect(statSync(join(env.HOME, ".bun", "install", "cache")).mode & 0o777).not.toBe(0o700); + }, +); + +// When no install cache directory resolves, the cache root is a private +// directory in the temp directory rather than a per-package directory one +// component below it: a `..` climb out of a package then stays inside a +// directory only the current user can write. +it.concurrent.skipIf(isWindows)( + "roots the cache in a private temp directory when no install cache resolves", + async () => { + const { x_dir, env } = setup(); + const uid = process.getuid!(); + delete env.BUN_INSTALL_CACHE_DIR; + delete env.BUN_INSTALL; + delete env.XDG_CACHE_HOME; + delete env.HOME; + + const subprocess = spawn({ + cmd: [bunExe(), "x", "uglify-js@3.14.1", "-v"], + cwd: x_dir, + stdout: "pipe", + stdin: "ignore", + stderr: "pipe", + env, + }); + const [err, out, exitCode] = await Promise.all([ + subprocess.stderr.text(), + subprocess.stdout.text(), + subprocess.exited, + ]); + expect(err).not.toContain("error:"); + expect(out.split(/\r?\n/)).toEqual(["uglify-js 3.14.1", ""]); + expect(exitCode).toBe(0); + + const cacheRoot = join(env.TMPDIR, `.bunx-${uid}`); + expect(statSync(cacheRoot).mode & 0o777).toBe(0o700); + expect(statSync(join(cacheRoot, "uglify-js@3.14.1", "node_modules", ".bin")).isDirectory()).toBe(true); + }, +); From 011368fb759ffd26b85916409086c282498e1ebf Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Thu, 1 Oct 2026 17:28:18 +0000 Subject: [PATCH 2/6] [autofix.ci] apply automated fixes --- src/runtime/cli/bunx_command.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/runtime/cli/bunx_command.rs b/src/runtime/cli/bunx_command.rs index 54f326d4aab3..0472b19bd505 100644 --- a/src/runtime/cli/bunx_command.rs +++ b/src/runtime/cli/bunx_command.rs @@ -766,7 +766,11 @@ impl BunxCommand { prefix_package_with_uid: false, }); } - bun_output::scoped_log!(bunx, "install cache root unusable: {}", BStr::new(&root[..])); + bun_output::scoped_log!( + bunx, + "install cache root unusable: {}", + BStr::new(&root[..]) + ); } // A failure to create this one is reported by the install below, which From 8b3f7ad5134a2b2a397bc8a6581837740af4fe3a Mon Sep 17 00:00:00 2001 From: robobun Date: Fri, 2 Oct 2026 05:57:03 +0000 Subject: [PATCH 3/6] bunx: create the cache root only when installing, and keep running when it is not usable Review of the first commit found four defects. The new test linked its fixture with `-Wl,-soname` and undefined napi symbols, which Apple ld rejects, so it failed on macOS instead of skipping. `$ORIGIN` in `DT_RUNPATH` is what it exercises, so it runs on Linux only now. The install bunx spawns has its own package.json in its own cache directory. The ancestor walk that looks for a workspace root reached the user's cache and home, so a malformed `package.json` there failed every cold bunx. The walk is skipped for that install. `EEXIST` from the root's `mkdirat` counted as failure, so when two cold bunx runs raced, the loser silently installed into the temp directory. It counts as success. The root was created during path resolution, so `bunx` wrote into the user's home even when it ran a binary from `$PATH` or from a local `node_modules`. It is created on the install path, and the temp directory is taken there if that fails. A root that exists but belongs to another user, or that a filesystem reports as world-writable, now falls back to the temp directory instead of failing every run with advice the sticky bit forbids. The refusal names `BUN_INSTALL_CACHE_DIR` when the root is in the temp directory. Tests: a module another user puts in the temp directory is not loaded (`require` walks the same way with no addon), a malformed ancestor package.json does not fail the install, and no directory is created when bunx does not install. --- src/install/PackageManager.rs | 8 +- .../PackageManagerDirectories.rs | 7 +- src/runtime/cli/bunx_command.rs | 284 +++++++----- src/runtime/cli/package_manager_command.rs | 16 +- test/cli/install/bunx.test.ts | 426 +++++++++++++----- 5 files changed, 507 insertions(+), 234 deletions(-) diff --git a/src/install/PackageManager.rs b/src/install/PackageManager.rs index 49e49cb8a7ac..0304c05ab7a7 100644 --- a/src/install/PackageManager.rs +++ b/src/install/PackageManager.rs @@ -1672,7 +1672,13 @@ pub fn init( // Check if this is a workspace; if so, use root package if subcommand.should_chdir_to_root() { - if !created_package_json && !no_project { + // The install `bunx` spawns has its own package.json in its own + // cache directory. The directories above it are the user's cache + // and home, so a `package.json` there is not a workspace root for + // this install, and a malformed one must not fail it. + let is_bunx_install = + bun_core::env_var::feature_flag::BUN_INTERNAL_BUNX_INSTALL.get() == Some(true); + if !created_package_json && !no_project && !is_bunx_install { while let Some(parent) = bun_core::dirname(this_cwd) { let parent_without_trailing_slash = strings::without_trailing_slash(parent); let mut parent_path_buf = bun_paths::path_buffer_pool::get(); diff --git a/src/install/PackageManager/PackageManagerDirectories.rs b/src/install/PackageManager/PackageManagerDirectories.rs index e46535aa3cf6..b2c3f05bcd08 100644 --- a/src/install/PackageManager/PackageManagerDirectories.rs +++ b/src/install/PackageManager/PackageManagerDirectories.rs @@ -375,11 +375,8 @@ unsafe fn ensure_cache_directory(this: *mut PackageManager) -> Dir { pub struct CacheDir { pub path: Vec, /// True when nothing named a cache directory and `path` is - /// `node_modules/.bun-cache` under the working directory. - /// - /// `bunx` roots its package cache in this directory and needs one that - /// only the current user can write. The working directory is not one, so - /// `bunx` uses its own per-user root in the temp directory instead. + /// `node_modules/.bun-cache` under the working directory. `bunx` needs a + /// root only the current user can write, and that is not one. pub is_cwd_fallback: bool, } diff --git a/src/runtime/cli/bunx_command.rs b/src/runtime/cli/bunx_command.rs index 0472b19bd505..e00cf79c7384 100644 --- a/src/runtime/cli/bunx_command.rs +++ b/src/runtime/cli/bunx_command.rs @@ -213,9 +213,7 @@ pub(crate) enum GetBinNameError { NeedToInstall, } -/// Name of the directory bunx roots its package cache in, inside the install -/// cache directory or the temp directory. `bun pm cache rm` clears it, so both -/// spell it from here. +/// `bun pm cache rm` clears this directory, so both spell it from here. pub(crate) fn cache_root_name() -> Vec { #[cfg(unix)] // SAFETY: getuid(2) is always-successful with no preconditions. @@ -227,19 +225,29 @@ pub(crate) fn cache_root_name() -> Vec { name } -/// The directory the bunx package cache tree is rooted in. One directory per -/// package sits directly below it. +/// Where the cache tree is rooted. One directory per package sits below it. struct CacheRoot { - /// Absolute path of the root. path: Vec, - /// Length of the directory the root was created in. bunx owns everything - /// below it and nothing above it, which is where the ownership checks - /// stop. + /// Length of the directory the root was created in. bunx owns what is + /// below it and nothing above it, so the ownership checks stop there. parent_len: usize, - /// True when the package directory carries the `bunx--` prefix - /// because the root holds more than the bunx cache (Windows, where the - /// temp directory is already per-user). + /// Windows shares the root with the rest of the temp directory, so the + /// package directory keeps the `bunx--` prefix. prefix_package_with_uid: bool, + /// True when the temp directory is still available as a fallback. + from_install_cache: bool, +} + +#[cfg(unix)] +#[derive(PartialEq, Eq)] +enum CacheRootState { + /// A directory of the current user that nobody else can write. + Private, + /// Not there yet. + Missing, + /// There, but not a directory of the current user, or group- or + /// world-writable. + Foreign, } impl BunxCommand { @@ -556,9 +564,9 @@ impl BunxCommand { /// Refuse to execute a binary resolved from inside the bunx cache unless /// it is owned by the current user. /// - /// The cache root is a private per-user directory, but bunx also reaches - /// it through `$PATH` and through paths built from `package.json`, so the - /// check runs on every candidate as defense in depth. Bun's bin linker + /// The root is private, but bunx also reaches it through `$PATH` and + /// through paths built from `package.json`, so the check runs on every + /// candidate as defense in depth. Bun's bin linker /// creates `.bin/` entries as *symlinks* on Unix /// (`Linker::create_symlink`), so a regular-file-only check would mark every /// legitimate cache hit as untrusted and reinstall on every invocation. @@ -595,10 +603,34 @@ impl BunxCommand { true } - /// Check every directory bunx owns on the way to `cache_root`: the - /// per-user cache root itself and each component below it. - /// `root_parent_len` is the length of the directory the root was created - /// in, which bunx does not own and does not judge here. + /// Refuse a cache root that somebody else could write, and say what to do. + /// A root in the shared temp directory is not the victim's to delete (the + /// sticky bit), so point at the knob that moves the cache instead. + fn refuse_cache_root(cache_dir: &[u8], in_temp_dir: bool) -> ! { + Output::err_generic( + "refusing to use bunx cache directory {} because it or a parent directory is not a directory owned by the current user.", + format_args!("{}", BStr::new(cache_dir)), + ); + if in_temp_dir { + bun_core::note!( + "another user owns that directory. Set BUN_INSTALL_CACHE_DIR (or HOME) to a directory you own." + ); + } else { + bun_core::note!("remove it and try again"); + } + Global::exit(1); + } + + /// A directory the current user owns and nobody else can write. + #[cfg(unix)] + fn is_private_dir(st: &bun_sys::Stat, uid: libc::uid_t) -> bool { + (st.st_mode & libc::S_IFMT) == libc::S_IFDIR + && st.st_uid == uid + && (st.st_mode & (libc::S_IWGRP | libc::S_IWOTH)) == 0 + } + + /// Check the cache root and every component below it. bunx does not own + /// the first `root_parent_len` bytes and does not judge them. #[cfg(unix)] fn is_trusted_cache_root(cache_root: &[u8], root_parent_len: usize, uid: libc::uid_t) -> bool { let mut buf = bun_paths::path_buffer_pool::get(); @@ -606,11 +638,7 @@ impl BunxCommand { return false; } buf[..cache_root.len()].copy_from_slice(cache_root); - let is_trusted_dir = |st: &bun_sys::Stat| { - (st.st_mode & libc::S_IFMT) == libc::S_IFDIR - && st.st_uid == uid - && (st.st_mode & (libc::S_IWGRP | libc::S_IWOTH)) == 0 - }; + let is_trusted_dir = |st: &bun_sys::Stat| Self::is_private_dir(st, uid); let mut start = root_parent_len + 1; loop { let end = match strings::index_of_char_pos(cache_root, bun_paths::SEP, start) { @@ -644,10 +672,8 @@ impl BunxCommand { true } - /// The same check as `is_trusted_cache_root`, bound to the directory bunx - /// actually opened: the leaf must be the same inode as the path it was - /// opened by, so a rename between the open and the install cannot redirect - /// it. + /// `is_trusted_cache_root` bound to the opened directory: the leaf must be + /// the inode the path resolved to, so a rename cannot redirect the install. #[cfg(unix)] fn is_trusted_opened_cache_dir( dir: Fd, @@ -655,11 +681,7 @@ impl BunxCommand { root_parent_len: usize, uid: libc::uid_t, ) -> bool { - let dir_ok = |st: &bun_sys::Stat| { - (st.st_mode & libc::S_IFMT) == libc::S_IFDIR - && st.st_uid == uid - && (st.st_mode & (libc::S_IWGRP | libc::S_IWOTH)) == 0 - }; + let dir_ok = |st: &bun_sys::Stat| Self::is_private_dir(st, uid); let opened = match bun_sys::fstat(dir) { Ok(st) if dir_ok(&st) => st, _ => return false, @@ -700,25 +722,32 @@ impl BunxCommand { true } - /// Make `root` usable as the cache root. Reports false only when it does - /// not exist and cannot be created, which is what sends bunx to the next - /// candidate root. A directory that exists is left for - /// `is_trusted_cache_root` to judge, so a root owned by somebody else is - /// reported to the user instead of being quietly worked around. - /// - /// Only the root itself is private. Its parents are the install cache - /// directory, which `bun install` creates with the default mode. + /// What a candidate cache root is before bunx touches it. #[cfg(unix)] - fn ensure_cache_root(root: &[u8]) -> bool { + fn cache_root_state(root: &[u8], uid: libc::uid_t) -> CacheRootState { let mut buf = bun_paths::path_buffer_pool::get(); if root.len() >= buf.len() { - return false; + return CacheRootState::Foreign; } buf[..root.len()].copy_from_slice(root); buf[root.len()] = 0; - if bun_sys::lstat(ZStr::from_buf(&buf[..], root.len())).is_ok() { - return true; + match bun_sys::lstat(ZStr::from_buf(&buf[..], root.len())) { + Ok(st) if Self::is_private_dir(&st, uid) => CacheRootState::Private, + Ok(_) => CacheRootState::Foreign, + Err(_) => CacheRootState::Missing, + } + } + + /// Create the cache root. Only the root gets 0700: its parents are the + /// install cache directory, which `bun install` creates. + #[cfg(unix)] + fn create_cache_root(root: &[u8]) -> bool { + let mut buf = bun_paths::path_buffer_pool::get(); + if root.len() >= buf.len() { + return false; } + buf[..root.len()].copy_from_slice(root); + buf[root.len()] = 0; let parent = match bun_paths::dirname(root) { Some(parent) => parent, None => return false, @@ -726,78 +755,88 @@ impl BunxCommand { if Fd::cwd().make_path(parent).is_err() { return false; } - bun_sys::mkdirat(Fd::cwd(), ZStr::from_buf(&buf[..], root.len()), 0o700).is_ok() + match bun_sys::mkdirat(Fd::cwd(), ZStr::from_buf(&buf[..], root.len()), 0o700) { + Ok(()) => true, + // Another bunx of this user won the race. + Err(err) => err.get_errno() == bun_sys::E::EEXIST, + } } - /// Pick the directory the bunx package cache tree is rooted in. - /// - /// A package bunx installs can carry a native addon whose library search - /// path climbs out of the package with `..`. The dynamic loader resolves - /// those paths, so the root has to sit in a tree where every directory - /// such a climb can reach belongs to the current user or to root. + #[cfg(not(unix))] + #[inline(always)] + fn create_cache_root(_root: &[u8]) -> bool { + true + } + + #[cfg(unix)] + fn join_cache_root(base: &[u8], name: &[u8]) -> CacheRoot { + let base = strings::without_trailing_slash(base); + let mut path = Vec::with_capacity(base.len() + name.len() + 1); + path.extend_from_slice(base); + path.push(bun_paths::SEP); + path.extend_from_slice(name); + CacheRoot { + parent_len: base.len(), + path, + prefix_package_with_uid: false, + from_install_cache: false, + } + } + + /// The root in the temp directory, which every user shares. A climb out of + /// a package is safe only as far as this root. + #[cfg(unix)] + fn temp_cache_root(temp_dir: &[u8]) -> CacheRoot { + Self::join_cache_root(temp_dir, &cache_root_name()) + } + + /// Pick the root, creating nothing: `bunx` with a binary already on `$PATH` + /// must not write anything. `exec` creates the one it picked when it has to + /// install, and takes the temp directory if that fails. /// - /// The install cache (`BUN_INSTALL_CACHE_DIR`, `BUN_INSTALL`, - /// `XDG_CACHE_HOME`, `HOME`) is that tree. When nothing names one, or the - /// root cannot be created there, bunx uses a private directory in the temp - /// directory instead: a climb that stays inside it is still safe, and it - /// is the only writable location left. + /// The install cache is in the user's own tree, so an addon's `..` climb + /// out of a package cannot reach another user's directory. See `exec`. #[cfg(unix)] fn user_cache_root( env: &mut bun_dotenv::Loader, temp_dir: &'static [u8], - ) -> crate::Result { - let root_name = cache_root_name(); - let join = |base: &[u8]| -> Vec { - let base = strings::without_trailing_slash(base); - let mut root = Vec::with_capacity(base.len() + root_name.len() + 1); - root.extend_from_slice(base); - root.push(bun_paths::SEP); - root.extend_from_slice(&root_name); - root - }; - + uid: libc::uid_t, + ) -> CacheRoot { let cache_dir = bun_install::package_manager::fetch_cache_directory_path(env, None); if !cache_dir.is_cwd_fallback { - let root = join(&cache_dir.path); - if Self::ensure_cache_root(&root) { - return Ok(CacheRoot { - parent_len: root.len() - root_name.len() - 1, - path: root, - prefix_package_with_uid: false, - }); + let mut root = Self::join_cache_root(&cache_dir.path, &cache_root_name()); + // A root that exists but belongs to somebody else, or that a + // filesystem reports as world-writable (drvfs, ntfs-3g, CIFS + // without `uid=`), would fail the check in `exec` on every run, so + // use the temp directory rather than refuse to run at all. + if Self::cache_root_state(&root.path, uid) != CacheRootState::Foreign { + root.from_install_cache = true; + return root; } bun_output::scoped_log!( bunx, - "install cache root unusable: {}", - BStr::new(&root[..]) + "install cache root is not private: {}", + BStr::new(&root.path[..]) ); } - - // A failure to create this one is reported by the install below, which - // needs the directory anyway. - let root = join(temp_dir); - Self::ensure_cache_root(&root); - Ok(CacheRoot { - parent_len: root.len() - root_name.len() - 1, - path: root, - prefix_package_with_uid: false, - }) + Self::temp_cache_root(temp_dir) } - /// Windows has no shared temp directory: `GetTempPath` is per-user, so the - /// layout stays as it was. + /// `GetTempPath` is per-user, so the layout stays as it was. #[cfg(not(unix))] fn user_cache_root( _env: &mut bun_dotenv::Loader, temp_dir: &'static [u8], - ) -> crate::Result { + _uid: u32, + ) -> CacheRoot { let root = strings::without_trailing_slash(temp_dir).to_vec(); let parent_len = root.len(); - Ok(CacheRoot { + CacheRoot { path: root, parent_len, prefix_package_with_uid: true, - }) + from_install_cache: false, + } } fn exit_with_usage() -> ! { @@ -1096,18 +1135,19 @@ impl BunxCommand { #[cfg(windows)] let uid = bun_sys::windows::user_unique_id(); - let cache_root = Self::user_cache_root(env_loader, temp_dir)?; - let bunx_cache_dir_buf: Vec = { - let mut v = Vec::with_capacity(cache_root.path.len() + package_fmt.len() + 1); - v.extend_from_slice(&cache_root.path); + let mut cache_root = Self::user_cache_root(env_loader, temp_dir, uid); + let package_dir_of = |root: &CacheRoot| -> Result, crate::Error> { + let mut v = Vec::with_capacity(root.path.len() + package_fmt.len() + 1); + v.extend_from_slice(&root.path); v.push(bun_paths::SEP); - if cache_root.prefix_package_with_uid { + if root.prefix_package_with_uid { write!(&mut v, "bunx-{uid}-").map_err(|_| crate::Error::Alloc(AllocError))?; } v.extend_from_slice(&package_fmt); - v + Ok(v) }; - let bunx_cache_dir: &[u8] = &bunx_cache_dir_buf; + let mut bunx_cache_dir_buf: Vec = package_dir_of(&cache_root)?; + let mut bunx_cache_dir: &[u8] = &bunx_cache_dir_buf; path = { let mut v = Vec::new(); @@ -1157,11 +1197,7 @@ impl BunxCommand { }; if !Self::is_trusted_cache_root(bunx_cache_dir, cache_root.parent_len, uid) { - Output::err_generic( - "refusing to use bunx cache directory {} because it or a parent directory is not a directory owned by the current user. Remove it and try again.", - format_args!("{}", BStr::new(bunx_cache_dir)), - ); - Global::exit(1); + Self::refuse_cache_root(bunx_cache_dir, !cache_root.from_install_cache); } let passthrough: &[Box<[u8]>] = opts.passthrough_list.as_slice(); @@ -1449,8 +1485,38 @@ impl BunxCommand { Global::exit(1); } - // The root carries 0700 from `user_cache_root`; `make_open_path` only - // creates the package directory below it. + // Nothing above needed the root to exist. The install does. + if !Self::create_cache_root(&cache_root.path) && cache_root.from_install_cache { + bun_output::scoped_log!( + bunx, + "cannot create {}, using the temp directory", + BStr::new(&cache_root.path[..]) + ); + cache_root = Self::temp_cache_root(temp_dir); + Self::create_cache_root(&cache_root.path); + bunx_cache_dir_buf = package_dir_of(&cache_root)?; + bunx_cache_dir = &bunx_cache_dir_buf; + path = { + let mut v = Vec::new(); + write!( + &mut v, + "{cache}{sep}node_modules{sep}.bin", + cache = BStr::new(bunx_cache_dir), + sep = bun_paths::SEP as char, + ) + .map_err(|_| crate::Error::Alloc(bun_alloc::AllocError))?; + if !original_path.is_empty() { + v.push(DELIMITER); + v.extend_from_slice(&original_path); + } + v + }; + env_loader.map.put(b"PATH", &path)?; + if !Self::is_trusted_cache_root(bunx_cache_dir, cache_root.parent_len, uid) { + Self::refuse_cache_root(bunx_cache_dir, true); + } + } + // The root exists and is 0700; this creates the package directory. let bunx_install_dir = Fd::cwd().make_open_path(bunx_cache_dir)?; if !Self::is_trusted_opened_cache_dir( bunx_install_dir.fd, @@ -1458,11 +1524,7 @@ impl BunxCommand { cache_root.parent_len, uid, ) { - Output::err_generic( - "refusing to use bunx cache directory {} because it is not a directory owned by the current user. Remove it and try again.", - format_args!("{}", BStr::new(bunx_cache_dir)), - ); - Global::exit(1); + Self::refuse_cache_root(bunx_cache_dir, !cache_root.from_install_cache); } 'create_package_json: { diff --git a/src/runtime/cli/package_manager_command.rs b/src/runtime/cli/package_manager_command.rs index c0213b29699a..e8e2be3c0896 100644 --- a/src/runtime/cli/package_manager_command.rs +++ b/src/runtime/cli/package_manager_command.rs @@ -441,10 +441,8 @@ Learn more about these at https://bun.com/docs/cli/pm.\n"; process_env.load_process()?; let cache_dir = fetch_cache_directory_path(&mut process_env, None); - // `bunx` roots its package cache at `/.bunx-`, - // or at `/.bunx-` when no cache directory - // resolves. Both are counted here; the first is inside the - // install cache tree deleted below, so count it first. + // The bunx root inside the install cache goes away with the + // tree deleted below, so count it before that. let mut deleted: usize = 0; let bunx_root_name = crate::cli::bunx_command::cache_root_name(); if !cache_dir.is_cwd_fallback { @@ -505,10 +503,8 @@ Learn more about these at https://bun.com/docs/cli/pm.\n"; }; let mut iter = bun_sys::iterate_dir(tmp_dir.fd()); - // This is to match 'bunx_command.BunxCommand.exec's logic: - // the per-package directories it writes directly in the - // temp directory (Windows, and caches written by an older - // bun). + // This is to match 'bunx_command.BunxCommand.exec's logic + // for the directories written straight into the temp dir. let mut prefix: Vec = Vec::new(); #[cfg(unix)] { @@ -541,8 +537,8 @@ Learn more about these at https://bun.com/docs/cli/pm.\n"; if !is_cache_root && !name.starts_with(prefix.as_slice()) { continue; } - // One package per entry for the flat layout, one per - // entry *inside* the cache root for the nested one. + // The root holds one entry per package, the flat + // layout one directory per package. let count = if is_cache_root { let mut packages: usize = 0; if let Ok(root_dir) = tmp_dir.open_at(name) { diff --git a/test/cli/install/bunx.test.ts b/test/cli/install/bunx.test.ts index 69f7418dd708..4925a0b1bca4 100644 --- a/test/cli/install/bunx.test.ts +++ b/test/cli/install/bunx.test.ts @@ -1,8 +1,8 @@ import { spawn } from "bun"; import { afterAll, beforeAll, beforeEach, describe, expect, it, setDefaultTimeout } from "bun:test"; import { mkdir, rm, writeFile } from "fs/promises"; -import { bunEnv, bunExe, isWindows, readdirSorted, tempDir, tmpdirSync } from "harness"; -import { chmodSync, copyFileSync, readdirSync, statSync, symlinkSync } from "node:fs"; +import { bunEnv, bunExe, isLinux, isWindows, readdirSorted, tempDir, tmpdirSync } from "harness"; +import { chmodSync, copyFileSync, existsSync, readdirSync, statSync, symlinkSync } from "node:fs"; import { tmpdir } from "os"; import { delimiter, dirname, join, resolve } from "path"; import { dummyAfterAll, dummyBeforeAll, dummyBeforeEach, dummyRegistry, getPort, setHandler } from "./dummy.registry"; @@ -1289,6 +1289,7 @@ it.concurrent.skipIf(isWindows)( // validation and fails later with the normal --no-install "could not // find an existing binary" message because the cache is empty. await mkdir(cacheRoot, { recursive: true }); + chmodSync(dirname(cacheRoot), 0o700); chmodSync(cacheRoot, 0o755); { const [err, out, exitCode] = await run(); @@ -1346,6 +1347,7 @@ it.concurrent.skipIf(isWindows)( }; await mkdir(scopeDir, { recursive: true }); + chmodSync(dirname(scopeDir), 0o700); chmodSync(scopeDir, 0o755); { const [err, out, exitCode] = await run(); @@ -1379,102 +1381,53 @@ it.concurrent.skipIf(isWindows)( }, ); -// A native addon carries its own library search paths (DT_RPATH, DT_RUNPATH, -// LC_RPATH). They are relative to the addon's own directory and may climb out -// of the package with `..`, which the dynamic loader resolves and searches. So -// the bunx cache has to sit in a tree whose directories belong to the current -// user: under the shared temp directory, a climb lands where any local user -// can put a library, and that library is loaded into the process of the user -// who ran bunx. Here `$TMPDIR` stands in for the shared temp directory. -const cc = isWindows ? null : (Bun.which("cc") ?? Bun.which("clang") ?? Bun.which("gcc")); - -it.concurrent.skipIf(isWindows || !cc)( - "does not load a library that another local user put in the temp directory", +// `require` walks every ancestor of the file that asks, looking for +// `node_modules/`. From a cache one component below the shared temp +// directory the walk reaches `/node_modules`, which any local user +// can create, so an optional dependency a CLI probes for (the +// `try { require("x") } catch {}` shape) runs that user's code. The cache root +// has to sit in a tree whose directories belong to the current user. `$TMPDIR` +// stands in for the shared temp directory. +it.concurrent.skipIf(isWindows || !Bun.which("node"))( + "does not load a module that another local user put in the temp directory", async () => { const { x_dir, env } = setup(); - // The default resolution: no cache directory is named, so bunx keys the - // cache off $HOME. `setup` points the install cache at a temp directory, - // which is not where a real user's cache lives. + // No cache directory is named, so bunx keys the cache off $HOME, as a real + // user's run does. delete env.BUN_INSTALL_CACHE_DIR; delete env.BUN_INSTALL; delete env.XDG_CACHE_HOME; env.HOME = tmpdirSync(); - const pkg = "bunx-rpath-fixture"; - const uid = process.getuid!(); - const marker = "BUNX_RPATH_PLANTED_LIB_LOADED"; - // `@img/sharp-linux-x64` climbs five levels. Cover one to eight. - const climbs = [1, 2, 3, 4, 5, 6, 7, 8]; - - using buildDir = tempDir("bunx-rpath-build", { + const pkg = "bunx-optional-dep-fixture"; + const marker = "BUNX_PLANTED_MODULE_LOADED"; + using buildDir = tempDir("bunx-optional-dep-build", { "package/package.json": JSON.stringify({ name: pkg, version: "1.0.0", bin: { [pkg]: "cli.js" } }), - "package/cli.js": `#!/usr/bin/env bun -let answer = null; -let error = null; + // The optional-dependency probe. It is not a dependency of the package. + // The shebang is node's, the shape almost every npm CLI ships. + "package/cli.js": `#!/usr/bin/env node +let helper = "none"; try { - answer = require("./lib/addon.node").answer; -} catch (e) { - error = e.code ?? String(e); -} -console.log(JSON.stringify({ ran: true, answer, error })); -`, - "package/foo.c": `int foo(void) { return 777; }\n`, - "package/addon.c": `typedef struct napi_env__* napi_env; -typedef struct napi_value__* napi_value; -int napi_create_int32(napi_env, int, napi_value*); -int napi_set_named_property(napi_env, napi_value, const char*, napi_value); -int foo(void); -napi_value napi_register_module_v1(napi_env env, napi_value exports) { - napi_value v; - napi_create_int32(env, foo(), &v); - napi_set_named_property(env, exports, "answer", v); - return exports; -} -`, - // What the other local user puts in the temp directory. - "planted.c": `#include -__attribute__((constructor)) static void init(void) { fprintf(stderr, "${marker}\\n"); } -int foo(void) { return 666; } + helper = require("zz-optional"); +} catch {} +console.log(JSON.stringify({ helper })); `, }); - const build = String(buildDir); - const pkgRoot = join(build, "package"); - await mkdir(join(pkgRoot, "lib"), { recursive: true }); - - const runCc = async (cwd: string, args: string[]) => { - await using proc = spawn({ cmd: [cc!, ...args], cwd, env: bunEnv, stdout: "pipe", stderr: "pipe" }); - const [stdout, stderr, code] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); - expect(`${stdout}${stderr}`.trim()).toBe(""); - expect(code).toBe(0); - }; - - // The addon needs libfoo.so and looks for it `..`-wards. The package does - // not ship the library, so only a search path decides what gets loaded. - await runCc(pkgRoot, ["-shared", "-fPIC", "foo.c", "-o", "libfoo.so", "-Wl,-soname,libfoo.so"]); - await runCc(pkgRoot, [ - "-shared", - "-fPIC", - "addon.c", - "-o", - "lib/addon.node", - "-L.", - "-lfoo", - ...climbs.map(n => "-Wl,-rpath,$ORIGIN/" + "../".repeat(n) + "x/lib"), - ]); - await rm(join(pkgRoot, "libfoo.so")); - + // Executable, so the shebang decides the runtime the way a published CLI does. + chmodSync(join(String(buildDir), "package", "cli.js"), 0o755); const tgzDir = tmpdirSync(); - await Bun.$`tar -czf ${join(tgzDir, `${pkg}-1.0.0.tgz`)} -C ${build} package`.quiet(); - - await mkdir(join(env.TMPDIR, "x", "lib"), { recursive: true }); - await runCc(build, [ - "-shared", - "-fPIC", - "planted.c", - "-o", - join(env.TMPDIR, "x", "lib", "libfoo.so"), - "-Wl,-soname,libfoo.so", - ]); + await Bun.$`tar -czf ${join(tgzDir, `${pkg}-1.0.0.tgz`)} -C ${String(buildDir)} package`.quiet(); + + // What any local user can put in a shared temp directory. + await mkdir(join(env.TMPDIR, "node_modules", "zz-optional"), { recursive: true }); + await writeFile( + join(env.TMPDIR, "node_modules", "zz-optional", "index.js"), + `console.error("${marker}");\nmodule.exports = "planted";\n`, + ); + await writeFile( + join(env.TMPDIR, "node_modules", "zz-optional", "package.json"), + JSON.stringify({ name: "zz-optional", version: "1.0.0", main: "index.js" }), + ); using registry = Bun.serve({ port: 0, @@ -1515,26 +1468,226 @@ int foo(void) { return 666; } const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); expect(stderr).not.toContain(marker); - // The addon ran and found no libfoo.so anywhere, rather than the require - // being skipped. - expect(JSON.parse(stdout.trim().split("\n").at(-1)!)).toEqual({ - ran: true, - answer: null, - error: "ERR_DLOPEN_FAILED", - }); + expect(JSON.parse(stdout.trim().split("\n").at(-1)!)).toEqual({ helper: "none" }); expect(exitCode).toBe(0); - - // The tree is under the per-user cache root, and nothing bunx owns is left - // in the temp directory. - const cacheRoot = join(env.HOME, ".bun", "install", "cache", `.bunx-${uid}`); - expect(statSync(cacheRoot).mode & 0o777).toBe(0o700); - expect(statSync(join(cacheRoot, `${pkg}@latest`, "node_modules", ".bin")).isDirectory()).toBe(true); - expect(readdirSync(env.TMPDIR).filter(entry => entry.includes("bunx-"))).toEqual([]); - // The install cache directory itself keeps the mode bun install gives it. - expect(statSync(join(env.HOME, ".bun", "install", "cache")).mode & 0o777).not.toBe(0o700); }, ); +// The install bunx spawns has its own package.json in its own cache directory. +// The directories above that are the user's cache and home, so a package.json +// there is not a workspace root for this install and must not fail it. +it.concurrent.skipIf(isWindows)("installs even when an ancestor package.json is malformed", async () => { + const { x_dir, env } = setup(); + delete env.BUN_INSTALL_CACHE_DIR; + delete env.BUN_INSTALL; + delete env.XDG_CACHE_HOME; + env.HOME = tmpdirSync(); + await writeFile(join(env.HOME, "package.json"), "{"); + + const pkg = "bunx-ancestor-fixture"; + using buildDir = tempDir("bunx-ancestor-build", { + "package/package.json": JSON.stringify({ name: pkg, version: "1.0.0", bin: { [pkg]: "cli.js" } }), + "package/cli.js": `#!/bin/sh\necho ancestor-ok\n`, + }); + chmodSync(join(String(buildDir), "package", "cli.js"), 0o755); + const tgzDir = tmpdirSync(); + await Bun.$`tar -czf ${join(tgzDir, `${pkg}-1.0.0.tgz`)} -C ${String(buildDir)} package`.quiet(); + + using registry = Bun.serve({ + port: 0, + async fetch(req) { + const path = decodeURIComponent(new URL(req.url).pathname); + if (path === `/${pkg}`) { + return Response.json({ + name: pkg, + "dist-tags": { latest: "1.0.0" }, + versions: { + "1.0.0": { + name: pkg, + version: "1.0.0", + bin: { [pkg]: "cli.js" }, + dist: { tarball: `http://localhost:${registry.port}/${pkg}/-/${pkg}-1.0.0.tgz` }, + }, + }, + }); + } + if (path === `/${pkg}/-/${pkg}-1.0.0.tgz`) return new Response(Bun.file(join(tgzDir, `${pkg}-1.0.0.tgz`))); + return new Response("not found", { status: 404 }); + }, + }); + + await using proc = spawn({ + cmd: [bunExe(), "x", pkg], + cwd: x_dir, + stdout: "pipe", + stdin: "ignore", + stderr: "pipe", + env: { ...env, npm_config_registry: `http://localhost:${registry.port}/` }, + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + + expect(stderr).not.toContain("package.json"); + expect(stdout).toContain("ancestor-ok"); + expect(exitCode).toBe(0); +}); + +// A native addon carries its own library search paths (DT_RPATH, DT_RUNPATH, +// LC_RPATH). They are relative to the addon's own directory and may climb out +// of the package with `..`, which the dynamic loader resolves and searches. So +// the bunx cache has to sit in a tree whose directories belong to the current +// user: under the shared temp directory, a climb lands where any local user +// can put a library, and that library is loaded into the process of the user +// who ran bunx. Here `$TMPDIR` stands in for the shared temp directory. +// +// Linux only: `$ORIGIN` in `DT_RUNPATH` is what the fixture links against, and +// macOS gives each user their own `$TMPDIR` anyway. The cache root is the same +// on both. +const cc = isLinux ? (Bun.which("cc") ?? Bun.which("clang") ?? Bun.which("gcc")) : null; + +it.concurrent.skipIf(!cc)("does not load a library that another local user put in the temp directory", async () => { + const { x_dir, env } = setup(); + // The default resolution: no cache directory is named, so bunx keys the + // cache off $HOME. `setup` points the install cache at a temp directory, + // which is not where a real user's cache lives. + delete env.BUN_INSTALL_CACHE_DIR; + delete env.BUN_INSTALL; + delete env.XDG_CACHE_HOME; + env.HOME = tmpdirSync(); + + const pkg = "bunx-rpath-fixture"; + const uid = process.getuid!(); + const marker = "BUNX_RPATH_PLANTED_LIB_LOADED"; + // `@img/sharp-linux-x64` climbs five levels. Cover one to eight. + const climbs = [1, 2, 3, 4, 5, 6, 7, 8]; + + using buildDir = tempDir("bunx-rpath-build", { + "package/package.json": JSON.stringify({ name: pkg, version: "1.0.0", bin: { [pkg]: "cli.js" } }), + "package/cli.js": `#!/usr/bin/env bun +let answer = null; +let error = null; +try { + answer = require("./lib/addon.node").answer; +} catch (e) { + error = e.code ?? String(e); +} +console.log(JSON.stringify({ ran: true, answer, error })); +`, + "package/foo.c": `int foo(void) { return 777; }\n`, + "package/addon.c": `typedef struct napi_env__* napi_env; +typedef struct napi_value__* napi_value; +int napi_create_int32(napi_env, int, napi_value*); +int napi_set_named_property(napi_env, napi_value, const char*, napi_value); +int foo(void); +napi_value napi_register_module_v1(napi_env env, napi_value exports) { + napi_value v; + napi_create_int32(env, foo(), &v); + napi_set_named_property(env, exports, "answer", v); + return exports; +} +`, + // What the other local user puts in the temp directory. + "planted.c": `#include +__attribute__((constructor)) static void init(void) { fprintf(stderr, "${marker}\\n"); } +int foo(void) { return 666; } +`, + }); + const build = String(buildDir); + const pkgRoot = join(build, "package"); + await mkdir(join(pkgRoot, "lib"), { recursive: true }); + + const runCc = async (cwd: string, args: string[]) => { + await using proc = spawn({ cmd: [cc!, ...args], cwd, env: bunEnv, stdout: "pipe", stderr: "pipe" }); + const [stdout, stderr, code] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(`${stdout}${stderr}`.trim()).toBe(""); + expect(code).toBe(0); + }; + + // The addon needs libfoo.so and looks for it `..`-wards. The package does + // not ship the library, so only a search path decides what gets loaded. + await runCc(pkgRoot, ["-shared", "-fPIC", "foo.c", "-o", "libfoo.so", "-Wl,-soname,libfoo.so"]); + await runCc(pkgRoot, [ + "-shared", + "-fPIC", + "addon.c", + "-o", + "lib/addon.node", + "-L.", + "-lfoo", + ...climbs.map(n => "-Wl,-rpath,$ORIGIN/" + "../".repeat(n) + "x/lib"), + ]); + await rm(join(pkgRoot, "libfoo.so")); + + const tgzDir = tmpdirSync(); + await Bun.$`tar -czf ${join(tgzDir, `${pkg}-1.0.0.tgz`)} -C ${build} package`.quiet(); + + await mkdir(join(env.TMPDIR, "x", "lib"), { recursive: true }); + await runCc(build, [ + "-shared", + "-fPIC", + "planted.c", + "-o", + join(env.TMPDIR, "x", "lib", "libfoo.so"), + "-Wl,-soname,libfoo.so", + ]); + + using registry = Bun.serve({ + port: 0, + async fetch(req) { + const path = decodeURIComponent(new URL(req.url).pathname); + if (path === `/${pkg}`) { + return Response.json({ + name: pkg, + "dist-tags": { latest: "1.0.0" }, + versions: { + "1.0.0": { + name: pkg, + version: "1.0.0", + bin: { [pkg]: "cli.js" }, + dist: { tarball: `http://localhost:${registry.port}/${pkg}/-/${pkg}-1.0.0.tgz` }, + }, + }, + }); + } + if (path === `/${pkg}/-/${pkg}-1.0.0.tgz`) return new Response(Bun.file(join(tgzDir, `${pkg}-1.0.0.tgz`))); + return new Response("not found", { status: 404 }); + }, + }); + + await using proc = spawn({ + cmd: [bunExe(), "x", pkg], + cwd: x_dir, + stdout: "pipe", + stdin: "ignore", + stderr: "pipe", + env: { + ...env, + npm_config_registry: `http://localhost:${registry.port}/`, + // `cli.js` starts with `#!/usr/bin/env bun`. + PATH: `${dirname(bunExe())}${delimiter}${env.PATH}`, + }, + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + + expect(stderr).not.toContain(marker); + // The addon ran and found no libfoo.so anywhere, rather than the require + // being skipped. + expect(JSON.parse(stdout.trim().split("\n").at(-1)!)).toEqual({ + ran: true, + answer: null, + error: "ERR_DLOPEN_FAILED", + }); + expect(exitCode).toBe(0); + + // The tree is under the per-user cache root, and nothing bunx owns is left + // in the temp directory. + const cacheRoot = join(env.HOME, ".bun", "install", "cache", `.bunx-${uid}`); + expect(statSync(cacheRoot).mode & 0o777).toBe(0o700); + expect(statSync(join(cacheRoot, `${pkg}@latest`, "node_modules", ".bin")).isDirectory()).toBe(true); + expect(readdirSync(env.TMPDIR).filter(entry => entry.includes("bunx-"))).toEqual([]); + // The install cache directory itself keeps the mode bun install gives it. + expect(statSync(join(env.HOME, ".bun", "install", "cache")).mode & 0o777).not.toBe(0o700); +}); + // When no install cache directory resolves, the cache root is a private // directory in the temp directory rather than a per-package directory one // component below it: a `..` climb out of a package then stays inside a @@ -1549,13 +1702,45 @@ it.concurrent.skipIf(isWindows)( delete env.XDG_CACHE_HOME; delete env.HOME; - const subprocess = spawn({ - cmd: [bunExe(), "x", "uglify-js@3.14.1", "-v"], + const pkg = "bunx-fallback-fixture"; + using buildDir = tempDir("bunx-fallback-build", { + "package/package.json": JSON.stringify({ name: pkg, version: "1.0.0", bin: { [pkg]: "cli.js" } }), + "package/cli.js": `#!/bin/sh\necho fallback-ok\n`, + }); + chmodSync(join(String(buildDir), "package", "cli.js"), 0o755); + const tgzDir = tmpdirSync(); + await Bun.$`tar -czf ${join(tgzDir, `${pkg}-1.0.0.tgz`)} -C ${String(buildDir)} package`.quiet(); + + using registry = Bun.serve({ + port: 0, + async fetch(req) { + const path = decodeURIComponent(new URL(req.url).pathname); + if (path === `/${pkg}`) { + return Response.json({ + name: pkg, + "dist-tags": { latest: "1.0.0" }, + versions: { + "1.0.0": { + name: pkg, + version: "1.0.0", + bin: { [pkg]: "cli.js" }, + dist: { tarball: `http://localhost:${registry.port}/${pkg}/-/${pkg}-1.0.0.tgz` }, + }, + }, + }); + } + if (path === `/${pkg}/-/${pkg}-1.0.0.tgz`) return new Response(Bun.file(join(tgzDir, `${pkg}-1.0.0.tgz`))); + return new Response("not found", { status: 404 }); + }, + }); + + await using subprocess = spawn({ + cmd: [bunExe(), "x", pkg], cwd: x_dir, stdout: "pipe", stdin: "ignore", stderr: "pipe", - env, + env: { ...env, npm_config_registry: `http://localhost:${registry.port}/` }, }); const [err, out, exitCode] = await Promise.all([ subprocess.stderr.text(), @@ -1563,11 +1748,38 @@ it.concurrent.skipIf(isWindows)( subprocess.exited, ]); expect(err).not.toContain("error:"); - expect(out.split(/\r?\n/)).toEqual(["uglify-js 3.14.1", ""]); + expect(out).toContain("fallback-ok"); expect(exitCode).toBe(0); const cacheRoot = join(env.TMPDIR, `.bunx-${uid}`); expect(statSync(cacheRoot).mode & 0o777).toBe(0o700); - expect(statSync(join(cacheRoot, "uglify-js@3.14.1", "node_modules", ".bin")).isDirectory()).toBe(true); + expect(statSync(join(cacheRoot, `${pkg}@latest`, "node_modules", ".bin")).isDirectory()).toBe(true); }, ); + +// `bunx` must not create anything when it does not install: the root is a +// directory in the user's home, and a run that finds its binary on `$PATH` +// has no business writing there. +it.concurrent.skipIf(isWindows)("creates nothing when it does not install", async () => { + const { x_dir, env } = setup(); + const uid = process.getuid!(); + delete env.BUN_INSTALL_CACHE_DIR; + delete env.BUN_INSTALL; + delete env.XDG_CACHE_HOME; + env.HOME = tmpdirSync(); + + await using subprocess = spawn({ + cmd: [bunExe(), "x", "--no-install", "bunx-absent-fixture"], + cwd: x_dir, + stdout: "pipe", + stdin: "ignore", + stderr: "pipe", + env, + }); + const [err, exitCode] = await Promise.all([subprocess.stderr.text(), subprocess.exited]); + expect(err).toContain("Could not find an existing 'bunx-absent-fixture' binary to run"); + expect(exitCode).toBe(1); + + expect(existsSync(join(env.HOME, ".bun"))).toBe(false); + expect(readdirSync(env.TMPDIR).filter(entry => entry.includes("bunx-"))).toEqual([]); +}); From bd1df82d35135fb3f59f0ed6394110c5337d056e Mon Sep 17 00:00:00 2001 From: robobun Date: Fri, 2 Oct 2026 07:50:29 +0000 Subject: [PATCH 4/6] bunx: pick one usable cache root, keep PATH and the environment whole in the fallback Review of the previous commit found six defects. Windows did not compile: the fallback called `temp_cache_root`, which exists on unix only. The fallback is unix-only now, as the root it falls back from is. A root bunx cannot stat (EACCES from a HOME under a directory it cannot enter, ENOTDIR from a HOME that is a file) counted as not created yet, so every run was refused. Only ENOENT means that. Anything else sends bunx to the temp directory. Where the install cache root can never be created, the warm lookup probed it, missed, and installed again on every run. A missing root now defers to the temp root when a previous run already made that one. The fallback rebuilt PATH from the PATH bunx started with, dropping the node shim directory and every local `node_modules/.bin`. It keeps the configured tail. `BUN_INTERNAL_BUNX_INSTALL` marks the install bunx spawns, and since the previous commit it skips the workspace walk. It stayed in the environment of the tool bunx then runs, so a scaffolder's own `bun install` inside a monorepo lost its workspace root. It is removed once the install has run. A root is verified after it is created: one that a filesystem reports as writable by others is treated like one that could not be created. If the temp root cannot be created either, bunx refuses instead of going on. bunx warns on stderr when it is pushed into the shared temp directory. --- src/runtime/cli/bunx_command.rs | 125 +++++++++++++++++++------------- 1 file changed, 74 insertions(+), 51 deletions(-) diff --git a/src/runtime/cli/bunx_command.rs b/src/runtime/cli/bunx_command.rs index e00cf79c7384..c54f473fde1f 100644 --- a/src/runtime/cli/bunx_command.rs +++ b/src/runtime/cli/bunx_command.rs @@ -734,7 +734,9 @@ impl BunxCommand { match bun_sys::lstat(ZStr::from_buf(&buf[..], root.len())) { Ok(st) if Self::is_private_dir(&st, uid) => CacheRootState::Private, Ok(_) => CacheRootState::Foreign, - Err(_) => CacheRootState::Missing, + Err(err) if err.get_errno() == bun_sys::E::ENOENT => CacheRootState::Missing, + // A parent bunx cannot enter (EACCES) or that is a file (ENOTDIR). + Err(_) => CacheRootState::Foreign, } } @@ -762,12 +764,6 @@ impl BunxCommand { } } - #[cfg(not(unix))] - #[inline(always)] - fn create_cache_root(_root: &[u8]) -> bool { - true - } - #[cfg(unix)] fn join_cache_root(base: &[u8], name: &[u8]) -> CacheRoot { let base = strings::without_trailing_slash(base); @@ -803,23 +799,44 @@ impl BunxCommand { uid: libc::uid_t, ) -> CacheRoot { let cache_dir = bun_install::package_manager::fetch_cache_directory_path(env, None); - if !cache_dir.is_cwd_fallback { - let mut root = Self::join_cache_root(&cache_dir.path, &cache_root_name()); - // A root that exists but belongs to somebody else, or that a - // filesystem reports as world-writable (drvfs, ntfs-3g, CIFS - // without `uid=`), would fail the check in `exec` on every run, so - // use the temp directory rather than refuse to run at all. - if Self::cache_root_state(&root.path, uid) != CacheRootState::Foreign { + if cache_dir.is_cwd_fallback { + return Self::temp_cache_root(temp_dir); + } + let mut root = Self::join_cache_root(&cache_dir.path, &cache_root_name()); + match Self::cache_root_state(&root.path, uid) { + CacheRootState::Private => { root.from_install_cache = true; - return root; + root + } + // A run that installs creates it, or takes the temp directory if + // it cannot. Take the temp directory here too when a run already + // did, so this one finds what that one installed. + CacheRootState::Missing => { + let temp = Self::temp_cache_root(temp_dir); + if Self::cache_root_state(&temp.path, uid) == CacheRootState::Private { + temp + } else { + root.from_install_cache = true; + root + } + } + // Somebody else's, reported world-writable by the filesystem + // (drvfs, ntfs-3g, CIFS without `uid=`), or not statable. Each + // would refuse every run. + CacheRootState::Foreign => { + Self::warn_weaker_root(&root.path); + Self::temp_cache_root(temp_dir) } - bun_output::scoped_log!( - bunx, - "install cache root is not private: {}", - BStr::new(&root.path[..]) - ); } - Self::temp_cache_root(temp_dir) + } + + /// The temp directory is shared, so say so when bunx is pushed into it. + #[cfg(unix)] + fn warn_weaker_root(unusable: &[u8]) { + bun_core::warn!( + "bunx cannot use {} as its cache and is using the shared temp directory instead. Set BUN_INSTALL_CACHE_DIR to a directory only you can write.", + BStr::new(unusable), + ); } /// `GetTempPath` is per-user, so the layout stays as it was. @@ -1135,6 +1152,8 @@ impl BunxCommand { #[cfg(windows)] let uid = bun_sys::windows::user_unique_id(); + // Only the unix fallback below reassigns these three. + #[cfg_attr(not(unix), allow(unused_mut))] let mut cache_root = Self::user_cache_root(env_loader, temp_dir, uid); let package_dir_of = |root: &CacheRoot| -> Result, crate::Error> { let mut v = Vec::with_capacity(root.path.len() + package_fmt.len() + 1); @@ -1146,25 +1165,31 @@ impl BunxCommand { v.extend_from_slice(&package_fmt); Ok(v) }; + #[cfg_attr(not(unix), allow(unused_mut))] let mut bunx_cache_dir_buf: Vec = package_dir_of(&cache_root)?; + #[cfg_attr(not(unix), allow(unused_mut))] let mut bunx_cache_dir: &[u8] = &bunx_cache_dir_buf; - path = { + let cache_bin_of = |cache_dir: &[u8], tail: &[u8]| -> Result, crate::Error> { let mut v = Vec::new(); - let path_is_nonzero = !path.is_empty(); write!( &mut v, "{cache}{sep}node_modules{sep}.bin", - cache = BStr::new(bunx_cache_dir), + cache = BStr::new(cache_dir), sep = bun_paths::SEP as char, ) .map_err(|_| crate::Error::Alloc(bun_alloc::AllocError))?; - if path_is_nonzero { + if !tail.is_empty() { v.push(DELIMITER); - v.extend_from_slice(&path); + v.extend_from_slice(tail); } - v + Ok(v) }; + // Where the part of PATH that is not the cache starts: the node shim + // and every local `node_modules/.bin`, which a fallback root keeps. + #[cfg(unix)] + let path_tail_at = bunx_cache_dir.len() + b"/node_modules/.bin".len() + 1; + path = cache_bin_of(bunx_cache_dir, &path)?; env_loader.map.put(b"PATH", &path)?; // SAFETY: `Transpiler::init` always sets `fs` to the process singleton. @@ -1485,34 +1510,30 @@ impl BunxCommand { Global::exit(1); } - // Nothing above needed the root to exist. The install does. - if !Self::create_cache_root(&cache_root.path) && cache_root.from_install_cache { - bun_output::scoped_log!( - bunx, - "cannot create {}, using the temp directory", - BStr::new(&cache_root.path[..]) - ); + // Nothing above needed the root to exist. The install does. A root + // the filesystem reports as writable by others right after it was + // created 0700 is as unusable as one somebody else owns. + #[cfg(unix)] + if !Self::create_cache_root(&cache_root.path) + || Self::cache_root_state(&cache_root.path, uid) != CacheRootState::Private + { + if !cache_root.from_install_cache { + Self::refuse_cache_root(&cache_root.path, true); + } + Self::warn_weaker_root(&cache_root.path); + let tail = if path.len() > path_tail_at { + path[path_tail_at..].to_vec() + } else { + Vec::new() + }; cache_root = Self::temp_cache_root(temp_dir); - Self::create_cache_root(&cache_root.path); bunx_cache_dir_buf = package_dir_of(&cache_root)?; bunx_cache_dir = &bunx_cache_dir_buf; - path = { - let mut v = Vec::new(); - write!( - &mut v, - "{cache}{sep}node_modules{sep}.bin", - cache = BStr::new(bunx_cache_dir), - sep = bun_paths::SEP as char, - ) - .map_err(|_| crate::Error::Alloc(bun_alloc::AllocError))?; - if !original_path.is_empty() { - v.push(DELIMITER); - v.extend_from_slice(&original_path); - } - v - }; + path = cache_bin_of(bunx_cache_dir, &tail)?; env_loader.map.put(b"PATH", &path)?; - if !Self::is_trusted_cache_root(bunx_cache_dir, cache_root.parent_len, uid) { + if !Self::create_cache_root(&cache_root.path) + || !Self::is_trusted_cache_root(bunx_cache_dir, cache_root.parent_len, uid) + { Self::refuse_cache_root(bunx_cache_dir, true); } } @@ -1631,6 +1652,8 @@ impl BunxCommand { }, }; + env_loader.map.remove(b"BUN_INTERNAL_BUNX_INSTALL"); + match &spawn_result.status { SpawnStatus::Exited(exited) => { // Any non-zero byte (incl. RT signals >31) is a valid signal. From f93beff050c7d1403c3d050cc7755fd8ec62a2ff Mon Sep 17 00:00:00 2001 From: robobun Date: Sat, 3 Oct 2026 05:53:24 +0000 Subject: [PATCH 5/6] bunx: age the cache by its own package.json, and do not let the temp root capture later runs The cache tree now sits on the filesystem of the install cache, so its files are hard links into it. The staleness check read the mtime of the bin through its link, which a reinstall never moves: once a package was a day old, every `bunx ` reinstalled it and asked the registry, and failed with no network. The same happened before this PR wherever the temp directory and the cache shared a filesystem. The age is read from the package.json bunx rewrites before each install, the file the second staleness check already uses. A missing install cache root deferred to the temp root when that one already existed, so a single run without a usable cache directory (cron, a service unit, `env -i`) sent every later run to the shared temp directory, without a word. A missing root is now always the root. The temp directory is taken only by the run that cannot create it. bunx warns when it installs into the shared temp directory, whatever sent it there, and the refusal note no longer says another user owns a directory that may have failed for some other reason. Tests: a day-old package is reinstalled once and then served from the cache, and a run with a HOME does not reuse the temp root an earlier run without one left behind. --- src/runtime/cli/bunx_command.rs | 67 ++++++++------ test/cli/install/bunx.test.ts | 155 +++++++++++++++++++++++++++++++- 2 files changed, 192 insertions(+), 30 deletions(-) diff --git a/src/runtime/cli/bunx_command.rs b/src/runtime/cli/bunx_command.rs index c54f473fde1f..9523d3e4e7af 100644 --- a/src/runtime/cli/bunx_command.rs +++ b/src/runtime/cli/bunx_command.rs @@ -613,7 +613,7 @@ impl BunxCommand { ); if in_temp_dir { bun_core::note!( - "another user owns that directory. Set BUN_INSTALL_CACHE_DIR (or HOME) to a directory you own." + "set BUN_INSTALL_CACHE_DIR (or HOME) to a directory you own" ); } else { bun_core::note!("remove it and try again"); @@ -808,35 +808,43 @@ impl BunxCommand { root.from_install_cache = true; root } - // A run that installs creates it, or takes the temp directory if - // it cannot. Take the temp directory here too when a run already - // did, so this one finds what that one installed. + // Not there yet. A run that installs creates it, and takes the + // temp directory only if it cannot: one run that had no usable + // cache directory must not send later runs to the shared + // directory. CacheRootState::Missing => { - let temp = Self::temp_cache_root(temp_dir); - if Self::cache_root_state(&temp.path, uid) == CacheRootState::Private { - temp - } else { - root.from_install_cache = true; - root - } + root.from_install_cache = true; + root } // Somebody else's, reported world-writable by the filesystem // (drvfs, ntfs-3g, CIFS without `uid=`), or not statable. Each // would refuse every run. - CacheRootState::Foreign => { - Self::warn_weaker_root(&root.path); - Self::temp_cache_root(temp_dir) - } + CacheRootState::Foreign => Self::temp_cache_root(temp_dir), } } - /// The temp directory is shared, so say so when bunx is pushed into it. + /// True when the last install into `cache_dir` is more than a day old. + /// Read from the package.json bunx rewrites before every install: the + /// installed files are hard links into the install cache, whose mtime a + /// reinstall never moves, so a bin would stay stale forever. #[cfg(unix)] - fn warn_weaker_root(unusable: &[u8]) { - bun_core::warn!( - "bunx cannot use {} as its cache and is using the shared temp directory instead. Set BUN_INSTALL_CACHE_DIR to a directory only you can write.", - BStr::new(unusable), - ); + fn install_is_stale(cache_dir: &[u8]) -> bool { + let mut buf = bun_paths::path_buffer_pool::get(); + let name = b"/package.json"; + if cache_dir.len() + name.len() >= buf.len() { + return true; + } + buf[..cache_dir.len()].copy_from_slice(cache_dir); + buf[cache_dir.len()..cache_dir.len() + name.len()].copy_from_slice(name); + let len = cache_dir.len() + name.len(); + buf[len] = 0; + match bun_sys::stat(ZStr::from_buf(&buf[..], len)) { + Ok(st) => { + bun_core::time::timestamp() - bun_sys::stat_mtime(&st).sec + > Self::SECONDS_CACHE_VALID + } + Err(_) => true, + } } /// `GetTempPath` is per-user, so the layout stays as it was. @@ -1335,13 +1343,7 @@ impl BunxCommand { } #[cfg(not(windows))] { - let stat = match bun_sys::stat(destination) { - Ok(s) => s, - Err(_) => break 'is_stale true, - }; - break 'is_stale bun_core::time::timestamp() - - bun_sys::stat_mtime(&stat).sec - > Self::SECONDS_CACHE_VALID; + break 'is_stale Self::install_is_stale(bunx_cache_dir); } }; @@ -1520,7 +1522,6 @@ impl BunxCommand { if !cache_root.from_install_cache { Self::refuse_cache_root(&cache_root.path, true); } - Self::warn_weaker_root(&cache_root.path); let tail = if path.len() > path_tail_at { path[path_tail_at..].to_vec() } else { @@ -1537,6 +1538,14 @@ impl BunxCommand { Self::refuse_cache_root(bunx_cache_dir, true); } } + // The temp directory is shared, so what bunx installs there can be + // reached from files other users put beside it. + #[cfg(unix)] + if !cache_root.from_install_cache { + bun_core::warn!( + "bunx is installing into the shared temp directory because no cache directory of your own is usable. Set BUN_INSTALL_CACHE_DIR to a directory only you can write." + ); + } // The root exists and is 0700; this creates the package directory. let bunx_install_dir = Fd::cwd().make_open_path(bunx_cache_dir)?; if !Self::is_trusted_opened_cache_dir( diff --git a/test/cli/install/bunx.test.ts b/test/cli/install/bunx.test.ts index 4925a0b1bca4..d572a5a22410 100644 --- a/test/cli/install/bunx.test.ts +++ b/test/cli/install/bunx.test.ts @@ -2,7 +2,7 @@ import { spawn } from "bun"; import { afterAll, beforeAll, beforeEach, describe, expect, it, setDefaultTimeout } from "bun:test"; import { mkdir, rm, writeFile } from "fs/promises"; import { bunEnv, bunExe, isLinux, isWindows, readdirSorted, tempDir, tmpdirSync } from "harness"; -import { chmodSync, copyFileSync, existsSync, readdirSync, statSync, symlinkSync } from "node:fs"; +import { chmodSync, copyFileSync, existsSync, readdirSync, statSync, symlinkSync, utimesSync } from "node:fs"; import { tmpdir } from "os"; import { delimiter, dirname, join, resolve } from "path"; import { dummyAfterAll, dummyBeforeAll, dummyBeforeEach, dummyRegistry, getPort, setHandler } from "./dummy.registry"; @@ -1783,3 +1783,156 @@ it.concurrent.skipIf(isWindows)("creates nothing when it does not install", asyn expect(existsSync(join(env.HOME, ".bun"))).toBe(false); expect(readdirSync(env.TMPDIR).filter(entry => entry.includes("bunx-"))).toEqual([]); }); + +// Counts what a run asks the registry for, so a test can tell a warm cache +// hit from a reinstall. +function fixtureRegistry(pkg: string, tgzDir: string) { + let requests = 0; + const server = Bun.serve({ + port: 0, + async fetch(req) { + requests++; + const path = decodeURIComponent(new URL(req.url).pathname); + if (path === `/${pkg}`) { + return Response.json({ + name: pkg, + "dist-tags": { latest: "1.0.0" }, + versions: { + "1.0.0": { + name: pkg, + version: "1.0.0", + bin: { [pkg]: "cli.js" }, + dist: { tarball: `http://localhost:${server.port}/${pkg}/-/${pkg}-1.0.0.tgz` }, + }, + }, + }); + } + if (path === `/${pkg}/-/${pkg}-1.0.0.tgz`) return new Response(Bun.file(join(tgzDir, `${pkg}-1.0.0.tgz`))); + return new Response("not found", { status: 404 }); + }, + }); + return { + server, + url: `http://localhost:${server.port}/`, + get requests() { + return requests; + }, + reset() { + requests = 0; + }, + [Symbol.dispose]() { + server.stop(true); + }, + }; +} + +async function packFixture(pkg: string, body: string) { + using buildDir = tempDir(`${pkg}-build`, { + "package/package.json": JSON.stringify({ name: pkg, version: "1.0.0", bin: { [pkg]: "cli.js" } }), + "package/cli.js": body, + }); + chmodSync(join(String(buildDir), "package", "cli.js"), 0o755); + const tgzDir = tmpdirSync(); + await Bun.$`tar -czf ${join(tgzDir, `${pkg}-1.0.0.tgz`)} -C ${String(buildDir)} package`.quiet(); + return tgzDir; +} + +// The cache holds hard links into the install cache, whose mtime a reinstall +// never moves, so bunx measures the age of the install from the package.json +// it writes itself. Reading it off an installed file instead leaves every run +// after the first day reinstalling, which also fails with no network. +it.concurrent.skipIf(isWindows)("serves a day-old package from the cache instead of reinstalling", async () => { + const { x_dir, env } = setup(); + delete env.BUN_INSTALL_CACHE_DIR; + delete env.BUN_INSTALL; + delete env.XDG_CACHE_HOME; + env.HOME = tmpdirSync(); + + const pkg = "bunx-stale-fixture"; + const tgzDir = await packFixture(pkg, `#!/bin/sh\necho stale-ok\n`); + using registry = fixtureRegistry(pkg, tgzDir); + + const run = async () => { + await using proc = spawn({ + cmd: [bunExe(), "x", pkg], + cwd: x_dir, + stdout: "pipe", + stdin: "ignore", + stderr: "pipe", + env: { ...env, npm_config_registry: registry.url }, + }); + const [stdout, , exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(stdout).toContain("stale-ok"); + expect(exitCode).toBe(0); + }; + + await run(); + registry.reset(); + await run(); + expect(registry.requests).toBe(0); + + // Age every file of the cache, the way a package installed yesterday looks. + const twoDaysAgo = new Date(Date.now() - 2 * 86400_000); + const age = (dir: string) => { + for (const entry of readdirSync(dir, { withFileTypes: true })) { + const p = join(dir, entry.name); + if (entry.isSymbolicLink()) continue; + if (entry.isDirectory()) age(p); + else utimesSync(p, twoDaysAgo, twoDaysAgo); + } + }; + age(env.HOME); + + // One reinstall, because the install really is a day old. + registry.reset(); + await run(); + expect(registry.requests).toBeGreaterThan(0); + + // And then warm again, rather than reinstalling on every run. + registry.reset(); + await run(); + expect(registry.requests).toBe(0); +}); + +// A run with no usable cache directory installs into the shared temp +// directory. It must not send later runs there: those have a cache directory +// of their own and the protection this file tests belongs to them. +it.concurrent.skipIf(isWindows)("does not keep using the temp directory once a cache directory works", async () => { + const { x_dir, env } = setup(); + const uid = process.getuid!(); + delete env.BUN_INSTALL_CACHE_DIR; + delete env.BUN_INSTALL; + delete env.XDG_CACHE_HOME; + + const pkg = "bunx-sticky-fixture"; + const tgzDir = await packFixture(pkg, `#!/bin/sh\necho sticky-ok\n`); + using registry = fixtureRegistry(pkg, tgzDir); + + const run = async (runEnv: Record) => { + await using proc = spawn({ + cmd: [bunExe(), "x", pkg], + cwd: x_dir, + stdout: "pipe", + stdin: "ignore", + stderr: "pipe", + env: { ...runEnv, npm_config_registry: registry.url }, + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(stdout).toContain("sticky-ok"); + expect(exitCode).toBe(0); + return stderr; + }; + + // No HOME: the temp directory is all that is left, and bunx says so. + const noHome = { ...env }; + delete noHome.HOME; + const warned = await run(noHome); + expect(statSync(join(env.TMPDIR, `.bunx-${uid}`, `${pkg}@latest`)).isDirectory()).toBe(true); + expect(warned).toContain("shared temp directory"); + + // With a HOME, the cache belongs under it, even though the temp root is + // there and holds this package. + const home = tmpdirSync(); + await run({ ...env, HOME: home }); + expect(statSync(join(home, ".bun", "install", "cache", `.bunx-${uid}`, `${pkg}@latest`)).isDirectory()).toBe(true); +}); From 009a9580c7dd273c0d28e943b505e09d3dcbf53c Mon Sep 17 00:00:00 2001 From: robobun Date: Sat, 3 Oct 2026 10:42:38 +0000 Subject: [PATCH 6/6] bunx: decide up front whether the cache root can be created, and flush the temp-directory warning Where the install cache root can never be created (a read-only home, a service account, `HOME=/nonexistent`), bunx picked it, looked there for a cached binary, missed, and only then fell back to the temp root while installing. So every run installed again and asked the registry, and a run with no network failed. The picker now asks, without writing, whether the nearest existing ancestor of the root is writable. If it is not, the temp root is the root from the start, and a warm run finds what the cold run installed. A home that can be written never takes the temp root, whatever an earlier run left there. On macOS bunx replaces itself with the tool (`execve`), which drops what is still in the stderr buffer. The warning that bunx installs into the shared temp directory was lost there, and the test that reads it failed on both macOS lanes. It is flushed. Test: with a home of mode 0555, the second run asks the registry for nothing and the third runs with the registry stopped. It cannot be set up as root, so it skips there. --- src/runtime/cli/bunx_command.rs | 43 +++++++++++++++++++++++++---- test/cli/install/bunx.test.ts | 49 +++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 6 deletions(-) diff --git a/src/runtime/cli/bunx_command.rs b/src/runtime/cli/bunx_command.rs index 9523d3e4e7af..d72d4335f4d4 100644 --- a/src/runtime/cli/bunx_command.rs +++ b/src/runtime/cli/bunx_command.rs @@ -764,6 +764,29 @@ impl BunxCommand { } } + /// True when the nearest existing ancestor of `path` is a directory this + /// user may write, so `path` could be created. Reads only. + #[cfg(unix)] + fn can_create(path: &[u8]) -> bool { + let mut buf = bun_paths::path_buffer_pool::get(); + let mut dir = bun_paths::dirname(path); + while let Some(d) = dir { + if d.is_empty() || d.len() >= buf.len() { + return false; + } + buf[..d.len()].copy_from_slice(d); + buf[d.len()] = 0; + match bun_sys::access(ZStr::from_buf(&buf[..], d.len()), libc::W_OK | libc::X_OK) { + Ok(()) => return true, + Err(err) if err.get_errno() == bun_sys::E::ENOENT => { + dir = bun_paths::dirname(d).filter(|parent| parent.len() < d.len()); + } + Err(_) => return false, + } + } + false + } + #[cfg(unix)] fn join_cache_root(base: &[u8], name: &[u8]) -> CacheRoot { let base = strings::without_trailing_slash(base); @@ -808,13 +831,18 @@ impl BunxCommand { root.from_install_cache = true; root } - // Not there yet. A run that installs creates it, and takes the - // temp directory only if it cannot: one run that had no usable - // cache directory must not send later runs to the shared - // directory. + // Not there yet. Whether this run may create it decides, not + // whether an earlier run left a temp root behind: a home that + // cannot be written always takes the temp root, so its warm runs + // find what its cold run installed, and a home that can be written + // never does. CacheRootState::Missing => { - root.from_install_cache = true; - root + if Self::can_create(&root.path) { + root.from_install_cache = true; + root + } else { + Self::temp_cache_root(temp_dir) + } } // Somebody else's, reported world-writable by the filesystem // (drvfs, ntfs-3g, CIFS without `uid=`), or not statable. Each @@ -1545,6 +1573,9 @@ impl BunxCommand { bun_core::warn!( "bunx is installing into the shared temp directory because no cache directory of your own is usable. Set BUN_INSTALL_CACHE_DIR to a directory only you can write." ); + // macOS replaces this process with the tool (`execve`), which + // drops whatever is still buffered. + Output::flush(); } // The root exists and is 0700; this creates the package directory. let bunx_install_dir = Fd::cwd().make_open_path(bunx_cache_dir)?; diff --git a/test/cli/install/bunx.test.ts b/test/cli/install/bunx.test.ts index d572a5a22410..f36ef776b7ed 100644 --- a/test/cli/install/bunx.test.ts +++ b/test/cli/install/bunx.test.ts @@ -1936,3 +1936,52 @@ it.concurrent.skipIf(isWindows)("does not keep using the temp directory once a c await run({ ...env, HOME: home }); expect(statSync(join(home, ".bun", "install", "cache", `.bunx-${uid}`, `${pkg}@latest`)).isDirectory()).toBe(true); }); + +// A home that cannot be written (a read-only container, a service account) +// has no cache directory of its own. bunx decides that before it looks for a +// cached binary, not after a failed install, so the second run finds what the +// first one installed and needs no network. Root may write anywhere, so this +// cannot be set up as root. +it.concurrent.skipIf(isWindows || process.getuid?.() === 0)( + "serves warm runs from the temp directory when the cache directory cannot be created", + async () => { + const { x_dir, env } = setup(); + const uid = process.getuid!(); + delete env.BUN_INSTALL_CACHE_DIR; + delete env.BUN_INSTALL; + delete env.XDG_CACHE_HOME; + using home = tempDir("bunx-readonly-home", {}); + chmodSync(String(home), 0o555); + env.HOME = String(home); + + const pkg = "bunx-readonly-home-fixture"; + const tgzDir = await packFixture(pkg, `#!/bin/sh\necho readonly-home-ok\n`); + using registry = fixtureRegistry(pkg, tgzDir); + + const run = async () => { + await using proc = spawn({ + cmd: [bunExe(), "x", pkg], + cwd: x_dir, + stdout: "pipe", + stdin: "ignore", + stderr: "pipe", + env: { ...env, npm_config_registry: registry.url }, + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(stdout).toContain("readonly-home-ok"); + expect(exitCode).toBe(0); + return stderr; + }; + + expect(await run()).toContain("shared temp directory"); + expect(statSync(join(env.TMPDIR, `.bunx-${uid}`, `${pkg}@latest`)).isDirectory()).toBe(true); + + registry.reset(); + await run(); + expect(registry.requests).toBe(0); + + // And with the registry gone. + registry.server.stop(true); + await run(); + }, +);