Files

95 lines
3.6 KiB
Go
Raw Permalink Normal View History

package main
import (
"crypto/sha256"
"encoding/base64"
"net/http"
"net/http/httptest"
"strings"
"testing"
"testing/fstest"
)
func scriptHash(text string) string {
sum := sha256.Sum256([]byte(text))
return "'sha256-" + base64.StdEncoding.EncodeToString(sum[:]) + "'"
}
func TestInlineScriptHashesCoversOnlyInlineScripts(t *testing.T) {
const bootstrap = "\n\t{\n\t\tconst element = document.currentScript.parentElement;\n\t}\n"
page := `<html><head><script src="/_app/start.js"></script>
<script type="module" crossorigin src="/_app/other.js"></script><script></script></head>
<body><script>` + bootstrap + `</script></body></html>`
got := inlineScriptHashes([]byte(page))
if len(got) != 1 || got[0] != scriptHash(bootstrap) {
t.Fatalf("inlineScriptHashes() = %v, want only %s", got, scriptHash(bootstrap))
}
}
func TestInlineScriptHashesNormalizesNewlinesLikeABrowser(t *testing.T) {
got := inlineScriptHashes([]byte("<script>a();\r\nb();\rc();</script>"))
if len(got) != 1 || got[0] != scriptHash("a();\nb();\nc();") {
t.Fatalf("inlineScriptHashes() = %v, want the hash of the LF-normalized text", got)
}
}
func TestDashboardCSPAllowsOnlyOwnAndHashedScripts(t *testing.T) {
const bootstrap = "start();"
policy := dashboardCSP([]byte("<body><script>" + bootstrap + "</script></body>"))
directives := map[string]string{}
for _, part := range strings.Split(policy, "; ") {
name, value, _ := strings.Cut(part, " ")
directives[name] = value
}
if want := "'self' " + scriptHash(bootstrap); directives["script-src"] != want {
t.Fatalf("script-src = %q, want %q", directives["script-src"], want)
}
for name, want := range map[string]string{
"default-src": "'self'",
"object-src": "'none'",
"base-uri": "'self'",
"frame-ancestors": "'none'",
} {
if directives[name] != want {
t.Fatalf("%s = %q, want %q", name, directives[name], want)
}
}
if strings.Contains(directives["script-src"], "unsafe") {
t.Fatalf("script-src = %q, must not allow unsafe-inline or unsafe-eval", directives["script-src"])
}
}
func TestDashboardCSPWithoutInlineScriptAllowsOnlyOwnOrigin(t *testing.T) {
policy := dashboardCSP([]byte("<html></html>"))
if !strings.Contains(policy, "script-src 'self';") {
t.Fatalf("policy = %q, want script-src limited to 'self'", policy)
}
}
func TestSPAFileServerSendsDashboardCSPOnThePageOnly(t *testing.T) {
const bootstrap = "start();"
root := fstest.MapFS{
"index.html": {Data: []byte("<html><body><script>" + bootstrap + "</script></body></html>")},
"_app/immutable/app.abc.js": {Data: []byte("console.log(1)")},
}
// Behind withSecurityHeaders, as in main: the page's policy replaces the
// frame-ancestors-only default instead of being added to it.
handler := withSecurityHeaders(spaFileServer(root))
for _, path := range []string{"/", "/index.html", "/lists/123"} {
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, httptest.NewRequest(http.MethodGet, path, nil))
values := recorder.Header().Values("Content-Security-Policy")
if len(values) != 1 || !strings.Contains(values[0], scriptHash(bootstrap)) || !strings.Contains(values[0], "frame-ancestors 'none'") {
t.Fatalf("GET %s Content-Security-Policy = %q, want one full policy with the bootstrap hash", path, values)
}
}
asset := httptest.NewRecorder()
handler.ServeHTTP(asset, httptest.NewRequest(http.MethodGet, "/_app/immutable/app.abc.js", nil))
if got := asset.Header().Get("Content-Security-Policy"); got != "frame-ancestors 'none'" {
t.Fatalf("asset Content-Security-Policy = %q, want the frame-ancestors default", got)
}
}