Skip to content

fix(studio-server): thumbnail origin from the server, sandbox previews from other sites - #7492

Merged
jrusso1020 merged 3 commits into
mainfrom
fix/studio-preview-origin
Nov 10, 2026
Merged

jrusso1020 merged 3 commits into
mainfrom
fix/studio-preview-origin

Conversation

@jrusso1020

@jrusso1020 jrusso1020 commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

What

The thumbnail route now builds the headless browser's URL from the server's own listening origin instead of the request's Host header, and project HTML/SVG served by the preview routes is sent with a sandbox CSP when it is opened from another site. Studio's own iframe, browser automation and CLI clients see no change.

Why

Hardening for the local preview server: the URL the thumbnail browser navigates to should not depend on a request header, and project documents opened from another site should not run with the API's origin.

Related work

Refs #7490 (which carries #6128 forward). That PR handles the write-side origin and host checks; this one does not touch them.

How

See the collapsed write-up below.

Test plan

  • Unit tests added/updated
  • Manual testing performed
  • Documentation updated (if applicable)
  • Comments follow CONTRIBUTING.md "Comments": they say why, not what, and a bug fix says what the code must do and how to reproduce the bug

No visible change

The only change under packages/studio is six lines in vite.adapter.ts that report the dev server's own origin to the thumbnail route. Studio renders and plays the same; the headless-Chrome check in the write-up below loads a composition in Studio's iframe and plays it on this branch.

Claude Opus 5.5

What changed

Thumbnail navigation target

  • StudioApiAdapter gains an optional serverOrigin(). The thumbnail route uses it for previewUrl. When a host does not provide it (or it returns nothing yet), the route accepts the Host header only if the whole header is a loopback name (localhost, 127.x.x.x, [::1], optional port, nothing after it), and otherwise answers 400 without calling generateThumbnail. An in-process request with no Host at all (tests, app.request) keeps using http://localhost.
  • New helper originForListeningAddress(server.address()) in @hyperframes/studio-server: an unspecified bind (0.0.0.0, ::) maps to 127.0.0.1, IPv6 literals are bracketed.
  • CLI hyperframes preview: listening (already passed to createStudioServer as origin for the history owner URL) is now derived from the bound address rather than hard-coded 127.0.0.1, and the adapter exposes it as serverOrigin. Default and 0.0.0.0 binds produce the same http://127.0.0.1:<port> as before; a HYPERFRAMES_PREVIEW_HOST bound to one specific address now gets that address, which is the one Chrome can actually reach.
  • Vite dev host (packages/studio/vite.adapter.ts): serverOrigin from server.httpServer.address().

