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.
58 lines
2.7 KiB
Go
58 lines
2.7 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"io/fs"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
)
|
|
|
|
// TestRoutesServeThePageBesideTheAPICatchAll guards a boot-time panic: a
|
|
// method-scoped "GET /" fallback cannot sit beside the "/api/" catch-all on one
|
|
// ServeMux, so the binary crashed on start with the dashboard built in. It also
|
|
// pins what each path answers: an unclaimed API path is a JSON 404 rather than
|
|
// the page, and the page is read-only.
|
|
func TestRoutesServeThePageBesideTheAPICatchAll(t *testing.T) {
|
|
dist := fstest.MapFS{
|
|
page: &fstest.MapFile{Data: []byte("<html>archie</html>")},
|
|
"assets/app-1.js": &fstest.MapFile{Data: []byte("console.log(1)")},
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
dist fs.FS
|
|
method string
|
|
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: "<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)", 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", 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) {
|
|
mux := http.NewServeMux()
|
|
Routes(mux, tt.dist)
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequestWithContext(t.Context(), tt.method, tt.path, nil))
|
|
if w.Code != tt.wantStatus {
|
|
t.Fatalf("status = %d, want %d", w.Code, tt.wantStatus)
|
|
}
|
|
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)
|
|
}
|
|
})
|
|
}
|
|
}
|