diff --git a/src/bundler/bundle_v2.rs b/src/bundler/bundle_v2.rs index 49d092acb299..51a66eb695d9 100644 --- a/src/bundler/bundle_v2.rs +++ b/src/bundler/bundle_v2.rs @@ -6195,11 +6195,13 @@ pub mod bv2_impl { let specifier_to_use = &import_record.path.text [Fs::FileSystem::instance().top_level_dir.len()..]; #[cfg(windows)] - { + if specifier_to_use.len() <= buf.len() { &*bun_paths::resolve_path::path_to_posix_buf::( specifier_to_use, &mut *buf, ) + } else { + specifier_to_use } #[cfg(not(windows))] { diff --git a/src/paths/resolve_path.rs b/src/paths/resolve_path.rs index 60abb6dc25eb..ef0ed9f0a495 100644 --- a/src/paths/resolve_path.rs +++ b/src/paths/resolve_path.rs @@ -13,6 +13,7 @@ use bun_core::{ZStr, strings}; // callers must not re-enter the accessor while a previous borrow is alive. thread_local! { static PARSER_JOIN_INPUT_BUFFER: UnsafeCell<[u8; 4096]> = const { UnsafeCell::new([0u8; 4096]) }; + static PARSER_JOIN_SPILL_BUFFER: UnsafeCell> = const { UnsafeCell::new(Vec::new()) }; static PARSER_BUFFER: UnsafeCell<[u8; 1024]> = const { UnsafeCell::new([0u8; 1024]) }; } @@ -1346,6 +1347,28 @@ pub fn join_abs<'a, P: PlatformT>(cwd: &'a [u8], part: &[u8]) -> &'a [u8] { join_abs_string::

(cwd, &[part]) } +#[inline] +fn join_abs_needed(cwd: &[u8], parts: &[&[u8]]) -> usize { + // Normalization never grows the concatenation; +1 sep per part, +1 leading sep, +1 NUL. + parts.iter().map(|p| p.len() + 1).sum::() + cwd.len() + 2 +} + +#[inline] +fn parser_join_out_buf(needed: usize) -> &'static mut [u8] { + if needed <= 4096 { + PARSER_JOIN_INPUT_BUFFER.with(|b| &mut tl_buf_mut(b)[..]) + } else { + PARSER_JOIN_SPILL_BUFFER.with(|b| { + // SAFETY: same single-live-borrow-per-thread invariant as `tl_buf_mut`. + let v: &'static mut Vec = unsafe { &mut *b.get() }; + if v.len() < needed { + v.resize(needed, 0); + } + &mut v[..] + }) + } +} + /// Convert parts of potentially invalid file paths into a single valid filpeath /// without querying the filesystem /// This is the equivalent of path.resolve @@ -1354,7 +1377,7 @@ pub fn join_abs<'a, P: PlatformT>(cwd: &'a [u8], part: &[u8]) -> &'a [u8] { // result borrows the thread-local buffer ('static) OR returns `cwd` // directly when `parts.is_empty()`. Return tied to `cwd`'s lifetime ('static: 'a). pub fn join_abs_string<'a, P: PlatformT>(cwd: &'a [u8], parts: &[&[u8]]) -> &'a [u8] { - PARSER_JOIN_INPUT_BUFFER.with(|b| join_abs_string_buf::

(cwd, tl_buf_mut(b), parts)) + join_abs_string_buf::

(cwd, parser_join_out_buf(join_abs_needed(cwd, parts)), parts) } /// Convert parts of potentially invalid file paths into a single valid filpeath @@ -1363,7 +1386,7 @@ pub fn join_abs_string<'a, P: PlatformT>(cwd: &'a [u8], parts: &[&[u8]]) -> &'a /// /// Returned path is stored in a temporary buffer. It must be copied if it needs to be stored. pub fn join_abs_string_z<'a, P: PlatformT>(cwd: &'a [u8], parts: &[&[u8]]) -> &'a ZStr { - PARSER_JOIN_INPUT_BUFFER.with(|b| join_abs_string_buf_z::

(cwd, tl_buf_mut(b), parts)) + join_abs_string_buf_z::

