From 4f72725e396c8cb80f9414a809e65c766431ab2b Mon Sep 17 00:00:00 2001 From: yetone Date: Fri, 13 Nov 2026 11:08:33 +0800 Subject: [PATCH] gateway: a drawn SVG is served under a CSP that runs nothing The image server's files (b2ccfe25d keeps them an hour, behind a link with a token) were served with the vendor's type and nosniff only. A Recraft SVG opened from its link is a document on the gateway's own origin, and a page there is same-origin to the gateway, which a local request reaches without a key: script in the SVG could have asked /v1/chat/completions on the user's subscriptions. An SVG is now served with "default-src 'none'; style-src 'unsafe-inline'; sandbox", as the GUI's SVG icons already are (library.go, providers.go). Other types are unchanged, so a video still plays in its own page. Run: go test -run TestImageMCP ./internal/gateway ok (mote). Broken by hand: with the header line removed, TestImageMCPSVGRunsNothing is red (mica). Not tried: a real Recraft SVG in a browser. --- internal/gateway/image_mcp.go | 6 ++++++ internal/gateway/image_mcp_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/internal/gateway/image_mcp.go b/internal/gateway/image_mcp.go index 13e08419e..0c7c92547 100644 --- a/internal/gateway/image_mcp.go +++ b/internal/gateway/image_mcp.go @@ -803,6 +803,12 @@ func (s *Server) imageMCPFile(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", f.mime) w.Header().Set("X-Content-Type-Options", "nosniff") w.Header().Set("Cache-Control", "private, max-age=3600") + if strings.HasPrefix(f.mime, "image/svg") { + // a vendor's SVG opened on its own runs nothing: script in it + // would be on the gateway's own origin, which a local page may + // ask without a key (as the GUI's SVG icons are served) + w.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; sandbox") + } // ranges, which a player asks a video in (Safari only plays an MP4 // served so) if f.path == "" { diff --git a/internal/gateway/image_mcp_test.go b/internal/gateway/image_mcp_test.go index 18fe9e299..18dc401cf 100644 --- a/internal/gateway/image_mcp_test.go +++ b/internal/gateway/image_mcp_test.go @@ -530,3 +530,27 @@ func TestImageMCPIsServed(t *testing.T) { t.Fatalf("apis: %d %s", w.Code, w.Body.String()) } } + +// an SVG a vendor drew is served so that a browser opening its link runs +// nothing in it: the gateway's origin is one a local page reaches without +// a key, so script there could spend the user's subscriptions +func TestImageMCPSVGRunsNothing(t *testing.T) { + s, _ := easeled(t) + h := lanGuard(s.Handler()) + svg := []byte(``) + s.imageStore.mu.Lock() + if s.imageStore.files == nil { + s.imageStore.files = map[string]*imageFile{} + } + s.imageStore.files["a.svg"] = &imageFile{mime: "image/svg+xml", mem: svg, size: len(svg), made: time.Now()} + s.imageStore.files["a.png"] = &imageFile{mime: "image/png", mem: pngBytes, size: len(pngBytes), made: time.Now()} + s.imageStore.mu.Unlock() + w := imageGet(h, "127.0.0.1:5000", "http://127.0.0.1:3425"+ImageMCPFiles+"a.svg") + csp := w.Header().Get("Content-Security-Policy") + if w.Code != 200 || w.Body.String() != string(svg) || !strings.Contains(csp, "default-src 'none'") || !strings.Contains(csp, "sandbox") || strings.Contains(csp, "script-src") { + t.Fatalf("svg: %d %v", w.Code, w.Header()) + } + if w := imageGet(h, "127.0.0.1:5000", "http://127.0.0.1:3425"+ImageMCPFiles+"a.png"); w.Code != 200 || w.Header().Get("Content-Security-Policy") != "" { + t.Fatalf("png: %d %v", w.Code, w.Header()) + } +}