Sandbox home and artifact files served on the app origin (H1)

GET /files and /artifacts/ now send the artifact sandbox CSP and nosniff
through one helper, and /files serves HTML, XHTML, SVG, XML, XSL,
JavaScript and multipart/x-mixed-replace as attachments, including when
the type is sniffed from an extensionless file. The web proxy adds the same
headers to every proxied /files and /artifacts/ response (defence in depth
against an older server), and the peer relay shares the helper.

show_html previews are unchanged by operator decision.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 23:16:46 +00:00
parent 806b5eca46
commit e39a9560ba
9 changed files with 233 additions and 21 deletions
+1 -2
View File
@@ -291,8 +291,7 @@ func (r *runner) serveArtifact(w http.ResponseWriter, req *http.Request) {
// Enforce the boundary on the response itself: opening the raw HTML/SVG in
// a new tab must be as safe as the embedded sandboxed preview. No scripts,
// network calls, forms, same-origin authority or top-level navigation.
w.Header().Set("Content-Security-Policy", "sandbox; default-src 'none'; script-src 'none'; connect-src 'none'; style-src 'unsafe-inline'; img-src 'self' data:; media-src 'self' data:; base-uri 'none'; form-action 'none'; frame-ancestors 'self'")
w.Header().Set("X-Content-Type-Options", "nosniff")
setUntrustedContentHeaders(w.Header())
w.Header().Set("Cache-Control", "private, no-store")
http.ServeContent(w, req, path.Base(name), opened.ModTime(), file)
}
+4 -2
View File
@@ -65,7 +65,9 @@ func serveHomeFileFrom(w http.ResponseWriter, r *http.Request, home string) {
defer file.Close()
w.Header().Set("Cache-Control", "private, no-cache")
w.Header().Set("X-Content-Type-Options", "nosniff")
// A home file is untrusted content (a download, a cloned repository, an
// agent's output), never part of the control UI.
setUntrustedContentHeaders(w.Header())
contentType := mime.TypeByExtension(filepath.Ext(realTarget))
if contentType == "" {
var sample [512]byte
@@ -85,7 +87,7 @@ func serveHomeFileFrom(w http.ResponseWriter, r *http.Request, home string) {
}
w.Header().Set("Content-Type", contentType)
disposition := "inline"
if r.URL.Query().Get("download") == "1" {
if r.URL.Query().Get("download") == "1" || scriptCapableType(contentType) {
disposition = "attachment"
}
w.Header().Set("Content-Disposition", contentDisposition(disposition, filepath.Base(realTarget)))
+51
View File
@@ -86,3 +86,54 @@ func TestServeHomeFile(t *testing.T) {
}
})
}
func TestServeHomeFileSandboxesScriptCapableTypes(t *testing.T) {
home := t.TempDir()
files := map[string]string{
"page.html": "<script>fetch('/connect')</script>",
"image.svg": `<svg xmlns="http://www.w3.org/2000/svg"><script>alert(1)</script></svg>`,
"page.xhtml": "<html xmlns='http://www.w3.org/1999/xhtml'></html>",
"feed.xml": "<?xml version='1.0'?><x/>",
"noext": "<!DOCTYPE html><html><script>alert(1)</script></html>",
"notes.txt": "plain",
"photo.png": "\x89PNG\r\n\x1a\n",
}
for name, body := range files {
if err := os.WriteFile(filepath.Join(home, name), []byte(body), 0o644); err != nil {
t.Fatal(err)
}
}
for name := range files {
t.Run(name, func(t *testing.T) {
recorder := httptest.NewRecorder()
serveHomeFileFrom(recorder, httptest.NewRequest(http.MethodGet, "/files?path=~/"+name, nil), home)
if recorder.Code != http.StatusOK {
t.Fatalf("status = %d", recorder.Code)
}
header := recorder.Header()
if csp := header.Get("Content-Security-Policy"); csp != untrustedContentPolicy || !strings.HasPrefix(csp, "sandbox;") {
t.Fatalf("csp = %q", csp)
}
if header.Get("X-Content-Type-Options") != "nosniff" {
t.Fatalf("nosniff = %q", header.Get("X-Content-Type-Options"))
}
wantAttachment := name != "notes.txt" && name != "photo.png"
if got := strings.HasPrefix(header.Get("Content-Disposition"), "attachment;"); got != wantAttachment {
t.Fatalf("disposition = %q (type %q), want attachment %v", header.Get("Content-Disposition"), header.Get("Content-Type"), wantAttachment)
}
})
}
}
func TestScriptCapableType(t *testing.T) {
for _, contentType := range []string{"text/html; charset=utf-8", "application/xhtml+xml", "image/svg+xml", "text/xml", "application/xml", "application/rss+xml", "TEXT/HTML"} {
if !scriptCapableType(contentType) {
t.Errorf("%q should be script-capable", contentType)
}
}
for _, contentType := range []string{"text/plain; charset=utf-8", "image/png", "application/pdf", "application/octet-stream", ""} {
if scriptCapableType(contentType) {
t.Errorf("%q should not be script-capable", contentType)
}
}
}
+40
View File
@@ -0,0 +1,40 @@
package server
import (
"mime"
"net/http"
"strings"
)
// untrustedContentPolicy is the response boundary for every file the server
// hands back that it did not write itself: run artifacts, files under the
// operator's home and anything an agent produced. The `sandbox` directive gives
// the document an opaque origin even when opened as a top-level page, so a
// crafted HTML or SVG file cannot script the app origin, reach /connect or read
// other files. The web proxy and the peer relay add the same policy.
const untrustedContentPolicy = "sandbox; default-src 'none'; script-src 'none'; connect-src 'none'; style-src 'unsafe-inline'; img-src 'self' data:; media-src 'self' data:; base-uri 'none'; form-action 'none'; frame-ancestors 'self'"
// setUntrustedContentHeaders applies the sandbox policy and turns off MIME
// sniffing, so a response is never interpreted as a more powerful type.
func setUntrustedContentHeaders(h http.Header) {
h.Set("Content-Security-Policy", untrustedContentPolicy)
h.Set("X-Content-Type-Options", "nosniff")
}
// scriptCapableType reports whether a browser could run script in a document of
// this content type when it is opened as a page: HTML, XHTML, SVG and every XML
// flavour (XSLT and XML stylesheets can build HTML). Such files are served as
// downloads; the sandbox policy is the second layer.
func scriptCapableType(contentType string) bool {
media, _, err := mime.ParseMediaType(contentType)
if err != nil {
media = strings.ToLower(strings.TrimSpace(strings.SplitN(contentType, ";", 2)[0]))
}
switch media {
case "text/html", "application/xhtml+xml", "image/svg+xml", "text/xml", "application/xml",
"text/xsl", "application/xslt+xml", "application/javascript", "text/javascript",
"application/ecmascript", "text/ecmascript", "multipart/x-mixed-replace":
return true
}
return strings.HasSuffix(media, "+xml") || strings.HasSuffix(media, "/xml")
}