(cwd, parser_join_out_buf(join_abs_needed(cwd, parts)), parts) } const JOIN_BUF_LEN: usize = 4096; diff --git a/src/resolver/resolver.rs b/src/resolver/resolver.rs index 092262ed0d3c..9182382bfc58 100644 --- a/src/resolver/resolver.rs +++ b/src/resolver/resolver.rs @@ -1818,6 +1818,9 @@ impl<'a> Resolver<'a> { }; let mut len = abs.len(); if ends_with_dir { + if len >= buf.len() { + return ResultUnion::NotFound; + } buf[len] = platform.separator(); len += 1; } @@ -4023,10 +4026,9 @@ impl<'a> Resolver<'a> { input_path = self.fs_ref().top_level_dir; } - // A path longer than MAX_PATH_BYTES cannot name a real directory. - // Bailing here also prevents overflowing `dir_info_uncached_path` - // below when called with user-controlled absolute import paths. - if input_path.len() > MAX_PATH_BYTES { + // `dir_info_cached_miss` slices `[..len + 1]` into `dir_info_uncached_path`, + // and a path this long cannot name a real directory anyway. + if input_path.len() >= MAX_PATH_BYTES { return Ok(None); } @@ -4115,9 +4117,8 @@ impl<'a> Resolver<'a> { dir_info_uncached_path_buf[..input_path_len].copy_from_slice(input_path); // The slice spans one byte past the copied path so the NUL-splice/restore at // `input_path_len` (queue index 0, processed last in the open-dir loop below) - // writes through `path`'s own provenance. `input_path_len + 1 ≤ MAX_PATH_BYTES + 1` - // (checked above) and `PathBuffer` always carries the +1 sentinel slot, so the - // safe slice is in-bounds and the threadlocal buffer outlives this fn. + // writes through `path`'s own provenance. The caller bounds `input_path_len + // < MAX_PATH_BYTES`, and the threadlocal buffer outlives this fn. let path: &mut [u8] = &mut dir_info_uncached_path_buf[..input_path_len + 1]; queue[0].write(DirEntryResolveQueueItem { @@ -5703,7 +5704,13 @@ impl<'a> Resolver<'a> { } // Try the path with extensions - bufs!(load_as_file)[..path.len()].copy_from_slice(path); + { + let buf = bufs!(load_as_file); + if path.len() >= buf.len() { + dec_ret!(None); + } + buf[..path.len()].copy_from_slice(path); + } // NOTE: index by `0..len` so each iteration takes a fresh short // borrow of `self.opts` that ends before `&mut self` is taken by // `load_extension` (matches `extra_cjs_extensions` loop below). @@ -5865,6 +5872,9 @@ impl<'a> Resolver<'a> { // `&mut self` calls below (debug_logs / fs_ref) don't conflict, while // each read stays a safe `BackRef: Deref`. let entries = bun_ptr::BackRef::new(entries); + if path.len() + ext.len() > MAX_PATH_BYTES { + return None; + } let buffer = &mut bufs!(load_as_file)[0..path.len() + ext.len()]; buffer[path.len()..].copy_from_slice(ext); let file_name = &buffer[path.len() - base.len()..buffer.len()]; diff --git a/test/bundler/bun-build-api.test.ts b/test/bundler/bun-build-api.test.ts index 2b7fa720b654..793c9c31f56e 100644 --- a/test/bundler/bun-build-api.test.ts +++ b/test/bundler/bun-build-api.test.ts @@ -766,6 +766,48 @@ describe("Bun.build", () => { expect(await html?.text()).toContain(""); }, ); + + test.concurrent("HTML entrypoint with rooted \`, + \`\`, + ]) + .join(""); + writeFileSync("./index.html", \`\${tags}\`); + const r = await Bun.build({ entrypoints: ["./index.html"], throw: false }); + console.log(JSON.stringify({ success: r.success, logs: r.logs.map(l => l.name) })); + `, + }); + await using proc = Bun.spawn({ + cmd: [bunExe(), "build.mjs"], + env: bunEnv, + cwd: String(dir), + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + + expect(stderr).toBe(""); + expect(JSON.parse(stdout)).toEqual({ + success: false, + logs: expect.arrayContaining(["ResolveMessage"]), + }); + expect(exitCode).toBe(0); + }); }); test.concurrent("macro with nested object", async () => {