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+162+012345678901234567890123456789-26-26-01234567890123456789
What
- When
app.appPathnames 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.appPaththat names an.asararchive 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 withPOLICY_DENIEDwhen 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 atout/renderer/index.htmlwas outside the boundary, soobserveandapp.screenshot()refused the app's own window. appDirectorytook the first document fromthis.windows.current(), which can be a window that has not loaded.screenshotFilewas given no way to check the boundary after the capture.placeMasksstopped every animation and played each one again after the capture, also one the app had paused.
How it works
- 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.
setUpreturns only the path the main process reports (packages/electron/src/app-process.ts:173,app-process.ts:194). The launch passes the project root toappDirectory. appDirectory(packages/electron/src/file-bound.ts:34): for a declared file that is not an.asararchive, the boundary ispackageOf(script), else the script's directory. Otherwise the order is as before: what the main process reports, the declared path, the first local document.packageOf(file-bound.ts:67) walks up from the script to the project root and stops at the firstpackage.jsonthat 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.screenshotFile(packages/electron/src/capture.ts:124) takes arefusedcallback. It calls it before the capture and records the page's document generation. After the capture, it refuses when the generation changed and callsrefusedagain.ElectronSurface.screenshotpasses() => this.leftApp(window)(packages/electron/src/surface.ts:520).placeMasksskips an animation whoseplayStateispaused(packages/electron/src/in-page/masks.ts:37).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 testandtest:integrationon 8a97effd9, this head beforemainwas merged in, and the electron testbed on this layer before the review fixes (CI'selectronjob 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):
| Test | PR below | this PR |
|---|---|---|
file-bound.test.ts: is the package a declared main script is in, past a manifest that only sets the module type | failed | passed |
launch.test.ts: writes no screenshot of a window that left the app while it was captured | failed | passed |
launch.test.ts: writes no screenshot when the window loaded another document while it was captured | failed | passed |
node-ops.test.ts: leaves an animation the app paused paused, and plays an endless one again after the capture | failed | passed |
launch.test.ts: takes its own directory from the window the launch ends on, when the splash it waited for closes during setup | failed | passed |
-
The
.asarand "no higher than the project" tests infile-bound.test.tspass on both. They guard the package walk this PR adds against widening the boundary. -
Behavior and timing against
main: see thepnpm bench:abtable in #2423. -
No changeset:
@e2e-dev/electronis 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.