MCPcopy Index your code
hub / github.com/coder/coder / TestCSPFrameAncestors

Function TestCSPFrameAncestors

coderd/httpmw/csp_test.go:15–70  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

13)
14
15func TestCSPFrameAncestors(t *testing.T) {
16 t.Parallel()
17
18 t.Run("DefaultSelf", func(t *testing.T) {
19 t.Parallel()
20
21 r := httptest.NewRequest(http.MethodGet, "/", nil)
22 rw := httptest.NewRecorder()
23
24 httpmw.CSPHeaders(false, func() []*proxyhealth.ProxyHost {
25 return nil
26 }, nil)(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
27 rw.WriteHeader(http.StatusOK)
28 })).ServeHTTP(rw, r)
29
30 csp := rw.Header().Get("Content-Security-Policy")
31 require.Contains(t, csp, "frame-ancestors 'self'")
32 })
33
34 t.Run("OverrideViaStaticAdditions", func(t *testing.T) {
35 t.Parallel()
36
37 r := httptest.NewRequest(http.MethodGet, "/", nil)
38 rw := httptest.NewRecorder()
39
40 httpmw.CSPHeaders(false, func() []*proxyhealth.ProxyHost {
41 return nil
42 }, map[httpmw.CSPFetchDirective][]string{
43 httpmw.CSPFrameAncestors: {"https://example.com"},
44 })(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
45 rw.WriteHeader(http.StatusOK)
46 })).ServeHTTP(rw, r)
47
48 csp := rw.Header().Get("Content-Security-Policy")
49 require.Contains(t, csp, "frame-ancestors https://example.com")
50 require.NotContains(t, csp, "frame-ancestors 'self'")
51 })
52
53 t.Run("OmitWhenEmpty", func(t *testing.T) {
54 t.Parallel()
55
56 r := httptest.NewRequest(http.MethodGet, "/", nil)
57 rw := httptest.NewRecorder()
58
59 httpmw.CSPHeaders(false, func() []*proxyhealth.ProxyHost {
60 return nil
61 }, map[httpmw.CSPFetchDirective][]string{
62 httpmw.CSPFrameAncestors: {},
63 })(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
64 rw.WriteHeader(http.StatusOK)
65 })).ServeHTTP(rw, r)
66
67 csp := rw.Header().Get("Content-Security-Policy")
68 require.NotContains(t, csp, "frame-ancestors")
69 })
70}
71
72func TestCSP(t *testing.T) {

Callers

nothing calls this directly

Calls 7

CSPHeadersFunction · 0.92
RunMethod · 0.65
GetMethod · 0.65
ServeHTTPMethod · 0.45
WriteHeaderMethod · 0.45
HeaderMethod · 0.45
ContainsMethod · 0.45

Tested by

no test coverage detected