Skip to content

webview: pass --no-sandbox to Chrome when Bun is root, and --disable-dev-shm-usage always - #39490

Closed
robobun wants to merge 1 commit into
mainfrom
farm/b8553a14/webview-chrome-tests-as-root
Closed

robobun wants to merge 1 commit into
mainfrom
farm/b8553a14/webview-chrome-tests-as-root

Conversation

@robobun

@robobun robobun commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • On Linux, new Bun.WebView({ backend: "chrome" }) never works when Bun runs as root, which is how most containers run it. Chrome exits during startup and the constructor or the first navigate() fails with error: Chrome exited. Chrome's own message is only visible with backend.stderr: "inherit": zygote_host_impl_linux.cc: Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
  • The flag list the runtime launches Chrome with (src/runtime/webview/ChromeProcess.rs, the argv vector in spawn()) has no --no-sandbox. A user can add it through backend.argv, except on one path: when auto-detect finds a stale DevToolsActivePort file and the WebSocket connect fails, ChromeBackend.cpp respawns with an empty argv, so that spawn cannot be fixed from JS.
  • Once Chrome starts in a container, the next default gets it: Docker gives /dev/shm 64 MB, Chrome puts its shared memory there, and in this container the third 1280x720 view (the size of the docs example) dies with Chrome killed by signal 5. Playwright and puppeteer both pass --disable-dev-shm-usage by default, as do both puppeteer harnesses in this repo (test/scripts/browser.js, test/integration/next-pages/test/dev-server-puppeteer.ts).
  • The tests show the same gap. As root, test/js/bun/webview/webview-chrome.test.ts fails 48 of 57 tests, and the workaround is being copied per test instead: two tests on main hardcode argv: ["--no-sandbox"] (webview-chrome-disconnect.test.ts, and the test webview: close the views orphaned by a browser death #39097 added to webview-chrome.test.ts), and webview: make Chrome evaluate() results follow the documented JSON round trip #39085, webview: size the Chrome page viewport with a device metrics override instead of the window #39090 and webview: decode JSON escapes in the Chrome backend's title, url and error messages #39091 each carry a further variant. CI does not see any of this because its agents are not root.

Fix

  • ChromeProcess.rs::spawn() appends --no-sandbox after the fixed flags, before backend.argv, when the target is Linux and geteuid() is 0. --disable-dev-shm-usage joins the fixed flags on every platform (Chrome ignores it where there is no /dev/shm), as in Playwright and puppeteer. These two hunks are the fix; src/sys/lib.rs only adds geteuid next to the existing getuid safe declaration.
  • Why a runtime default is correct: as root, Chrome has exactly one configuration that launches, so the flag removes nothing a root user could have had. It turns a launch that always fails into the launch Playwright gives every user by default (its chromiumSandbox option defaults to false, and this flag set already follows Playwright's, see the comments in spawn()). Every non-root user keeps the sandbox, and the argv they get gains only --disable-dev-shm-usage. A user who already passes either flag gets a duplicate switch, which Chrome accepts.
  • Why only root: Chrome's check also refuses a process whose real and effective ids differ, but whether such a process hands Chrome mixed ids depends on the launcher (the google-chrome launcher script is bash, which makes the ids uniform again), so no fixed rule for that case is right on both paths, and nobody has reported it. Details in the notes. A non-root container that blocks Chrome's sandbox is a separate case the runtime cannot detect; the docs now say to pass argv: ["--no-sandbox"] there.
  • Linux only for --no-sandbox (target_os = "linux" or "android"): the refusal comes from Chrome's Linux-only zygote host (zygote_host_impl_linux.cc, the file named in the error), so there is nothing to add on macOS, and Windows has no uid.
  • The docs flag list (docs/runtime/webview.mdx) and the Backend JSDoc in bun.d.ts state the new defaults (the JSDoc list was already missing half of the flags, so it now carries the full list). The two hardcoded --no-sandbox copies in the tests are removed, since the runtime now supplies the flag.
  • Tests, in test/js/bun/webview/webview-chrome-pipe.test.ts. The fake browser there is bun running fake-chrome-fixture.ts, so the switches the runtime passes show up in its process.execArgv and no browser is needed:
    • "the browser is launched with the documented switches": the exact documented list, plus --no-sandbox exactly when the test process itself is root. Runs on every lane. On the unfixed build it fails everywhere (no --disable-dev-shm-usage); as root it also fails for the missing --no-sandbox.
    • "--no-sandbox is added when bun is root": runs the scenario under unshare -U -r, which makes the unprivileged CI user root inside a user namespace, and asserts the flag literally. It probes first (bun spawning bun inside the namespace) and skips where user namespaces are unavailable. In CI build 101118 it ran and passed on the Debian 13 lanes (the file reports 9 pass, 2 skip there, the 2 being the Windows-only tests) and skipped on Ubuntu 25.04 (8 pass, 3 skip), which restricts unprivileged user namespaces. It also skips in the container used for the work here.
    • The two real-Chrome tests that lost their hardcoded flag, and the untouched rest of webview-chrome.test.ts, are verified by hand as root: bun bd test test/js/bun/webview/ with Google Chrome 151 installed passes in full (48 failures in webview-chrome.test.ts before).
    • The argv handed to the browser, recorded with BUN_CHROME_PATH pointed at a script that dumps "$@": as uid 1500 it is the unfixed list plus --disable-dev-shm-usage, as root it additionally ends in --no-sandbox.
    • Six 1280x720 views with screenshots in this container (64 MB /dev/shm): Chrome dies at the third view without --disable-dev-shm-usage and survives all six with it.
    • bun run rust:check-all: 12 targets ok. test/integration/bun-types/bun-types.test.ts passes.

Background

  • Bun.WebView's Chrome backend spawns one Chrome per process with a fixed flag list and talks to it over --remote-debugging-pipe. backend.argv is appended after that list, and only the first new Bun.WebView() in a process decides the flags, because later views reuse the running Chrome.
  • Chrome's Linux sandbox starts renderer processes through a zygote process. Before it starts the zygote, Chrome checks its own ids and exits when it is root, unless --no-sandbox is passed. Chrome made this a hard exit on purpose (crbug.com/638180), so there is no other flag or environment setting that makes Chrome start as root.
  • /dev/shm is the tmpfs Chrome uses by default for the shared memory that carries rendered tiles between its processes. Docker mounts it with a 64 MB limit unless --shm-size is given. --disable-dev-shm-usage makes Chrome create those files in the temporary directory instead.
  • Auto-detect: with a plain backend: "chrome", Bun first looks for a running Chrome's DevToolsActivePort file and connects to it. If the file is stale, the connection fails and Bun falls back to spawning its own Chrome. That fallback spawn does not carry the options from the constructor call, which is why the flag has to live in the runtime's own list to cover it.
  • A user namespace (unshare -U -r) lets an unprivileged process see itself as uid 0 inside the namespace without gaining any privilege outside it. The runtime's geteuid() returns 0 there, which is what the second test relies on.
Notes

Chrome's full check. ZygoteHostImpl::Init calls sandbox::Credentials::GetRESIds(), which fails when the real, effective and saved uids are not all equal or the gids are not all equal, and exits with the "Running as root" message when that call fails or the uid is 0. Confirmed against Chrome 151 by starting /opt/google/chrome/chrome directly after setresuid/setresgid (with --disable-crash-reporter, because in this container the crash handler otherwise dies first in every non-root state):

uids (real, effective, saved) gids (real, effective, saved) result
0, 0, 0 0, 0, 0 refused
0, 65534, 65534 65534, 65534, 65534 refused
65534, 0, 0 0, 0, 0 refused
1000, 1001, 1001 1000, 1000, 1000 refused (no root involved)
65534, 65534, 65534 0, 65534, 65534 refused (gids mixed)
65534, 65534, 65534 65534, 65534, 65534 passes the check
65534, 65534, 65534 0, 0, 0 passes the check (uniform gid 0 is fine)

Every refused row starts with --no-sandbox added.

Why the mixed-id rows are not handled. The rows only describe a directly executed binary. The first candidates find_chrome() looks for (google-chrome-stable, google-chrome, most distro chromium entries) are shell scripts, and a shell started with differing ids resets the effective ids to the real ones, so through them Chrome sees uniform ids: a process with real uid 0 and a dropped effective uid launches a root Chrome (flag needed), while the mixed non-root rows launch a uniform non-root Chrome that the sandbox check accepts (flag harmful). Passing the flag for mixed ids therefore fixes the direct-binary path and removes a working sandbox on the default path, and not passing it does the reverse. Earlier versions of this PR tried getuid() == 0 || geteuid() == 0 and then Chrome's full rule; with no report of either situation, the PR keeps the rule to the case that is reproduced, and backend.argv remains the way to handle the others.

Earlier versions. The first version changed only webview-chrome.test.ts, threading a root-conditional argv through its launch sites. Self-review pointed out that this was another copy of the per-test workaround and could never cover the argv-less fallback spawn, so the fix moved into the launcher. Later rounds added --disable-dev-shm-usage (the container the docs describe still killed Chrome after two views), widened the uid rule as described above, and then narrowed it back.


no test proof · iteration 1 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/bun/webview/webview-chrome.test.ts, test/js/bun/webview/webview-chrome-pipe.test.ts, test/js/bun/webview/webview-chrome-disconnect.test.ts

@robobun

robobun commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status: ready for a maintainer. The change is a runtime default, so it needs a design sign-off as well as a code review; the PR description makes the case.

Reproduced as root (uid 0) with Google Chrome 151 at /usr/bin/google-chrome: on main, bun bd test test/js/bun/webview/webview-chrome.test.ts fails 48 of 57 tests and Chrome's stderr shows Running as root without --no-sandbox is not supported. The same launch fails for every user of the chrome backend who runs as root, so the launcher (ChromeProcess.rs) now adds --no-sandbox when Bun's effective uid is 0 on Linux, and --disable-dev-shm-usage always (in this 64 MB /dev/shm container Chrome otherwise dies at the third 1280x720 view). The PR description records the versions in between (test-only, then wider uid rules) and why they were dropped.

Verification, head 1b3e0a2 (52def55 rebased onto main after #40165; the only conflict was the harness import line in webview-chrome-pipe.test.ts, resolved by keeping both sides' imports): as root, the webview-chrome-pipe.test.ts switch assertion fails on the unfixed binary and passes on the fixed one, and test/js/bun/webview/ passes in full with the two hardcoded --no-sandbox copies removed. As uid 1500 the argv handed to Chrome gains only --disable-dev-shm-usage. In CI (build 101118) the namespace-based test ran and passed on the Debian 13 lanes and skipped on Ubuntu 25.04, as designed. The build finished with 177 of 179 jobs green, including every lane that runs the changed tests. The two red jobs are test/bake/deinitialization.test.ts on Windows 2019 x64 and test/js/node/tls/node-tls-server.test.ts on macOS aarch64; both fail on main as well and neither touches anything this change affects.

@coderabbitai

coderabbitai Bot commented Aug 18, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 996aadbd-0808-4f36-be72-6fe215b79a1e

📥 Commits

Reviewing files that changed from the base of the PR and between 6cd6fa1 and f176faf.

📒 Files selected for processing (6)
  • docs/runtime/webview.mdx
  • packages/bun-types/bun.d.ts
  • src/runtime/webview/ChromeProcess.rs
  • src/sys/lib.rs
  • test/js/bun/webview/webview-chrome-disconnect.test.ts
  • test/js/bun/webview/webview-chrome-pipe.test.ts

Included review availability: Your plan provides up to 5 included reviews per hour; 1 remains after this review.


Walkthrough

Changes

WebView Chrome launch flags

Layer / File(s) Summary
Chrome launch logic and credential APIs
src/sys/lib.rs, src/runtime/webview/ChromeProcess.rs
The Chrome backend adds --disable-dev-shm-usage and conditionally adds --no-sandbox on Linux and Android. Unix FFI declarations expose effective user and group IDs.
Launch behavior validation
test/js/bun/webview/webview-chrome-pipe.test.ts, test/js/bun/webview/webview-chrome-disconnect.test.ts
Tests verify the documented Chrome switches and automatic --no-sandbox behavior for mismatched user or group IDs.
WebView flag documentation
docs/runtime/webview.mdx, packages/bun-types/bun.d.ts
Documentation lists the shared-memory flag and Linux conditions for automatic --no-sandbox handling.

Possibly related PRs

Suggested reviewers: alii, dylan-conway, jarred-sumner

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly summarizes the main runtime changes for root environments and shared-memory usage.
Description check ✅ Passed The description explains the problem, fix, rationale, implementation, documentation, and extensive verification results.

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

@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.

LGTM — test-only fix that conditionally passes --no-sandbox to Chrome when the test runs as root.

What was reviewed:

  • Confirmed extra_argv is appended after core flags in ChromeProcess.rs:549, so an empty argv: [] on non-root runs leaves Chrome's argv byte-identical to before.
  • Checked that url: false + argv does not trip the connect-mode/spawn-option validation (that guard fires only on a string url).
  • chromeSource interpolation into the bun -e subprocess literals is safe — the object contains only fixed JSON-safe values.
  • process.getuid?.() correctly yields undefined on Windows, leaving the flag off there.
Extended reasoning...

Overview

This PR touches a single test file, test/js/bun/webview/webview-chrome.test.ts. It introduces a chromeArgv array that is ["--no-sandbox"] when process.getuid?.() === 0 and [] otherwise, then threads that array through every Chrome launch site in the file: the shared chrome backend object, the path object-form test, the argv test (spread before --user-agent), and the six bun -e subprocess tests via a new chromeSource = JSON.stringify(chrome) literal. No runtime code is touched.

Security risks

None. This is test-only harness plumbing. --no-sandbox is only added when uid is 0, which is exactly the environment in which Chrome refuses to start without it (crbug.com/638180). Non-root and CI runs keep launching Chrome with the runtime's default flags, sandbox included. The JSON.stringify interpolation into subprocess source contains only fixed string/boolean/array literals — no user-controlled data.

Level of scrutiny

Low. The change is mechanical test infrastructure, mirrors an existing precedent in the sibling webview-chrome-disconnect.test.ts:248, and the PR description verifies (via a BUN_CHROME_PATH argv-dump script) that non-root argv is unchanged. I cross-checked src/runtime/webview/ChromeProcess.rs — extra_argv is null-or-slice, iterated and appended after the fixed flag vector at line 549, so an empty array is a true no-op. The connect-mode validation (url: <string> combined with spawn options) does not apply since url: false selects spawn mode.

Other factors

The argv test still proves what it did before: --user-agent=BunWebViewTest/1.0 must reach Chrome for navigator.userAgent to match, regardless of what precedes it. process.getuid is undefined on Windows, so the optional chain leaves chromeArgv empty there. The bug hunting system found no issues. This is a self-contained improvement to developer ergonomics for root-container testing with zero effect on CI behavior.

@robobun
robobun force-pushed the farm/b8553a14/webview-chrome-tests-as-root branch from 6cd6fa1 to 11e821b Compare August 18, 2026 09:17
@robobun
robobun requested a review from alii as a code owner August 18, 2026 09:17
Comment thread src/runtime/webview/ChromeProcess.rs Resolved
@robobun robobun changed the title webview: make the Chrome tests run as root webview: pass --no-sandbox to Chrome when Bun runs as root on Linux Aug 18, 2026
Comment thread src/runtime/webview/ChromeProcess.rs Resolved
@robobun

robobun commented Aug 18, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 10:54 PM PT - Aug 22nd, 2026

✅ @robobun, your commit 1b3e0a2c9eb2963ea6a135783fad1ff38e7dca0a passed in Build #103938! 🎉


🧪   To try this PR locally:

bunx bun-pr 39490

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

bun-39490 --bun

@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.

I reviewed this PR and didn't find any bugs. The change is small and the reasoning is sound, but it changes a user-facing runtime default (auto-adding --no-sandbox for root) and touches packages/bun-types/bun.d.ts which is CODEOWNER-protected, so a maintainer sign-off would be worthwhile.

What was reviewed:

  • The geteuid() predicate matches Chrome's own check and the safe fn declaration follows the existing getuid/getgid pattern in src/sys/lib.rs.
  • Flag insertion order: --no-sandbox lands before extra_argv, so a user's backend.argv still overrides (Chrome last-wins), and the #[cfg] gate keeps non-Linux argv byte-identical.
  • The new pipe test pins the full documented switch list via process.execArgv on the fake-chrome fixture and correctly branches on process.platform === "linux" && geteuid() === 0.
  • Considered whether disabling the sandbox weakens anything a root user had before — it does not, since Chrome refuses to start at all in that configuration.
Extended reasoning...

Overview

This PR appends --no-sandbox to the Chrome launch argv in src/runtime/webview/ChromeProcess.rs::spawn() when geteuid() == 0 on Linux/Android. Supporting changes: a safe fn geteuid() declaration in src/sys/lib.rs (mirroring the existing getuid), a two-line doc addition in docs/runtime/webview.mdx, a two-line JSDoc addition in packages/bun-types/bun.d.ts, removal of the now-redundant hardcoded --no-sandbox from webview-chrome-disconnect.test.ts, and a new argv-assertion test in webview-chrome-pipe.test.ts.

Security risks

The change disables Chrome's renderer sandbox, but only when the effective uid is 0 — a configuration in which Chrome otherwise hard-exits at startup (crbug.com/638180). So no protection that would have existed is removed; the alternative is no browser at all. Playwright's launcher (which this flag list already follows) defaults chromiumSandbox to false unconditionally, so this is strictly narrower. Non-root users see zero argv change. I don't see an injection, auth, or data-exposure surface here.

Level of scrutiny

Medium. The core logic is 4 lines behind a #[cfg] gate and a uid check, and the geteuid FFI declaration is trivially safe (no args, never fails, same shape as getuid right above it). What raises it above "mechanical" is that it's a runtime default change — the launcher now emits a different argv for a class of users without them asking. The PR argues convincingly that this is the only launchable configuration for that class, and that it also covers the ChromeBackend.cpp fallback-spawn path that user-supplied backend.argv cannot reach. Still, opting the runtime into --no-sandbox by default is the kind of API-surface decision a maintainer should confirm.

Other factors

  • CODEOWNERS: packages/bun-types/bun.d.ts is owned per .github/CODEOWNERS. The change there is JSDoc-only (no type signature changes), but the ownership rule applies.
  • Tests: The new webview-chrome-pipe.test.ts test asserts the exact argv list against the fake-chrome fixture (runs everywhere, no browser needed) and correctly conditions --no-sandbox on process.platform === 'linux' && process.geteuid?.() === 0. The webview-chrome-disconnect.test.ts change turns an existing real-Chrome test into a second regression guard (as root, it now fails without the runtime fix). The PR description reports rust:check-all passing across 12 targets and the full test/js/bun/webview/ suite passing as root.
  • Comment-cop feedback: Both bot comments were addressed (comment shortened to two lines in 7777501) and the threads are resolved.
  • No prior review from me on this PR.

Comment thread src/runtime/webview/ChromeProcess.rs Resolved
Comment thread src/runtime/webview/ChromeProcess.rs Resolved
@robobun robobun changed the title webview: pass --no-sandbox to Chrome when Bun runs as root on Linux webview: make the default Chrome flags work as root and in containers Aug 18, 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.

I reviewed this PR and didn't find any bugs (the earlier isLinux nit was addressed in 21617ab). Because it changes a security-relevant runtime default — passing --no-sandbox to Chrome automatically when Bun runs as root — a maintainer should confirm the design decision.

What was reviewed:

  • The argv change in ChromeProcess.rs: --no-sandbox is gated on Linux/Android + getuid()==0 || geteuid()==0, inserted before user argv so users can still override; non-root users only gain --disable-dev-shm-usage.
  • src/sys/lib.rs: geteuid declared exactly like the existing getuid (safe fn, #[cfg(unix)]).
  • The new webview-chrome-pipe.test.ts assertions match the documented flag list and the rootOnLinux predicate mirrors the Rust condition; the seteuid(65534) test correctly runs only when the harness process is real-root on Linux.
  • Docs (webview.mdx) and bun.d.ts JSDoc list match the argv vector in spawn().
Extended reasoning...

Overview

The PR appends two switches to the argv ChromeProcess.rs::spawn() builds for the Chrome subprocess: --disable-dev-shm-usage unconditionally, and --no-sandbox on Linux/Android when either the real or effective uid is 0. src/sys/lib.rs adds a safe fn geteuid() declaration next to the existing getuid(). Docs and the Backend JSDoc are updated to list the full flag set, a hardcoded argv: ["--no-sandbox"] is removed from webview-chrome-disconnect.test.ts, and two new tests in webview-chrome-pipe.test.ts assert the exact argv the runtime hands to the (fake) browser.

Security risks

The change disables Chrome's renderer sandbox when Bun runs as root. The reasoning is well-argued: Chrome hard-exits as root with the sandbox on (crbug.com/638180), so root users get nothing today, and Playwright's chromiumSandbox defaults to false for everyone. Non-root users are unaffected — they only pick up --disable-dev-shm-usage, which is inert outside containers with a small /dev/shm. Still, "the runtime silently turns off a browser sandbox based on uid" is exactly the kind of default a maintainer should sign off on rather than have auto-approved.

Level of scrutiny

The Rust diff is ~15 lines and mechanically simple: a c"..." literal pushed into an existing Vec<*const c_char>, and a #[cfg]-gated if that reads two libc functions already declared as safe fn. No memory-safety concerns (all string literals are 'static). The #[cfg(any(target_os = "linux", target_os = "android"))] on both the use bun_core::ZStr import and the uid check keeps other platforms compiling cleanly (the author reports rust:check-all passes 12 targets). What warrants human review is not the implementation but the product decision to change a default.

Other factors

The PR description is unusually thorough — it documents why both uids are checked (bash launcher scripts reset euid to ruid), why the fix belongs in the launcher rather than per-test (the auto-detect fallback spawn ignores backend.argv), and cites Playwright/puppeteer precedent plus two in-repo puppeteer harnesses that already pass --disable-dev-shm-usage. The new tests use the fake-chrome fixture so the argv assertion runs on every CI lane without a real browser, and the author verified fail-before/pass-after both as root and as uid 1500. The comment-cop bot fired on the inline comments; the author shortened them and the remaining ones follow the file's existing per-switch comment pattern. My only prior finding (the local isLinux redefinition) was fixed in 21617ab.

Comment thread src/runtime/webview/ChromeProcess.rs Resolved

@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.

I reviewed this PR and didn't find any bugs (the earlier isLinux nit was addressed in 21617ab). Because it changes a security-relevant default — auto-passing --no-sandbox to Chrome based on the process's uid/gid state — a human should confirm the policy choice before merge.

What was reviewed: the three-term uid/gid condition against Chrome's ZygoteHostImpl::Init/GetRESIds semantics; that --no-sandbox is appended before user argv so users can still override; that the new geteuid/getegid decls sit under #[cfg(unix)] alongside the existing getuid/getgid; that sandboxRefused() and asRoot short-circuit on hasZygoteCheck so process.getuid!() never runs on Windows/macOS; and that isAndroid exists in the harness.

Extended reasoning...

Overview

This PR changes the default Chrome launch flags in Bun.WebView's Chrome backend so it works out of the box when Bun runs as root (the common container case) and in Docker's default 64 MB /dev/shm. It touches:

  • src/runtime/webview/ChromeProcess.rs — appends --disable-dev-shm-usage unconditionally and --no-sandbox on Linux/Android when geteuid()==0 || getuid()!=geteuid() || getgid()!=getegid().
  • src/sys/lib.rs — adds geteuid/getegid safe fn declarations next to the existing getuid/getgid under #[cfg(unix)].
  • docs/runtime/webview.mdx and packages/bun-types/bun.d.ts — document the new defaults.
  • test/js/bun/webview/webview-chrome-pipe.test.ts — three new tests asserting the exact argv against a fake-Chrome fixture, one per term of the condition.
  • test/js/bun/webview/webview-chrome-disconnect.test.ts — removes the now-redundant hardcoded argv: ["--no-sandbox"].

Security risks

The core change disables Chrome's renderer sandbox when Bun is root or has mixed real/effective ids. The PR's reasoning is sound — Chrome hard-exits in exactly those states without the flag, so no security is lost relative to "doesn't launch at all", and Playwright ships --no-sandbox for every user by default. Non-root users keep the sandbox; the only argv change they see is --disable-dev-shm-usage. Still, this is a change to the default security posture of a user-facing API, and there's a defensible alternative position (fail loudly and require explicit opt-in via backend.argv). That policy call belongs to a maintainer.

Level of scrutiny

Medium-high. The Rust change is small and mechanical (two hunks in spawn(), two new safe FFI decls), and the condition is derived directly from Chrome's own ZygoteHostImpl::Init check with an empirical matrix in the PR description. But "automatically disable the browser sandbox" is the kind of default a human should sign off on.

Other factors

  • My prior review left one nit (isLinux redefined locally instead of imported from harness); it was fixed in 21617ab and the import now includes isAndroid and isLinux from harness.
  • The comment-cop bot repeatedly flagged the two-line comments above the new flags; the author's responses (each switch in that vector already carries a short why-comment, and the three-term condition needs the ZygoteHostImpl pointer to be verifiable) are consistent with the file's existing style and were resolved.
  • Test coverage is thorough: the fake-Chrome fixture asserts the exact argv on every platform, plus two root-only Linux tests that isolate each remaining term of the condition, plus the real-Chrome disconnect test now depending on the runtime-supplied flag.
  • I checked that sandboxRefused() and the asRoot gate both short-circuit on hasZygoteCheck = isLinux || isAndroid before touching process.getuid!(), so the test file loads on Windows/macOS where those functions are undefined.

@robobun
robobun force-pushed the farm/b8553a14/webview-chrome-tests-as-root branch from 6a5184b to 52def55 Compare August 19, 2026 07:23
Comment thread src/runtime/webview/ChromeProcess.rs Resolved
@robobun robobun changed the title webview: make the default Chrome flags work as root and in containers webview: pass --no-sandbox to Chrome when Bun is root, and --disable-dev-shm-usage always Aug 19, 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.

LGTM — the earlier nit (import isLinux from harness) was addressed.

What was reviewed:

  • The two-flag addition in ChromeProcess.rs: --disable-dev-shm-usage unconditionally, --no-sandbox only on Linux/Android when geteuid() == 0 — placed before user argv so last-wins override still works.
  • geteuid declaration in src/sys/lib.rs follows the existing getuid/getgid pattern under #[cfg(unix)].
  • New pipe tests: process.geteuid!() is guarded by hasRootCheck short-circuit so it's never called on Windows; isAndroid/isLinux both exist in harness; userNamespaceWorks() probe wraps spawnSync in try/catch for missing unshare.
  • The two removed argv: ["--no-sandbox"] test workarounds are now covered by the runtime default.
Extended reasoning...

Overview

This PR makes Bun.WebView({ backend: "chrome" }) work in root containers by appending two Chrome launch flags in src/runtime/webview/ChromeProcess.rs::spawn(): --disable-dev-shm-usage always (Docker's 64 MB /dev/shm otherwise kills Chrome after a few tabs), and --no-sandbox on Linux/Android when geteuid() == 0 (Chrome hard-exits as root otherwise, per crbug.com/638180). src/sys/lib.rs gains a one-line geteuid safe FFI declaration next to the existing getuid. Docs (webview.mdx) and JSDoc (bun.d.ts) are updated to list the new defaults. Two tests that hardcoded --no-sandbox drop it, and webview-chrome-pipe.test.ts gains two new assertions that verify the argv via the existing fake-chrome fixture (one always, one under unshare -U -r to cover the root path on non-root CI).

Security risks

--no-sandbox disables Chrome's renderer sandbox, which is a security-relevant flag. The mitigation is that the condition is narrowly scoped: it only fires when geteuid() == 0 on Linux/Android, and in that state Chrome refuses to start at all without the flag — so no working sandbox is being removed. Non-root users keep the sandbox; the only argv change they see is --disable-dev-shm-usage, which redirects shared-memory files to tmpdir and is the default in both Playwright and puppeteer. The flag is inserted before user-supplied argv, so Chrome's last-wins duplicate-switch resolution lets users override either default.

Level of scrutiny

Medium-low. The native change is ~10 lines and follows the file's established pattern (every flag in the vector has a short justifying comment). The geteuid addition is a mechanical mirror of the neighboring getuid declaration under the same #[cfg(unix)] gate. The behavior matches what Playwright ships to every user by default. The PR description is unusually thorough (includes a table of Chrome's uid/gid check states verified against Chrome 151, and records why earlier wider conditions were narrowed back to euid-only).

Other factors

  • CI is green (build 100673, 179/179).
  • My prior review left one nit (locally redefined isLinux), which was fixed in 21617ab — verified in the current diff.
  • The one unresolved thread is the automated comment-cop bot's fifth identical "paragraph-long comment" flag on the two-line --no-sandbox comment; the author has rebutted this same heuristic four times with sound reasoning (the comment matches the file's per-flag convention and cites the upstream bug), and the current text is indeed two short lines. Not a substantive blocker.
  • Test design is solid: the fake-chrome fixture lets argv be asserted exactly on every platform without a browser; the unshare -U -r probe correctly falls back to test.skip when user namespaces are unavailable; process.geteuid!() is only reached after the hasRootCheck short-circuit so Windows never evaluates it.

…dev-shm-usage always

Chrome exits at startup when it runs as root with its sandbox on
(crbug.com/638180), so the chrome backend never worked as root, which
is how Bun usually runs inside a container. backend.argv can add the
flag for an explicit spawn, but not for the spawn ChromeBackend.cpp
falls back to when a stale DevToolsActivePort file fails to connect,
which passes no argv. spawn() in ChromeProcess.rs now appends
--no-sandbox when geteuid() is 0, on Linux only.

Docker gives /dev/shm 64MB, which Chrome fills after two 1280x720 tabs
and then dies. --disable-dev-shm-usage joins the fixed flags, as in
Playwright and puppeteer. The docs and the Backend JSDoc list both.

webview-chrome-pipe.test.ts asserts the switches the fake browser is
started with: the documented list, plus --no-sandbox when the test
itself runs as root, and a second test that runs the scenario under
unshare -U -r so the flag is also asserted on CI lanes that allow
unprivileged user namespaces. The two tests that hardcoded the flag
now rely on the runtime; with this, test/js/bun/webview/ passes as root
with a real Chrome installed (48 failures in webview-chrome.test.ts
before).
@robobun
robobun force-pushed the farm/b8553a14/webview-chrome-tests-as-root branch from 52def55 to 1b3e0a2 Compare August 23, 2026 05:36

@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.

I reviewed this PR and didn't find any bugs. Since it changes a user-facing runtime default and conditionally disables Chrome's sandbox, a maintainer sign-off on the design is still warranted (as the author already noted).

What was reviewed:

  • The geteuid() addition in src/sys/lib.rs follows the existing getuid/getgid pattern and is correctly #[cfg(unix)]-gated for the Linux/Android call site.
  • Flag placement in spawn() — --no-sandbox lands after the fixed switches and before user argv, so a user can still override it; --disable-dev-shm-usage matches what Playwright/puppeteer and this repo's own puppeteer harnesses already pass.
  • The new pipe tests: process.geteuid!() is guarded by hasRootCheck so it never runs on Windows/macOS, and the unshare -U -r probe skips cleanly where user namespaces are unavailable.
  • The two removed hardcoded argv: ["--no-sandbox"] sites are now covered by the runtime default when root; non-root runs of those tests were already unaffected.
Extended reasoning...

Overview

This PR changes the default Chrome launch flags in Bun.WebView's Chrome backend (src/runtime/webview/ChromeProcess.rs): it always passes --disable-dev-shm-usage, and on Linux/Android it additionally passes --no-sandbox when Bun's effective uid is 0. Supporting changes: a safe geteuid() declaration in src/sys/lib.rs, updated docs (docs/runtime/webview.mdx) and JSDoc (packages/bun-types/bun.d.ts), two new tests in webview-chrome-pipe.test.ts that assert the exact argv via the fake-Chrome fixture, and removal of two now-redundant hardcoded --no-sandbox copies from existing tests.

Security risks

The change disables Chrome's renderer sandbox — but only when Bun is already root, where Chrome refuses to start with the sandbox enabled anyway (crbug.com/638180). So the flag turns a guaranteed launch failure into the same configuration Playwright ships by default; it does not remove a sandbox any root user could have had. Non-root users keep the sandbox and only gain --disable-dev-shm-usage, which redirects shared-memory files to the temp dir with no security implication. There is no injection or auth surface here — argv construction uses C string literals and the existing user-argv append path is unchanged.

Level of scrutiny

Medium-high. The code change is small and mechanically simple (one flag pushed unconditionally, one behind a two-line cfg-gated uid check), but it is a runtime default change to user-facing API behavior, and it touches a security-adjacent knob (browser sandboxing). The PR author explicitly flagged that this needs design sign-off from a maintainer, and REVIEW.md's API-design guidance applies. This is exactly the kind of decision — "is auto-disabling the sandbox for root the right default for Bun" — that a human maintainer should weigh in on, even though the implementation itself is sound.

Other factors

  • All prior review threads are resolved: the comment-cop bot's repeated "paragraph-long comment" flags were addressed/rebutted, and my earlier nit about importing isLinux from harness was fixed in 21617ab.
  • Test coverage is thorough: the new "documented switches" test asserts the exact argv on every lane (fails on unfixed builds for the missing --disable-dev-shm-usage), and the unshare-based test covers the root path on lanes that support user namespaces. CI build 101118 confirms the namespace test ran on Debian 13 and skipped on Ubuntu 25.04 as designed.
  • The PR description is unusually detailed about alternatives considered (wider uid rules, mixed-uid handling via bash launcher scripts) and why they were dropped — this is exactly the record a maintainer needs to evaluate the design choice.

@robobun

robobun commented Nov 17, 2026

Copy link
Copy Markdown
Collaborator Author

Closing as stale: this PR conflicts with main, and its last commit is from 2026-08-23.

The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR.

@robobun robobun closed this Nov 17, 2026
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.

1 participant