forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsrf_test.go
331 lines (308 loc) · 9.04 KB
/
csrf_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package csrf
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/setting"
)
func TestMiddlewareCSRF(t *testing.T) {
tests := []struct {
name string
cookieName string
method string
origin string
host string
code int
}{
{
name: "mismatched origin and host is forbidden",
cookieName: "foo",
method: "GET",
origin: "http://notLocalhost",
host: "localhost",
code: http.StatusForbidden,
},
{
name: "mismatched origin and host is NOT forbidden with a 'Safe Method'",
cookieName: "foo",
method: "TRACE",
origin: "http://notLocalhost",
host: "localhost",
code: http.StatusOK,
},
{
name: "mismatched origin and host is NOT forbidden without a cookie",
cookieName: "",
method: "GET",
origin: "http://notLocalhost",
host: "localhost",
code: http.StatusOK,
},
{
name: "malformed host is a bad request",
cookieName: "foo",
method: "GET",
host: "localhost:80:80",
code: http.StatusBadRequest,
},
{
name: "host works without port",
cookieName: "foo",
method: "GET",
host: "localhost",
origin: "http://localhost",
code: http.StatusOK,
},
{
name: "port does not have to match",
cookieName: "foo",
method: "GET",
host: "localhost:80",
origin: "http://localhost:3000",
code: http.StatusOK,
},
{
name: "IPv6 host works with port",
cookieName: "foo",
method: "GET",
host: "[::1]:3000",
origin: "http://[::1]:3000",
code: http.StatusOK,
},
{
name: "IPv6 host (with longer address) works with port",
cookieName: "foo",
method: "GET",
host: "[2001:db8::1]:3000",
origin: "http://[2001:db8::1]:3000",
code: http.StatusOK,
},
{
name: "IPv6 host (with longer address) works without port",
cookieName: "foo",
method: "GET",
host: "[2001:db8::1]",
origin: "http://[2001:db8::1]",
code: http.StatusOK,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rr := csrfScenario(t, tt.cookieName, tt.method, tt.origin, tt.host)
require.Equal(t, tt.code, rr.Code)
})
}
}
func TestCSRF_Check(t *testing.T) {
tests := []struct {
name string
request *http.Request
getCfg func() *setting.Cfg
addtHeader map[string]struct{}
trustedOrigins map[string]struct{}
safeEndpoints map[string]struct{}
expectedOK bool
expectedStatus int
}{
{
name: "base case",
getCfg: func() *setting.Cfg {
return setting.NewCfg()
},
request: postRequest(t, "", nil, true),
expectedOK: true,
},
{
name: "base with null origin header",
getCfg: func() *setting.Cfg {
return setting.NewCfg()
},
request: postRequest(t, "", map[string]string{"Origin": "null"}, true),
expectedStatus: http.StatusForbidden,
},
{
name: "grafana.org",
getCfg: func() *setting.Cfg {
return setting.NewCfg()
},
request: postRequest(t, "grafana.org", map[string]string{"Origin": "https://grafana.org"}, true),
expectedOK: true,
},
{
name: "grafana.org with X-Forwarded-Host",
getCfg: func() *setting.Cfg {
return setting.NewCfg()
},
request: postRequest(t, "grafana.localhost", map[string]string{"X-Forwarded-Host": "grafana.org", "Origin": "https://grafana.org"}, true),
expectedStatus: http.StatusForbidden,
},
{
name: "grafana.org with X-Forwarded-Host and header trusted",
getCfg: func() *setting.Cfg {
return setting.NewCfg()
},
request: postRequest(t, "grafana.localhost", map[string]string{"X-Forwarded-Host": "grafana.org", "Origin": "https://grafana.org"}, true),
addtHeader: map[string]struct{}{"X-Forwarded-Host": {}},
expectedOK: true,
},
{
name: "grafana.org from grafana.com",
getCfg: func() *setting.Cfg {
return setting.NewCfg()
},
request: postRequest(t, "grafana.org", map[string]string{"Origin": "https://grafana.com"}, true),
expectedStatus: http.StatusForbidden,
},
{
name: "grafana.org from grafana.com explicit trust for grafana.com",
getCfg: func() *setting.Cfg {
return setting.NewCfg()
},
request: postRequest(t, "grafana.org", map[string]string{"Origin": "https://grafana.com"}, true),
trustedOrigins: map[string]struct{}{"grafana.com": {}},
expectedOK: true,
},
{
name: "grafana.org from grafana.com with X-Forwarded-Host and header trusted",
getCfg: func() *setting.Cfg {
return setting.NewCfg()
},
request: postRequest(t, "grafana.localhost", map[string]string{"X-Forwarded-Host": "grafana.org", "Origin": "https://grafana.com"}, true),
addtHeader: map[string]struct{}{"X-Forwarded-Host": {}},
trustedOrigins: map[string]struct{}{"grafana.com": {}},
expectedOK: true,
},
{
name: "safe endpoint",
getCfg: func() *setting.Cfg {
return setting.NewCfg()
},
request: postRequest(t, "example.org/foo/bar", map[string]string{"Origin": "null"}, true),
safeEndpoints: map[string]struct{}{"foo/bar": {}},
expectedOK: true,
},
{
name: "grafana.org with X-Forwarded-Host; will skip csrf check if login cookie is not present; without login cookie, should return nil because login cookie is not present",
getCfg: func() *setting.Cfg {
cfg := setting.NewCfg()
cfg.SectionWithEnvOverrides("security").Key("csrf_always_check").SetValue("false")
return cfg
},
request: postRequest(t, "grafana.localhost", map[string]string{"X-Forwarded-Host": "grafana.org", "Origin": "https://grafana.org"}, false),
expectedOK: true,
},
{
name: "grafana.org with X-Forwarded-Host; will perform csrf check even if login cookie is not present, should return error because host name does not match origin",
getCfg: func() *setting.Cfg {
cfg := setting.NewCfg()
cfg.SectionWithEnvOverrides("security").Key("csrf_always_check").SetValue("true")
return cfg
},
request: postRequest(t, "grafana.localhost", map[string]string{"X-Forwarded-Host": "grafana.org", "Origin": "https://grafana.org"}, false),
expectedStatus: http.StatusForbidden,
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
csrf := ProvideCSRFFilter(tc.getCfg())
csrf.trustedOrigins = tc.trustedOrigins
csrf.headers = tc.addtHeader
csrf.safeEndpoints = tc.safeEndpoints
csrf.cfg.LoginCookieName = "LoginCookie"
err := csrf.check(tc.request)
if tc.expectedOK {
require.NoError(t, err)
} else {
require.Error(t, err)
var actual *errorWithStatus
require.True(t, errors.As(err, &actual))
assert.EqualValues(t, tc.expectedStatus, actual.HTTPStatus)
}
})
}
}
func postRequest(t testing.TB, hostname string, headers map[string]string, withLoginCookie bool) *http.Request {
t.Helper()
urlParts := strings.SplitN(hostname, "/", 2)
path := "/"
if len(urlParts) == 2 {
path = urlParts[1]
}
r, err := http.NewRequest(http.MethodPost, path, nil)
require.NoError(t, err)
r.Host = urlParts[0]
if withLoginCookie {
r.AddCookie(&http.Cookie{
Name: "LoginCookie",
Value: "this should not be important",
})
}
for k, v := range headers {
r.Header.Set(k, v)
}
return r
}
func csrfScenario(t *testing.T, cookieName, method, origin, host string) *httptest.ResponseRecorder {
req, err := http.NewRequest(method, "/", nil)
if err != nil {
t.Fatal(err)
}
req.AddCookie(&http.Cookie{
Name: cookieName,
})
// Note: Not sure where host header populates req.Host, or how that works.
req.Host = host
req.Header.Set("HOST", host)
req.Header.Set("ORIGIN", origin)
testHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
})
rr := httptest.NewRecorder()
cfg := setting.NewCfg()
cfg.LoginCookieName = cookieName
service := ProvideCSRFFilter(cfg)
handler := service.Middleware()(testHandler)
handler.ServeHTTP(rr, req)
return rr
}
func TestProvideCSRFFilter(t *testing.T) {
t.Parallel()
tests := []struct {
getInput func() *setting.Cfg
expectedAlwaysCheck bool
}{
{
getInput: func() *setting.Cfg {
return setting.NewCfg()
},
// Should default to false when config value is not set.
expectedAlwaysCheck: false,
},
{
getInput: func() *setting.Cfg {
cfg := setting.NewCfg()
cfg.SectionWithEnvOverrides("security").Key("csrf_always_check").SetValue("false")
return cfg
},
// Should be false when config value is set to false.
expectedAlwaysCheck: false,
},
{
getInput: func() *setting.Cfg {
cfg := setting.NewCfg()
cfg.SectionWithEnvOverrides("security").Key("csrf_always_check").SetValue("true")
return cfg
},
// Should be true when config value is set to true.
expectedAlwaysCheck: true,
},
}
for _, tc := range tests {
csrf := ProvideCSRFFilter(tc.getInput())
assert.Equal(t, tc.expectedAlwaysCheck, csrf.alwaysCheck)
}
}