Repository navigation
fix(server): serve file reads as downloads with explicit content headers - #62422
Conversation
There was a problem hiding this comment.
The fix works as described. I checked it against opencode serve from source on the base and on this PR:
- Base: an
.htmlfile came back from/api/fs/read/*inline astext/html, and cross-site navigations or calls from any origin got 200. - This PR: the same file comes back as a
text/plainattachment with the sandboxed CSP andnosniff. Cross-site navigations and calls from origins that aren't allowed get 403. Requests from app.opencode.ai and no-cors range requests (how<video>/<audio>load) still succeed. - Tests and types: the server tests pass, and
tsc -bin packages/server is clean.
Public API impact, limited to the server's HTTP behaviour (no SDK, OpenAPI, config or CLI changes):
GET/HEAD /api/fs/read/*: responses now includecontent-disposition: attachmentwith the filename, a sandboxedcontent-security-policyandx-content-type-options: nosniff. The content-type changes from html/js/xml types (including svg) totext/plain; charset=utf-8.GET /api/experimental/media/:hash: PNG, JPEG, GIF and WebP images now also getcontent-disposition: attachment, and the CSP allowsimg-src/media-src 'self'./api/*(new): returns403 ForbiddenErrorfor browser requests markedSec-Fetch-Site: cross-site/same-sitethat are navigations, or that carry anOriginoutside the allowed CORS list.
This is backwards compatible for the CLI, TUI, SDK and the app, which send no Sec-Fetch-* headers or use allowed origins. Pages on other origins that aren't passed with --cors can no longer call the API, and opening an API URL from another site now gives 403. That is the intent of the change.
No blocking concerns.
Fixes #58914
There was a problem hiding this comment.
The new commit only changes a test: it switches to a filename that Windows accepts. The test still checks both the escaped filename= fallback and the percent-encoded filename*= value, and test/fs.test.ts passes. Server behaviour and the public API impact are the same as in my previous review. No new concerns.
Ports upstream b3b02ae (anomalyco#62422, v2.2.6) to the v2.2.5 base; retires at the next base move. /api/fs/read served every file inline with the file's own content type, so a browser sent to that URL by another site rendered an HTML or SVG file as a page on the server's origin, where the SameSite=Lax session cookie goes along. ContentHeaders.download now makes every read a sandboxed download: content-disposition attachment with the file name (RFC 6266), a CSP of default-src 'none' with img-src and media-src 'self' and sandbox, and nosniff. HTML, JavaScript and XML (SVG included) are labelled text/plain; other types keep theirs, so img, audio and video elements still play them. The media route uses the same disposition and policy for every blob. RequestSite.middleware, ahead of CORS in both the server process and the fetch handler, answers 403 to an /api request that carries Sec-Fetch-Site cross-site or same-site when it navigates, or when its Origin is not allowed by the CORS rules. Requests without Sec-Fetch headers (CLI, TUI, SDK), same-origin and typed-in URLs, pairing links and artifact views pass. Files: packages/server/src/content-headers.ts and request-site.ts (new), fetch.ts, process.ts, handlers/fs.ts, handlers/media.ts; upstream's app e2e fixtures packages/app/e2e/utils/mock-server.ts and regression/review.spec.ts, which serve the same headers as the server; tests in packages/server/test/request-site.test.ts (new), fs.test.ts and media.test.ts. Browser check (ACF): the web app's video viewer test in review.spec.ts, with the download headers, passes on headless Microsoft Edge (Playwright channel msedge): the video loads its size and length and plays. The same test fails when the media request is refused, so it does detect a viewer that cannot load. Edge also played the video when it was labelled text/plain with nosniff, so media playback does not depend on the declared type. Not exercised in a browser: the real server end to end, and RequestSite with a browser's media request (a media load is not a navigation, and sends an Origin only in CORS mode, where the app's origin is the allowed one).
ACF-CHANGES.md gets one row per fix ported on this branch: UTF-8 refusal (anomalyco#64868, with ACF's byte order mark change), Code Mode fetch redirects (anomalyco#60502), URL credentials and rule matching (anomalyco#61701, anomalyco#62373), site-scoped "Allow always" (anomalyco#66000, with the image_generate redirect limit), service password out of the CLI log (anomalyco#64911), the session_move ask and its scope (anomalyco#62204), and file reads as downloads with cross-site API refusal (anomalyco#62422).
… the base Ports ahead of the base, each with ACF's own fixes from three review rounds: refuse non-UTF-8 rewrites (anomalyco#64868); check Code Mode fetch redirects (anomalyco#60502); reject URL credentials and match rules as written (anomalyco#61701, anomalyco#62373); site-scoped Always for web fetch (anomalyco#66000); keep the service password out of the CLI log (anomalyco#64911); session_move asks external_directory (anomalyco#62204) on the real destination, re-checked before the move, with worktrees exempt only when the project's own Git lists them (read exactly, -z); file reads as downloads and cross-site API refusal (anomalyco#62422). Plus ACF's byte-only BOM sync and the template Ask test. Reviews: 2026-11-29T18-08, 19-01, 19-28 and 20-02 relays (Associate GPT-6.2 Sol). Gates on Bun 1.4.4 at 0c082b0231 (one ledger sentence changed since): core 1398/3 skip/2 fail (project-icon symlink EPERM, same on base), typecheck:tsc 0 in core/cli/server/util, root check only the known custom-elements.d.ts symlink baseline. Private scan: 8 'email' matches, all synthetic URL userinfo examples ([email protected], [email protected]); gitleaks clean.
file reads now always come back as downloads with consistent headers, and API urls only answer navigations from the same origin.
GET/HEAD /api/fs/read/*(200, 206 and 304) sendcontent-disposition: attachmentwith the filename,x-content-type-options: nosniffand a sandboxedcontent-security-policy. html, js and xml types (svg included) are sent astext/plain, since every client reads those as bytes and picks the type from the path. images, audio and video keep their type, so<img>/<video>/<audio>and range seeking work the same as before./api/experimental/media/:hashsendscontent-disposition: attachmentfor inline image types too, and uses the same policy./api/*withSec-Fetch-Site: cross-siteorsame-siteget a 403 if they are navigations, or if they carry anOriginthat is not an allowed CORS origin. clients withoutSec-Fetch-*headers (cli, tui, sdk), same-origin pages, typed urls, app.opencode.ai, the desktop renderer,--corsorigins and media elements (which send no origin) all pass./auth/connect/*and artifact views are not affected.no flag: this is a fix.
Added by the opencode bot (AI-generated).
Fixes #58914
Public API: fs.read and media responses are now sandboxed attachments (html/js/xml served as text/plain); cross-site browser navigations or disallowed origins to /api/* get 403 (backwards compatible for CLI, TUI, SDK and the app)