Skip to content

fix(install): fall back to a private node shim directory instead of dropping node - #19

Merged
steipete merged 1 commit into
mainfrom
claude/node-shim-private-fallback
Sep 28, 2026
Merged

steipete merged 1 commit into
mainfrom
claude/node-shim-private-fallback

Conversation

@steipete

Copy link
Copy Markdown

What does this PR do?

Follow-up to #17. #17 fixed the shared-/tmp collision by giving each user their own shim directory, /tmp/bun-node-<uid>-<sha>. It left three gaps, and this PR closes them.

1. node still vanished silently when the per-user directory was unusable. Another user can pre-create /tmp/bun-node-<victim-uid>-<sha>. The directory could also be group/other-writable, be a regular file, or fail mkdir. In all of these cases create_fake_temporary_node_executable returned without touching PATH or saying anything, and lifecycle scripts failed with node: command not found.

Bun now decides the shim directory once per process:

  • Preferred: use the preferred directory when it passes the existing ownership/mode guard, which is unchanged.
  • Fallback: otherwise create a private <preferred>-<16 random hex> with mkdir 0700. The name comes from BoringSSL RAND_bytes, and creation is retried only on EEXIST. A successful mkdir means Bun created and owns the directory, so a squatter can neither predict nor pre-create it.
  • Warning: if no directory or symlink can be created, one warning names the directory and suggests BUN_TMPDIR, instead of silence.

2. NODE could point at the unusable path. bun run sets NODE/npm_node_execpath to the shim directory (a pre-existing quirk kept here). With --bun, load_node_js_config set them to the preferred path before the directory was chosen, so a fallback would leave NODE pointing at a squatted, possibly attacker-planted path. Both variables are now re-set from the directory actually in use, after the shim exists, for both --bun and plain bun run. The old code pointed NODE at the shared /tmp path the same way.

3. The shim root now honors BUN_TMPDIR, like the node-gyp directory lifecycle scripts already get (get_default_temp_dir). It is only used when absolute and free of :, the PATH delimiter. $TMPDIR stays ignored, matching Bun's temp-dir convention everywhere else.

The fork's naming stays a strict superset of upstream oven-sh/bun#35565. A one-line marker at bun_node_dir() says to keep the fork side when an upstream sync conflicts there. Windows is untouched.

Production delta: src/install/lib.rs +90/-26, src/runtime/cli/run_command.rs +15/-10. Tests +117/-2. One changelog entry.

How did you verify your code works?

New tests. Each uses its own BUN_TMPDIR, an empty PATH, and no NODE/npm_node_execpath, so they never touch /tmp and are safe to run concurrently:

  • bun-install-lifecycle-scripts.test.ts:
    • "node shim directory honors BUN_TMPDIR": the lifecycle node probe.mjs runs under Bun through <BUN_TMPDIR>/bun-node-<uid>….
    • "node shim falls back to a private directory when the preferred one is unusable": two cases, one with the preferred directory chmod 0777 and one with it replaced by a regular file. The second install still runs under Bun, through <preferred>-<16 hex>, owned by the user, mode 0700.
  • bun-run.test.ts, "node shim fallback keeps NODE and npm_node_execpath aligned with PATH": plain bun run and bun --bun run with an unusable preferred directory. NODE and npm_node_execpath equal the fallback directory on PATH.

Results:

  • Before the fix: every new test fails with stock Bun 1.4.2 (USE_SYSTEM_BUN=1), and all pass with bun bd test.
  • Control for the NODE change: with only the run_command.rs refresh reverted, the --bun case fails with NODE still set to …/bun-node-501-debug while PATH uses …/bun-node-501-debug-8f0260ef08ac0610. It passes with the refresh.
  • Neighboring tests: the fix(install): key the lifecycle node shim directory on the user id #17 uid test and the node-gyp shim test pass. test/cli/run/as-node.test.ts passes 11/11.
  • Full test/cli/install/bun-run.test.ts: 287 pass / 2 skip / 10 fail. These are exactly the 10 failures this Mac's debug build shows on clean main (two SIGILL expectations, eight cases where the local shell can't find bun-debug).
  • Warning path: checked separately. A missing parent directory and EACCES on symlink creation each print exactly one warning and add no shim entry to PATH.
  • Static checks: rust:check-all for x86_64-pc-windows-msvc and x86_64-unknown-linux-gnu passes. rustfmt and Prettier are clean.
  • Review: Codex review of the final diff found nothing actionable.
…ropping node

When the preferred per-user shim directory was unusable (another user
pre-created it, it was group/other-writable, it was not a directory, or
mkdir failed), `create_fake_temporary_node_executable` returned without
touching PATH and without saying anything, so lifecycle scripts failed
with `node: command not found`.

Decide the shim directory once per process: use the preferred
`<root>/bun-node-<uid>-<sha>` when it passes the existing ownership and
mode guard, otherwise create a private `<preferred>-<16 random hex>`
with mkdir 0700 (a successful mkdir means we own it). If no directory or
symlink can be created, print one warning naming the directory and
suggesting BUN_TMPDIR.

The root now honors BUN_TMPDIR when it is absolute and free of ':' (the
PATH delimiter), like the node-gyp directory lifecycle scripts already
get; Bun still ignores $TMPDIR here.

`bun run` sets NODE and npm_node_execpath from the directory actually in
use after the shim is created, in both the --bun and plain paths. With
--bun, load_node_js_config had set them to the preferred path before the
directory was chosen, so a fallback left NODE pointing at the unusable
(possibly attacker-planted) path.

Windows is unchanged. The fork's shim naming stays a superset of
upstream oven-sh#35565; keep the fork side when syncing.
@steipete
steipete merged commit 13794b5 into main Sep 28, 2026
10 of 11 checks passed
@steipete
steipete deleted the claude/node-shim-private-fallback branch September 28, 2026 00:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant