Skip to content

feat: engage the macOS helper sandbox before framework code runs - #55962

Open
claude[bot] wants to merge 4 commits into
mainfrom
claude/issue-54663-aperitif-helpers
Open

claude[bot] wants to merge 4 commits into
mainfrom
claude/issue-54663-aperitif-helpers

Conversation

@claude

@claude claude Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Requested by John Kleinschmidt · Slack thread

Description of Change

Fixes #54663.

On macOS, helper processes (renderers, GPU, utility) used to engage the sandbox from main(). By the time main() runs, dyld has already loaded Electron Framework and every system library it links, and run all of their static initializers, unsandboxed. In a renderer, which handles untrusted web content, anything those initializers acquire (open files, Mach ports, connections to system services) stays available after the sandbox is on.

This change adopts the layout Chromium moved to in crbug.com/40794783: the sandbox is now engaged before any code other than libSystem runs.

  • New libelectron_aperitif.dylib. Bundled in the framework's Libraries directory. Its static initializer registers PartitionAlloc's early malloc zone, repairs closed stdio file descriptors, and then, in helpers launched with seatbelt arguments, engages the sandbox using Chromium's seatbelt code. Mac App Store builds use it only for the malloc zone and stdio steps.
  • Plain C entry point. The main and helper executables are now built from shell/app/electron_main_mac.c, which links only libelectron_aperitif, Electron Framework and libSystem, in that order, so the library initializes first. The run-as-node and helper --type handling moved from the old C++ launcher into the framework (ElectronExecutableMain()).
  • Early malloc zone registration. Electron's executables never called EarlyMallocZoneRegistration(), which PartitionAlloc expects to run before the framework's allocator initializes. It now runs once in every process, as it does in Chrome.
  • Build check. The build verifies that each executable loads exactly those three libraries in that order, and that libelectron_aperitif links only libSystem and libsandbox. A dependency added there later would run before the sandbox, so it fails the build.
  • Spec. A new spec launches an app with DYLD_PRINT_INITIALIZERS=1 and checks that libelectron_aperitif initializes before everything except libSystem in the browser and in a sandboxed renderer.

Size and performance. The helper executables no longer carry their own copy of libc++ and the seatbelt code, so each shrinks from about 214 KB to about 27 KB, roughly 0.7 MB less per architecture after adding the new dylib. Chromium's startup and memory gains from this project came from loading its framework at launch instead of with dlopen(); Electron's helpers already did that, and startup time and browser and renderer memory are unchanged. The GPU process and network service each use about 0.7 MiB more: they compile their sandbox policy before PartitionAlloc is installed, so the policy compiler's temporary buffers come from system malloc, which keeps one freed block resident. The library returns the rest of those pages after the sandbox is engaged.

App developers who sign or package Electron themselves should make sure the new dylib in Electron Framework.framework/Libraries/ is signed along with the rest of the framework.

Checklist

🤖 Generated with Claude Code

https://claude.ai/code/session_01NayLZiGwUtmxxg3gkkqg5P

Release Notes

Notes: Shrank macOS helper executables and sandboxed them before framework code runs.

Link the non-MAS helper apps against Chromium's libaperitif.dylib, which
starts the seatbelt sandbox from its own static initializer, and remove
the SeatbeltExecServer block from the helper's main(). libaperitif is
linked before Electron Framework, so the sandbox is on before any code
from the framework or from system libraries other than libSystem runs.

- Link the framework's shared library (`+link_nested`) after
  libaperitif, since `-framework` entries are placed before all shared
  libraries by the Apple link rule.
- Rewrite libaperitif's install name, which assumes Chrome's bundle
  layout, to the framework's Libraries directory, and bundle the dylib
  there.
- Verify that each helper loads exactly libaperitif, Electron Framework
  and libSystem, in that order, as Chrome does.
- Add libaperitif.dylib to the macOS dist zip manifests.

MAS builds are unchanged.

Refs #54663

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NayLZiGwUtmxxg3gkkqg5P
@release-clerk

release-clerk Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Release note looks good now.

@claude

claude Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

CI note on the macos-arm64 / test / test (mas, 1) failure (job): I don't think this PR causes it.

What fails: one test, api-web-contents.spec.ts:3488 > webContents module > setImageAnimationPolicy() > animates images by default. It fails with AssertionError: expected [ 'red' ] to deeply equal ArrayContaining ["red", "green"] after about 10 s. The visible 200×200 window only ever captured the first frame of the animated image (red), never the green one. The other 1840 tests in the shard passed. That includes the next test, stops images from animating when set to "noAnimation". node.spec.ts, which has this PR's new spec, isn't in this shard.

