-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Fix panic when require.resolve options.paths contains non-absolute paths #32680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 The guard uses native
bun_paths::is_absolute(POSIX: leading/only), but the else branch joins viaabs_buf_checked, which usesplatform::Loosesemantics — Loose treats Windows drive paths likeC:/fooas absolute on any host and replaces the cwd base, so the joined result is stillC:/foo, which then fails the nativeassert!(bun_paths::is_absolute(...))indir_info_cached_maybe_log(resolver.rs:4203). On Linux/macOS,require.resolve("pkg", { paths: ["C:/foo"] })(or"C:\\foo") still panics after this PR. Sinceoptions.pathsis arbitrary userland input — the very motivation for this fix — either gate on the Loose predicate so the guard matches the join, or re-check the joined result with the native predicate andcontinueif it isn't native-absolute.Extended reasoning...
What the bug is
The new guard at
resolver.rs:2095and the join it falls through to atresolver.rs:2102use different absolute-path predicates, so there is a class of inputs that fails the guard, gets "resolved" by the join, and yet still fails the downstream native-absolute assertion atresolver.rs:4203. Specifically, on POSIX hosts, a Windows-style drive path inoptions.paths(e.g."C:/foo"or"C:\\foo") still panics the process after this PR — the exact failure mode the PR is meant to eliminate.The code path
bun_paths::is_absolute(paths/lib.rs:202) →bun_core::path_sep::is_absolute_native. On#[cfg(not(windows))]this is justp[0] == b'/'. Forb"C:/foo",'C' != '/'→ false → falls into theelsebranch.self.fs_ref().abs_buf_checked(&[b"C:/foo"], buf)(resolver/lib.rs:353-355) →join_abs_string_buf_checked::<platform::Loose>(top_level_dir, buf, parts)._join_abs_string_buf(resolve_path.rs:1730) on POSIX:P::P == Loose(notWindows) andcfg!(windows) == false→ does not take the Windows-specific impl.P::P.is_absolute(parts[0])withP::P == Loose→is_absolute_windows_t(paths/lib.rs:77-93). Forb"C:/foo":len >= 3 && p[1] == ':' && p[2] == '/'→ true. Socwdis replaced withb"C:/foo"andpartsbecomes empty (line 1784-1785).leading_separator_indexforLoosedelegates toWindowsfirst (resolve_path.rs:1322-1324) → matches'C' in 'A'..='Z' && p[1]==':' && p[2]=='/'→Some(2)(resolve_path.rs:1299-1308).leading_len = 3, prefixb"C:/"is preserved.buf:b"C:/foo".check_package_path(b"C:/foo", ...)(resolver.rs:2264) immediately callsself.dir_info_cached(source_dir)→dir_info_cached_maybe_log. On POSIX: not.//.;len < MAX_PATH_BYTES; the#[cfg(windows)]normalization block (4171-4201) is skipped. Reaches:is_absolute_native(b"C:/foo")on POSIX →'C' != '/'→ panic. This isassert!, notdebug_assert!, so it fires in release builds.The same trace holds for
"C:\\foo"(is_absolute_windows_tacceptsp[2] == '\\';slashes_to_posix_in_placeat line 1825 normalizes the prefix toC:/).Why the existing fix doesn't catch it
The fix assumes
abs_buf_checkedalways returns something native-absolute when given a non-native-absolute input. Butabs_buf_checkedusesplatform::Loose, which intentionally honors Windows drive prefixes on any host (resolve_path.rs:1219, 1322-1324). When a part is Loose-absolute, it replaces the cwd base rather than being appended to it, so the function can return a value that is Loose-absolute but not native-absolute on POSIX. The guard predicate (native) and the join semantics (Loose) simply don't agree.Step-by-step proof (Linux/macOS)
custom_utf8.slice() = b"C:/foo".bun_paths::is_absolute(b"C:/foo")→b'C' != b'/'→false→ else branch.abs_buf_checked([b"C:/foo"])→ Loose join:Loose.is_absolute(b"C:/foo") == true→ cwd ←b"C:/foo", parts ←[]→ resultb"C:/foo".check_package_path(b"C:/foo", ...)→dir_info_cached(b"C:/foo").assert!(bun_paths::is_absolute(b"C:/foo"))→false→ panic: "cannot resolve DirInfo for non-absolute path: C:/foo".Impact
Same severity as the bug being fixed: a user-reachable process crash from arbitrary userland input to
require.resolve/Module._resolveFilenameon Linux and macOS. The PR description states "any relative string ... crashed the process" as the problem to solve; Windows-style drive paths are still in that crash class on POSIX after this change.How to fix
Two straightforward options:
b"C:/foo"directly). Better:abs_buf_checkedreturnsSome(p), checkbun_paths::is_absolute(p)andcontinueif false (the path can't name a real directory on this host anyway). Equivalently, drop the input guard entirely and always join, then check the result:It would also be worth adding
{ paths: ["C:/foo"] }to the "does not crash" regression test (POSIX-only) so this input class is covered.