fix(security): validate postMessage origin in sandbox preview bootstrap#4113
Open
0xcucumbersalad wants to merge 1 commit into
Open
fix(security): validate postMessage origin in sandbox preview bootstrap#41130xcucumbersalad wants to merge 1 commit into
0xcucumbersalad wants to merge 1 commit into
Conversation
The IFRAME_BOOTSTRAP_SCRIPT injected into all sandbox preview pages listened for `visual-editor::activate` messages with a `script` field and executed them via `new Function(e.data.script)()` without checking `e.origin`. Any page that framed the preview could inject arbitrary JS into the preview context (the daemon proxy strips X-Frame-Options and CSP headers to allow embedding). Two fixes: 1. shared.ts: The bootstrap listener now validates `e.origin` against the parent origin (derived from `document.referrer`) before executing the script. Messages from unknown origins are silently dropped. 2. preview.tsx: Replace `postMessage(..., "*")` with a computed target origin from `previewUrl`, preventing message delivery if the iframe navigated to an unexpected origin. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
IFRAME_BOOTSTRAP_SCRIPTinjected into all sandbox preview pages executed arbitrary JavaScript vianew Function(e.data.script)()on receipt of avisual-editor::activatepostMessage — without checkinge.origin. Since the daemon proxy stripsX-Frame-OptionsandContent-Security-Policyheaders from preview responses, any attacker page could frame a preview URL and inject arbitrary JS into the preview context.Attack vector
Fixes
packages/sandbox/shared.tse.originagainst parent origin (fromdocument.referrer) before executing scriptapps/mesh/src/web/components/sandbox/preview/preview.tsxpostMessage(..., "*")with computed target origin frompreviewUrlapps/mesh/src/web/components/deck/use-deck-editor.ts"*"is necessary for sandboxed (opaque-origin) deck iframesTest plan
bun run fmt— passesbun run lint— passes (0 errors)bun run check— pre-existing NATS type errors only (same on main)🤖 Generated with Claude Code
Summary by cubic
Prevents arbitrary script injection in sandbox previews by validating postMessage origins and sending messages with a specific targetOrigin. Blocks attacker pages from injecting JS when framing preview URLs.
packages/sandbox/shared.ts: Bootstrap listener validatese.originagainst the parent origin (fromdocument.referrer) before executingvisual-editor::activatescripts.apps/mesh/src/web/components/sandbox/preview/preview.tsx: ReplacespostMessage(..., "*")with a computed origin frompreviewUrlfor activate/deactivate messages.apps/mesh/src/web/components/deck/use-deck-editor.ts: Adds a note explaining why"*"is required for sandboxed (opaque-origin) deck iframes.Written for commit 8e30b0f. Summary will update on new commits.