Repository navigation
fix(integrations): save facade screenshot artifacts only as images in a dedicated directory - #3350
Conversation
… a dedicated directory Run screenshots are now written only as PNG/JPEG files, under ./stagehand-artifacts by default (STAGEHAND_FACADE_ARTIFACT_ROOT for the stdio server and pi), and an existing file is replaced by rename rather than written through. Same in the Python host. stagehand-middleware 0.1.2 (npm + PyPI).
…t where screenshots land
🦋 Changeset detectedLatest commit: 33d6c99 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
2 issues found across 40 files
Confidence score: 2/5
- In
packages/integrations/core/python/src/stagehand_middleware/facade/tools.py, a concurrent writer can replace a checked parent directory with a symlink, letting writes escaperoot. Traverse from pinned directory handles with no-follow checks. - In
packages/integrations/core/src/facade/tools.ts, replacing a checked parent with a symlink can redirect writes outsideartifactRoot. Use no-follow directory-handle traversal here too.
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="packages/integrations/core/python/src/stagehand_middleware/facade/tools.py">
<violation number="1" location="packages/integrations/core/python/src/stagehand_middleware/facade/tools.py:378">
P1: A concurrent writer can swap a checked parent directory for a symlink before the path-based `os.open` or `os.replace`; `O_NOFOLLOW` protects only the temporary basename, allowing writes outside `root`. Pin checked directories with `dir_fd` operations and use relative opens and renames.
(Based on your team's feedback about constraining screenshot artifact writes.) .</violation>
</file>
<file name="packages/integrations/core/src/facade/tools.ts">
<violation number="1" location="packages/integrations/core/src/facade/tools.ts:485">
P2: Another process with write access can replace a checked parent with a symlink before this path-based `open`; `O_NOFOLLOW` protects only the temp basename, allowing writes outside `artifactRoot`. Traverse through no-follow directory handles so each parent check remains tied to the write.
(Based on your team's feedback about constraining screenshot artifact writes.)</violation>
</file>
Architecture diagram
sequenceDiagram
participant Agent as AI Agent
participant Host as Host Process (TS/Python)
participant Facade as StagehandFacadeTools
participant SW as Extension Service Worker
participant FS as File System
Note over Agent,FS: Facade Screenshot Artifact Flow
Agent->>Host: run tool with code or actions
Host->>SW: Execute code (page.screenshot({ path }))
alt Valid image extension (.png/.jpg/.jpeg)
SW-->>Host: Run result with artifacts [{path, base64}]
Host->>Facade: Verify limits (count, size, total)
Facade->>Facade: Check ALL artifacts before writing
alt All artifacts pass validation
Facade->>FS: Create root (default ./stagehand-artifacts)
loop Each artifact
Facade->>Facade: Validate path confinement + extension + PNG/JPEG signature
Facade->>FS: Write temp file with O_EXCL
alt Existing file
Facade->>FS: Preserve permissions via chmod
end
Facade->>FS: Rename temp over target (atomic replace)
end
FS-->>Facade: Artifacts written
Facade-->>Host: Success
Host-->>Agent: Run result
else Any artifact rejected
Facade-->>Host: Contract error (TYPE/CONTENT/PATH)
Note over FS: Nothing written, root not created
Host-->>Agent: Error returned
end
else Non-image extension
SW-->>Host: Throw error before capture
Host-->>Agent: Error returned
end
opt Filesystem error during write
FS-->>Facade: OSError (e.g., EACCES, ENAMETOOLONG)
Facade->>Facade: Map to SCREENSHOT_ARTIFACT_WRITE_ERROR with errno code
Facade-->>Host: Contract error (no host paths exposed)
Host-->>Agent: Error returned
end
Note over Host,FS: Artifact root resolution
alt Configurable root
Host->>Facade: artifactRoot from STAGEHAND_FACADE_ARTIFACT_ROOT
else Default
Host->>Facade: ./stagehand-artifacts under cwd
end
Reply with feedback, questions, or to request a fix.
View guided diff | Turn on auto-fix | Re-trigger cubic
| ) | ||
| temporary = os.path.join(directory, f".stagehand-{secrets.token_hex(8)}.tmp") | ||
| flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | no_follow | ||
| descriptor = os.open(temporary, flags, 0o600) |
There was a problem hiding this comment.
P1: A concurrent writer can swap a checked parent directory for a symlink before the path-based os.open or os.replace; O_NOFOLLOW protects only the temporary basename, allowing writes outside root. Pin checked directories with dir_fd operations and use relative opens and renames.
(Based on your team's feedback about constraining screenshot artifact writes.) .
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/integrations/core/python/src/stagehand_middleware/facade/tools.py, line 378:
<comment>A concurrent writer can swap a checked parent directory for a symlink before the path-based `os.open` or `os.replace`; `O_NOFOLLOW` protects only the temporary basename, allowing writes outside `root`. Pin checked directories with `dir_fd` operations and use relative opens and renames.
(Based on your team's feedback about constraining screenshot artifact writes.) .</comment>
<file context>
@@ -297,52 +315,80 @@ def _write_artifacts(self, artifacts: list[dict[str, str]]) -> None:
+ )
+ temporary = os.path.join(directory, f".stagehand-{secrets.token_hex(8)}.tmp")
+ flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | no_follow
+ descriptor = os.open(temporary, flags, 0o600)
+ try:
+ with os.fdopen(descriptor, "wb") as file:
</file context>
There was a problem hiding this comment.
Needs a concurrent writer inside the artifact root; run code can't race it (artifacts are written after the batch, runs are serialized). Tracking dir-handle based writes as a follow-up.
| if (existing && !existing.isFile()) { | ||
| throw new StagehandFacadeInputError(SCREENSHOT_ARTIFACT_NOT_FILE_ERROR); | ||
| } | ||
| const temporary = path.join(directory, `.stagehand-${randomBytes(8).toString("hex")}.tmp`); |
There was a problem hiding this comment.
P2: Another process with write access can replace a checked parent with a symlink before this path-based open; O_NOFOLLOW protects only the temp basename, allowing writes outside artifactRoot. Traverse through no-follow directory handles so each parent check remains tied to the write.
(Based on your team's feedback about constraining screenshot artifact writes.)
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At packages/integrations/core/src/facade/tools.ts, line 485:
<comment>Another process with write access can replace a checked parent with a symlink before this path-based `open`; `O_NOFOLLOW` protects only the temp basename, allowing writes outside `artifactRoot`. Traverse through no-follow directory handles so each parent check remains tied to the write.
(Based on your team's feedback about constraining screenshot artifact writes.) </comment>
<file context>
@@ -362,53 +421,86 @@ export class StagehandFacadeTools {
+ if (existing && !existing.isFile()) {
+ throw new StagehandFacadeInputError(SCREENSHOT_ARTIFACT_NOT_FILE_ERROR);
+ }
+ const temporary = path.join(directory, `.stagehand-${randomBytes(8).toString("hex")}.tmp`);
+ const file = await fsp.open(
+ temporary,
</file context>
There was a problem hiding this comment.
Same as the Python thread: needs a concurrent writer inside the root, which run code can't be. Follow-up.
This PR was opened by the [Changesets release](https://github.com/changesets/action) GitHub action. When you're ready to do a release, you can merge this and the packages will be published to npm automatically. If you're not ready to do a release yet, that's fine, whenever you add more changesets to main, this PR will be updated. # Releases ## @browserbasehq/[email protected] ### Minor Changes - [#3305](#3305) [`aeb1ba0`](aeb1ba0) Thanks [@miguelg719](https://github.com/miguelg719)! - add `browser.disconnect()`, which closes an attached Stagehand instance and the connection to a browser without closing the browser or releasing its Browserbase session (Browserbase still ends sessions without `keepAlive` on disconnect). `browser.close()` after `disconnect()` now throws (connect again to close the browser), a launched local browser receives `Browser.close` before it is terminated, browser handles expose `cdpUrl` (`cdp_url` in Python) for local browsers, and `connected` reports whether the handle still has a live connection - [#3325](#3325) [`d59afde`](d59afde) Thanks [@miguelg719](https://github.com/miguelg719)! - Add an extension watchdog for lost SDK clients ### Patch Changes - [#3339](#3339) [`44d99df`](44d99df) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Adopt cross-origin iframes that were already loaded when Stagehand attached, or that are nested inside another cross-origin iframe, so snapshots and actions can reach them. - [#3330](#3330) [`dc8eb40`](dc8eb40) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - bump zod to 4.6.6 - [#3340](#3340) [`edb76b0`](edb76b0) Thanks [@akeimach](https://github.com/akeimach)! - Fix act, observe and extract with Claude Opus 5.5 by bumping `@ai-sdk/anthropic` and `ai` in the extension. - [#3387](#3387) [`066e875`](066e875) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fixed domain policy bypasses involving URLs with credentials, background requests, and speculative page loads - [#3383](#3383) [`1e50db6`](1e50db6) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip variable substitution for drag-and-drop arguments in act(). - [#3388](#3388) [`18b96f2`](18b96f2) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fix issue where calling act() prevents cross-origin iframes from loading ## @browserbasehq/[email protected] ### Patch Changes - [#3330](#3330) [`dc8eb40`](dc8eb40) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - bump zod to 4.6.6 - [#3350](#3350) [`b7a4b89`](b7a4b89) Thanks [@miguelg719](https://github.com/miguelg719)! - Save run screenshots only as PNG or JPEG files, under ./stagehand-artifacts by default - Updated dependencies [[`44d99df`](44d99df), [`dc8eb40`](dc8eb40), [`edb76b0`](edb76b0), [`066e875`](066e875), [`1e50db6`](1e50db6), [`aeb1ba0`](aeb1ba0), [`18b96f2`](18b96f2), [`d59afde`](d59afde)]: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Minor Changes - [#3325](#3325) [`d59afde`](d59afde) Thanks [@miguelg719](https://github.com/miguelg719)! - Add an extension watchdog for lost SDK clients ### Patch Changes - [#3339](#3339) [`44d99df`](44d99df) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Adopt cross-origin iframes that were already loaded when Stagehand attached, or that are nested inside another cross-origin iframe, so snapshots and actions can reach them. - [#3330](#3330) [`dc8eb40`](dc8eb40) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - bump zod to 4.6.6 - [#3340](#3340) [`edb76b0`](edb76b0) Thanks [@akeimach](https://github.com/akeimach)! - Fix act, observe and extract with Claude Opus 5.5 by bumping `@ai-sdk/anthropic` and `ai` in the extension. - [#3387](#3387) [`066e875`](066e875) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fixed domain policy bypasses involving URLs with credentials, background requests, and speculative page loads - [#3383](#3383) [`1e50db6`](1e50db6) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip variable substitution for drag-and-drop arguments in act(). - [#3388](#3388) [`18b96f2`](18b96f2) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fix issue where calling act() prevents cross-origin iframes from loading ## @browserbasehq/[email protected] ### Minor Changes - [#3305](#3305) [`aeb1ba0`](aeb1ba0) Thanks [@miguelg719](https://github.com/miguelg719)! - add `browser.disconnect()`, which closes an attached Stagehand instance and the connection to a browser without closing the browser or releasing its Browserbase session (Browserbase still ends sessions without `keepAlive` on disconnect). `browser.close()` after `disconnect()` now throws (connect again to close the browser), a launched local browser receives `Browser.close` before it is terminated, browser handles expose `cdpUrl` (`cdp_url` in Python) for local browsers, and `connected` reports whether the handle still has a live connection - [#3325](#3325) [`d59afde`](d59afde) Thanks [@miguelg719](https://github.com/miguelg719)! - Add an extension watchdog for lost SDK clients ### Patch Changes - [#3339](#3339) [`44d99df`](44d99df) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - Adopt cross-origin iframes that were already loaded when Stagehand attached, or that are nested inside another cross-origin iframe, so snapshots and actions can reach them. - [#3330](#3330) [`dc8eb40`](dc8eb40) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - bump zod to 4.6.6 - [#3340](#3340) [`edb76b0`](edb76b0) Thanks [@akeimach](https://github.com/akeimach)! - Fix act, observe and extract with Claude Opus 5.5 by bumping `@ai-sdk/anthropic` and `ai` in the extension. - [#3387](#3387) [`066e875`](066e875) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fixed domain policy bypasses involving URLs with credentials, background requests, and speculative page loads - [#3383](#3383) [`1e50db6`](1e50db6) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - skip variable substitution for drag-and-drop arguments in act(). - [#3388](#3388) [`18b96f2`](18b96f2) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - fix issue where calling act() prevents cross-origin iframes from loading ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies [[`44d99df`](44d99df), [`dc8eb40`](dc8eb40), [`edb76b0`](edb76b0), [`066e875`](066e875), [`1e50db6`](1e50db6), [`aeb1ba0`](aeb1ba0), [`18b96f2`](18b96f2), [`d59afde`](d59afde)]: - @browserbasehq/[email protected] - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies []: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies []: - @browserbasehq/[email protected] - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies [[`44d99df`](44d99df), [`dc8eb40`](dc8eb40), [`edb76b0`](edb76b0), [`066e875`](066e875), [`1e50db6`](1e50db6), [`aeb1ba0`](aeb1ba0), [`18b96f2`](18b96f2), [`d59afde`](d59afde)]: - @browserbasehq/[email protected] - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies []: - @browserbasehq/[email protected] - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies []: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies [[`44d99df`](44d99df), [`dc8eb40`](dc8eb40), [`edb76b0`](edb76b0), [`066e875`](066e875), [`1e50db6`](1e50db6), [`aeb1ba0`](aeb1ba0), [`18b96f2`](18b96f2), [`d59afde`](d59afde)]: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies []: - @browserbasehq/[email protected] - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies []: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies []: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies []: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies []: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies [[`44d99df`](44d99df), [`dc8eb40`](dc8eb40), [`edb76b0`](edb76b0), [`066e875`](066e875), [`1e50db6`](1e50db6), [`aeb1ba0`](aeb1ba0), [`18b96f2`](18b96f2), [`d59afde`](d59afde)]: - @browserbasehq/[email protected] - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies []: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies []: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies [[`44d99df`](44d99df), [`dc8eb40`](dc8eb40), [`edb76b0`](edb76b0), [`066e875`](066e875), [`1e50db6`](1e50db6), [`aeb1ba0`](aeb1ba0), [`18b96f2`](18b96f2), [`d59afde`](d59afde)]: - @browserbasehq/[email protected] - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies []: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - Updated dependencies []: - @browserbasehq/[email protected] ## @browserbasehq/[email protected] ### Patch Changes - [#3330](#3330) [`dc8eb40`](dc8eb40) Thanks [@seanmcguire12](https://github.com/seanmcguire12)! - bump zod to 4.6.6 Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
…#3351) # why stagehand-middleware 0.1.2 is published with the screenshot artifact changes from #3350; the deepagents package and examples still locked 0.1.0. # what changed - `stagehand-middleware>=0.1.2,<0.2` in deepagents and both examples (local, managed). - Relocked all three `uv.lock` files (0.1.0 → 0.1.2). # test plan - [x] deepagents pytest (12) and ruff against the published 0.1.2 <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Updates `stagehand-middleware` from 0.1.0 to 0.1.2 in the deepagents package and both examples, and relocks the root `uv.lock` to pick up the screenshot artifact changes from #3350. <sup>Written for commit db5befa. Summary will update on new commits.</sup> <a href="https://cubic.dev/pr/browserbase/stagehand/pull/3351?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a> <a href="https://www.cubic.dev/action/auto-fix/pr/browserbase/stagehand/3351?returnTo=https%3A%2F%2Fgithub.com%2Fbrowserbase%2Fstagehand%2Fpull%2F3351&source=description" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img alt="Turn on auto-fix" src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a> <!-- End of auto-generated description by cubic. -->
why
Screenshot artifacts returned by facade
runcode were written under the host's working directory with whatever name and bytes the run produced, truncating existing files in place. Hosts that set no artifact root (stdio server, pi, eve, claude-cua) wrote straight into the project directory.what changed
.png/.jpg/.jpegfiles whose bytes start with a PNG or JPEG signature; every artifact is checked before anything is written, so a rejected run writes nothing../stagehand-artifacts(DEFAULT_ARTIFACT_DIRECTORY);STAGEHAND_FACADE_ARTIFACT_ROOTconfigures it for the stdio server, pi and deepagents. The run tool description tells the agent where screenshots land.test plan
page.screenshot({ path })lands instagehand-artifacts/