test(19.3-06): unit tests for the review fixes
- TestCORSCredentialedOriginRules, TestCORSAnchoredPatternNoSuffixMatch (D-04) - TestAPIDocsCSPNoHostSource (D-17) - TestLoadCookieConfigProductionInferredSecure, TestZeroCookieConfigDefaults (D-03) - TestSiteCachePurgeWaitAndClose, TestSiteCacheBulkCommitCoalesced (D-10)
This commit is contained in:
@@ -305,3 +305,67 @@ func TestAPIDocsTitleEscaped(t *testing.T) {
|
||||
t.Fatal("default title missing")
|
||||
}
|
||||
}
|
||||
|
||||
// cspDirectives splits a Content-Security-Policy value into its directives.
|
||||
func cspDirectives(csp string) map[string][]string {
|
||||
out := map[string][]string{}
|
||||
for _, part := range strings.Split(csp, ";") {
|
||||
fields := strings.Fields(part)
|
||||
if len(fields) == 0 {
|
||||
continue
|
||||
}
|
||||
out[fields[0]] = fields[1:]
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func TestAPIDocsCSPNoHostSource(t *testing.T) {
|
||||
h, err := Assemble(apiDocsApp(t, "api_docs:\n enabled: true\n"), []party.Plugin{specPlugin{id: "acme.zoo", spec: orderedSpecYAML}})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
nonces := map[string]bool{}
|
||||
for range 2 {
|
||||
rec := apiGet(h, "/api-docs")
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("GET /api-docs = %d", rec.Code)
|
||||
}
|
||||
dirs := cspDirectives(rec.Header().Get("Content-Security-Policy"))
|
||||
script := dirs["script-src"]
|
||||
if len(script) != 1 || !strings.HasPrefix(script[0], "'nonce-") || !strings.HasSuffix(script[0], "'") {
|
||||
t.Fatalf("script-src = %q, want exactly one nonce source", script)
|
||||
}
|
||||
nonce := strings.TrimSuffix(strings.TrimPrefix(script[0], "'nonce-"), "'")
|
||||
if nonce == "" {
|
||||
t.Fatal("empty nonce")
|
||||
}
|
||||
for _, d := range []string{"object-src", "base-uri", "frame-ancestors"} {
|
||||
if got := dirs[d]; len(got) != 1 || got[0] != "'none'" {
|
||||
t.Errorf("%s = %q, want 'none'", d, got)
|
||||
}
|
||||
}
|
||||
page := rec.Body.String()
|
||||
tags := regexp.MustCompile(`<script[^>]*>`).FindAllString(page, -1)
|
||||
if len(tags) != 3 {
|
||||
t.Fatalf("script tags = %q, want 3", tags)
|
||||
}
|
||||
for _, tag := range tags {
|
||||
if !strings.Contains(tag, ` nonce="`+nonce+`"`) {
|
||||
t.Errorf("script tag %q lacks nonce %q", tag, nonce)
|
||||
}
|
||||
}
|
||||
for _, sri := range []string{
|
||||
"sha384-Ov4/wv3j2bmct8cDc5X4ngJZohVPzEmc6uDPH8WeljUxO5vtoykvMEfbu9Vh6RaW",
|
||||
"sha384-ZPehFMQommnnuaZ4rpxgkgTT2DKFVp4hZC/7pLit+9Lek9T1YGSo23eHFbvNkXkw",
|
||||
"sha384-My2aDM4r2Mbm3ybHcubKm9O9U8FEjvF/O5nGvE9YK5dzqOTbWEKa79RPJ1krdMaF",
|
||||
} {
|
||||
if !strings.Contains(page, `integrity="`+sri+`"`) {
|
||||
t.Errorf("page lacks integrity %s", sri)
|
||||
}
|
||||
}
|
||||
nonces[nonce] = true
|
||||
}
|
||||
if len(nonces) != 2 {
|
||||
t.Fatal("two requests got the same nonce")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user