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 6dae4163a5c5..b2c3f05bcd08 100644 --- a/src/install/PackageManager/PackageManagerDirectories.rs +++ b/src/install/PackageManager/PackageManagerDirectories.rs @@ -374,47 +374,53 @@ 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` needs a + /// root only the current user can write, and that is not one. + 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..d72d4335f4d4 100644 --- a/src/runtime/cli/bunx_command.rs +++ b/src/runtime/cli/bunx_command.rs @@ -213,6 +213,43 @@ pub(crate) enum GetBinNameError { NeedToInstall, } +/// `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. + 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 +} + +/// Where the cache tree is rooted. One directory per package sits below it. +struct CacheRoot { + path: Vec, + /// 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, + /// 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 { /// Adds `create-` to the string, but also handles scoped packages correctly. /// Always clones the string in the process. @@ -268,8 +305,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 +564,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 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. @@ -538,8 +575,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,19 +603,43 @@ impl BunxCommand { true } + /// 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!( + "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], 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); - 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 mut start = temp_dir_len + 1; + 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) { Some(i) => i, @@ -607,22 +668,20 @@ 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 } + /// `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, cache_dir: &[u8], - temp_dir_len: usize, + 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, @@ -646,7 +705,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 +716,182 @@ 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 } + /// What a candidate cache root is before bunx touches it. + #[cfg(unix)] + 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 CacheRootState::Foreign; + } + buf[..root.len()].copy_from_slice(root); + buf[root.len()] = 0; + 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(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, + } + } + + /// 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, + }; + if Fd::cwd().make_path(parent).is_err() { + return false; + } + 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, + } + } + + /// 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); + 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 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], + uid: libc::uid_t, + ) -> CacheRoot { + let cache_dir = bun_install::package_manager::fetch_cache_directory_path(env, None); + 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; + root + } + // 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 => { + 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 + // would refuse every run. + CacheRootState::Foreign => Self::temp_cache_root(temp_dir), + } + } + + /// 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 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. + #[cfg(not(unix))] + fn user_cache_root( + _env: &mut bun_dotenv::Loader, + temp_dir: &'static [u8], + _uid: u32, + ) -> CacheRoot { + let root = strings::without_trailing_slash(temp_dir).to_vec(); + let parent_len = root.len(); + CacheRoot { + path: root, + parent_len, + prefix_package_with_uid: true, + from_install_cache: false, + } + } + fn exit_with_usage() -> ! { crate::cli::command::tag_print_help(Command::Tag::BunxCommand, false); Global::exit(1); @@ -931,17 +1160,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,31 +1188,48 @@ impl BunxCommand { #[cfg(windows)] let uid = bun_sys::windows::user_unique_id(); - path = { + // 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); + v.extend_from_slice(&root.path); + v.push(bun_paths::SEP); + if root.prefix_package_with_uid { + write!(&mut v, "bunx-{uid}-").map_err(|_| crate::Error::Alloc(AllocError))?; + } + 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; + + 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, - "{tmp}{sep}bunx-{uid}-{pkg}{sep}node_modules{sep}.bin", - tmp = BStr::new(temp_dir), + "{cache}{sep}node_modules{sep}.bin", + cache = BStr::new(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 { + 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. 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,12 +1257,8 @@ 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) { - 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); + if !Self::is_trusted_cache_root(bunx_cache_dir, cache_root.parent_len, uid) { + Self::refuse_cache_root(bunx_cache_dir, !cache_root.from_install_cache); } let passthrough: &[Box<[u8]>] = opts.passthrough_list.as_slice(); @@ -1120,13 +1371,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); } }; @@ -1235,8 +1480,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,18 +1540,52 @@ impl BunxCommand { Global::exit(1); } + // 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); + } + 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); + bunx_cache_dir_buf = package_dir_of(&cache_root)?; + bunx_cache_dir = &bunx_cache_dir_buf; + path = cache_bin_of(bunx_cache_dir, &tail)?; + env_loader.map.put(b"PATH", &path)?; + 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); + } + } + // 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." + ); + // 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)?; 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( - "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: { @@ -1414,6 +1692,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. @@ -1489,9 +1769,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..e8e2be3c0896 100644 --- a/src/runtime/cli/package_manager_command.rs +++ b/src/runtime/cli/package_manager_command.rs @@ -440,6 +440,24 @@ 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); + + // 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 { + 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, @@ -486,6 +504,7 @@ 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 + // for the directories written straight into the temp dir. let mut prefix: Vec = Vec::new(); #[cfg(unix)] { @@ -499,7 +518,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 +533,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; + } + // 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) { + 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..f36ef776b7ed 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, isLinux, isWindows, readdirSorted, tempDir, tmpdirSync } from "harness"; +import { chmodSync, copyFileSync, existsSync, readdirSync, statSync, symlinkSync, utimesSync } 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({ @@ -1290,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(); @@ -1300,9 +1300,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 +1332,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({ @@ -1348,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(); @@ -1380,3 +1380,608 @@ it.concurrent.skipIf(isWindows)( } }, ); + +// `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(); + // 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-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" } }), + // 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 { + helper = require("zz-optional"); +} catch {} +console.log(JSON.stringify({ helper })); +`, + }); + // 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 ${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, + 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); + expect(JSON.parse(stdout.trim().split("\n").at(-1)!)).toEqual({ helper: "none" }); + expect(exitCode).toBe(0); + }, +); + +// 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 +// 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 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, npm_config_registry: `http://localhost:${registry.port}/` }, + }); + const [err, out, exitCode] = await Promise.all([ + subprocess.stderr.text(), + subprocess.stdout.text(), + subprocess.exited, + ]); + expect(err).not.toContain("error:"); + 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, `${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([]); +}); + +// 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); +}); + +// 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(); + }, +);