Skip to content

fix(server): sandbox file reads against same-origin script - #58763

Closed
JasonFLee wants to merge 1 commit into
anomalyco:devfrom
JasonFLee:sandbox-fs-read
Closed

JasonFLee wants to merge 1 commit into
anomalyco:devfrom
JasonFLee:sandbox-fs-read

Conversation

@JasonFLee

@JasonFLee JasonFLee commented Oct 24, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #58914

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

GET /api/fs/read/* served files with their inferred MIME type and no hardening headers. Because the app and API share an origin and the session cookie is SameSite=Lax, navigating to an .html or script-bearing .svg file in a workspace ran its script in the app's origin, where it could call the authenticated API.

This adds two headers to every response from that endpoint, including 304 revalidations: Content-Security-Policy: sandbox and X-Content-Type-Options: nosniff. The body, content type, range handling and cache validators are unchanged, so image and media loading through the same endpoint is unaffected.

How did you verify your code works?

Added a test in packages/server/test/fs.test.ts that writes a real page.html, requests it through the endpoint, and asserts text/html plus both headers. It fails on the unpatched handler and passes with this change.

cd packages/server && bun test — 98 pass, 1 skip, 0 fail.
cd packages/server && bun run typecheck — clean.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions github-actions Bot added needs:issue needs:compliance This means the issue will auto-close after 2 hours. labels Oct 24, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for your contribution!

This PR doesn't have a linked issue. All PRs must reference an existing issue.

Please:

  1. Open an issue describing the bug/feature (if one doesn't exist)
  2. Add Fixes #<number> or Closes #<number> to this PR description

See CONTRIBUTING.md for details.

The fs.read endpoint serves repository (and other on-disk) content from the
same origin as the HTTP API. A paired browser holds a SameSite=Lax session
cookie, so a top-level navigation to /api/fs/read/page.html sends the cookie
and a browser renders the response as an HTML document in the API origin.
Any script in that file can then call the authenticated API, including
credential.list, shell, and fs.write.

Mark file responses as inert content: sandbox gives the document an opaque
origin and blocks scripts, and nosniff prevents MIME reinterpretation. The
bytes and content type are unchanged, so image and media loading keeps
working.

Test: assert both headers on a real .html response; the test fails on the
previous handler.

@opencode-agent opencode-agent 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.

Looks correct. I ran opencode serve from source on the base and on this branch and requested an .html file through /api/fs/read/*. The base returns it as text/html with no CSP or nosniff. With this change it also sends Content-Security-Policy: sandbox and X-Content-Type-Options: nosniff, and body, validators and range handling are unchanged. A 304 still sends only the validators; that's fine because browsers keep the stored 200's headers. test/fs.test.ts passes, the new assertion fails on the base, and tsc -b is clean. I couldn't run a browser here, so the script blocking itself wasn't observed.

The web app only loads fs.read files as images and markdown <img> tags, or through fetch. I found no iframe, embed or object use, so sandbox and nosniff shouldn't break anything there.

Public API: two response headers are added on GET /api/fs/read/*. Status codes, body and schema are unchanged, so it's backwards compatible.

No blocking concerns.

@opencode-agent opencode-agent 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 is fine. The 304 revalidation response now carries Content-Security-Policy: sandbox and X-Content-Type-Options: nosniff, the same as the 200/206 responses. I checked with curl against opencode serve run from source. test/fs.test.ts passes and tsc -b is clean.

Public API: the 304 response for GET /api/fs/read/* now also has the two headers. This is backwards compatible.

No new concerns.

@github-actions github-actions Bot removed the needs:compliance This means the issue will auto-close after 2 hours. label Oct 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@github-actions

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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