-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhelmet_test.go
174 lines (145 loc) · 4.76 KB
/
helmet_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package helmet
import (
"testing"
)
func TestHelmet_Secure_default(t *testing.T) {
t.Parallel()
rr, r := newRecorderRequest(t)
// default Helmet
helmet := Default()
addXPoweredByHelmetMiddleware(helmet.Secure(mockNext)).ServeHTTP(rr, r)
resp := rr.Result()
t.Run(HeaderXPoweredBy, func(t *testing.T) {
t.Parallel()
header := resp.Header.Get(HeaderXPoweredBy)
if header != "" {
t.Errorf("X-Powered-By header should be removed\tActual: %s\n", header)
}
})
testCases := []struct {
name string
header string
}{
{HeaderContentSecurityPolicy, ""},
{HeaderXContentTypeOptions, XContentTypeOptionsNoSniff.String()},
{HeaderXDNSPrefetchControl, XDNSPrefetchControlOff.String()},
{HeaderXDownloadOptions, XDownloadOptionsNoOpen.String()},
{HeaderExpectCT, ""},
{HeaderFeaturePolicy, ""},
{HeaderXFrameOptions, XFrameOptionsSameOrigin.String()},
{HeaderXPermittedCrossDomainPolicies, ""},
{HeaderReferrerPolicy, ""},
{HeaderStrictTransportSecurity, "max-age=5184000; includeSubDomains"},
{HeaderXXSSProtection, "1; mode=block"},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
header := resp.Header.Get(tc.name)
if header != tc.header {
t.Errorf("Expected: %s\tActual: %s\n", tc.header, header)
}
})
}
testMockNext(t, resp)
}
func TestHelmet_Secure_empty(t *testing.T) {
t.Parallel()
rr, r := newRecorderRequest(t)
// blank slate Helmet
helmet := Empty()
addXPoweredByHelmetMiddleware(helmet.Secure(mockNext)).ServeHTTP(rr, r)
resp := rr.Result()
t.Run("X-Powered-By", func(t *testing.T) {
t.Parallel()
header := resp.Header.Get(HeaderXPoweredBy)
if header != "Helmet" {
t.Errorf("X-Powered-By is wrong\tExpected: %s\tActual: %s\n", "Helmet", header)
}
})
t.Run("X-XSS-Protection", func(t *testing.T) {
t.Parallel()
header := resp.Header.Get(HeaderXXSSProtection)
if header != "0" {
t.Errorf("X-XSS-Protection is wrong\tExpected: %s\tActual: %s\n", "0", header)
}
})
testCases := []struct {
header string
}{
{HeaderContentSecurityPolicy},
{HeaderXContentTypeOptions},
{HeaderXDNSPrefetchControl},
{HeaderXDownloadOptions},
{HeaderExpectCT},
{HeaderFeaturePolicy},
{HeaderXFrameOptions},
{HeaderXPermittedCrossDomainPolicies},
{HeaderReferrerPolicy},
{HeaderStrictTransportSecurity},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.header, func(t *testing.T) {
t.Parallel()
header := resp.Header.Get(tc.header)
if header != "" {
t.Errorf("Header exists when it shouldn't: %s\n", header)
}
})
}
testMockNext(t, resp)
}
func TestHelmet_Secure_custom(t *testing.T) {
t.Parallel()
rr, r := newRecorderRequest(t)
// fill Helmet with custom parameters
helmet := Empty()
helmet.ContentSecurityPolicy = NewContentSecurityPolicy(map[CSPDirective][]CSPSource{
DirectiveDefaultSrc: {SourceNone},
})
helmet.XContentTypeOptions = XContentTypeOptionsNoSniff
helmet.XDNSPrefetchControl = XDNSPrefetchControlOn
helmet.XDownloadOptions = XDownloadOptionsNoOpen
helmet.ExpectCT = NewExpectCT(30, true, "/report-uri")
helmet.FeaturePolicy = NewFeaturePolicy(map[FeaturePolicyDirective][]FeaturePolicyOrigin{
DirectiveGeolocation: {OriginSelf, OriginSrc},
})
helmet.XFrameOptions = XFrameOptionsDeny
helmet.XPermittedCrossDomainPolicies = PermittedCrossDomainPoliciesAll
helmet.XPoweredBy = NewXPoweredBy(false, "PHP 4.2.0")
helmet.ReferrerPolicy = NewReferrerPolicy(DirectiveNoReferrer, DirectiveStrictOriginWhenCrossOrigin)
helmet.StrictTransportSecurity = NewStrictTransportSecurity(31536000, true, true)
helmet.XXSSProtection = NewXXSSProtection(true, DirectiveModeBlock, "/report-uri")
addXPoweredByHelmetMiddleware(helmet.Secure(mockNext)).ServeHTTP(rr, r)
resp := rr.Result()
testCases := []struct {
name string
header string
}{
{HeaderContentSecurityPolicy, "default-src 'none'"},
{HeaderXContentTypeOptions, XContentTypeOptionsNoSniff.String()},
{HeaderXDNSPrefetchControl, XDNSPrefetchControlOn.String()},
{HeaderXDownloadOptions, XDownloadOptionsNoOpen.String()},
{HeaderExpectCT, `max-age=30, enforce, report-uri="/report-uri"`},
{HeaderFeaturePolicy, "geolocation 'self' 'src'"},
{HeaderXFrameOptions, "DENY"},
{HeaderXPermittedCrossDomainPolicies, PermittedCrossDomainPoliciesAll.String()},
{HeaderXPoweredBy, "PHP 4.2.0"},
{HeaderReferrerPolicy, "no-referrer, strict-origin-when-cross-origin"},
{HeaderStrictTransportSecurity, "max-age=31536000; includeSubDomains; preload"},
{HeaderXXSSProtection, "1; mode=block; report=/report-uri"},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
header := resp.Header.Get(tc.name)
if header != tc.header {
t.Errorf("Expected: %s\tActual: %s\n", tc.header, header)
}
})
}
testMockNext(t, resp)
}