Skip to content

fix(studio): serve material and Results as sandboxed documents - #471

Open
wjsoj wants to merge 2 commits into
hypit-ai:mainfrom
wjsoj:fix/studio-inert-media
Open

wjsoj wants to merge 2 commits into
hypit-ai:mainfrom
wjsoj:fix/studio-inert-media

Conversation

@wjsoj

@wjsoj wjsoj commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Closes #478

Problem

Studio serves project bytes from its own origin: /__studio/material/<res> (authored Sources such as media:Image), /__studio/artifact?build=&output= (Results), /__studio/surface-preview and /__studio/storyboard/<res>. These responses take their content-type from the Source or Result. media:Image accepts media-type="image/svg+xml", and the build library's isMedia check (^(image|video|audio)/) also lets image/svg+xml through. None of these responses sets a CSP or nosniff.

When someone opens an SVG like that directly (for example with "Open image in new tab" on a Studio preview), its <script> runs as a document in the Studio origin. From there it passes allowsStudioMutation, because Host is localhost, Origin is the same origin and Sec-Fetch-Site is same-origin. It can then:

  • read /__studio/session, the library and feedback comments;
  • PUT /__studio/source to rewrite the project's SVML/SVRun files;
  • POST /__studio/mutation, PUT /__studio/artifact-name and POST /__studio/feedback.

Impact

Medium. Stored XSS in the local Studio origin, triggered by an SVG asset taken from an untrusted source (a downloaded logo, a shared template project, a Result). It needs the victim to open the material URL as a top-level document. Resource ids are random UUIDs, so a third-party site can't easily guess the URL and navigate to it.

Reproduction (on origin/main)

  1. Project with <media:Image id="logo" src="./evil.svg" media-type="image/svg+xml"/> used by a visual:Clip. evil.svg contains <script>fetch('/__studio/session').then(r=>r.text()).then(t=>document.title='PWNED '+t.length)</script>.
  2. hypit studio --run main.svrun --project .
  3. Open http://localhost:5179/__studio/material/<res> (the id appears in /__studio/session) in Chrome.
  4. The page title becomes PWNED 37681. From that document, fetch('/__studio/source', {method:'PUT', body:'{}'}) returns 400 (body validation), not 403. So the origin guard accepts the request.

Fix

A small inertMedia() helper sets Content-Security-Policy: sandbox allow-downloads and X-Content-Type-Options: nosniff on the material, Result, Surface preview and storyboard responses. Opened as documents, they get an opaque origin with scripts disabled (allow-downloads only lets native video/audio controls save the file; without allow-scripts no script runs). Used through <img>, <video> and <audio> they behave as before. /__studio/visual.html is left alone because it is the intended scripted preview.

Raw workspace files through /@fs

Studio's Vite server lists the author workspace in server.fs.allow, so /@fs/<absolute workspace path> served the same files raw (e.g. evil.svg as image/svg+xml, evil.html as text/html) in the Studio origin with no CSP, at a more guessable URL. Nothing in the Studio UI loads workspace files that way (material goes through /__studio/material), but the workspace can't simply be dropped from fs.allow: it is also the default package root, where Studio and its dependencies resolve through node_modules. Instead, studioPlugin registers a middleware ahead of Vite's own /@fs and transform middlewares that applies the same inertMedia() headers to /@fs and /@id requests for paths inside the workspace root, except under node_modules. Vite's module serving for Studio's UI is unchanged.

Tests

  • New packages/studio/test/inert-media.test.ts starts a real Vite server with studioPlugin, laid out like Studio (Vite root is the Studio package, the workspace is only in fs.allow), on a project that contains an SVG media:Image. It checks that:
    • the SVG material bytes are unchanged and carry the sandbox CSP and nosniff, including for a Range request (206) and a HEAD request;
    • the Result route (/__studio/artifact) carries the same headers on 200, 206 and HEAD;
    • /@fs/<workspace>/logo.svg and /@fs/<workspace>/evil.html are refused or carry the sandbox CSP (without the middleware the SVG is served with status 200 and no CSP, so the test fails);
    • Studio's own /src/ui/main.ts is still served normally without the CSP.
      A second unit test checks which request URLs are treated as workspace files (workspace paths via /@fs and /@id yes; node_modules, paths outside the workspace and Studio routes no).
  • Manual check in Chromium: with the fix the SVG script no longer runs, and a fetch from that document fails because its origin is opaque.
  • pnpm install --frozen-lockfile, pnpm check: pass.
  • Studio package tests: 83 pass.
  • pnpm test: 1149 tests, 1126 pass, 0 fail (23 skipped by environment gates).

wjsoj added 2 commits November 5, 2026 08:06
An SVG Source or Result opened directly from Studio ran its scripts in the
Studio origin, where the same-origin mutation guard admits writes to the
project Source. Mark material, Result, Surface preview and storyboard
responses with a sandbox CSP and nosniff so they stay inert as documents
while <img>, <video> and <audio> use is unchanged.
Vite serves any file under server.fs.allow at /@fs/<absolute path>, and the
author workspace is on that list, so a workspace SVG or HTML file opened
there still ran in the Studio origin. Mark workspace files reached through
/@fs or /@id (outside node_modules) with the same sandbox CSP and nosniff
headers as material and Results.

Allow downloads in the sandbox so a Result video opened in its own tab can
still be saved from its native controls; scripts stay disabled. Cover Range
and HEAD responses and the Result route in the inert-media test.
SanHsien added a commit to SanHsien/hypit that referenced this pull request Nov 7, 2026
…i#460-hypit-ai#474; advance watermarks

No PR was merged upstream, so nothing was cherry-picked: 11 PRs = 5 defer
(hypit-ai#464 hypit-ai#466 hypit-ai#467 hypit-ai#469 hypit-ai#471, pinned in monitored) + 6 not applicable; 5 issues =
1 defer (hypit-ai#473) + 4 not applicable. hypit-ai#464 npm pin is moot because the fork gates
publish jobs to upstream (b641cce). ROADMAP row updated with the deferred items.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
SanHsien added a commit to SanHsien/hypit that referenced this pull request Nov 7, 2026
…of upstream PR hypit-ai#471)

Ported before upstream merge: project bytes with an active media type (SVG)
opened as a top-level document ran scripts in the Studio origin and passed the
same-origin mutation guard. inertMedia() adds a sandbox CSP and nosniff to the
material, Result, Surface-preview and storyboard responses, and a middleware
does the same for workspace files Vite serves through /@fs and /@id. Tests
fail without each half and pass with it. Replace with upstream's version once
hypit-ai#471 is merged.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

This branch has not been deployed

No deployments
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.

[Bug] Studio serves project SVGs so that their scripts run in the Studio origin

1 participant