Files
pipelines/internal/server/untrusted_content.go
T
alan e39a9560ba 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>
2026-12-03 23:16:46 +00:00

41 lines
1.9 KiB
Go

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")
}