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