diff --git a/src/bundler/bundle_v2.rs b/src/bundler/bundle_v2.rs index d0f11093d9dd..725506a1777b 100644 --- a/src/bundler/bundle_v2.rs +++ b/src/bundler/bundle_v2.rs @@ -894,38 +894,36 @@ pub mod bv2_impl { pub map: bun_collections::StringHashMap>, } impl FileMap { - pub(crate) fn get(&self, specifier: &[u8]) -> Option<&[u8]> { + /// 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; } #[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,20 +955,9 @@ 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. 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 @@ -984,12 +971,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 +1009,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..9e19987bdf3c 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,102 @@ 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("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"; + 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)'], + }); + }); + }); });