fix(electron): bound a main-script app by its package, and keep no screenshot of a window that left it (ENG-892)

Merged
KD
19h ago
+162-26

What

  • When app.appPath names a main script (out/main/index.js), the local file boundary is the script's package inside the project, not the script's own directory. A build that puts its pages beside the script (out/renderer/) is in the app. A manifest that sets only "type" does not count as a package, and no manifest above the project root counts.
  • An app.appPath that names an .asar archive keeps the archive as the boundary.
  • The boundary comes from the window the launch waited for, not from the first window registered, which can be a splash screen or a blank page.
  • app.screenshot() checks the boundary before and after the capture, and refuses with POLICY_DENIED when the window loaded another document during the capture. Before, it checked only before, so a window that navigated out of the app during the capture was written to disk.
  • A screenshot leaves an animation the app paused paused. Before, the capture played it again.

Why

The fourth PR of the stack for the review of #2237 (ENG-892), which added @e2e-dev/electron. These are defects in the local file boundary and in screenshots, which AGENTS.md lists as a security invariant: a window that shows a local file outside the app is not observed, traced, or captured.

  • Electron's app.getAppPath() for a main script is the script's directory. With an electron-vite layout, the app's own renderer page at out/renderer/index.html was outside the boundary, so observe and app.screenshot() refused the app's own window.
  • appDirectory took the first document from this.windows.current(), which can be a window that has not loaded.
  • screenshotFile was given no way to check the boundary after the capture.
  • placeMasks stopped every animation and played each one again after the capture, also one the app had paused.

How it works

  1. After setup, the launch takes its directory from the window it ends on, which need not be the first registered (a splash or a blank page), nor the window it waited for, if that one closed during setup. That window's URL is the first document. setUp returns only the path the main process reports (packages/electron/src/app-process.ts:173, app-process.ts:194). The launch passes the project root to appDirectory.
  2. appDirectory (packages/electron/src/file-bound.ts:34): for a declared file that is not an .asar archive, the boundary is packageOf(script), else the script's directory. Otherwise the order is as before: what the main process reports, the declared path, the first local document.
  3. packageOf (file-bound.ts:67) walks up from the script to the project root and stops at the first package.json that is not only {"type": ...} (onlyModuleType, file-bound.ts:78). Build tools write such a manifest into their output. A manifest above the project, such as a monorepo's or a home directory's, is not the app's.
  4. screenshotFile (packages/electron/src/capture.ts:124) takes a refused callback. It calls it before the capture and records the page's document generation. After the capture, it refuses when the generation changed and calls refused again. ElectronSurface.screenshot passes () => this.leftApp(window) (packages/electron/src/surface.ts:520).
  5. placeMasks skips an animation whose playState is paused (packages/electron/src/in-page/masks.ts:37).
  6. docs/electron.mdx (Security, local file boundary) says what the boundary is for a main script.

Issue

Part of ENG-892. Stack: #2419 → #2420 → #2421 → this PR → #2423.

Verified

Ran it locally: yes

  • pnpm --filter @e2e-dev/electron test and test:integration on 8a97effd9, this head before main was merged in, and the electron testbed on this layer before the review fixes (CI's electron job runs it on this head):
@e2e-dev/electron unit + integration: Tests  158 passed (158)
apps/electron-testbed:                Tests  75 passed (75)
  • The new tests of this PR on the code of the PR below it (the bug) and on this PR (the fix):
TestPR belowthis PR
file-bound.test.ts: is the package a declared main script is in, past a manifest that only sets the module typefailedpassed
launch.test.ts: writes no screenshot of a window that left the app while it was capturedfailedpassed
launch.test.ts: writes no screenshot when the window loaded another document while it was capturedfailedpassed
node-ops.test.ts: leaves an animation the app paused paused, and plays an endless one again after the capturefailedpassed
launch.test.ts: takes its own directory from the window the launch ends on, when the splash it waited for closes during setupfailedpassed
  • The .asar and "no higher than the project" tests in file-bound.test.ts pass on both. They guard the package walk this PR adds against widening the boundary.

  • Behavior and timing against main: see the pnpm bench:ab table in #2423.

  • No changeset: @e2e-dev/electron is not released yet (0.0.0, not on npm).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Screenshots are withheld when the app leaves its allowed local-file boundary or loads a different document during capture; refused captures do not write a screenshot.
    • The app’s package is identified more accurately when launched from a main script, while package lookup stays within the project.
    • Applying visual masks preserves animations that were already paused, while running animations retain their play state.
    • Downloads receive unique numbered names when they collide with existing files, including case-insensitive matches, and numbering continues after an app restart.
  • Documentation
    • Clarified how the local-file boundary is determined when the app launches from a main script, and how download naming and early downloads work.