Skip to content

repl: ignore non-regular history files, and never replace a history file that was not read - #38217

Open
robobun wants to merge 6 commits into
mainfrom
farm/5171eed1/repl-history-hostile-files
Open

robobun wants to merge 6 commits into
mainfrom
farm/5171eed1/repl-history-hostile-files

Conversation

@robobun

@robobun robobun commented Aug 13, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • ln -s /dev/null ~/.bun_repl_history turns the history off. Since 1.4.0 (Hardening: input validation and protocol tightening across 24 subsystems (round 7) #31495), History::save (src/runtime/cli/repl.rs) sets the mode of what it opened to 0600. As root, that locks other users out of the device.
  • A FIFO at that path hangs bun repl before it prints anything: History::load uses a blocking open.
  • If load cannot read an existing file, save replaces it at exit.

Fix

  • History::open_file opens with O_NONBLOCK and accepts only a regular file. load and save open through it.
  • load arms save only after it read the file, or if no file exists.
  • Verified: test/js/bun/repl/repl.test.ts, "REPL history file". bun 1.4.2 fails 7 of its 15 tests on Linux (Notes).
  • Self-reviewed: 38 concerns raised, 34 addressed. Notes lists the other 4.

Background

  • bun repl loads ~/.bun_repl_history at startup and rewrites it at exit. No option turns that off.
  • open(2) of a FIFO blocks until a peer opens the other end. O_NONBLOCK prevents that.
  • On Windows, openat returns an asynchronous handle for O_NONBLOCK, so open_file leaves the flag out there. sys(windows): openat always opens a synchronous handle #46338 removes that mapping.
  • The first revision read only the last 4 MiB. That erased entries which save had written (Notes).

Downsides

  • A history file that cannot be read is no longer replaced. No session saves history until the user repairs it.
  • With O_NONBLOCK, an open fails at once under a file lease. That session loads or saves no history. main waits.
  • One more fstat per open of the file: 11 syscalls in a saving session, 9 on bun 1.4.2.
Notes

Tests, and what bun 1.4.2 does on Linux

  • "a FIFO at the history path does not hang the startup": times out.
  • "a FIFO that takes the place of the history file does not hang the exit": times out. Here the session read a regular file first, so the open in save is what meets the FIFO.
  • "a FIFO that has a reader keeps its mode and receives nothing": the mode becomes 0600 and 1 + 1 arrives in the FIFO.
  • "a device behind the history path keeps its mode" and "a device that takes the place of the history file keeps its mode": the mode becomes 0600. The tests make their own device node with the numbers of /dev/null. They need root on Linux and skip elsewhere.
  • "does not replace a history file that it cannot read": the file holds only 2 + 2 afterwards.
  • "loads a file with half a million lines": times out. One remove(0) per entry over the cap is quadratic: 200,000 lines take 3.8 s on 1.4.2. A debug build of this PR loads 1,000,000 lines in 1.1 s.
  • The other 8 pass on 1.4.2. They hold what must not change: a second session loads what the first one saved, the cap of 1000 entries at 999, 1000, 1001 and 1200 lines, an entry longer than 4 MiB, and a symlink to a regular file.
  • The whole file passes with a debug build (144 tests). cargo check --workspace passes for both Windows targets and for aarch64-apple-darwin.

Windows

  • The first revision passed O_NONBLOCK on every platform. On Windows each save then left the history file empty: Buildkite build 94860, both Windows lanes, three tests.
  • open_file now leaves the flag out with cfg!(unix), as script_or_command.rs and lint/driver/fs.rs do. The arm can go after sys(windows): openat always opens a synchronous handle #46338.
  • "a second session loads what the first one saved" runs on Windows. It checks the file after each of two sessions. Nothing on Windows was run locally.

Why the 4 MiB limit is gone

  • save has no limit in bytes, so it wrote files that load could not read back. With a newest entry of 5 MiB after 300 short ones, the next session listed no history and left 1 line of 301. bun 1.4.2 keeps all of them.
  • Without the limit, a 256 MiB file costs its size in memory at startup again, as on main. bun does not write such a file: save keeps 1000 entries.

File leases

  • open(2) returns EWOULDBLOCK if O_NONBLOCK is set and another process holds an incompatible lease (a Samba oplock, an NFS delegation).
  • Under a write lease load fails, so the session has no history and saves none. Under a read lease save fails, so the new entries are lost. Nothing that is in the file is lost.

Left on purpose

  • .load <file> and .save <file> open a path that the user typed. They are not changed.
  • bun --interactive has its own history code (src/js/internal/repl/history.js, the port of Node's). It is not changed.
  • A symlink to a regular file is still followed for reading and writing.
  • Other commands that open a file without this check, and other places that fchmod after an open in place, are not changed.

Self-review, the 4 concerns that are not done

  • A retry without O_NONBLOCK when the open fails under a lease. It needs a second open, and a test that holds a lease through FFI. Nothing in the file is lost without the retry.
  • A PR of its own for the mode fix. It is the main part of this PR now.
  • A list of the other places that fchmod after an open in place. Other open PRs own them.
  • The shared helper for this check. Read whole files only when they are regular files #39734 adds it.

Other changes in load

Other open PRs

…e ones

History::load opened ~/.bun_repl_history with a plain blocking open and read
the whole file, so a FIFO at that path hung `bun repl` before it printed
anything (and History::save hung it again at exit), and a huge file was read
into memory in full before being trimmed to the last 1000 entries.

Both now go through History::open_file, which opens with O_NONBLOCK and
skips anything that is not a regular file. load reads at most the last
MAX_HISTORY_FILE_BYTES of the file and collects entries from the end, which
also replaces the quadratic remove(0) trimming loop.
@coderabbitai

coderabbitai Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: oven-sh/bun/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 09dff34c-1b3b-4662-ac7e-3cc06e6c6278
📥 Commits

Reviewing files that changed from the base of the PR and between 9634520 and 959f82f.

📒 Files selected for processing: 2
  • src/runtime/cli/repl.rs
  • test/js/bun/repl/repl.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 0 remain after this review.

Walkthrough

REPL history loading and saving now use a shared file-opening helper that rejects non-regular files. New tests cover persistence, history limits, and behavior with symlinks, FIFOs, device nodes, and unreadable files.

Changes

REPL history file handling

Layer / File(s) Summary
Validate history file access
src/runtime/cli/repl.rs
History::open_file sets Unix open flags and rejects non-regular files. History::load and History::save use this helper. History trimming removes excess entries in one operation.
Verify history behavior
test/js/bun/repl/repl.test.ts
The test harness accepts optional user credentials. Tests cover persistence, the 1000-entry limit, large files and entries, symlinks, FIFOs, device nodes, and unreadable files.

Suggested reviewers: jarred-sumner

Priority: ⬇️ Low

Merge Risk: ⚪ Minimal · up to 959f8

This change stops the REPL from blocking on history files that are FIFOs, devices or directories, and it adds tests for those cases. No merge-blocking risk was identified.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly summarizes the primary changes: rejecting non-regular history files and preventing replacement of files that could not be read.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description is complete and relevant. It explains the problem, the implementation, scope, tradeoffs, testing, and known limitations. It does not use the exact template headings, but it provides th…
Full details: Description check

Explanation

The description is complete and relevant. It explains the problem, the implementation, scope, tradeoffs, testing, and known limitations. It does not use the exact template headings, but it provides the required information through the Problem, Fix, Background, Downsides, and Notes sections.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@robobun

robobun commented Aug 13, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 3:42 PM PT - Oct 21st, 2026

✅ @robobun, your commit 355126bed2fdf1581dc0bc38f18059e2e10a9f50 passed in Build #130213! 🎉


🧪   To try this PR locally:

bunx bun-pr 38217

That installs a local version of the PR into your bun-38217 executable, so you can run:

bun-38217 --bun

@robobun

robobun commented Aug 13, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: reworked and pushed, waiting for CI.

Reproduced with bun 1.4.2 on Linux, with HOME set to a scratch directory:

  • As root, with the history path a symlink to a device node: one session changes the mode of the device from 0666 to 0600. With this PR it stays 0666.
  • mkfifo $HOME/.bun_repl_history; bun repl prints nothing and never returns. With this PR it starts, evaluates 1 + 1, and exits 0.
  • A history file of mode 0200 with two entries, and a session as a user who cannot read it: the file holds only the new entry afterwards. With this PR it is unchanged.
  • The 15 tests of "REPL history file" in test/js/bun/repl/repl.test.ts: bun 1.4.2 fails 7 of them, and a debug build of this PR passes all.

The first revision left the history file empty on Windows at each save (Buildkite build 94860). This revision keeps O_NONBLOCK off on Windows. #46338 removes the cause in openat.

Comment thread src/runtime/cli/repl.rs Resolved
…as not read

The first revision of this change read only the last 4 MiB of the
history file. save has no limit in bytes, so it wrote files that load
could not read back: one entry over 4 MiB emptied the history at the
next exit. load reads the whole file again, as on main.

- History::open_file leaves O_NONBLOCK out on Windows. openat returns an
  asynchronous handle for it there and write fails on that handle, so
  each save left the history file empty.
- load sets file_path only after it read the file, or if the file does
  not exist. save then does not replace a file that load could not read.
- The trim to MAX_HISTORY_SIZE entries is one drain.
- Tests: two sessions, the entry cap at its boundaries, an entry over
  4 MiB, a symlink, a FIFO with a reader, a device node, and a file that
  cannot be read.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

The Windows handle concern from the earlier review is addressed: O_NONBLOCK is now gated on cfg!(unix) in open_file, and the remaining O::NOCTTY/O::CLOEXEC bits are 0 / ignored by openat_windows, so the history handle stays synchronous there.

Beyond the inline findings, I also checked that save opening with O_TRUNC before the kind() check is harmless: the kernel ignores O_TRUNC on FIFOs and device nodes and refuses O_WRONLY on a directory, so nothing non-regular is modified before the rejection.

Extended reasoning...

The change touches only History::load/open_file/save in src/runtime/cli/repl.rs plus new history-file tests; no auth, crypto or injection surface. The earlier Windows async-handle concern was fixed by the cfg!(unix) gate, but confirmed findings remain (the PR description's tail-read/pread claim is not in the code, the unconditional O_NONBLOCK can turn a lease conflict into a silent no-history, and the exit-side FIFO guard is untested), so a human look is still warranted.

Comment thread src/runtime/cli/repl.rs Resolved
Comment thread test/js/bun/repl/repl.test.ts Resolved
Comment thread src/runtime/cli/repl.rs Resolved
@robobun robobun changed the title repl: ignore non-regular history files and only read the tail of large ones repl: ignore non-regular history files, and never replace a history file that was not read Oct 21, 2026

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review completed

Nothing new to post: everything this review found is already covered by existing comments on this pull request or didn't merit a separate one.

Still open from earlier reviews (3):

  • Unresolved: 3 minor or pre-existing.

…ing a session

These two tests reach the open in History::save. In the other tests of a
FIFO or a device, the load fails first and save does not run.

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline finding, I also checked the cfg!(unix) gate on O_NONBLOCK in History::open_file against the Windows branch of File::kind() (GetFileType maps disk files to FileKind::File, and the handle stays synchronous for save's write_all), and confirmed the new swap-in FIFO/device tests reach save's open_file after a successful load, so the exit-side check is now exercised.

Extended reasoning...

The change touches only History::load/save/open_file in src/runtime/cli/repl.rs and the matching tests in test/js/bun/repl/repl.test.ts; it opens $HOME/.bun_repl_history with O_NONBLOCK/O_NOCTTY/O_CLOEXEC, rejects non-regular files, and leaves file_path unset when the file could not be read. No security-sensitive surface beyond file-mode handling of the user's own history file. The earlier Windows asynchronous-handle concern is addressed by the cfg!(unix) gate, and the exit-side FIFO/device cases now have tests; one inline nit about the nodev tmp mount remains, and the NFS-lease thread is still open with an author reply.

Comment on lines +2106 to +2110
const canMakeDevices = (() => {
if (!isLinux || !isRoot) return false;
using dir = tempDir("repl-history-device-probe", {});
return makeNullDevice(path.join(String(dir), "null"));
})();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 nit (optional): on a Linux runner whose tmp is mounted nodev (systemd's default tmpfs /tmp), both root-only device tests pass on the base branch too, so they do not protect the mode fix. The probe at test/js/bun/repl/repl.test.ts:2106-2110 only checks that mknod succeeds; on a nodev mount the kernel refuses to open the node with EACCES, so the REPL never reaches fchmod on any branch and mode 0666 is kept regardless. Fix: have canMakeDevices also open the probe node for writing (openSync(device, "w")) and skip with that reason when the open fails, which covers both device tests at 2114 and 2130.

Why this was flagged

The device tests run only when isLinux and isRoot and mknod succeeds in a tempDir under os.tmpdir() (test/harness.ts:607, test/js/bun/repl/repl.test.ts:2106-2110). On a filesystem mounted with nodev, which systemd's tmp.mount uses for tmpfs /tmp, mknod as root still succeeds but open(2) of the character device returns EACCES. In that environment the REPL's open at src/runtime/cli/repl.rs:238 (and the base branch's open_a in save) fails before the kind check and before fchmod at src/runtime/cli/repl.rs:271, so statSync(device).mode stays 0o666 on the base branch as well and the assertions at 2122 and 2141 hold on both branches. The tests therefore cannot fail for the right reason there, which the repository review rules call out as a test that passes both ways. Nothing in the probe verifies that the node can actually be opened; checking that once in canMakeDevices and skipping with a reason otherwise makes the skip honest.

Verification: nit. With root on Linux and os.tmpdir() on a nodev mount, canMakeDevices at test/js/bun/repl/repl.test.ts:2106-2110 only checks mknod exits 0, but open(2) of the node fails with EACCES on a MNT_NODEV mount. On the base branch History::save returns on Err(_) before the fchmod at repl.rs:257, so both device tests (:2114, :2130) see mode 0666 and pass on base as on the PR branch.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants