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.
This commit is contained in:
yetone
2026-11-13 11:08:44 +08:00
parent dd9b2bcef5
commit 4f72725e39
2 changed files with 30 additions and 0 deletions
+6
View File
@@ -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 == "" {
+24
View File
@@ -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(`<svg xmlns="http://www.w3.org/2000/svg"><script>fetch("/v1/models")</script></svg>`)
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())
}
}