Repository navigation
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Essentials Run ID: 📒 Files selected for processing (3)
Included review availability: Your plan provides up to 10 included reviews per hour; 7 remain after this review. WalkthroughChangesSecure module resolution
Suggested reviewers: Merge Risk: ⚪ Minimal · up to Automatic tsconfig/jsconfig discovery now ignores world-writable sticky directories on POSIX systems while preserving explicit configuration overrides, preventing untrusted scratch-directory configs from changing module resolution. No current merge-blocking risk remains. 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
|
Status: draft, waiting for a maintainer The head is 4e39797. The rule: a directory vouches for the configs in it unless other users can add files to it (its owner is not trusted, it is world-writable, or it is sticky and group-writable). In such a directory a config is read only when it opens, without following a symlink, as a regular file with one link and a trusted owner, and it is read through that same descriptor. Ordinary directories behave as before. Reproduction with two uids, on this commit: uid 65534 wrote Proof: What a maintainer needs to know before looking at the diff:
|
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline findings, I checked that both tsconfig.json and jsconfig.json auto-discovery flow through the single guarded site (the check sits after both filename lookups and before the --tsconfig-override branch, so the explicit-override path is correctly unaffected).
Extended reasoning...
The conventions note asked to confirm both config filenames reach the one patched site — reading dir_info_uncached shows the is_shared_scratch_dir guard is placed after the sequential tsconfig.json / jsconfig.json lookups and inside the tsconfig_override.is_none() arm, so both filenames are covered and --tsconfig-override is untouched. That coverage question is ruled out; the remaining concerns are the inline findings.
927aafd to
7b168d9
Compare
|
Updated 3:15 AM PT - Oct 2nd, 2026
❌ @robobun, your commit 4e39797 has 3 failures in
🧪 To try this PR locally: bunx bun-pr 41797That installs a local version of the PR into your bun-41797 --bun |
There was a problem hiding this comment.
Re-reviewed after the two follow-up commits — all three earlier points are addressed: the guard now takes the already-open directory FD and fails closed (!dir.is_valid() → true, fstat error → true via map_or(true, …)), the stray SAFETY: comment is gone with the path-buffer code, and the --tsconfig-override test now drains stderr in the same Promise.all. No new issues found. Since this is a security hardening in the resolver's config auto-discovery, a human look is still worthwhile before merge.
What was reviewed:
is_shared_scratch_dirfail-closed semantics on both the invalid-FD and fstat-error branches, and that the non-unix arm compiles as a no-op.- Placement of the check inside the auto-discovery arm only, so
--tsconfig-override(theparent.is_none()branch) is unaffected. - The five new tests: pipes drained concurrently,
chmodSync(0o1777)vs0o755covers both sides of the boundary,describe.skipIf(isWindows)gating.
Extended reasoning...
Overview
The PR hardens dir_info_uncached in src/resolver/resolver.rs so that a tsconfig.json/jsconfig.json auto-discovered in a world-writable + sticky directory (/tmp, /var/tmp, /dev/shm) is ignored, closing a local-attacker import-rewrite hole. Since the prior review, the helper was rewritten to fstat the already-open directory FD instead of stating a path through a pooled buffer, and it now fails closed on every branch where the mode cannot be established. The SAFETY: comment and ZStr::from_buf path went away with that rewrite. In the test file, the --tsconfig-override case now includes proc.stderr.text() in the Promise.all, so all five subprocess tests drain both pipes.
Security risks
This is itself a security fix. The residual risk is scope: the guard sits only in the auto-discovery arm (inside the else if parent.is_none() sibling is the override path, which intentionally bypasses it). Switching to fstat on the FD the resolver already holds removes the path-length and stat-race concerns raised earlier. The map_or(true, …) shape means any fstat failure skips the config rather than trusting it, satisfying the fail-closed rule in REVIEW.md. The non-unix arm returns false, which is the documented no-op on Windows where the sticky-bit model does not exist.
Level of scrutiny
Moderate-to-high. The diff is small (~30 native lines, ~100 test lines, one docs paragraph), but it changes a security-relevant decision in the resolver that runs for every source file. The mechanism is straightforward — one bitmask check on an fstat result — and the test matrix covers runtime, bundler, jsconfig, the negative (0o755) case, and the override escape hatch. Still, resolver behavior changes have wide blast radius, so a maintainer sign-off is appropriate.
Other factors
All three inline concerns from the previous run were addressed by commits 2423584 and 7b168d9, and no new candidate issues surfaced this run. There are no outstanding CHANGES_REQUESTED reviews from human reviewers in the timeline. The PR description's rationale for choosing the sticky-bit boundary over package.json or ownership checks is sound and matches the code.
…ectory The upward walk for tsconfig.json and jsconfig.json read every ancestor directory and inherited the file into every subdirectory below it. Its compilerOptions.paths and baseUrl are applied before the node_modules walk, so a file found that way rewrites bare imports of a project that installed its own dependencies. In a world-writable sticky directory (mode 1777: /tmp, /var/tmp, /dev/shm) every user can create a file, so the file found there can belong to another user. Skip auto-discovery in such a directory. --tsconfig-override still loads the file, because the person who runs bun names it. Without the sticky bit the same directory lets one user rename another user's project, so nothing below it is trustworthy either way. That is why the sticky bit, and not the write bit alone, is the boundary.
…he override test Use the directory fd the walk already holds instead of a second path lookup, and treat a directory whose mode cannot be read as shared. Drop the SAFETY label that sat on a safe call. The --tsconfig-override test now drains stderr with the other two streams.
|
Rebased onto main bf42a52 for a fresh CI run (was c8d2aff, now 5bc5a49). There was no conflict and the diff is unchanged: the added and removed lines are the same as before. Checked on a local debug build of bf42a52 with this branch and 12 other rebased branches merged in: the |
c8d2aff to
5bc5a49
Compare
There was a problem hiding this comment.
Beyond the inline findings, I also checked whether the fail-closed !dir.is_valid() branch in is_shared_scratch_dir could drop a legitimate tsconfig: open_dir is only FD::INVALID for a permission-denied ancestor, and that directory's entry set is left empty (src/resolver/resolver.rs, the if open_dir.is_valid() iterate block), so no tsconfig.json is ever found there and the branch cannot fire on a readable directory. With store_fd off the walk re-opens each directory, so fd is still a live descriptor at the fstat call.
Extended reasoning...
The change adds a single fstat-based predicate in Resolver::dir_info_uncached that clears tsconfig_path when the enclosing directory is mode 1777, plus a docs paragraph and five subprocess tests. It touches the module-resolution trust boundary, so it is security-sensitive; three verified findings are posted inline and two more verified findings were withheld, which rules out approval. The ruled-out note covers the one concrete adjacent concern examined this run that is not already an inline finding.
| } | ||
| } | ||
| if let Some(found) = tsconfig_path { | ||
| if is_shared_scratch_dir(fd) { |
There was a problem hiding this comment.
🟣 pre-existing, not blocking: pre-existing, security-relevant: after this merge an attacker-planted package.json in /tmp still rewrites bare imports for bun build (default browser target), which the PR says it closes for bun build. The new guard at src/resolver/resolver.rs:6446 only clears tsconfig_path; the browser map of a package.json in the same 1777 directory is still adopted at resolver.rs:6376-6379 and inherited by every subdirectory. A victim package.json without a browser field does not reset that scope, so {"browser":{"lib":"./evil.js"}} beats the vendored node_modules/lib. Fix: apply the same shared-scratch predicate to every ancestor-supplied remap in dir_info_uncached, at least the browser-scope adoption, so bun build is covered like bun run.
A small fix can ride a push you are already making; otherwise a short reply is enough.
Why this was flagged
Any user writes /tmp/package.json containing {"browser":{"lib":"./evil.js"}} and /tmp/evil.js. A victim runs bun build ./app.js in a private mktemp directory under /tmp that has its own package.json and node_modules/lib. In dir_info_uncached the /tmp DirInfo parses that package.json (src/resolver/resolver.rs:6339-6373) and, because pkg.browser_map.count() > 0, sets enclosing_browser_scope and package_json_for_browser_field to /tmp (resolver.rs:6376-6379); every child DirInfo copies both fields from its parent (resolver.rs:6232-6233) and the victim's package.json only replaces them when it has its own browser map. Default bun build target is Browser, so care_about_browser_field is true (resolver.rs:909) and resolve_without_symlinks consults the enclosing browser scope before the node_modules walk (resolver.rs:2238-2294): check_browser_map matches the bare key "lib" (resolver.rs:6658) and the import is rewritten to ./evil.js resolved from /tmp. The new is_shared_scratch_dir check is only applied to tsconfig_path (resolver.rs:6445-6457), so the attack remains open through package.json for bun build; the base branch behaves identically here.
Verification: Same route on the base branch; the diff does not touch the package.json branch. The new guard (src/resolver/resolver.rs:6445-6456) only resets tsconfig_path; when pkg.browser_map.count() > 0, 6376-6379 adopts /tmp's browser scope, children inherit it at 6230-6233, and check_path("lib") hits map.get(path_to_check) (6658), so import_path = "./evil.js".
There was a problem hiding this comment.
Confirmed on a release build of main at 367d939. With {"browser":{"lib":"./evil.js"}} in the ancestor package.json, bun build ./app.js bundles evil.js. --target bun, --target node and bun app.js use the installed package. esbuild 0.25.1 gives the same result for its default platform. The description said that an ancestor package.json needs no change. That note only tested imports. It is corrected, and this case is now listed as open.
There was a problem hiding this comment.
Not fixed in this PR, and left unresolved on purpose. The description lists it under Downsides and in the Notes. The browser scope is inherited two lines above the tsconfig inherit, so the owner predicate could cover it. I kept it out because esbuild 0.25.1 applies an ancestor browser map the same way for its default platform, so the build leg matches the reference, while the run-time paths leg has no such precedent. A maintainer can ask for it in this change.
…ctory mode The previous rule in this branch skipped a config in a world-writable sticky directory. It never looked at who owned the file, so a user's own tsconfig.json in /tmp was skipped too, with no message. It also missed a group-writable sticky directory and an ancestor directory that another user owns. Read an auto-discovered tsconfig.json or jsconfig.json only when its owner is one this process already trusts: the invoking user, root, or the owner of the directory bun was started in. The last clause keeps a container that runs as root over a bind mount owned by the host user reading its own config, which an euid-only rule would break. The check is one lstat of the file, so a symlink another user planted is judged by who planted it. An owner that cannot be read is not trusted. POSIX only. --tsconfig-override still loads the named file.
…can write The owner check from the previous commit had two defects. A config owned by a trusted uid but hard-linked into a shared directory by another user passed it. And it called lstat on the path while the file was opened again later by name, so the file could be replaced between the two. A directory now vouches for the configs in it unless other users can add files to it: its owner is not trusted, it is world-writable, or it is sticky and group-writable. In such a directory a config is read only when it opens without following a symlink as a regular file with one link that a trusted user owns, and it is read through that same descriptor. A skipped tsconfig.json no longer hides a jsconfig.json in the same directory. An ordinary directory costs one fstat of the descriptor the walk already holds and is otherwise unchanged: symlinked and hard-linked configs in it are read as before. POSIX only. --tsconfig-override is not affected.
|
Closing as stale: this PR conflicts with main, and its last commit is from 2026-10-02. The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR. |
Problem
tsconfig.jsonorjsconfig.jsonthat another user puts in/tmpapplies to every project below it. Itspathsbeatnode_modules, so that user's module runs under the victim's uid.dir_info_uncachedreads the config in each ancestor directory, and every child inherits it.Fix
resolve.test.ts, 14 new blocks. Eight fail on an unfixed release build, five without a second user.Background
/tmpwhere the OS allows it, and a path check can race the read.package.jsonbreaks a monorepo root config, and esbuild and tsc both read past it.Downsides
fstatper directory that holds a config, plus one in a shared directory. Not traced: nostracehere.package.jsonbrowsermap rewrites the same import inbun build.Notes
The rule, exactly
For a
tsconfig.jsonorjsconfig.jsonfound in the listing of directoryDduring the upward walk (POSIX only):fstatthe descriptor the walk already holds forD. IfD's owner is trusted,Dis not world-writable, andDis not both sticky and group-writable, read the config as before. Symlinked and hard-linked configs in such a directory behave as they did.O_NOFOLLOW | O_NONBLOCK,fstatthe result, and accept it only when it is a regular file, its link count is 1 and its owner is trusted. The config is then read from that descriptor. Anything else is skipped, and the walk triesjsconfig.jsonnext.A group-writable directory without the sticky bit counts as ordinary. Many distributions give each user a private group and a
umaskof002, so most project directories there are group-writable.--tsconfig-overrideandextendsare not vetted. The person who runs bun, or a config that already passed, names those files.Repro with two uids
uid 65534 wrote the config and the module into a mode
1777directory owned by root. Root ran a private project below it, withlibinstalled in its ownnode_modules:This run is on this commit (4e39797).
Why the owner alone was not enough
The previous revision of this branch trusted any config whose owner was trusted. A config owned by root, hard-linked into the shared directory, with
"baseUrl": "."in it, made alib.jscreated beside it by uid 65534 win over the installed package. That revision and the unfixed build both printed the other user's value. This commit prints the installed one. On the test kernel the link itself is refused for a user who does not own the file (fs.protected_hardlinks=1,EPERM), so the old rule was only as strong as that setting. Systems that allow such links were not tested.That revision also called
lstaton the path, while the file was opened again later by name. This revision checks the descriptor that it reads.Tests
test/js/bun/resolve/resolve.test.ts, describeauto-discovered tsconfig in a directory other users can write.Fail on the unfixed release build (367d939), pass here:
1777directory is not followedbun buildbundletsconfig.jsondoes not hide your ownjsconfig.json1775) counts tootsconfig.jsonanother user owns there is not readjsconfig.jsonPass both ways:
1777directory applies--tsconfig-overrideloads a config the walk would skipRun as uid 65534 on this commit, 10 blocks pass and the 4 root-only blocks skip.
Other runs on this commit
resolve.test.tspasses on macOS x64 and arm64 (104 pass, 0 fail, root-only blocks skipped), and no Linux lane reports a failure intest/js/bun/resolve/. The red jobs are an x64-asanspawn.test.tsfailure that other PR builds also show, and one macOS arm64 agent that timed out onbunx,datadog-pprofandexpo. Those three pass on macOS x64 in the same build.test/js/bun/resolve/passes 414 of 423, with 5 skipped and 3 todo. The one failure isload-same-js-file-a-lot.test.ts, a 5 s timeout. It times out the same way on a debug build without this diff (3 of 3 runs each way, on a host with a load average above 200).test/cli/run/tsconfig-override.test.tsandtest/bundler/bundler_jsx.test.tspassed locally one commit earlier. The local rerun on this commit did not finish because the host was overloaded. CI ran both on this commit.cargo clippyandcargo fmtare clean.cargo check -p bun_resolverpasses forx86_64-pc-windows-msvc,aarch64-apple-darwin,x86_64-unknown-linux-muslandx86_64-unknown-freebsd.Limits
chmod -R 777.node_moduleswalk is unchanged. Node does the same walk.The ancestor
package.jsonbrowsermapOn a release build at 367d939,
{"browser":{"lib":"./evil.js"}}in an ancestorpackage.jsonmakesbun build ./app.jsbundleevil.js.--target bun,--target nodeand the runtime use the installed package. The scope is inherited two lines above the tsconfig inherit. esbuild 0.25.1 does the same for its default platform, so this change leaves it alone.Parity
--bundleappliespathsfrom a config above the project'spackage.json.--showConfig, run inside the project, prints that same ancestor config.tsconfig.json.The message level
A skip is recorded with
Log::add_debug_fmt, which prints nothing by default. A warning would print on every bun run below a shared directory while the file exists, so any user could add a line to every other user's stderr.History of this branch
1777directory. It ignored who owned the file, so it dropped a user's own/tmp/tsconfig.json.Earlier versions of this description overstated some results: a build that was never run, a self-review that had returned no result, an ancestor
package.jsoncalled safe after onlyimportswas tested, and a claim that the owner rule closed cases it did not.Review status: no review run returned a verdict for any revision of this branch. The design review ended without a pick. The self-reviews of the first two revisions ended without a result. The self-review of this revision was stopped twice by infrastructure restarts. The rule is checked only by the tests and runs listed here.
no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/bun/resolve/resolve.test.ts