-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_test.go
More file actions
435 lines (385 loc) · 11.6 KB
/
Copy pathplugin_test.go
File metadata and controls
435 lines (385 loc) · 11.6 KB
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package traefik_api_key_auth
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
func TestCreateConfigDefaults(t *testing.T) {
cfg := CreateConfig()
if cfg.QueryParam {
t.Fatal("queryParam should default to false")
}
if cfg.PathSegment {
t.Fatal("pathSegment should default to false")
}
if !cfg.AuthenticationHeader || !cfg.BearerHeader {
t.Fatal("header and bearer should default to true")
}
}
func TestFindMatchingKey(t *testing.T) {
index := buildKeyIndex([]string{"key1", "secret-key", "abc"})
tests := []struct {
name string
provided string
want string
}{
{"exact match first", "key1", "key1"},
{"exact match middle", "secret-key", "secret-key"},
{"no match", "wrong", ""},
{"empty provided", "", ""},
{"prefix only no match", "key", ""},
{"substring no match", "secret", ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := findMatchingKey(tt.provided, index)
if got != tt.want {
t.Errorf("findMatchingKey() = %q, want %q", got, tt.want)
}
})
}
}
func TestExtractBearerToken(t *testing.T) {
tests := []struct {
header string
want string
}{
{"Bearer mytoken", "mytoken"},
{"Bearer spaced ", "spaced"},
{"Bearer x", "x"},
{"bearer lower", "lower"},
{"BEARER upper", "upper"},
{"Bearer too many spaces token", ""},
{"Invalid", ""},
{"", ""},
}
for _, tt := range tests {
got := extractBearerToken(tt.header)
if got != tt.want {
t.Errorf("extractBearerToken(%q) = %q, want %q", tt.header, got, tt.want)
}
}
}
func TestPathSegmentMatchesKey(t *testing.T) {
index := buildKeyIndex([]string{"api-key-123", "token"})
tests := []struct {
path string
want string
}{
{"/api/api-key-123/foo", "api-key-123"},
{"/api-key-123", "api-key-123"},
{"/prefix/api-key-123", "api-key-123"},
{"/no/match/here", ""},
{"/api/token", "token"},
{"/token/rest", "token"},
}
for _, tt := range tests {
got := pathSegmentMatchesKey(tt.path, index)
if got != tt.want {
t.Errorf("pathSegmentMatchesKey(%q) = %q, want %q", tt.path, got, tt.want)
}
}
}
func TestResolveKeys(t *testing.T) {
got, err := resolveKeys([]string{"a", "b", "a"})
if err != nil {
t.Fatal(err)
}
if len(got) != 2 || got[0] != "a" || got[1] != "b" {
t.Errorf("resolveKeys static = %v", got)
}
t.Setenv("TEST_API_KEY_PLUGIN", "from-env")
got, err = resolveKeys([]string{"static", "env:TEST_API_KEY_PLUGIN"})
if err != nil {
t.Fatal(err)
}
if len(got) != 2 || got[0] != "static" || got[1] != "from-env" {
t.Errorf("resolveKeys with env = %v", got)
}
_, err = resolveKeys([]string{"env:NOT_SET_VAR"})
if err == nil {
t.Error("resolveKeys with only unset env should error")
}
}
func TestNew(t *testing.T) {
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {})
ctx := context.Background()
_, err := New(ctx, next, nil, "test")
if err == nil {
t.Error("New with nil config should error")
}
_, err = New(ctx, next, &Config{Keys: nil}, "test")
if err == nil {
t.Error("New with no keys should error")
}
_, err = New(ctx, next, &Config{Keys: []string{}}, "test")
if err == nil {
t.Error("New with empty keys should error")
}
_, err = New(ctx, next, &Config{
Keys: []string{"k"},
AuthenticationHeader: false,
BearerHeader: false,
QueryParam: false,
PathSegment: false,
}, "test")
if err == nil {
t.Error("New with no method enabled should error")
}
_, err = New(ctx, next, &Config{Keys: []string{"k"}, AuthenticationHeader: true, AuthenticationHeaderName: ""}, "test")
if err == nil {
t.Error("New should error when authentication header name is empty")
}
_, err = New(ctx, next, &Config{Keys: []string{"k"}, BearerHeader: true, BearerHeaderName: ""}, "test")
if err == nil {
t.Error("New should error when bearer header name is empty")
}
_, err = New(ctx, next, &Config{Keys: []string{"k"}, QueryParam: true, QueryParamName: ""}, "test")
if err == nil {
t.Error("New should error when query param name is empty")
}
_, err = New(ctx, next, &Config{Keys: []string{"k"}, AuthenticationHeader: true, AuthenticationHeaderName: "X-API-KEY"}, "test")
if err != nil {
t.Errorf("New valid config: %v", err)
}
}
func TestKeyAuthServeHTTP(t *testing.T) {
ctx := context.Background()
t.Run("valid header removes source header and overwrites internal forward header", func(t *testing.T) {
var receivedInternal string
var receivedAuth string
var internalHeaderValues int
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
receivedInternal = r.Header.Get("X-Internal-Key")
receivedAuth = r.Header.Get("X-API-KEY")
internalHeaderValues = len(r.Header.Values("X-Internal-Key"))
w.WriteHeader(http.StatusOK)
})
h, err := New(ctx, next, &Config{
Keys: []string{"valid-key"},
AuthenticationHeader: true,
AuthenticationHeaderName: "X-API-KEY",
BearerHeader: false,
QueryParam: false,
PathSegment: false,
RemoveHeadersOnSuccess: true,
InternalForwardHeaderName: "X-Internal-Key",
}, "test")
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Add("X-Internal-Key", "attacker-value")
req.Header.Set("X-API-KEY", "valid-key")
rw := httptest.NewRecorder()
h.ServeHTTP(rw, req)
if rw.Code != http.StatusOK {
t.Fatalf("status = %d", rw.Code)
}
if receivedAuth != "" {
t.Fatalf("expected auth header to be removed, got %q", receivedAuth)
}
if receivedInternal != "valid-key" {
t.Fatalf("expected internal header %q, got %q", "valid-key", receivedInternal)
}
if internalHeaderValues != 1 {
t.Fatalf("expected exactly one internal header value, got %d", internalHeaderValues)
}
})
t.Run("valid bearer token allows request", func(t *testing.T) {
nextCalled := false
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
nextCalled = true
w.WriteHeader(http.StatusOK)
})
h, err := New(ctx, next, &Config{
Keys: []string{"valid-key"},
AuthenticationHeader: false,
BearerHeader: true,
BearerHeaderName: "Authorization",
QueryParam: false,
PathSegment: false,
}, "test")
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("Authorization", "bearer valid-key")
rw := httptest.NewRecorder()
h.ServeHTTP(rw, req)
if !nextCalled {
t.Fatal("next handler should be called")
}
})
t.Run("valid query param strips token", func(t *testing.T) {
var rawQuery string
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rawQuery = r.URL.RawQuery
w.WriteHeader(http.StatusOK)
})
h, err := New(ctx, next, &Config{
Keys: []string{"valid-key"},
AuthenticationHeader: false,
BearerHeader: false,
QueryParam: true,
QueryParamName: "token",
PathSegment: false,
}, "test")
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "/?token=valid-key&x=1", nil)
rw := httptest.NewRecorder()
h.ServeHTTP(rw, req)
if rw.Code != http.StatusOK {
t.Fatalf("status = %d", rw.Code)
}
if rawQuery != "x=1" {
t.Fatalf("expected query to be sanitized, got %q", rawQuery)
}
})
t.Run("valid path segment allows request", func(t *testing.T) {
nextCalled := false
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
nextCalled = true
w.WriteHeader(http.StatusOK)
})
h, err := New(ctx, next, &Config{
Keys: []string{"valid-key"},
AuthenticationHeader: false,
BearerHeader: false,
QueryParam: false,
PathSegment: true,
}, "test")
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "/api/valid-key/v1", nil)
rw := httptest.NewRecorder()
h.ServeHTTP(rw, req)
if !nextCalled {
t.Fatal("next handler should be called")
}
})
t.Run("invalid key returns 403", func(t *testing.T) {
nextCalled := false
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
nextCalled = true
w.WriteHeader(http.StatusOK)
})
h, err := New(ctx, next, &Config{
Keys: []string{"valid-key"},
AuthenticationHeader: true,
AuthenticationHeaderName: "X-API-KEY",
BearerHeader: false,
QueryParam: false,
PathSegment: false,
}, "test")
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("X-API-KEY", "wrong")
rw := httptest.NewRecorder()
h.ServeHTTP(rw, req)
if nextCalled {
t.Fatal("next handler should not be called")
}
if rw.Code != http.StatusForbidden {
t.Fatalf("status = %d, want 403", rw.Code)
}
})
t.Run("permissive mode allows invalid request", func(t *testing.T) {
nextCalled := false
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
nextCalled = true
w.WriteHeader(http.StatusOK)
})
h, err := New(ctx, next, &Config{
Keys: []string{"valid-key"},
AuthenticationHeader: true,
AuthenticationHeaderName: "X-API-KEY",
BearerHeader: false,
QueryParam: false,
PathSegment: false,
PermissiveMode: true,
}, "test")
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "/", nil)
req.Header.Set("X-API-KEY", "wrong")
rw := httptest.NewRecorder()
h.ServeHTTP(rw, req)
if !nextCalled {
t.Fatal("next handler should be called")
}
if rw.Code != http.StatusOK {
t.Fatalf("status = %d, want 200", rw.Code)
}
})
t.Run("internal error route rewrites path and clears query", func(t *testing.T) {
var nextPath string
var nextQuery string
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
nextPath = r.URL.Path
nextQuery = r.URL.RawQuery
w.WriteHeader(http.StatusOK)
})
h, err := New(ctx, next, &Config{
Keys: []string{"valid-key"},
AuthenticationHeader: true,
AuthenticationHeaderName: "X-API-KEY",
BearerHeader: false,
QueryParam: false,
PathSegment: false,
InternalErrorRoute: "errors/forbidden",
}, "test")
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "/private?token=wrong", nil)
req.Header.Set("X-API-KEY", "wrong")
rw := httptest.NewRecorder()
h.ServeHTTP(rw, req)
if nextPath != "/errors/forbidden" {
t.Fatalf("path = %q, want /errors/forbidden", nextPath)
}
if nextQuery != "" {
t.Fatalf("query should be cleared, got %q", nextQuery)
}
})
t.Run("exempt paths are normalized and skip auth", func(t *testing.T) {
nextCalled := false
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
nextCalled = true
w.WriteHeader(http.StatusOK)
})
h, err := New(ctx, next, &Config{
Keys: []string{"valid-key"},
AuthenticationHeader: true,
AuthenticationHeaderName: "X-API-KEY",
BearerHeader: false,
QueryParam: false,
PathSegment: false,
ExemptPaths: []string{" health/ ", "/metrics/"},
}, "test")
if err != nil {
t.Fatal(err)
}
req := httptest.NewRequest(http.MethodGet, "/health", nil)
rw := httptest.NewRecorder()
h.ServeHTTP(rw, req)
if !nextCalled {
t.Fatal("next handler should be called for /health")
}
nextCalled = false
req = httptest.NewRequest(http.MethodGet, "/metrics/node", nil)
rw = httptest.NewRecorder()
h.ServeHTTP(rw, req)
if !nextCalled {
t.Fatal("next handler should be called for /metrics/node")
}
})
}