Skip to content

Fix #2084: no workspace SVG runs script in Tower's origin - #2086

Merged
waleedkadous merged 14 commits into
mainfrom
builder/bugfix-2084
Dec 5, 2026
Merged

waleedkadous merged 14 commits into
mainfrom
builder/bugfix-2084

Conversation

@waleedkadous

@waleedkadous waleedkadous commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #2084

Summary

A workspace SVG holding a <script> could run it in Tower's origin, which reaches the whole API, every agent's terminal included. There were three ways to get there, not one:

  1. The raw address, as the issue says. GET /api/file/<tab>/raw and GET /api/annotate/<tab>/api/image|video|model|pdf served the SVG as image/svg+xml with no nosniff and no content policy. Both routes are keyed, so this needs a client that presents the key, such as a reverse proxy that adds it.
  2. The annotation viewer's image opened as a page ("Open image in new tab"), which never touches the raw address. The viewer fetches the bytes with the key and shows them from a blob: URL it makes in Tower's origin. Response headers do not reach a blob: URL. The SVG's script ran there and read the key off Tower's landing page.
  3. The Decisions tab's image opened as a page. It is the same blob: pattern in the web dashboard. The Decisions image route already sent the sandboxing headers; they did not help.

A survey of every place the clients turn workspace bytes into a URL in Tower's origin found a fourth:

  1. A 3MF texture in the 3D viewer. The vendored three.js 3MF loader makes each texture's object URL with the type the 3MF file claims, which could be image/svg+xml.

Reproduced in headless Chromium against a scratch Tower (never the live one), then shown closed: see Test Plan. Findings 2 and 3 are on the issue as a comment.

Root Cause

  • The two older routes relied on the content type alone.
  • The clients show workspace bytes from object URLs, and an object URL belongs to the page that makes it: Tower's origin, for the viewer and the dashboard.
  • An image/svg+xml document is scriptable. Nothing sandboxes a blob: URL: the policy that sandboxes the route's response does not travel with the bytes into the blob.