Preview sandbox

  • One middleware on /projects/:id/preview/* in createStudioApi (Hono's /* also matches the bare /preview, so it covers the bundled preview, /preview/comp/* and the project-file route). It always appends Vary: Sec-Fetch-Site, and adds Content-Security-Policy: sandbox allow-scripts (no allow-same-origin) when Sec-Fetch-Site is cross-site or same-site. same-origin, none and an absent header are unchanged.
  • Vary matters because the bundled and sub-composition previews are private, no-cache with an ETag: without it a response cached in one context could be revalidated in the other and carry or drop the CSP. Project HTML/SVG files are already no-store.

Routes checked for the same issue

  • Other routes that return project content answer JSON (/files/*, /gsap-animations/*) or fixed non-document types (render outputs: mp4/webm/mov/gif; fonts: font/* as attachment; thumbnails: jpeg/png). The CLI's own static routes serve the Studio bundle and GSAP vendor files, not project files.
  • serveStaticProjectHtml (used by check, snapshot, compare) serves project files on a random-port, loopback-only, short-lived server that has no Studio API, so it is left as is.
  • Header values the product sends, measured in headless Chrome against the built CLI: Studio's preview iframe request is Sec-Fetch-Site: same-origin, Sec-Fetch-Dest: iframe. Puppeteer page.goto (thumbnails, capture) sends none; Node fetch (prebuild) sends nothing.

What I measured

Unit tests (drive the real route handlers)

  • src/routes/thumbnail.origin.test.ts (3): a foreign Host with serverOrigin set navigates to http://127.0.0.1:3002/api/projects/demo/preview?...; without serverOrigin, four non-loopback Host shapes (including 127.0.0.1:3002@other.example and 127.0.0.1.other.example:3002) get 400 and the adapter is never called; loopback Hosts pass through as given.
  • src/routes/preview.sandbox.test.ts (21): through createStudioApi, for a project .html, a project .svg, the bundled preview and a sub-composition preview, cross-site and same-site get the sandbox CSP; same-origin, none and no header do not; all carry Vary: Sec-Fetch-Site, including a 304.
  • src/helpers/serverOrigin.test.ts (6) and one CLI test that createStudioServer's adapter reports the origin option as serverOrigin.
  • Against origin/main's sources (tests unchanged): 23 of the 24 route tests fail; the one that passes is the loopback-Host passthrough, which is meant to hold on both. All 8 sandbox cases, the 304 case and both thumbnail-destination cases fail there; the 12 "keeps its origin" cases fail on main only on the Vary assertion. The helper test file cannot import on main.
  • Package suites on the branch: studio-server 95 files, 1682 passed, 2 skipped. CLI src/server/ plus the preview command tests: all pass (one existing mock in preview.startFailure.test.ts needed the new export). Studio vite.adapter* / vite.studio-server-module / vite.config tests: 38 passed.
  • tsc --noEmit for studio-server, cli and studio; root bun run lint; oxfmt --check; check-comment-citations and comment-ratchet against the merge-base: all clean.

Real path (built CLI, hyperframes preview on a scaffolded project, a logging HTTP listener on 127.0.0.1:3699 standing in for another local service)

  • curl -H 'Host: other.example:3612' .../api/projects/demo/thumbnail/index.html?t=1: main returns 500 (Chrome tried to load other.example); this branch returns 200, a 240x135 JPEG of the composition.
  • curl -H 'Host: localhost:3699' .../thumbnail/index.html?t=1.5: on main the listener logged GET /api/projects/demo/preview?hf-capture=1 host=localhost:3699; on this branch it logged no request and the thumbnail is the real composition (same 2956 bytes as above).
  • curl -H 'Sec-Fetch-Site: cross-site' .../preview/page.html: content-security-policy: sandbox allow-scripts, vary: Sec-Fetch-Site (same for same-site, an .svg, and the bundled /preview). With same-origin, none or no header: no CSP. Main: no CSP in any case.
  • Headless Chrome: Studio at http://127.0.0.1:3612/ loads the composition in its iframe (response has no CSP), the parent reads iframe.contentWindow (origin http://127.0.0.1:3612, 6 timelines), and Space plays it (player time 0 to 2.02 s in 2 s). A page on http://localhost:3699 framing page.html, and the same page navigated top-level from there: the document's scripts run, window.origin is "null", and a fetch of /api/projects fails. Opening page.html directly (address bar, Sec-Fetch-Site: none): origin http://127.0.0.1:3612, fetch 200.

What I did NOT exercise

  • The Vite dev host (bun run studio) end to end: its serverOrigin is typechecked and its existing tests pass, but I did not request a thumbnail through a running dev server.
  • HYPERFRAMES_PREVIEW_HOST bound to a specific LAN address, IPv6-only binds, and an https Vite server: covered by the helper's unit cases only.
  • Safari and Firefox. Sec-Fetch-Site is sent by current Chromium, Firefox and Safari; an older browser that omits it is treated like a non-browser client and gets no CSP.
  • Desktop app embedding: it loads Studio from the preview server's own origin, so its preview frame should stay same-origin, but I did not run it.
  • fix(studio-server): refuse cross-origin writes and unknown hosts on the preview server #7490 also edits createStudioApi.ts, studioServer.ts, studioServer.test.ts and index.ts; whichever lands second may need a small rebase.

— Rames Jusso

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Edit accuracy: accurate 267 (base branch 267), smooth 88 of those

The gate passes.
Smoothness is reported in the artifact, not gated. A case fails only if it fails 2 of 3 runs.

Quarantined, measured but not gated (8)

Unstable (1)

  • seqrotatenudge-none-pct-r0-root-z100: tracking 0.03, pressJump 0, drop 0, reload 3.03, render 0.04, renderKey -, undo false, teleport true / tracking 0.03, pressJump 0, drop 0, reload 0, render 0.08, renderKey -, undo true, teleport true / tracking 0.03, pressJump 0, drop 0, reload 0, render 0.08, renderKey -, undo true, teleport true

@heygengenesis heygengenesis Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: APPROVE · 0 blocker · 0 important · 0 nits · f9065fb

The required CI passed at this head (CI, Windows tests/render, captures, regression, CodeQL).

Scope

I read the full diff. createStudioServer has a single caller (runEmbeddedMode), and it always supplies origin, so the loopback-Host 400 fallback only applies before the server is listening or in in-process requests. The Vite adapter now reports serverOrigin for its own generateThumbnail. Nothing under studio-server sets a CSP that the new middleware would overwrite. Hono re-wraps a finalized response before c.header, so the post-next() header write is safe. I also checked a downstream consumer: HFI desktop already sandboxes opaque previews and refuses foreign /preview work with its own middleware, so this change doesn't affect desktop until it bumps the package. It also doesn't conflict with desktop's frame-ancestors, which is only set on renderer files. I didn't run the Vite dev host end to end. The approval covers f9065fb only; if #7490 lands first, the rebased head needs fresh CI.

— Genesis

@jerrai-bot-heygen jerrai-bot-heygen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Findings-only review at f9065fb: no demonstrated blocker in the built-in CLI/Vite thumbnail-origin fix or the normal Studio/render/snapshot paths. Both thumbnail adapters supply the listening origin; the cross-site/same-site CSP is mounted only on Studio API preview documents. Studio's same-origin iframe stays unsandboxed by this response, and hyperframes snapshot and producer renders use their own file servers rather than that middleware. Hosted exact-head CLI/studio-server tests executed (including thumbnail-origin, sandbox and origin helper cases). Compatibility boundary: an externally embedded preview is different: sandbox allow-scripts gives its document an opaque origin. Relative URLs still resolve, and ordinary scripts/images/media may run/display, but local @font-face, ES modules, and fetch() of local assets can fail CORS; the preview asset responses do not add CORS permission. Current fixtures prove header routing, not faithful rendering of those asset classes. This is a consequence of the intended isolation, not proof of a security bypass; document/test it if external preview fidelity is a product requirement. #7490's separate public-editor Host allowlist requirement remains for Cut Room on a CLI upgrade, and the combined tree needs fresh review after either PR rebases. No approval, merge, or live render by me. — Jerrai

…s from other sites

The thumbnail route builds the headless browser's URL from the server's own
listening origin (a new optional `serverOrigin` on the adapter, wired in the CLI
preview server and the Vite dev host) instead of the request's Host header.
Hosts that report no origin fall back to a loopback Host only.

Preview responses (bundled, sub-composition and project files) opened from
another site carry `Content-Security-Policy: sandbox allow-scripts` and
`Vary: Sec-Fetch-Site`. Studio's same-origin iframe, browser automation and
non-browser clients are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jrusso1020
jrusso1020 force-pushed the fix/studio-preview-origin branch from f9065fb to d1fc1d3 Compare November 10, 2026 10:36

@heygengenesis heygengenesis Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: APPROVE · 0 blocker · 1 important · 0 nits · f9065fb → d1fc1d3

Range-diff against f9065fb changes only the createStudioApi.ts hunk. The sandbox middleware stays outermost, ahead of #7490's write guard. All CI is green at this head. One new interaction with #7490 is noted inline.

Comment thread packages/cli/src/commands/preview.ts Resolved
With HYPERFRAMES_PREVIEW_HOST set to a hostname, the listening address is
the IP it resolved to, and the Host check accepts the bind name, not that
IP. The thumbnail browser then loaded a 403 page. Use the bind name when it
is a hostname; IP and wildcard binds keep the listening address.

— Rames Jusso

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@heygengenesis heygengenesis Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: REQUEST CHANGES — 1 resolved, 0 open, 1 new · d1fc1d3 → a13fffd

…kage's comment share

The hostname-bind rule is stated by its test name in serverOrigin.test.ts.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@heygengenesis heygengenesis Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verdict: APPROVE — 1 resolved, 0 open, 0 new · a13fffd → d00f595. Comments is green now, and CI passed on the rerun.

@jrusso1020
jrusso1020 added this pull request to the merge queue Nov 10, 2026
Merged via the queue into main with commit 983e865 Nov 10, 2026
138 of 139 checks passed
@jrusso1020
jrusso1020 deleted the fix/studio-preview-origin branch November 10, 2026 14:59
@miguel-heygen miguel-heygen mentioned this pull request Nov 10, 2026
3 of 4 tasks
SanHsien added a commit to SanHsien/hyperframes that referenced this pull request Nov 29, 2026
Adapt upstream heygen-com#7492 and preserve existing content security policies.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants