fix(files): serve inline SVG previews with a script-blocking CSP - #520
Open
hobostay wants to merge 1 commit into
Open
fix(files): serve inline SVG previews with a script-blocking CSP#520hobostay wants to merge 1 commit into
hobostay wants to merge 1 commit into
Conversation
…ed as a document streamFile() served image/svg+xml inline with no Content-Security-Policy or X-Content-Type-Options, while every other document the app generates (the DOCX preview) already ships a restrictive policy. SVG is the only inline preview type a browser executes as a document, so a repo-controlled SVG opened directly — for example by following a transcript link such as http://127.0.0.1:30141/api/files/.../logo.svg?type=read — runs script in the Pi Web origin, with access to every /api route (and the user's Basic Auth credentials when PI_WEB_PASSWORD is set). Serve SVG with the same policy the DOCX preview uses, mirroring its directives so legit SVGs keep inline styles and data-URI images, and add nosniff to all streamed responses. CSP response headers do not affect <img> embedding, so in-app previews render exactly as before. Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
GET /api/files/<path>.svg?type=readservesimage/svg+xmlinline throughstreamFile(), which sets onlyContent-Type/Cache-Control/Accept-Ranges/Content-Disposition— noContent-Security-Policy, noX-Content-Type-Options. The DOCX preview route (?type=preview) already ships a restrictive CSP, but SVG is the one repo-controlled preview type a browser executes as a document, and it was left open.SVG is HTML-capable, so an SVG inside a project opened in Pi Web can carry
<script>:Navigating to the file URL renders it as a same-origin document (
http://127.0.0.1:30141/api/files/...), where the script runs with full access to every/api/*route — including routes that read/write provider credentials — and with the user's Basic Auth credentials attached automatically whenPI_WEB_PASSWORDis set. A realistic entry point: the agent quotes or emits a link to an SVG asset in a transcript, andMarkdownBodyrenders non-local hrefs astarget="_blank"links; one click executes. This matters precisely for the "open a repo you don't fully trust" workflow the rest of the codebase (path-security, request-security, DOCX CSP) is already hardened against.Fix
image/svg+xmlwith the same policy the DOCX preview already uses (default-src 'none'; img-src data:; style-src 'unsafe-inline'; base-uri 'none'; form-action 'none'; frame-ancestors 'self'+Referrer-Policy: no-referrer), so scripts can never run while legit SVGs keep inline styles and data-URI images when viewed as a document.X-Content-Type-Options: nosniffto everystreamFile()response.<img>embedding, so the file viewer renders SVGs exactly as before.Tests:
app/api/files/stream-route.test.mjs(source-assertion, following the existingwatch-route.test.mjsconvention) asserts the headers exist on the shared header object used by allstreamFileresponses. Full suite 590/590,tsc --noEmitandeslintclean.🤖 Generated with Claude Code