Fix

  • One helper for the headers: workspaceFileHeaders(filePath, length) in tower-utils.ts returns the type from the file's name, its length, X-Content-Type-Options: nosniff and Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; sandbox. All three routes use it for every type they serve (file raw, annotate media, Decisions image). A guard test fails if any source file names a type from a file's name outside it.
  • The viewer (templates/open.html) shows an SVG from a data: URL instead of an object URL. Every other type keeps its object URL.
  • The Decisions view (workspace-views DecisionsMarkdown, used by the web dashboard, the Tower page's Site view and the VS Code panel) does the same:
    • a click on an SVG still opens it full size;
    • the web host's full-size page accepts a data: SVG;
    • the VS Code webview's img-src gains data:, and nothing else in its policy changes.
  • The 3MF loader types a texture's object URL PNG or JPEG, the only texture types 3MF defines, never the type the file claims. The vendored three.js files are gitignored and generated at build by scripts/copy-three.mjs, so the change is a second rewrite there. That rewrite fails the build if the line is not found exactly once.
  • Adjacent bug, fixed in place: the 3D viewer loaded nothing. Its import map gave bare values (vendor/three.module.js). Browsers ignore an import map value that is not a URL, so three never resolved, ever since three.js was vendored on 2026-08-12. The values now start with ./. (On main the 3MF texture path above was unreachable only because of this.)

PDF: the sandboxing policy applies to PDF on the routes too, and nothing was relaxed for any type. The viewer loads a PDF as fetched bytes into an object URL, so the response's policy never reaches the embedded PDF viewer. The PDF still displays (screenshot below).

Every place workspace bytes become a URL

The places I looked, in the dashboard, the viewers, the web host and the VS Code webview:

Place Turns bytes into Origin Outcome
GET /api/file/<tab>/raw response Tower fixed: workspaceFileHeaders
GET /api/annotate/<tab>/api/{image,video,model,pdf} response Tower fixed: workspaceFileHeaders
GET /api/decisions/image response Tower already sandboxed; now through workspaceFileHeaders
Annotation viewer, loadMedia (open.html), SVG blob: → now data: Tower fixed
Annotation viewer, PNG/JPEG/GIF/WebP blob: Tower kept: the type comes from the extension map (getMimeTypeForFile), and a raster image type is rendered as an image; browsers sniff an image type only among image types, never to a scriptable one
Annotation viewer, video (MP4/WebM/MOV) blob: Tower kept: a media document runs no script, and a media type is sniffed only among media types
Annotation viewer, PDF blob: (application/pdf) Tower kept: the browser's PDF viewer renders it in its own extension context. A PDF's own scripts, where a browser runs any, stay inside that viewer, with no access to the page's origin
3D viewer, 3MF textures (vendored 3MFLoader) blob: with the file's claimed type Tower fixed: PNG or JPEG only
3D viewer, fflate's unzip worker blob: (text/javascript) Tower not workspace bytes: the library's own code
3D viewer, STL none: parsed to geometry – n/a
Decisions view (DecisionsMarkdown), SVG blob: → now data: Tower on the web dashboard and the Tower page; the webview's own origin in VS Code fixed
Decisions view, PNG/JPEG/GIF/WebP (the route serves no other type) blob: as above kept: raster image types, as for the viewer
Web host "open full size" (apps/web decisionsHost.ts) blob: (text/html) Tower the page's markup is built with DOM APIs: one <img>, its alt and src set as attributes and escaped by the serializer. It holds no workspace bytes and no <script>. It now also accepts a data: SVG
VS Code Decisions host (decisions-host.ts) and full-size view Blob → the view's URL; <img> the webview's own (vscode-webview://), never Tower's SVG now data: there too; img-src gains data:
Dashboard file tabs (FileViewer.tsx) <iframe> of the annotation viewer Tower covered by the viewer rows
getFileRawUrl (apps/web api.ts) the raw route's address – has no caller
Viewer HTML preview <iframe sandbox="allow-scripts" srcdoc> opaque (no allow-same-origin) unchanged: never Tower's origin
GET /file?path= and annotate GET /file response, text/plain; charset=utf-8 Tower unchanged: a fixed type, not taken from the name, read as text. text/plain is sniffed only to text or binary, never to a scriptable type
Paste-image upload (uploadPasteImage) none: browser → Tower – n/a

What "open full size" does for an SVG now

  • Web dashboard and the Tower page's Site view: a click on the image opens a new tab holding a page the host builds. The page has one <img src="data:image/svg+xml;base64,…"> and no script, and the host revokes it after 60 s, as before. The SVG renders as an image there, so its script cannot run.
  • VS Code: the panel's own full-size view, an <img> of the data: URL.
  • The annotation viewer has no full-size action; it zooms in place.

Opening the image's own address:

  • A page cannot open a data: URL as a top-level document: window.open of it opened nothing in the tests.
  • Typed into a new tab's address bar, a data: SVG's script runs in an opaque (null) origin that cannot read Tower's responses. The tests assert exactly that: it ran in null and its read of the key was refused. It runs no script in Tower's origin; it does still run in its own.

data: URL size

  • A data: URL is about 4/3 of the file (base64). MDN: Chromium and Firefox allow 512 MB, Safari 2048 MB. Firefox allowed 32 MB from version 97 to 135, and 512 MB since 136.
  • Decisions images: capped at 10 MB by the route (MAX_DECISIONS_IMAGE_BYTES), about 13.4 MB as a data: URL, well inside every limit.
  • The viewer: has no server cap. In Chromium a 20 MB SVG displays (checked below). An SVG over about 24 MB would not display in Firefox 97–135, and one over about 384 MB in Chromium or current Firefox. An SVG that large in a workspace is unusual. If one does not display, the viewer shows "Failed to load image", as for a corrupt file.

Test Plan

  • Regression tests added: browser, route and client, each failing without the fix
  • Build passes (npm run build)
  • All tests pass (npm test in packages/codev, the full suite, run once at a 1-minute load under 10; plus the targeted workspace-views, apps/vscode and apps/web files)

Browser, committed (src/agent-farm/__tests__/e2e/bugfix-2084-workspace-svg.test.ts). It runs through playwright.decisions.config.ts: a throwaway Tower on its own port and dirs, with mkdtemp workspaces. The nightly CI step is renamed "Run Decisions tab and workspace SVG tests". Each test shows an SVG whose script, if it runs, marks where it ran and whether it read the key, then opens it as a page:

Test Old code (main's sources rebuilt, import-map fix kept) This PR
raw address (both routes), opened by a client presenting the key fails: ran in http://localhost:<port>, read the key passes: no script ran; nosniff + sandbox
annotation viewer: shown, then opened by the page and typed into a tab fails: opened by the page, ran in Tower's origin, read the key passes: nothing opened; typed, ran only in null, key refused; PNG still an object URL
Decisions tab: shown, opened as a page, full size fails: same as the viewer passes, and the full-size page has no <script> and shows the image
3D viewer: a 3MF whose texture claims image/svg+xml fails: texture object URL typed image/svg+xml passes: typed image/png; the model loads (1 triangle)

The existing Decisions suite passes too. Its one changed expectation: a workspace SVG is now a data: URL.

These browser tests run in the nightly dashboard-e2e.yml workflow, not in PR CI. The 3D viewer's revival (the import-map fix) is covered only there and by the runs above.

Unit:

  • tower-routes.test.ts: each route (raw, and annotate image|video|model|pdf) serves .svg, .png, .pdf and .txt with nosniff, the policy and the right type. 20 cases, all failing on the old routes with exactly those two headers missing.
  • bugfix-2084-workspace-file-headers.test.ts: the helper, its policy, and the guard that no route names a type from a file's name outside it.
  • decisions-routes.test.ts: the Decisions image's SVG carries the shared policy.
  • bugfix-2084-viewer-svg.test.ts runs the viewer's own loadMedia, cut from open.html, in jsdom:
    • SVG becomes data:, other types keep object URLs;
    • only object URLs are revoked;
    • an SVG it cannot read shows the viewer's load error, never an object URL.
  • DecisionsView.images.test.tsx: an SVG becomes a data: URL and goes full size from there. A read failure shows the "Image not loaded" note.
  • createDecisionsHost.test.ts: the full-size page for a data: SVG and for an object URL; nothing opens for any other data: URL.
  • workspace-views-panel-template.test.ts: img-src blob: data: http: https:.

The new unit and browser tests each failed with the fix removed, and pass with it.

Manual, headless Chromium against a scratch Tower:

Before the fix, an SVG opened by its raw address with the key ran its script in Tower's origin and read the key. After it, checked by a script against that scratch Tower (not committed):

  • Headers: both routes send nosniff and the policy for an SVG, a PNG, a WebM and a PDF.
  • Raw address, opened as a page by a client presenting the key: both routes return 200, and the script does not run.
  • Annotation viewer:
    • PNG from blob: (160×120), SVG from data: (320×160), WebM from blob: (160×120), PDF from blob: (shown in the browser's PDF viewer);
    • a 20 MB SVG displays from a 28 MB data: URL;
    • the SVG opened by the page: nothing opened; typed into a new tab: ran in null, its read of the key refused.
  • 3D viewer: a 3MF whose texture claims image/svg+xml loads (1 triangle); its texture's object URL is image/png.
  • Decisions tab:
    • SVG from data:, PNG from blob:;
    • the SVG typed into a new tab: ran in null, key refused;
    • full size: a page with no <script>, whose <img> shows the data: SVG (120×80).

Screenshots are element crops only, with no header, path or name:

Viewer: PNG Viewer: SVG (its script is inert) Viewer: WebM
The annotation viewer showing a PNG test card The annotation viewer showing an SVG with a circle and the words SVG shown The annotation viewer showing a WebM video with its controls
Viewer: PDF 3D viewer: 3MF with an SVG-typed texture
The annotation viewer showing a one-page PDF reading Hello PDF The 3D viewer showing one triangle on its grid
Decisions tab: an SVG and a PNG inline Decisions tab: the SVG full size
A decision item showing a red SVG rectangle and a PNG test card inline The full-size page showing the red SVG rectangle

Not checked:

  • Firefox and Safari: only Chromium is installed here.
  • The browser's own context-menu "Open image in new tab": Playwright cannot click a native menu. The tests open the address the two ways a page and a person can, by the page and by the address bar.

Unrelated, environmental: 97 apps/web tests (terminal, toolbar, Site view) fail in this worktree because jsdom has no canvas.getContext. They fail the same way with main's versions of the two apps/web files this PR touches.

CMAP

codex APPROVE (HIGH), claude APPROVE (HIGH). Gemini is off in this project's consultation config.

Addressed from claude's comments:

  • The SVG type check failed open. An exact === 'image/svg+xml' would have put an SVG back on an object URL if a producer ever added a charset. The viewer and the Decisions view now compare the type's essence. Tested with Image/SVG+XML; charset=utf-8.
  • A stale mock. spec-761-api-state.test.ts mocked tower-utils without workspaceFileHeaders, which tower-routes.ts now imports. It passes the real one through.
  • The guard test's doc claimed more than the test checks. It now says what it guards: the type from a file's name is only taken inside the helper. It does not catch a route that writes a type by hand, and no route sends workspace bytes that way today.

Left as is:

  • allow-downloads on the raw route's sandbox. The route has no caller, and I have not verified how a sandboxed response behaves as a download, so a comment would be a guess.
  • rewriteOnce's error names 3MFLoader.js. Both rewrites are in that file, so the message is accurate.

… loads

The import map gave bare values (vendor/three.module.js). Browsers ignore an
import map value that is not a URL, so 'three' never resolved and the 3D viewer
loaded nothing since three.js was vendored. Each value now starts with ./
- workspaceFileHeaders (tower-utils): every route serving a workspace file
  with a type from its name sends nosniff and a sandboxing content policy:
  the file raw route, the annotate media routes and the Decisions image.
- The annotation viewer and the Decisions view show an SVG from a data: URL,
  never an object URL of Tower's origin, which ran the SVG's scripts as Tower
  when opened as a page. The web host's full-size page takes a data: SVG; the
  VS Code webview's img-src admits data:.
- The vendored 3MF loader types a texture's object URL PNG or JPEG, never the
  type the file claims.
…surface

- Browser (playwright.decisions.config.ts, throwaway Tower and workspaces):
  the raw address opened by a keyed client, the annotation viewer, the
  Decisions tab (opened as a page, and full size) and the 3D viewer's 3MF
  textures: the SVG's script never runs in Tower's origin nor reads the key.
- Routes: each route serving workspace bytes sends nosniff and the sandboxing
  policy for an .svg, .png, .pdf and .txt; a guard fails if a route names a
  type from a file's name outside workspaceFileHeaders.
- The viewer's own loader (cut from open.html), the Decisions view and the web
  host's full-size page: an SVG is a data: URL, never an object URL.
… data: URL

The viewer and the Decisions view compared the blob's type to image/svg+xml
exactly, so a producer adding a charset would have put the SVG back on an
object URL of Tower's origin. Both now compare the type's essence.
…y the header helper

- The viewer and the Decisions view show an 'Image/SVG+XML; charset=utf-8'
  blob from a data: URL.
- spec-761-api-state's tower-utils mock passes through workspaceFileHeaders,
  which tower-routes now imports.
- The guard test says what it guards.
@waleedkadous
waleedkadous merged commit d629cfa into main Dec 5, 2026
2 checks passed
waleedkadous added a commit that referenced this pull request Dec 5, 2026
waleedkadous added a commit that referenced this pull request Dec 5, 2026
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.

Security: Tower serves workspace SVG files without sandboxing headers, so an SVG opened as a page runs script in Tower's origin

1 participant