From 094a46132b8277208c6b607d1a2cddc94bebf8bf Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 24 Aug 2026 13:21:58 +0000 Subject: [PATCH 1/5] bundler: do not abort on a Bun.build files specifier longer than a path buffer FileMap::resolve joined every non-absolute specifier onto the importer's directory with join_abs_string_buf, which writes into a fixed PathBuffer with no bounds check. A CSS url() or an import specifier longer than the buffer (4096 bytes on Linux, 1024 on macOS) panicked with "range end index N out of range for slice of length 4095" and aborted the process. Use join_abs_string_buf_checked and treat a result that does not fit as "not a virtual file", so the specifier falls through to the resolver (which marks data: URLs external and reports too-long paths as not found). Guard the importer path the same way, and bound the Windows separator normalization in get, contains and resolve, which copied the raw specifier into a PathBuffer. Fixes #39252 --- src/bundler/bundle_v2.rs | 68 +++++++++++++------------- test/bundler/bundler_files.test.ts | 77 +++++++++++++++++++++++++++++- 2 files changed, 110 insertions(+), 35 deletions(-) diff --git a/src/bundler/bundle_v2.rs b/src/bundler/bundle_v2.rs index d0f11093d9dd..c627633c204f 100644 --- a/src/bundler/bundle_v2.rs +++ b/src/bundler/bundle_v2.rs @@ -894,38 +894,39 @@ pub mod bv2_impl { pub map: bun_collections::StringHashMap>, } impl FileMap { - pub(crate) fn get(&self, specifier: &[u8]) -> Option<&[u8]> { + /// Looks `specifier` up as a map key. Keys are stored with + /// forward slashes (`file_map_from_js`), so on Windows the + /// specifier is normalized into a path buffer first. A specifier + /// that does not fit one is never a key. + fn get_key_value(&self, specifier: &[u8]) -> Option<(&[u8], &[u8])> { if self.map.is_empty() { return None; } #[cfg(not(windows))] { - self.map.get(specifier).map(|b| b.as_ref()) + self.map + .get_key_value(specifier) + .map(|(key, value)| (key.as_ref(), value.as_ref())) } #[cfg(windows)] { let mut buf = bun_paths::path_buffer_pool::get(); + if specifier.len() > buf.len() { + return None; + } let normalized = bun_paths::resolve_path::path_to_posix_buf(specifier, &mut **buf); - self.map.get(normalized).map(|b| b.as_ref()) + self.map + .get_key_value(normalized) + .map(|(key, value)| (key.as_ref(), value.as_ref())) } } + pub(crate) fn get(&self, specifier: &[u8]) -> Option<&[u8]> { + self.get_key_value(specifier).map(|(_, value)| value) + } #[inline] pub fn contains(&self, specifier: &[u8]) -> bool { - if self.map.is_empty() { - return false; - } - #[cfg(not(windows))] - { - self.map.contains_key(specifier) - } - #[cfg(windows)] - { - let mut buf = bun_paths::path_buffer_pool::get(); - let normalized = - bun_paths::resolve_path::path_to_posix_buf(specifier, &mut **buf); - self.map.contains_key(normalized) - } + self.get_key_value(specifier).is_some() } /// Returns a `resolver::Result` for a file in the map, or `None` if /// not found. Handles direct key matches and relative specifiers @@ -957,25 +958,21 @@ pub mod bv2_impl { unsafe { bun_ptr::detach_lifetime(arena.alloc_slice_copy(key)) } }; - // Direct key match (must use `getKey` to return the map-owned - // key, not the parameter). - #[cfg(not(windows))] - if let Some((key, _)) = self.map.get_key_value(specifier) { - return Some(Self::result_for_key(dupe(key.as_ref()))); - } - #[cfg(windows)] - { - let mut buf = bun_paths::path_buffer_pool::get(); - let normalized = - bun_paths::resolve_path::path_to_posix_buf(specifier, &mut **buf); - if let Some((key, _)) = self.map.get_key_value(normalized) { - return Some(Self::result_for_key(dupe(key.as_ref()))); - } + // Direct key match (must return the map-owned key, not the + // parameter). + if let Some((key, _)) = self.get_key_value(specifier) { + return Some(Self::result_for_key(dupe(key))); } // Also try joining a relative specifier against the importer's // directory. Relative = not posix-absolute and not Windows // drive-absolute (e.g. `C:/`). + // + // The specifier is user input of any length (an import + // string, a CSS `url()`), so every step below that writes + // into a path buffer is bounds-checked. A join that does + // not fit one is not a virtual file; the resolver then + // handles the specifier like it does for a file on disk. if !specifier.is_empty() && !bun_paths::is_absolute_loose(specifier) { // `source_file` may itself be relative (e.g. on Windows // when the bundler stores paths relative to cwd). @@ -984,12 +981,15 @@ pub mod bv2_impl { source_file } else { bun_resolver::fs::FileSystem::instance() - .abs_buf(&[source_file], &mut *abs_source_buf) + .abs_buf_checked(&[source_file], &mut *abs_source_buf)? }; // Normalize `source_file` to forward slashes (Windows paths // from the real filesystem may use backslashes). let mut source_file_buf = bun_paths::path_buffer_pool::get(); + if abs_source_file.len() > source_file_buf.len() { + return None; + } let normalized_source_file = bun_paths::resolve_path::path_to_posix_buf::( abs_source_file, &mut **source_file_buf, @@ -1019,11 +1019,11 @@ pub mod bv2_impl { }; // `.loose` preserves Windows drive letters; normalize // separators in-place on Windows afterwards. - let joined_len = bun_paths::resolve_path::join_abs_string_buf::< + let joined_len = bun_paths::resolve_path::join_abs_string_buf_checked::< bun_paths::platform::Loose, >( effective_source_dir, &mut **buf, &[specifier] - ) + )? .len(); if cfg!(windows) { bun_paths::resolve_path::platform_to_posix_in_place::( diff --git a/test/bundler/bundler_files.test.ts b/test/bundler/bundler_files.test.ts index 732226a86be4..c732d5758dc5 100644 --- a/test/bundler/bundler_files.test.ts +++ b/test/bundler/bundler_files.test.ts @@ -1,5 +1,5 @@ import { describe, expect, test } from "bun:test"; -import { tempDir } from "harness"; +import { bunEnv, bunExe, tempDir } from "harness"; describe("bundler files option", () => { test("basic in-memory file bundling", async () => { @@ -601,4 +601,79 @@ describe("bundler files option", () => { const output = await result.outputs[0].text(); expect(output).toContain("injected by plugin"); }); + + // Specifiers are joined onto the importer's directory to find a matching + // in-memory file. A specifier longer than a path buffer (4096 bytes on + // Linux, 1024 on macOS, 98302 on Windows) used to abort the process. These + // run in a child process because the bug is a crash. + describe.concurrent("specifiers longer than a path buffer", () => { + async function buildInChild(script: string) { + await using proc = Bun.spawn({ + cmd: [bunExe(), "-e", script], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect(stderr).toBe(""); + expect(exitCode).toBe(0); + return JSON.parse(stdout); + } + + test("a CSS url() is kept as-is", async () => { + const result = await buildInChild(` + const url = "data:image/svg+xml," + Buffer.alloc(131072, "A").toString(); + const result = await Bun.build({ + entrypoints: ["/style.css"], + files: { "/style.css": '.x { background: url("' + url + '") }' }, + throw: false, + }); + const output = result.success ? await result.outputs[0].text() : ""; + console.log(JSON.stringify({ + success: result.success, + logs: result.logs.map(String), + outputHasUrl: output.includes(url), + })); + `); + expect(result).toEqual({ success: true, logs: [], outputHasUrl: true }); + }); + + test("a relative import reports a resolve error", async () => { + const result = await buildInChild(` + const specifier = "./" + Buffer.alloc(131072, "A").toString() + ".js"; + const result = await Bun.build({ + entrypoints: ["/entry.js"], + files: { "/entry.js": 'import "' + specifier + '";' }, + throw: false, + }); + console.log(JSON.stringify({ + success: result.success, + logs: result.logs.map(log => log.message.replace(specifier, "")), + })); + `); + expect(result).toEqual({ + success: false, + logs: ['Could not resolve: ""'], + }); + }); + + test("a relative entry point reports a resolve error", async () => { + const result = await buildInChild(` + const entryPoint = Buffer.alloc(131072, "A").toString() + ".js"; + const result = await Bun.build({ + entrypoints: [entryPoint], + files: { "/entry.js": "" }, + throw: false, + }); + console.log(JSON.stringify({ + success: result.success, + logs: result.logs.map(log => log.message.replace(entryPoint, "")), + })); + `); + expect(result).toEqual({ + success: false, + logs: ['ModuleNotFound resolving "" (entry point)'], + }); + }); + }); }); From 6dbf4858d2057a5d26b7124bc21077050006791a Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 24 Aug 2026 13:43:14 +0000 Subject: [PATCH 2/5] ci: retrigger From ab3fc04042277953fdbbe8143c17adad0e450c36 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 24 Aug 2026 13:48:24 +0000 Subject: [PATCH 3/5] bundler: shorten the FileMap lookup comments --- src/bundler/bundle_v2.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/src/bundler/bundle_v2.rs b/src/bundler/bundle_v2.rs index c627633c204f..9dcb473afba5 100644 --- a/src/bundler/bundle_v2.rs +++ b/src/bundler/bundle_v2.rs @@ -894,10 +894,8 @@ pub mod bv2_impl { pub map: bun_collections::StringHashMap>, } impl FileMap { - /// Looks `specifier` up as a map key. Keys are stored with - /// forward slashes (`file_map_from_js`), so on Windows the - /// specifier is normalized into a path buffer first. A specifier - /// that does not fit one is never a key. + /// Keys are stored with forward slashes (`file_map_from_js`), so on + /// Windows the specifier is normalized before the lookup. fn get_key_value(&self, specifier: &[u8]) -> Option<(&[u8], &[u8])> { if self.map.is_empty() { return None; @@ -958,21 +956,16 @@ pub mod bv2_impl { unsafe { bun_ptr::detach_lifetime(arena.alloc_slice_copy(key)) } }; - // Direct key match (must return the map-owned key, not the - // parameter). + // Direct key match. Return the map-owned key, not the parameter. if let Some((key, _)) = self.get_key_value(specifier) { return Some(Self::result_for_key(dupe(key))); } // Also try joining a relative specifier against the importer's // directory. Relative = not posix-absolute and not Windows - // drive-absolute (e.g. `C:/`). - // - // The specifier is user input of any length (an import - // string, a CSS `url()`), so every step below that writes - // into a path buffer is bounds-checked. A join that does - // not fit one is not a virtual file; the resolver then - // handles the specifier like it does for a file on disk. + // drive-absolute (e.g. `C:/`). A specifier can be any length + // (a CSS `url()`); one that does not fit a path buffer is not + // a virtual file and falls through to the resolver. if !specifier.is_empty() && !bun_paths::is_absolute_loose(specifier) { // `source_file` may itself be relative (e.g. on Windows // when the bundler stores paths relative to cwd). From 939574e50af0829160d239bb0484ec8286f73dc5 Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 24 Aug 2026 13:50:39 +0000 Subject: [PATCH 4/5] bundler: trim the FileMap lookup comments to one line --- src/bundler/bundle_v2.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/bundler/bundle_v2.rs b/src/bundler/bundle_v2.rs index 9dcb473afba5..725506a1777b 100644 --- a/src/bundler/bundle_v2.rs +++ b/src/bundler/bundle_v2.rs @@ -894,8 +894,7 @@ pub mod bv2_impl { pub map: bun_collections::StringHashMap>, } impl FileMap { - /// Keys are stored with forward slashes (`file_map_from_js`), so on - /// Windows the specifier is normalized before the lookup. + /// Keys are stored with forward slashes (`file_map_from_js`). fn get_key_value(&self, specifier: &[u8]) -> Option<(&[u8], &[u8])> { if self.map.is_empty() { return None; @@ -963,9 +962,7 @@ pub mod bv2_impl { // Also try joining a relative specifier against the importer's // directory. Relative = not posix-absolute and not Windows - // drive-absolute (e.g. `C:/`). A specifier can be any length - // (a CSS `url()`); one that does not fit a path buffer is not - // a virtual file and falls through to the resolver. + // drive-absolute (e.g. `C:/`). if !specifier.is_empty() && !bun_paths::is_absolute_loose(specifier) { // `source_file` may itself be relative (e.g. on Windows // when the bundler stores paths relative to cwd). From de882e34722db119f136041cb208d6c81cf09d4e Mon Sep 17 00:00:00 2001 From: robobun <117481402+robobun@users.noreply.github.com> Date: Mon, 24 Aug 2026 14:47:10 +0000 Subject: [PATCH 5/5] test: cover HTML script and stylesheet references longer than a path buffer --- test/bundler/bundler_files.test.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/bundler/bundler_files.test.ts b/test/bundler/bundler_files.test.ts index c732d5758dc5..9e19987bdf3c 100644 --- a/test/bundler/bundler_files.test.ts +++ b/test/bundler/bundler_files.test.ts @@ -657,6 +657,29 @@ describe("bundler files option", () => { }); }); + test("HTML script and stylesheet references report resolve errors", async () => { + const result = await buildInChild(` + const long = Buffer.alloc(131072, "A").toString(); + const result = await Bun.build({ + entrypoints: ["/index.html"], + files: { + "/index.html": + '' + + '', + }, + throw: false, + }); + console.log(JSON.stringify({ + success: result.success, + logs: result.logs.map(log => log.message.replace(long, "")).sort(), + })); + `); + expect(result).toEqual({ + success: false, + logs: ['Could not resolve: "./.css"', 'Could not resolve: "./.js"'], + }); + }); + test("a relative entry point reports a resolve error", async () => { const result = await buildInChild(` const entryPoint = Buffer.alloc(131072, "A").toString() + ".js";