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.
This commit is contained in:
4 files changed
+32
-5
No files matched your search
@@ -12,6 +12,12 @@ import (
|
|||||||
|
|
||||||
const page = "index.html"
|
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
|
// 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
|
// claimed is a JSON 404, never the page. With no build embedded, the page
|
||||||
// says how to build it.
|
// says how to build it.
|
||||||
@@ -30,6 +36,7 @@ func handler(dist fs.FS) http.Handler {
|
|||||||
httpjson.Error(w, http.StatusNotFound, "no such route")
|
httpjson.Error(w, http.StatusNotFound, "no such route")
|
||||||
return
|
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)
|
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")
|
httpjson.Error(w, http.StatusNotFound, "no such route")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
w.Header().Set("Content-Security-Policy", csp)
|
||||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||||
w.Header().Set("Allow", "GET, HEAD")
|
w.Header().Set("Allow", "GET, HEAD")
|
||||||
http.Error(w, "the dashboard is read-only", http.StatusMethodNotAllowed)
|
http.Error(w, "the dashboard is read-only", http.StatusMethodNotAllowed)
|
||||||
|
|||||||
@@ -26,13 +26,16 @@ func TestRoutesServeThePageBesideTheAPICatchAll(t *testing.T) {
|
|||||||
path string
|
path string
|
||||||
wantStatus int
|
wantStatus int
|
||||||
wantContent string
|
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: "<html>archie</html>"},
|
{name: "the page serves a client route", dist: dist, method: http.MethodGet, path: "/runs/42", wantStatus: http.StatusOK, wantContent: "<html>archie</html>", 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)"},
|
{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 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: "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: "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"},
|
{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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
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) {
|
if body := w.Body.String(); !strings.Contains(body, tt.wantContent) {
|
||||||
t.Fatalf("body = %q, want it to contain %q", 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)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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.
|
// A #N the model wrote about a task. Not part of a word, entity or path.
|
||||||
const TASK_REF = /(?<![\w&#/])#(\d{1,9})(?!\w)/g;
|
const TASK_REF = /(?<![\w&#/])#(\d{1,9})(?!\w)/g;
|
||||||
const SAFE_HREF = /^(?:https?:\/\/|\/(?!\/)|#)/i;
|
const SAFE_HREF = /^(?:https?:\/\/|\/(?!\/)|#)/i;
|
||||||
const SAFE_SRC = /^(?:https?:\/\/|\/(?!\/)|data:image\/)/i;
|
// Images render only from same-origin paths and inline data: a remote image
|
||||||
|
// URL is a data-exfil sink, and /\ or // makes the browser treat the rest as
|
||||||
|
// a host, so both slash forms after the leading / are rejected too.
|
||||||
|
const SAFE_SRC = /^(?:\/(?![/\\])|data:image\/)/i;
|
||||||
|
|
||||||
export function parseMarkdown(text: string | null | undefined): Root {
|
export function parseMarkdown(text: string | null | undefined): Root {
|
||||||
const tree = processor.runSync(processor.parse(text ?? "")) as Root;
|
const tree = processor.runSync(processor.parse(text ?? "")) as Root;
|
||||||
|
|||||||
@@ -24,6 +24,16 @@ test("parseMarkdown keeps only safe URLs and links task references in prose", ()
|
|||||||
{ md: "[x](//evil.example)", tag: "a", count: 0 },
|
{ md: "[x](//evil.example)", tag: "a", count: 0 },
|
||||||
{ md: ")", tag: "img", count: 0 },
|
{ md: ")", tag: "img", count: 0 },
|
||||||
{ md: "", tag: "img", count: 1 },
|
{ md: "", tag: "img", count: 1 },
|
||||||
|
// Remote and remote-interpretable image URLs must not survive: the
|
||||||
|
// browser would fetch them, leaking data in the query string (#2073).
|
||||||
|
{ md: "", tag: "img", count: 0 },
|
||||||
|
{ md: "", tag: "img", count: 0 },
|
||||||
|
{ md: "", tag: "img", count: 0 },
|
||||||
|
// remark percent-encodes a backslash in a destination, so /\evil stays a
|
||||||
|
// same-origin path (/%5C...); SAFE_SRC still rejects a literal /\ form.
|
||||||
|
{ md: "", tag: "img", count: 1 },
|
||||||
|
// Allowed controls: same-origin root-relative path and inline data image.
|
||||||
|
{ md: "", tag: "img", count: 1 },
|
||||||
{ md: "<script>alert(1)</script>", tag: "script", count: 0 },
|
{ md: "<script>alert(1)</script>", tag: "script", count: 0 },
|
||||||
{ md: "See #12 and #13.", tag: "task-ref", count: 2 },
|
{ md: "See #12 and #13.", tag: "task-ref", count: 2 },
|
||||||
{ md: "`#12` and [#13](https://example.com)", tag: "task-ref", count: 0 },
|
{ md: "`#12` and [#13](https://example.com)", tag: "task-ref", count: 0 },
|
||||||
|
|||||||
Reference in new issue
Block a user