Why it isn't this PR:

  • The PR doesn't change MAS binaries. Every change in BUILD.gn is gated on electron_helpers_use_aperitif = !is_mas_build, so MAS helpers keep the same deps (:electron_framework_link, generate_mas_config), the same rpaths and the same framework Libraries contents. Every code change in shell/app/electron_main_mac.cc is inside #if defined(HELPER_EXECUTABLE) && !IS_MAS_BUILD(). On MAS the only difference left is two #includes that were dropped.
  • The failing test samples capturePage() pixels from a shown window to see an animation advance. It doesn't touch helper startup, sandboxing or ELECTRON_RUN_AS_NODE. It is new: it landed on main yesterday in test: cover webContents editing commands, device emulation and menu roles #55903 (34f7c90). Its file runs with retry: 0, so a single timing miss on the runner fails the shard.
  • The same test passed in every other macOS job of this run: macos-x64 (mas, 1) and the darwin shards on both arches. Those darwin shards are the configurations where this PR does change the helpers.
  • I didn't find this exact failure on main or on other branches since test: cover webContents editing commands, device emulation and menu roles #55903 landed. I checked every failing macOS test job in Build runs since then: main run 44477664561 failed on navigator.mediaDevices tests and runs 44394934382 and 44367759620 failed on media-handler tests, but this test passed in each of them (for example 559 ms and 302 ms). So I'm reading this as an intermittent failure in a one-day-old test on this runner, not a regression. Re-running the shard should confirm that.

All other macOS jobs are green. That covers macos-arm64 and macos-x64 build (which includes the new verify_libraries_electron_helper_app_* actions on the darwin build), gn-check, clang-tidy, test (darwin, 1/2) on arm64, test (darwin, 1) and test (mas, 1) on x64. node.spec.ts, including the new ELECTRON_RUN_AS_NODE helper spec, ran and passed in macos-arm64 (darwin, 1) and macos-x64 (darwin, 1).


Generated by Claude Code

Address review feedback on the Apéritif helpers change:

- Replace the C++ launcher with a plain C entry point
  (shell/app/electron_main_mac.c) for the main and helper executables. It
  checks that Apéritif initialized and calls a new framework export,
  ElectronExecutableMain(), which now holds the run-as-node and helper
  --type handling.
- Build Electron's own libelectron_aperitif.dylib with an Electron install
  name instead of rewriting Chromium's libaperitif. Its static initializer
  registers PartitionAlloc's early malloc zone once, repairs stdio, and then
  engages the Seatbelt sandbox using Chromium's seatbelt code. Executables
  link it before Electron Framework via +link_nested.
- MAS builds link the library too, for early malloc zone registration, but
  without Seatbelt.
- Verify that the main and helper executables load exactly
  libelectron_aperitif, Electron Framework and libSystem, in that order, and
  that the library links only libSystem and libsandbox.
- Add a spec that launches a sandboxed window with DYLD_PRINT_INITIALIZERS=1
  and checks that only libSystem initializes before libelectron_aperitif in
  the browser and renderer processes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@claude

claude Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

CI failure on 64d33b0: macos-arm64 / test / test (mas, 1) (job). This looks like a flaky test, not a problem with this PR.

The only failure (1 failed, 1841 passed):

FAIL  api-web-contents.spec.ts:2645 > webContents module > DevTools embedder messages > reattach > gives the frontend a fresh session on the inspected page
Error: waitUntil timed out after 10000ms
 ❯ api-web-contents.spec.ts:2655:9

Line 2655 is the first await waitUntil(() => isPrintMedia(w)). It runs right after the test sends Emulation.setEmulatedMedia { media: 'print' } through the DevTools frontend's protocol connection, and it timed out because the page never reported print media. The test failed before it reached reattach.

