From f77419c5e92e5f99dc5313fd1b8f0e93e726595e Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 6 Sep 2026 04:58:36 +0000 Subject: [PATCH 1/7] install: verify the gzip trailer of package tarballs Patch libarchive's gzip read filter to check the CRC32 and ISIZE trailer fields. Upstream leaves this as a TODO, so a corrupted member decompressed without error on the streaming path. Treat a libdeflate BAD_DATA result as a hard error instead of falling back to the streaming path. --- patches/libarchive/gzip-verify-trailer.patch | 62 +++++++++++ scripts/build/deps/libarchive.ts | 4 + src/install/extract_tarball.rs | 22 +++- .../bun-install-tarball-integrity.test.ts | 101 ++++++++++++++++++ 4 files changed, 187 insertions(+), 2 deletions(-) create mode 100644 patches/libarchive/gzip-verify-trailer.patch diff --git a/patches/libarchive/gzip-verify-trailer.patch b/patches/libarchive/gzip-verify-trailer.patch new file mode 100644 index 000000000000..ff0b01468504 --- /dev/null +++ b/patches/libarchive/gzip-verify-trailer.patch @@ -0,0 +1,62 @@ +--- a/libarchive/archive_read_support_filter_gzip.c ++++ b/libarchive/archive_read_support_filter_gzip.c +@@ -68,6 +68,11 @@ struct private_data { + * a gzip header). + */ + char trailer_pending; ++ /* ++ * BUN PATCH: uncompressed byte count of the current member, ++ * modulo 2^32, checked against the ISIZE trailer field. ++ */ ++ uint32_t member_isize; + }; + + /* Gzip Filter. */ +@@ -386,6 +391,7 @@ consume_header(struct archive_read_filter *self) + + /* Initialize CRC accumulator. */ + state->crc = crc32(0L, NULL, 0); ++ state->member_isize = 0; + + /* + * Initialize compression library. next_in / avail_in are +@@ -469,7 +475,17 @@ consume_trailer(struct archive_read_filter *self) + if (p == NULL || avail == 0) + return (ARCHIVE_FATAL); + +- /* XXX TODO: Verify the length and CRC. */ ++ /* ++ * BUN PATCH: verify the trailer. Upstream leaves this as a TODO, ++ * which lets a corrupted member decompress without any error. ++ */ ++ if (archive_le32dec(p) != (uint32_t)state->crc || ++ archive_le32dec(p + 4) != state->member_isize) { ++ archive_set_error(&self->archive->archive, ++ ARCHIVE_ERRNO_MISC, ++ "gzip trailer mismatch: bad CRC32 or uncompressed size"); ++ return (ARCHIVE_FATAL); ++ } + + /* We've verified the trailer, so consume it now. */ + __archive_read_filter_consume(self->upstream, 8); +@@ -484,6 +500,8 @@ gzip_filter_read(struct archive_read_filter *self, const void **p) + struct private_data *state; + size_t decompressed; + ssize_t avail_in, max_in; ++ unsigned char *out_before; ++ size_t produced; + int ret; + + state = (struct private_data *)self->data; +@@ -548,7 +566,11 @@ gzip_filter_read(struct archive_read_filter *self, const void **p) + state->stream.avail_in = (uInt)avail_in; + + /* Decompress and consume some of that data. */ ++ out_before = state->stream.next_out; + ret = inflate(&(state->stream), 0); ++ produced = state->stream.next_out - out_before; ++ state->crc = crc32(state->crc, out_before, (uInt)produced); ++ state->member_isize += (uint32_t)produced; + switch (ret) { + case Z_OK: /* Decompressor made some progress. */ + __archive_read_filter_consume(self->upstream, diff --git a/scripts/build/deps/libarchive.ts b/scripts/build/deps/libarchive.ts index 736f7a9da4ea..5185e19e9bdb 100644 --- a/scripts/build/deps/libarchive.ts +++ b/scripts/build/deps/libarchive.ts @@ -108,6 +108,10 @@ export const libarchive: Dependency = { // machinery (and its heap) are pure overhead and needless cross-thread // CRT-heap traffic. "patches/libarchive/archive_string-codepage-cache.patch", + // Upstream's gzip read filter never checks the CRC32/ISIZE trailer + // (it is a TODO there). Verify it, so a corrupted tarball fails the + // extract instead of installing the corrupted bytes. + "patches/libarchive/gzip-verify-trailer.patch", ], // zlib-ng generates zlib.h during its own build; libarchive's gzip filter diff --git a/src/install/extract_tarball.rs b/src/install/extract_tarball.rs index d1e039353b49..5d3b477f6b54 100644 --- a/src/install/extract_tarball.rs +++ b/src/install/extract_tarball.rs @@ -318,8 +318,26 @@ impl ExtractTarball { &mut zlib_pool.list, libdeflate::Encoding::Gzip, ); - if result.status == libdeflate::Status::Success { - decompressed_in_memory = true; + match result.status { + libdeflate::Status::Success => decompressed_in_memory = true, + // The ISIZE trailer is only the size mod 2^32, so a + // too-small buffer is not proof of corruption. Stream it. + libdeflate::Status::InsufficientSpace => {} + libdeflate::Status::BadData | libdeflate::Status::ShortOutput => { + log.add_error_fmt( + None, + bun_ast::Loc::EMPTY, + format_args!( + "Corrupt gzip data decompressing \"{}\" to \"{}\"", + bun_fmt::s(name), + bun_core::fmt::fmt_path_u8( + tmpname.as_bytes(), + Default::default() + ), + ), + ); + return Err(crate::Error::InstallFailed); + } } } } diff --git a/test/cli/install/bun-install-tarball-integrity.test.ts b/test/cli/install/bun-install-tarball-integrity.test.ts index 422352e6a667..bb2893415010 100644 --- a/test/cli/install/bun-install-tarball-integrity.test.ts +++ b/test/cli/install/bun-install-tarball-integrity.test.ts @@ -854,3 +854,104 @@ describe.concurrent.each(["hoisted", "isolated"] as const)("tarball download fai }); }); }); + +// A tarball whose gzip trailer (CRC32 + ISIZE) does not match the data must +// not install. For `file:` tarballs and manifests without `dist.integrity` +// the gzip trailer is the only corruption check. Both decompression paths +// (libdeflate in memory, libarchive streaming) must reject it. +describe.concurrent("gzip trailer verification", () => { + function octal(n: number, width: number) { + return n.toString(8).padStart(width - 1, "0") + "\0"; + } + function tarHeader(name: string, size: number) { + const buf = Buffer.alloc(512, 0); + buf.write(name, 0, 100, "utf8"); + buf.write(octal(0o644, 8), 100); + buf.write(octal(0, 8), 108); + buf.write(octal(0, 8), 116); + buf.write(octal(size, 12), 124); + buf.write(octal(0, 12), 136); + buf.fill(" ", 148, 156); + buf.write("0", 156); + buf.write("ustar\0", 257); + buf.write("00", 263); + let sum = 0; + for (let i = 0; i < 512; i++) sum += buf[i]; + buf.write(octal(sum, 8), 148); + return buf; + } + function tarFile(name: string, body: Buffer) { + return [tarHeader(name, body.length), body, Buffer.alloc((512 - (body.length % 512)) % 512, 0)]; + } + const indexJs = Buffer.from(`module.exports = "pkg@1.0.0:GOOD";\n`); + // Stored (level 0) deflate blocks: flipping a payload byte keeps the + // deflate stream valid, so only the CRC32 in the trailer can catch it. + const goodTgz = gzipSync( + Buffer.concat([ + ...tarFile("package/package.json", Buffer.from(JSON.stringify({ name: "pkg", version: "1.0.0" }))), + ...tarFile("package/index.js", indexJs), + Buffer.alloc(1024, 0), + ]), + { level: 0 }, + ); + const badCrcTgz = Buffer.from(goodTgz); + { + const i = badCrcTgz.indexOf("module.exports"); + expect(i).toBeGreaterThan(0); + badCrcTgz[i + 18] ^= 0x04; // "pkg" -> "tkg" + } + // Valid data, but ISIZE claims 100 MB: skips the in-memory path and + // goes through libarchive's streaming gzip filter. + const badIsizeTgz = Buffer.from(goodTgz); + badIsizeTgz.writeUInt32LE(100 * 1024 * 1024, badIsizeTgz.length - 4); + + async function install(name: string, tgz: Buffer, extraEnv: Record = {}) { + using dir = tempDir(name, { + "pkg.tgz": tgz, + "package.json": JSON.stringify({ name: "app", dependencies: { pkg: "file:./pkg.tgz" } }), + }); + await using proc = spawn({ + cmd: [bunExe(), "install"], + cwd: String(dir), + stdout: "pipe", + stderr: "pipe", + env: { ...env, BUN_INSTALL_CACHE_DIR: join(String(dir), ".cache"), ...extraEnv }, + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + const installed = await file(join(String(dir), "node_modules", "pkg", "index.js")).exists(); + return { stdout, stderr, exitCode, installed }; + } + + it("installs a tarball with a valid trailer", async () => { + const { stdout, exitCode, installed } = await install("gzip-trailer-ok", goodTgz); + expect(stdout).toContain("1 package installed"); + expect(exitCode).toBe(0); + expect(installed).toBe(true); + }); + + it("rejects a CRC32 mismatch", async () => { + const { stdout, stderr, exitCode, installed } = await install("gzip-trailer-crc", badCrcTgz); + expect(stderr).toContain("Corrupt gzip data"); + expect(stdout).not.toContain("1 package installed"); + expect(exitCode).toBe(1); + expect(installed).toBe(false); + }); + + it("rejects a CRC32 mismatch on the streaming path", async () => { + const { stdout, stderr, exitCode, installed } = await install("gzip-trailer-crc-stream", badCrcTgz, { + BUN_FEATURE_FLAG_NO_LIBDEFLATE: "1", + }); + expect(stderr).toContain("extracting tarball from pkg"); + expect(stdout).not.toContain("1 package installed"); + expect(exitCode).toBe(1); + expect(installed).toBe(false); + }); + + it("rejects an ISIZE mismatch", async () => { + const { stdout, stderr, exitCode, installed } = await install("gzip-trailer-isize", badIsizeTgz); + expect(stderr).toContain("extracting tarball from pkg"); + expect(stdout).not.toContain("1 package installed"); + expect(exitCode).toBe(1); + expect(installed).toBe(false); + }); +}); From 340f5529af1b1cb7d02f021c4be74d6d923013a1 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 6 Sep 2026 07:23:36 +0000 Subject: [PATCH 2/7] install: remove the temp extraction directory when the buffered extract fails --- src/install/extract_tarball.rs | 395 +++++++++--------- .../bun-install-tarball-integrity.test.ts | 20 +- 2 files changed, 224 insertions(+), 191 deletions(-) diff --git a/src/install/extract_tarball.rs b/src/install/extract_tarball.rs index 5d3b477f6b54..737843288715 100644 --- a/src/install/extract_tarball.rs +++ b/src/install/extract_tarball.rs @@ -256,219 +256,242 @@ impl ExtractTarball { return Err(crate::Error::InstallFailed); }; - let mut resolved: &'static [u8] = b""; let tmpname = FileSystem::tmpname(tmpname_suffix, &mut tmpname_buf.0, bun_core::fast_random())?; - { - let extract_destination = match bun_sys::make_path::make_open_path( - tmpdir, - tmpname.as_bytes(), - Default::default(), - ) { - Ok(d) => d, - Err(err) => { - log.add_error_fmt( - None, - bun_ast::Loc::EMPTY, - format_args!( - "{} when create temporary directory named \"{}\" (while extracting \"{}\")", - bun_fmt::s(err.name()), - bun_fmt::s(tmpname.as_bytes()), - bun_fmt::s(name), - ), - ); - return Err(crate::Error::InstallFailed); - } - }; + let extract_destination = match bun_sys::make_path::make_open_path( + tmpdir, + tmpname.as_bytes(), + Default::default(), + ) { + Ok(d) => d, + Err(err) => { + log.add_error_fmt( + None, + bun_ast::Loc::EMPTY, + format_args!( + "{} when create temporary directory named \"{}\" (while extracting \"{}\")", + bun_fmt::s(err.name()), + bun_fmt::s(tmpname.as_bytes()), + bun_fmt::s(name), + ), + ); + return Err(crate::Error::InstallFailed); + } + }; - use bun_libarchive::Archiver; - let mut zlib_pool = Npm::Registry::BodyPool::get(); - zlib_pool.reset(); - // `defer Npm.Registry.BodyPool.release(zlib_pool)` → PoolGuard's Drop releases. + // Every failure in `extract_into` leaves a partial (or, on a bad gzip + // trailer, complete) extraction behind. Remove it before the error + // propagates. + let extracted = self.extract_into(log, tgz_bytes, &extract_destination, tmpname, name); + + // Explicitly close the temp extraction dir before the rename (or the + // removal). On Windows a still-open handle to the source directory + // can fail `NtSetInformationFile` with EBUSY; spelling out the close + // keeps the timing visible instead of relying on block-end Drop. + drop(extract_destination); + + let resolved = match extracted { + Ok(resolved) => resolved, + Err(err) => { + let _ = tmpdir.delete_tree(tmpname.as_bytes()); + return Err(err); + } + }; - let time_started_for_verbose_logs: u64 = if PackageManager::verbose_install() { - bun_core::Timespec::now_allow_mocked_time().ns() - } else { - 0 - }; + self.move_to_cache_directory(log, tmpname, name, basename, resolved) + } - // libarchive gunzips on the fly (`BufferReadStream::open_read`), - // so hand it the compressed bytes and never buffer the full tar. - // Small tarballs still try libdeflate first for speed; the gzip - // ISIZE trailer (size mod 2^32) is only trusted when small. - let mut decompressed_in_memory = false; - if bun_core::FeatureFlags::is_libdeflate_enabled() && tgz_bytes.len() > 16 { - let isize: u32 = u32::from_le_bytes( - tgz_bytes[tgz_bytes.len() - 4..][..4] - .try_into() - .expect("infallible: size matches"), - ); - if isize > 16 && isize < 64 * 1024 * 1024 { - if zlib_pool.list.capacity() == 0 { - let _ = zlib_pool.list.try_reserve_exact(isize as usize); - } else { - let _ = zlib_pool.ensure_unused_capacity(isize as usize); - } - if zlib_pool.list.capacity() > 16 { - use bun_libdeflate_sys::libdeflate; - if let Some(mut decompressor) = libdeflate::OwnedDecompressor::new() { - zlib_pool.list.clear(); - let result = decompressor.decompress_to_vec( - tgz_bytes, - &mut zlib_pool.list, - libdeflate::Encoding::Gzip, - ); - match result.status { - libdeflate::Status::Success => decompressed_in_memory = true, - // The ISIZE trailer is only the size mod 2^32, so a - // too-small buffer is not proof of corruption. Stream it. - libdeflate::Status::InsufficientSpace => {} - libdeflate::Status::BadData | libdeflate::Status::ShortOutput => { - log.add_error_fmt( - None, - bun_ast::Loc::EMPTY, - format_args!( - "Corrupt gzip data decompressing \"{}\" to \"{}\"", - bun_fmt::s(name), - bun_core::fmt::fmt_path_u8( - tmpname.as_bytes(), - Default::default() - ), + /// Decompress `tgz_bytes` and extract it into `extract_destination`. + /// Returns the resolved tag for a GitHub tarball (empty otherwise). + fn extract_into( + &self, + log: &mut bun_ast::Log, + tgz_bytes: &[u8], + extract_destination: &Dir, + tmpname: &ZStr, + name: &[u8], + ) -> Result<&'static [u8], Error> { + use bun_libarchive::Archiver; + let mut resolved: &'static [u8] = b""; + let mut zlib_pool = Npm::Registry::BodyPool::get(); + zlib_pool.reset(); + // `defer Npm.Registry.BodyPool.release(zlib_pool)` → PoolGuard's Drop releases. + + let time_started_for_verbose_logs: u64 = if PackageManager::verbose_install() { + bun_core::Timespec::now_allow_mocked_time().ns() + } else { + 0 + }; + + // libarchive gunzips on the fly (`BufferReadStream::open_read`), + // so hand it the compressed bytes and never buffer the full tar. + // Small tarballs still try libdeflate first for speed; the gzip + // ISIZE trailer (size mod 2^32) is only trusted when small. + let mut decompressed_in_memory = false; + if bun_core::FeatureFlags::is_libdeflate_enabled() && tgz_bytes.len() > 16 { + let isize: u32 = u32::from_le_bytes( + tgz_bytes[tgz_bytes.len() - 4..][..4] + .try_into() + .expect("infallible: size matches"), + ); + if isize > 16 && isize < 64 * 1024 * 1024 { + if zlib_pool.list.capacity() == 0 { + let _ = zlib_pool.list.try_reserve_exact(isize as usize); + } else { + let _ = zlib_pool.ensure_unused_capacity(isize as usize); + } + if zlib_pool.list.capacity() > 16 { + use bun_libdeflate_sys::libdeflate; + if let Some(mut decompressor) = libdeflate::OwnedDecompressor::new() { + zlib_pool.list.clear(); + let result = decompressor.decompress_to_vec( + tgz_bytes, + &mut zlib_pool.list, + libdeflate::Encoding::Gzip, + ); + match result.status { + libdeflate::Status::Success => decompressed_in_memory = true, + // The ISIZE trailer is only the size mod 2^32, so a + // too-small buffer is not proof of corruption. Stream it. + libdeflate::Status::InsufficientSpace => {} + libdeflate::Status::BadData | libdeflate::Status::ShortOutput => { + log.add_error_fmt( + None, + bun_ast::Loc::EMPTY, + format_args!( + "Corrupt gzip data decompressing \"{}\" to \"{}\"", + bun_fmt::s(name), + bun_core::fmt::fmt_path_u8( + tmpname.as_bytes(), + Default::default() ), - ); - return Err(crate::Error::InstallFailed); - } + ), + ); + return Err(crate::Error::InstallFailed); } } } } } + } - let tar_input: &[u8] = if decompressed_in_memory { - &zlib_pool.list - } else { - zlib_pool.list.clear(); - tgz_bytes - }; + let tar_input: &[u8] = if decompressed_in_memory { + &zlib_pool.list + } else { + zlib_pool.list.clear(); + tgz_bytes + }; - if PackageManager::verbose_install() { - bun_core::pretty_errorln!( - "[{}] Extract {} ({} tgz file)", - bun_fmt::s(name), - bun_fmt::s(tmpname.as_bytes()), - bun_core::fmt::size(tgz_bytes.len(), Default::default()), - ); - } + if PackageManager::verbose_install() { + bun_core::pretty_errorln!( + "[{}] Extract {} ({} tgz file)", + bun_fmt::s(name), + bun_fmt::s(tmpname.as_bytes()), + bun_core::fmt::size(tgz_bytes.len(), Default::default()), + ); + } - match self.resolution.tag { - ResolutionTag::Github => { - // BORROW_PARAM: out-param writing the first dirname back into a stack local. - struct DirnameReader<'a> { - needs_first_dirname: bool, // = true - outdirname: &'a mut &'static [u8], - } - impl<'a> ArchiveAppender for DirnameReader<'a> { - const HAS_ON_FIRST_DIRECTORY_NAME: bool = true; - fn needs_first_dirname(&self) -> bool { - self.needs_first_dirname - } - fn on_first_directory_name(&mut self, first_dirname: &[u8]) { - debug_assert!(self.needs_first_dirname); - self.needs_first_dirname = false; - *self.outdirname = FileSystem::instance() - .dirname_store() - .append(first_dirname) - .expect("unreachable"); - } + match self.resolution.tag { + ResolutionTag::Github => { + // BORROW_PARAM: out-param writing the first dirname back into a stack local. + struct DirnameReader<'a> { + needs_first_dirname: bool, // = true + outdirname: &'a mut &'static [u8], + } + impl<'a> ArchiveAppender for DirnameReader<'a> { + const HAS_ON_FIRST_DIRECTORY_NAME: bool = true; + fn needs_first_dirname(&self) -> bool { + self.needs_first_dirname } - let mut dirname_reader = DirnameReader { - needs_first_dirname: true, - outdirname: &mut resolved, - }; - - let _ = Archiver::extract_to_dir( - tar_input, - extract_destination.fd(), - None, - &mut dirname_reader, - ExtractOptions { - // for GitHub tarballs, the root dir is always -- - depth_to_skip: 1, - log: PackageManager::verbose_install(), - ..Default::default() - }, - )?; - - let lockfile_tag = self.github_resolved.slice(); - if !lockfile_tag.is_empty() { - resolved = FileSystem::instance() + fn on_first_directory_name(&mut self, first_dirname: &[u8]) { + debug_assert!(self.needs_first_dirname); + self.needs_first_dirname = false; + *self.outdirname = FileSystem::instance() .dirname_store() - .append(lockfile_tag) + .append(first_dirname) .expect("unreachable"); } + } + let mut dirname_reader = DirnameReader { + needs_first_dirname: true, + outdirname: &mut resolved, + }; + + let _ = Archiver::extract_to_dir( + tar_input, + extract_destination.fd(), + None, + &mut dirname_reader, + ExtractOptions { + // for GitHub tarballs, the root dir is always -- + depth_to_skip: 1, + log: PackageManager::verbose_install(), + ..Default::default() + }, + )?; + + let lockfile_tag = self.github_resolved.slice(); + if !lockfile_tag.is_empty() { + resolved = FileSystem::instance() + .dirname_store() + .append(lockfile_tag) + .expect("unreachable"); + } - // This tag is used to know which version of the package was - // installed from GitHub. package.json version becomes sort of - // meaningless in cases like this. - if !resolved.is_empty() { - // Create/truncate `.bun-tag`, then write the resolved tag. - if sys::File::openat( + // This tag is used to know which version of the package was + // installed from GitHub. package.json version becomes sort of + // meaningless in cases like this. + if !resolved.is_empty() { + // Create/truncate `.bun-tag`, then write the resolved tag. + if sys::File::openat( + extract_destination.fd(), + ZStr::from_static(b".bun-tag\0"), + sys::O::WRONLY + | sys::O::CREAT + | sys::O::TRUNC + | if cfg!(windows) { 0 } else { sys::O::NOFOLLOW }, + 0o664, + ) + .and_then(|f| f.write_all(resolved)) + .is_err() + { + let _ = sys::unlinkat( extract_destination.fd(), ZStr::from_static(b".bun-tag\0"), - sys::O::WRONLY - | sys::O::CREAT - | sys::O::TRUNC - | if cfg!(windows) { 0 } else { sys::O::NOFOLLOW }, - 0o664, - ) - .and_then(|f| f.write_all(resolved)) - .is_err() - { - let _ = sys::unlinkat( - extract_destination.fd(), - ZStr::from_static(b".bun-tag\0"), - ); - } + ); } } - _ => { - let _ = Archiver::extract_to_dir( - tar_input, - extract_destination.fd(), - None, - &mut (), - ExtractOptions { - // packages usually have root directory `package/`, and scoped packages usually have root `/` - // https://github.com/npm/cli/blob/93883bb6459208a916584cad8c6c72a315cf32af/node_modules/pacote/lib/fetcher.js#L442 - depth_to_skip: 1, - npm: true, - log: PackageManager::verbose_install(), - ..Default::default() - }, - )?; - } } - - // Explicitly close the temp extraction dir before the rename. On - // Windows a still-open handle to the source directory can fail - // `NtSetInformationFile` with EBUSY; spelling out the close keeps - // the timing visible instead of relying on block-end Drop. - drop(extract_destination); - - if PackageManager::verbose_install() { - let elapsed = bun_core::Timespec::now_allow_mocked_time().ns() - - time_started_for_verbose_logs; - bun_core::pretty_errorln!( - "[{}] Extracted to {} ({})", - bun_fmt::s(name), - bun_fmt::s(tmpname.as_bytes()), - bun_core::fmt::fmt_duration_one_decimal(elapsed), - ); - Output::flush(); + _ => { + let _ = Archiver::extract_to_dir( + tar_input, + extract_destination.fd(), + None, + &mut (), + ExtractOptions { + // packages usually have root directory `package/`, and scoped packages usually have root `/` + // https://github.com/npm/cli/blob/93883bb6459208a916584cad8c6c72a315cf32af/node_modules/pacote/lib/fetcher.js#L442 + depth_to_skip: 1, + npm: true, + log: PackageManager::verbose_install(), + ..Default::default() + }, + )?; } } - self.move_to_cache_directory(log, tmpname, name, basename, resolved) + if PackageManager::verbose_install() { + let elapsed = + bun_core::Timespec::now_allow_mocked_time().ns() - time_started_for_verbose_logs; + bun_core::pretty_errorln!( + "[{}] Extracted to {} ({})", + bun_fmt::s(name), + bun_fmt::s(tmpname.as_bytes()), + bun_core::fmt::fmt_duration_one_decimal(elapsed), + ); + Output::flush(); + } + Ok(resolved) } /// Rename the freshly-extracted temp directory into the cache, read diff --git a/test/cli/install/bun-install-tarball-integrity.test.ts b/test/cli/install/bun-install-tarball-integrity.test.ts index bb2893415010..84d11bedc252 100644 --- a/test/cli/install/bun-install-tarball-integrity.test.ts +++ b/test/cli/install/bun-install-tarball-integrity.test.ts @@ -915,11 +915,18 @@ describe.concurrent("gzip trailer verification", () => { cwd: String(dir), stdout: "pipe", stderr: "pipe", - env: { ...env, BUN_INSTALL_CACHE_DIR: join(String(dir), ".cache"), ...extraEnv }, + env: { + ...env, + BUN_INSTALL_CACHE_DIR: join(String(dir), ".cache"), + BUN_TMPDIR: join(String(dir), ".tmp"), + ...extraEnv, + }, }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); const installed = await file(join(String(dir), "node_modules", "pkg", "index.js")).exists(); - return { stdout, stderr, exitCode, installed }; + // A failed extraction must not leave its temp directory behind. + const leftovers = await readdirSorted(join(String(dir), ".tmp")).catch(() => []); + return { stdout, stderr, exitCode, installed, leftovers }; } it("installs a tarball with a valid trailer", async () => { @@ -930,28 +937,31 @@ describe.concurrent("gzip trailer verification", () => { }); it("rejects a CRC32 mismatch", async () => { - const { stdout, stderr, exitCode, installed } = await install("gzip-trailer-crc", badCrcTgz); + const { stdout, stderr, exitCode, installed, leftovers } = await install("gzip-trailer-crc", badCrcTgz); expect(stderr).toContain("Corrupt gzip data"); expect(stdout).not.toContain("1 package installed"); expect(exitCode).toBe(1); expect(installed).toBe(false); + expect(leftovers).toEqual([]); }); it("rejects a CRC32 mismatch on the streaming path", async () => { - const { stdout, stderr, exitCode, installed } = await install("gzip-trailer-crc-stream", badCrcTgz, { + const { stdout, stderr, exitCode, installed, leftovers } = await install("gzip-trailer-crc-stream", badCrcTgz, { BUN_FEATURE_FLAG_NO_LIBDEFLATE: "1", }); expect(stderr).toContain("extracting tarball from pkg"); expect(stdout).not.toContain("1 package installed"); expect(exitCode).toBe(1); expect(installed).toBe(false); + expect(leftovers).toEqual([]); }); it("rejects an ISIZE mismatch", async () => { - const { stdout, stderr, exitCode, installed } = await install("gzip-trailer-isize", badIsizeTgz); + const { stdout, stderr, exitCode, installed, leftovers } = await install("gzip-trailer-isize", badIsizeTgz); expect(stderr).toContain("extracting tarball from pkg"); expect(stdout).not.toContain("1 package installed"); expect(exitCode).toBe(1); expect(installed).toBe(false); + expect(leftovers).toEqual([]); }); }); From b62c8f357e673c1b5a82303306ff2064ef5240c8 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 6 Sep 2026 07:28:38 +0000 Subject: [PATCH 3/7] install: check the gzip trailer after consuming it --- patches/libarchive/gzip-verify-trailer.patch | 20 ++++++++++---------- scripts/build/deps/libarchive.ts | 6 +++--- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/patches/libarchive/gzip-verify-trailer.patch b/patches/libarchive/gzip-verify-trailer.patch index ff0b01468504..59a6252fc7ee 100644 --- a/patches/libarchive/gzip-verify-trailer.patch +++ b/patches/libarchive/gzip-verify-trailer.patch @@ -20,14 +20,14 @@ /* * Initialize compression library. next_in / avail_in are -@@ -469,7 +475,17 @@ consume_trailer(struct archive_read_filter *self) - if (p == NULL || avail == 0) - return (ARCHIVE_FATAL); +@@ -473,6 +479,18 @@ consume_trailer(struct archive_read_filter *self) -- /* XXX TODO: Verify the length and CRC. */ + /* We've verified the trailer, so consume it now. */ + __archive_read_filter_consume(self->upstream, 8); + /* -+ * BUN PATCH: verify the trailer. Upstream leaves this as a TODO, -+ * which lets a corrupted member decompress without any error. ++ * BUN PATCH: compare the trailer with what was inflated. Upstream ++ * skips this, so a corrupted member decompressed without any ++ * error. `p` is still readable: consume only moves the cursor. + */ + if (archive_le32dec(p) != (uint32_t)state->crc || + archive_le32dec(p + 4) != state->member_isize) { @@ -36,10 +36,10 @@ + "gzip trailer mismatch: bad CRC32 or uncompressed size"); + return (ARCHIVE_FATAL); + } + state->trailer_pending = 0; - /* We've verified the trailer, so consume it now. */ - __archive_read_filter_consume(self->upstream, 8); -@@ -484,6 +500,8 @@ gzip_filter_read(struct archive_read_filter *self, const void **p) + return (ARCHIVE_OK); +@@ -484,6 +502,8 @@ gzip_filter_read(struct archive_read_filter *self, const void **p) struct private_data *state; size_t decompressed; ssize_t avail_in, max_in; @@ -48,7 +48,7 @@ int ret; state = (struct private_data *)self->data; -@@ -548,7 +566,11 @@ gzip_filter_read(struct archive_read_filter *self, const void **p) +@@ -548,7 +568,11 @@ gzip_filter_read(struct archive_read_filter *self, const void **p) state->stream.avail_in = (uInt)avail_in; /* Decompress and consume some of that data. */ diff --git a/scripts/build/deps/libarchive.ts b/scripts/build/deps/libarchive.ts index 5185e19e9bdb..c2c5369c89cf 100644 --- a/scripts/build/deps/libarchive.ts +++ b/scripts/build/deps/libarchive.ts @@ -108,9 +108,9 @@ export const libarchive: Dependency = { // machinery (and its heap) are pure overhead and needless cross-thread // CRT-heap traffic. "patches/libarchive/archive_string-codepage-cache.patch", - // Upstream's gzip read filter never checks the CRC32/ISIZE trailer - // (it is a TODO there). Verify it, so a corrupted tarball fails the - // extract instead of installing the corrupted bytes. + // Upstream's gzip read filter never checks the CRC32/ISIZE trailer. + // Verify it, so a corrupted tarball fails the extract instead of + // installing the corrupted bytes. "patches/libarchive/gzip-verify-trailer.patch", ], From eccc24e4a8518cde02397df7e5035bc09af36358 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 6 Sep 2026 07:31:09 +0000 Subject: [PATCH 4/7] install: shorten the temp dir comments in extract --- src/install/extract_tarball.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/install/extract_tarball.rs b/src/install/extract_tarball.rs index 737843288715..6b9328a7bace 100644 --- a/src/install/extract_tarball.rs +++ b/src/install/extract_tarball.rs @@ -279,20 +279,15 @@ impl ExtractTarball { } }; - // Every failure in `extract_into` leaves a partial (or, on a bad gzip - // trailer, complete) extraction behind. Remove it before the error - // propagates. let extracted = self.extract_into(log, tgz_bytes, &extract_destination, tmpname, name); - // Explicitly close the temp extraction dir before the rename (or the - // removal). On Windows a still-open handle to the source directory - // can fail `NtSetInformationFile` with EBUSY; spelling out the close - // keeps the timing visible instead of relying on block-end Drop. + // Windows cannot rename or delete a directory that still has an open handle. drop(extract_destination); let resolved = match extracted { Ok(resolved) => resolved, Err(err) => { + // A failed extract leaves the temp directory populated. let _ = tmpdir.delete_tree(tmpname.as_bytes()); return Err(err); } From 6173d953372832c23edfb4346e988d4e894b37ca Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Sun, 6 Sep 2026 07:41:29 +0000 Subject: [PATCH 5/7] test: cover a tar that ends on the gzip filter buffer boundary --- .../bun-install-tarball-integrity.test.ts | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/test/cli/install/bun-install-tarball-integrity.test.ts b/test/cli/install/bun-install-tarball-integrity.test.ts index 84d11bedc252..5bfa3ba96e4d 100644 --- a/test/cli/install/bun-install-tarball-integrity.test.ts +++ b/test/cli/install/bun-install-tarball-integrity.test.ts @@ -904,11 +904,28 @@ describe.concurrent("gzip trailer verification", () => { // goes through libarchive's streaming gzip filter. const badIsizeTgz = Buffer.from(goodTgz); badIsizeTgz.writeUInt32LE(100 * 1024 * 1024, badIsizeTgz.length - 4); + // A tar whose end-of-archive blocks end exactly at the 64 KiB boundary of + // libarchive's gzip output buffer. The tar reader must still read on to + // the end of the gzip member, or the trailer is never checked. + const alignedBody = Buffer.alloc(2 * 65536 - 512 - 512 - 512 - 1024, "x"); + const alignedTar = Buffer.concat([ + ...tarFile("package/package.json", Buffer.from(JSON.stringify({ name: "pkg", version: "1.0.0" }))), + ...tarFile("package/big.txt", alignedBody), + Buffer.alloc(1024, 0), + ]); + expect(alignedTar.length).toBe(2 * 65536); + const badCrcAlignedTgz = gzipSync(alignedTar, { level: 0 }); + { + const i = badCrcAlignedTgz.indexOf("xxxxxxxx"); + expect(i).toBeGreaterThan(0); + badCrcAlignedTgz[i + 1000] ^= 0x04; + } async function install(name: string, tgz: Buffer, extraEnv: Record = {}) { using dir = tempDir(name, { "pkg.tgz": tgz, "package.json": JSON.stringify({ name: "app", dependencies: { pkg: "file:./pkg.tgz" } }), + ".tmp/.keep": "", }); await using proc = spawn({ cmd: [bunExe(), "install"], @@ -925,7 +942,7 @@ describe.concurrent("gzip trailer verification", () => { const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); const installed = await file(join(String(dir), "node_modules", "pkg", "index.js")).exists(); // A failed extraction must not leave its temp directory behind. - const leftovers = await readdirSorted(join(String(dir), ".tmp")).catch(() => []); + const leftovers = (await readdirSorted(join(String(dir), ".tmp"))).filter(e => e !== ".keep"); return { stdout, stderr, exitCode, installed, leftovers }; } @@ -956,6 +973,17 @@ describe.concurrent("gzip trailer verification", () => { expect(leftovers).toEqual([]); }); + it("rejects a CRC32 mismatch when the tar ends on the filter buffer boundary", async () => { + const { stdout, stderr, exitCode, installed, leftovers } = await install("gzip-trailer-aligned", badCrcAlignedTgz, { + BUN_FEATURE_FLAG_NO_LIBDEFLATE: "1", + }); + expect(stderr).toContain("extracting tarball from pkg"); + expect(stdout).not.toContain("1 package installed"); + expect(exitCode).toBe(1); + expect(installed).toBe(false); + expect(leftovers).toEqual([]); + }); + it("rejects an ISIZE mismatch", async () => { const { stdout, stderr, exitCode, installed, leftovers } = await install("gzip-trailer-isize", badIsizeTgz); expect(stderr).toContain("extracting tarball from pkg"); From ea9e3737a848222cbddd68f15d74b26f23ee7331 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 17 Sep 2026 22:13:07 +0000 Subject: [PATCH 6/7] install: regenerate the gzip trailer patch for libarchive 3.8.9 libarchive 3.8.9 renamed the gzip filter's state struct and its variables, so the patch written for 3.8.7 no longer applied. --- patches/libarchive/gzip-verify-trailer.patch | 44 ++++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/patches/libarchive/gzip-verify-trailer.patch b/patches/libarchive/gzip-verify-trailer.patch index 59a6252fc7ee..3afce4fb68eb 100644 --- a/patches/libarchive/gzip-verify-trailer.patch +++ b/patches/libarchive/gzip-verify-trailer.patch @@ -1,6 +1,6 @@ --- a/libarchive/archive_read_support_filter_gzip.c +++ b/libarchive/archive_read_support_filter_gzip.c -@@ -68,6 +68,11 @@ struct private_data { +@@ -67,6 +67,11 @@ struct gzip { * a gzip header). */ char trailer_pending; @@ -12,51 +12,51 @@ }; /* Gzip Filter. */ -@@ -386,6 +391,7 @@ consume_header(struct archive_read_filter *self) +@@ -374,6 +379,7 @@ consume_header(struct archive_read_filter *f) /* Initialize CRC accumulator. */ - state->crc = crc32(0L, NULL, 0); -+ state->member_isize = 0; + gzip->crc = crc32(0L, NULL, 0); ++ gzip->member_isize = 0; /* - * Initialize compression library. next_in / avail_in are -@@ -473,6 +479,18 @@ consume_trailer(struct archive_read_filter *self) + * BUN PATCH: Initialize compression library. next_in / avail_in +@@ -459,6 +465,18 @@ consume_trailer(struct archive_read_filter *f) /* We've verified the trailer, so consume it now. */ - __archive_read_filter_consume(self->upstream, 8); + __archive_read_filter_consume(f->upstream, 8); + /* + * BUN PATCH: compare the trailer with what was inflated. Upstream + * skips this, so a corrupted member decompressed without any + * error. `p` is still readable: consume only moves the cursor. + */ -+ if (archive_le32dec(p) != (uint32_t)state->crc || -+ archive_le32dec(p + 4) != state->member_isize) { -+ archive_set_error(&self->archive->archive, ++ if (archive_le32dec(p) != (uint32_t)gzip->crc || ++ archive_le32dec(p + 4) != gzip->member_isize) { ++ archive_set_error(&f->archive->archive, + ARCHIVE_ERRNO_MISC, + "gzip trailer mismatch: bad CRC32 or uncompressed size"); + return (ARCHIVE_FATAL); + } - state->trailer_pending = 0; + gzip->trailer_pending = 0; return (ARCHIVE_OK); -@@ -484,6 +502,8 @@ gzip_filter_read(struct archive_read_filter *self, const void **p) - struct private_data *state; +@@ -470,6 +488,8 @@ gzip_filter_read(struct archive_read_filter *f, const void **p) + struct gzip *gzip = f->data; size_t decompressed; ssize_t avail_in, max_in; + unsigned char *out_before; + size_t produced; int ret; - state = (struct private_data *)self->data; -@@ -548,7 +568,11 @@ gzip_filter_read(struct archive_read_filter *self, const void **p) - state->stream.avail_in = (uInt)avail_in; + /* Empty our output buffer. */ +@@ -532,7 +552,11 @@ gzip_filter_read(struct archive_read_filter *f, const void **p) + gzip->stream.avail_in = (uInt)avail_in; /* Decompress and consume some of that data. */ -+ out_before = state->stream.next_out; - ret = inflate(&(state->stream), 0); -+ produced = state->stream.next_out - out_before; -+ state->crc = crc32(state->crc, out_before, (uInt)produced); -+ state->member_isize += (uint32_t)produced; ++ out_before = gzip->stream.next_out; + ret = inflate(&(gzip->stream), 0); ++ produced = gzip->stream.next_out - out_before; ++ gzip->crc = crc32(gzip->crc, out_before, (uInt)produced); ++ gzip->member_isize += (uint32_t)produced; switch (ret) { case Z_OK: /* Decompressor made some progress. */ - __archive_read_filter_consume(self->upstream, + __archive_read_filter_consume(f->upstream, From 085cbbca018edaa70d1945a0cb8b1e0b62342833 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Thu, 17 Sep 2026 22:39:56 +0000 Subject: [PATCH 7/7] install: remove the temp extraction directory when the move into the cache fails --- src/install/extract_tarball.rs | 18 ++++++++---------- .../bun-install-tarball-integrity.test.ts | 9 +++++++++ .../cli/install/symlink-path-traversal.test.ts | 4 ++++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/install/extract_tarball.rs b/src/install/extract_tarball.rs index 6b9328a7bace..bc76cb43bfc5 100644 --- a/src/install/extract_tarball.rs +++ b/src/install/extract_tarball.rs @@ -284,16 +284,14 @@ impl ExtractTarball { // Windows cannot rename or delete a directory that still has an open handle. drop(extract_destination); - let resolved = match extracted { - Ok(resolved) => resolved, - Err(err) => { - // A failed extract leaves the temp directory populated. - let _ = tmpdir.delete_tree(tmpname.as_bytes()); - return Err(err); - } - }; - - self.move_to_cache_directory(log, tmpname, name, basename, resolved) + let result = extracted.and_then(|resolved| { + self.move_to_cache_directory(log, tmpname, name, basename, resolved) + }); + if result.is_err() { + // Still populated unless the rename into the cache already happened. + let _ = tmpdir.delete_tree(tmpname.as_bytes()); + } + result } /// Decompress `tgz_bytes` and extract it into `extract_destination`. diff --git a/test/cli/install/bun-install-tarball-integrity.test.ts b/test/cli/install/bun-install-tarball-integrity.test.ts index 5bfa3ba96e4d..0bfff1e7d17a 100644 --- a/test/cli/install/bun-install-tarball-integrity.test.ts +++ b/test/cli/install/bun-install-tarball-integrity.test.ts @@ -966,6 +966,9 @@ describe.concurrent("gzip trailer verification", () => { const { stdout, stderr, exitCode, installed, leftovers } = await install("gzip-trailer-crc-stream", badCrcTgz, { BUN_FEATURE_FLAG_NO_LIBDEFLATE: "1", }); + // libdeflate logs this when it rejects the data, so its absence shows + // that libarchive's trailer check did. + expect(stderr).not.toContain("Corrupt gzip data"); expect(stderr).toContain("extracting tarball from pkg"); expect(stdout).not.toContain("1 package installed"); expect(exitCode).toBe(1); @@ -977,6 +980,9 @@ describe.concurrent("gzip trailer verification", () => { const { stdout, stderr, exitCode, installed, leftovers } = await install("gzip-trailer-aligned", badCrcAlignedTgz, { BUN_FEATURE_FLAG_NO_LIBDEFLATE: "1", }); + // libdeflate logs this when it rejects the data, so its absence shows + // that libarchive's trailer check did. + expect(stderr).not.toContain("Corrupt gzip data"); expect(stderr).toContain("extracting tarball from pkg"); expect(stdout).not.toContain("1 package installed"); expect(exitCode).toBe(1); @@ -986,6 +992,9 @@ describe.concurrent("gzip trailer verification", () => { it("rejects an ISIZE mismatch", async () => { const { stdout, stderr, exitCode, installed, leftovers } = await install("gzip-trailer-isize", badIsizeTgz); + // libdeflate logs this when it rejects the data, so its absence shows + // that libarchive's trailer check did. + expect(stderr).not.toContain("Corrupt gzip data"); expect(stderr).toContain("extracting tarball from pkg"); expect(stdout).not.toContain("1 package installed"); expect(exitCode).toBe(1); diff --git a/test/cli/install/symlink-path-traversal.test.ts b/test/cli/install/symlink-path-traversal.test.ts index 9d2400d10d28..80a9b11e31cf 100644 --- a/test/cli/install/symlink-path-traversal.test.ts +++ b/test/cli/install/symlink-path-traversal.test.ts @@ -988,6 +988,7 @@ it.skipIf(isWindows)( version: "1.0.0", dependencies: { "test-package": "github:user/repo#main" }, }), + ".tmp/.keep": "", }); const installDir = String(dir); @@ -1000,12 +1001,15 @@ it.skipIf(isWindows)( ...env, GITHUB_API_URL: `http://localhost:${server.port}`, BUN_INSTALL_CACHE_DIR: join(installDir, ".bun-cache"), + BUN_TMPDIR: join(installDir, ".tmp"), }, }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); expect(stderr).toContain('tarball root directory "pkg.root/extra" is not a valid folder name'); expect(stdout).not.toContain("1 package installed"); + // The refusal comes after the extract, so the temp directory must be removed. + expect(await readdir(join(installDir, ".tmp"))).toEqual([".keep"]); expect(exitCode).not.toBe(0); }, 60000,