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()) + } +}