Why I don't think this PR causes it:

  • The test and its suite are new. They were added on main in test: add coverage for DevTools embedder messages #55932 (6d1696b) on Nov 18.
  • The same suite already flakes on main on the same configuration. In main Build run 44628191769, macos-arm64 / test / test (mas, 2) (job 135952866564) failed on DevTools embedder messages > preferences > keeps preferences for later DevTools windows in the same session only with Test timed out in 30000ms.
  • This test passed on this PR's previous head (31f9be8) on arm64 MAS in both attempts: 3642ms in job 135761875643 and 2209ms in job 135804241115.
  • What this commit changes on MAS is how the process starts: the C entry point, libelectron_aperitif.dylib and malloc zone registration. CDP emulation runs over the DevTools frontend's connection inside an already-running browser and renderer, and nothing in this commit touches that path. Every other DevTools embedder-message test in this shard passed (loadNetworkResource, openInNewTab, preferences, searchInPath), and so did the DevTools native integration tests. The one spec that exercises the new startup path, aperitif.spec.ts, did not run in this shard.
  • setImageAnimationPolicy() > animates images by default, which flaked here on the previous head, passed this time.

No code change is needed for this PR. Re-running the shard should clear it.


Generated by Claude Code

@claude

claude Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

CI triage for run 44778206912 (head 64d33b0): the two test failures look unrelated to this PR.

windows-x64 / test (win, 1) (job): one failure out of 2588 passing tests.

FAIL api-web-contents.spec.ts:2550 > webContents module > DevTools embedder messages > preferences > keeps preferences for later DevTools windows in the same session only
Error: Test timed out in 30000ms.

The tests just before it in the same block ran very slowly too: sets, reads and removes a preference took 29146ms and clears every stored preference took 10004ms.

macos-arm64 / test (mas, 1) (job):

FAIL api-web-contents.spec.ts:2645 > webContents module > DevTools embedder messages > reattach > gives the frontend a fresh session on the inspected page
Error: waitUntil timed out after 10000ms

Why I think they're unrelated:

  • Both tests come from the DevTools embedder message coverage added on main in test: add coverage for DevTools embedder messages #55932 (6d1696b, Nov 18). This PR doesn't touch spec/api-web-contents.spec.ts or any DevTools code.
  • The same preferences test fails the same way on main: Build run 44628191769 at b3afd52, in macos-arm64 / test (mas, 2), with api-web-contents.spec.ts:2550 ... Test timed out in 30000ms.
  • The Windows failure can't come from this PR's changes. Everything the PR adds or changes is macOS-only: libelectron_aperitif, electron_main_mac.c, the run-as-node and helper --type handling in electron_library_main.mm, and the mac zip manifests. aperitif.spec.ts is gated to darwin, and the win shard skipped it (aperitif.spec.ts (1 test | 1 skipped)). The other Windows shards passed: windows-x64 (win, 2) and windows-arm64 (win, 1).
  • The previous head 31f9be8 (run 44576399955) passed every test shard.

The shards that exercise this PR's changes passed or are still running: macos-arm64 (darwin, 1) and macos-x64 (mas, 1) passed, and macos-arm64 (darwin, 2), macos-x64 (darwin, 1) and windows-arm64 (win, 2) were still running when I wrote this. A rerun of the two failed shards should be enough.


Generated by Claude Code

…licy

GPU and network service helpers compile their Seatbelt policy in
libelectron_aperitif's initializer, before PartitionAlloc is installed. The
compiler's temporary buffers therefore come from the system malloc zone, which
keeps the freed pages resident. Call malloc_zone_pressure_relief() after the
sandbox is engaged to return them, which recovers about 0.3 MiB in each of
those processes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jkleinsc jkleinsc changed the title build: start the macOS helper sandbox from libaperitif build: engage the macOS helper sandbox before framework code runs Nov 20, 2026
@jkleinsc jkleinsc added semver/minor backwards-compatible functionality and removed semver/none labels Nov 20, 2026
@jkleinsc jkleinsc changed the title build: engage the macOS helper sandbox before framework code runs feat: engage the macOS helper sandbox before framework code runs Nov 20, 2026
@jkleinsc
jkleinsc marked this pull request as ready for review November 23, 2026 16:10
@jkleinsc
jkleinsc requested a review from a team as a code owner November 23, 2026 16:10

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Code review found no issues

No high-confidence issues detected in this change.

@deepak1556 deepak1556 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@deepak1556

Copy link
Copy Markdown
Member

Can this be changed to Fixes instead of Refs https://github.com/electron/electron/issues/54663., whats pending after this lands ?

@jkleinsc jkleinsc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

API LGTM

@jkleinsc jkleinsc added target/46-x-y PR should also be added to the "46-x-y" branch. no-backport and removed target/46-x-y PR should also be added to the "46-x-y" branch. labels Nov 24, 2026

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.

Explore Aperitif helpers on macOS

3 participants