From 9af3b293420219081da2c8478c5b48df7bdfa42c Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 21 Nov 2026 02:52:11 +1100 Subject: [PATCH] fix(dashboard): drop remote images from chat Markdown and add an image-only CSP (#2073) SAFE_SRC in ui/src/chat/markdown.ts now keeps only same-origin root-relative paths and data:image/ URLs, rejecting http(s), protocol-relative and backslash-after-slash forms that the browser would read as a remote host. The shared parser also covers task approvals, plans and run transcripts. modules/dashboard sets Content-Security-Policy: img-src 'self' data: on every non-API response (page, assets, errors, no-build fallback) as the browser backstop; the /api/ JSON fallback is unchanged. No proxy, no migration: stored replies re-render from their text. --- modules/dashboard/dashboard.go | 8 ++++++++ modules/dashboard/dashboard_test.go | 14 ++++++++++---- ui/src/chat/markdown.ts | 5 ++++- ui/test/chat-markdown.test.ts | 10 ++++++++++ 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/modules/dashboard/dashboard.go b/modules/dashboard/dashboard.go index 6334ad8c0..ad55e8345 100644 --- a/modules/dashboard/dashboard.go +++ b/modules/dashboard/dashboard.go @@ -12,6 +12,12 @@ import ( const page = "index.html" +// csp is the browser-side backstop for chat-rendered Markdown: no remote +// image may load, so a reply cannot leak data through an image request. It +// is image-only on purpose; every other directive would risk breaking the +// SPA's scripts and styles. +const csp = "img-src 'self' data:" + // Routes serves the SPA from dist on mux. A path under /api/ that no module // claimed is a JSON 404, never the page. With no build embedded, the page // says how to build it. @@ -30,6 +36,7 @@ func handler(dist fs.FS) http.Handler { httpjson.Error(w, http.StatusNotFound, "no such route") return } + w.Header().Set("Content-Security-Policy", csp) http.Error(w, "this binary was built without the dashboard; run task ui and rebuild", http.StatusNotFound) }) } @@ -39,6 +46,7 @@ func handler(dist fs.FS) http.Handler { httpjson.Error(w, http.StatusNotFound, "no such route") return } + w.Header().Set("Content-Security-Policy", csp) if r.Method != http.MethodGet && r.Method != http.MethodHead { w.Header().Set("Allow", "GET, HEAD") http.Error(w, "the dashboard is read-only", http.StatusMethodNotAllowed) diff --git a/modules/dashboard/dashboard_test.go b/modules/dashboard/dashboard_test.go index 284d8167d..c0336e676 100644 --- a/modules/dashboard/dashboard_test.go +++ b/modules/dashboard/dashboard_test.go @@ -26,13 +26,16 @@ func TestRoutesServeThePageBesideTheAPICatchAll(t *testing.T) { path string wantStatus int wantContent string + // wantCSP pins the image-only Content-Security-Policy backstop on + // every non-API response (#2073); API fallbacks must not carry it. + wantCSP string }{ - {name: "the page serves a client route", dist: dist, method: http.MethodGet, path: "/runs/42", wantStatus: http.StatusOK, wantContent: "archie"}, - {name: "an asset serves as itself", dist: dist, method: http.MethodGet, path: "/assets/app-1.js", wantStatus: http.StatusOK, wantContent: "console.log(1)"}, + {name: "the page serves a client route", dist: dist, method: http.MethodGet, path: "/runs/42", wantStatus: http.StatusOK, wantContent: "archie", wantCSP: "img-src 'self' data:"}, + {name: "an asset serves as itself", dist: dist, method: http.MethodGet, path: "/assets/app-1.js", wantStatus: http.StatusOK, wantContent: "console.log(1)", wantCSP: "img-src 'self' data:"}, {name: "an unclaimed API path is JSON", dist: dist, method: http.MethodGet, path: "/api/nope", wantStatus: http.StatusNotFound, wantContent: "no such route"}, {name: "an API path denies every method", dist: dist, method: http.MethodPost, path: "/api/nope", wantStatus: http.StatusNotFound, wantContent: "no such route"}, - {name: "the page is read-only", dist: dist, method: http.MethodPost, path: "/runs", wantStatus: http.StatusMethodNotAllowed, wantContent: "read-only"}, - {name: "a build without the dashboard says how to make it", method: http.MethodGet, path: "/runs", wantStatus: http.StatusNotFound, wantContent: "task ui"}, + {name: "the page is read-only", dist: dist, method: http.MethodPost, path: "/runs", wantStatus: http.StatusMethodNotAllowed, wantContent: "read-only", wantCSP: "img-src 'self' data:"}, + {name: "a build without the dashboard says how to make it", method: http.MethodGet, path: "/runs", wantStatus: http.StatusNotFound, wantContent: "task ui", wantCSP: "img-src 'self' data:"}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { @@ -46,6 +49,9 @@ func TestRoutesServeThePageBesideTheAPICatchAll(t *testing.T) { if body := w.Body.String(); !strings.Contains(body, tt.wantContent) { t.Fatalf("body = %q, want it to contain %q", body, tt.wantContent) } + if csp := w.Header().Get("Content-Security-Policy"); csp != tt.wantCSP { + t.Fatalf("Content-Security-Policy = %q, want %q", csp, tt.wantCSP) + } }) } } diff --git a/ui/src/chat/markdown.ts b/ui/src/chat/markdown.ts index 6dd8e32ca..8ede232ae 100644 --- a/ui/src/chat/markdown.ts +++ b/ui/src/chat/markdown.ts @@ -15,7 +15,10 @@ const processor = unified().use(remarkParse).use(remarkGfm).use(remarkRehype); // A #N the model wrote about a task. Not part of a word, entity or path. const TASK_REF = /(?alert(1)", tag: "script", count: 0 }, { md: "See #12 and #13.", tag: "task-ref", count: 2 }, { md: "`#12` and [#13](https://example.com)", tag: "task-ref", count: 0 },