Skip to content

fix(server): serve file reads as downloads with explicit content headers - #62422

Merged
thdxr merged 2 commits into
devfrom
file-read-headers
Nov 6, 2026
Merged

thdxr merged 2 commits into
devfrom
file-read-headers

Conversation

@thdxr

@thdxr thdxr commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

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) send content-disposition: attachment with the filename, x-content-type-options: nosniff and a sandboxed content-security-policy. html, js and xml types (svg included) are sent as text/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/:hash sends content-disposition: attachment for inline image types too, and uses the same policy.
  • requests to /api/* with Sec-Fetch-Site: cross-site or same-site get a 403 if they are navigations, or if they carry an Origin that is not an allowed CORS origin. clients without Sec-Fetch-* headers (cli, tui, sdk), same-origin pages, typed urls, app.opencode.ai, the desktop renderer, --cors origins 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)

@opencodejr opencodejr 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.

The fix works as described. I checked it against opencode serve from source on the base and on this PR:

  • Base: an .html file came back from /api/fs/read/* inline as text/html, and cross-site navigations or calls from any origin got 200.
  • This PR: the same file comes back as a text/plain attachment with the sandboxed CSP and nosniff. 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 -b in 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 include content-disposition: attachment with the filename, a sandboxed content-security-policy and x-content-type-options: nosniff. The content-type changes from html/js/xml types (including svg) to text/plain; charset=utf-8.
  • GET /api/experimental/media/:hash: PNG, JPEG, GIF and WebP images now also get content-disposition: attachment, and the CSP allows img-src/media-src 'self'.
  • /api/* (new): returns 403 ForbiddenError for browser requests marked Sec-Fetch-Site: cross-site/same-site that are navigations, or that carry an Origin outside 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

@opencodejr opencodejr 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.

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.

@thdxr
thdxr merged commit b3b02ae into dev Nov 6, 2026
20 checks passed
@thdxr
thdxr deleted the file-read-headers branch November 6, 2026 21:00
@amrkmn amrkmn mentioned this pull request Nov 7, 2026
1 task
autonull pushed a commit to autonull/opencode_spacegraph that referenced this pull request Nov 7, 2026
agentsofdtd Bot pushed a commit to DefendTheDisabled/acf-opencode that referenced this pull request Dec 1, 2026
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).
agentsofdtd Bot pushed a commit to DefendTheDisabled/acf-opencode that referenced this pull request Dec 1, 2026
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).
agentsofdtd Bot pushed a commit to DefendTheDisabled/acf-opencode that referenced this pull request Dec 1, 2026
… 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.
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.

fs.read serves HTML/SVG as same-origin documents with no sandbox headers

1 participant