-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcallback_test.go
More file actions
521 lines (460 loc) · 14.6 KB
/
callback_test.go
File metadata and controls
521 lines (460 loc) · 14.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package main
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/go-authgate/sdk-go/credstore"
)
func TestSanitizeOAuthError(t *testing.T) {
tests := []struct {
name string
errorCode string
errorDescription string
wantContains string
wantNotContains string
}{
{
name: "access_denied",
errorCode: "access_denied",
errorDescription: "User denied the request",
wantContains: "Authorization was denied",
wantNotContains: "User denied",
},
{
name: "invalid_request",
errorCode: "invalid_request",
errorDescription: "Missing required parameter: redirect_uri",
wantContains: "Invalid request",
wantNotContains: "redirect_uri",
},
{
name: "unauthorized_client",
errorCode: "unauthorized_client",
errorDescription: "Client authentication failed",
wantContains: "Client is not authorized",
wantNotContains: "authentication failed",
},
{
name: "server_error",
errorCode: "server_error",
errorDescription: "Internal database connection failed",
wantContains: "Server error",
wantNotContains: "database",
},
{
name: "temporarily_unavailable",
errorCode: "temporarily_unavailable",
errorDescription: "Service overloaded",
wantContains: "temporarily unavailable",
wantNotContains: "overloaded",
},
{
name: "unknown_error",
errorCode: "custom_error_code",
errorDescription: "Some internal error details",
wantContains: "Authentication failed",
wantNotContains: "internal",
},
{
name: "empty_description",
errorCode: "access_denied",
errorDescription: "",
wantContains: "Authorization was denied",
wantNotContains: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := sanitizeOAuthError(tt.errorCode, tt.errorDescription)
if tt.wantContains != "" && !strings.Contains(got, tt.wantContains) {
t.Errorf("sanitizeOAuthError() = %q, want to contain %q", got, tt.wantContains)
}
if tt.wantNotContains != "" && strings.Contains(got, tt.wantNotContains) {
t.Errorf(
"sanitizeOAuthError() = %q, should not contain %q",
got,
tt.wantNotContains,
)
}
})
}
}
func TestSanitizeTokenExchangeError(t *testing.T) {
tests := []struct {
name string
err error
wantContains string
wantNotContains []string
}{
{
name: "generic_error",
err: errors.New("unauthorized_client: client authentication failed"),
wantContains: "Token exchange failed",
wantNotContains: []string{"unauthorized_client", "authentication"},
},
{
name: "backend_service_error",
err: errors.New("backend service error: database connection failed"),
wantContains: "Token exchange failed",
wantNotContains: []string{"backend", "database", "service"},
},
{
name: "internal_error",
err: errors.New("internal error: validation failed for user account"),
wantContains: "Token exchange failed",
wantNotContains: []string{"internal", "validation", "account"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := sanitizeTokenExchangeError(tt.err)
if !strings.Contains(got, tt.wantContains) {
t.Errorf(
"sanitizeTokenExchangeError() = %q, want to contain %q",
got,
tt.wantContains,
)
}
for _, notWant := range tt.wantNotContains {
if strings.Contains(strings.ToLower(got), strings.ToLower(notWant)) {
t.Errorf(
"sanitizeTokenExchangeError() = %q, should not contain %q",
got,
notWant,
)
}
}
})
}
}
type callbackServerResult struct {
storage *credstore.Token
err error
}
// startCallbackServerAsync starts the callback server in a goroutine and
// returns a channel that will receive the result (storage or error).
func startCallbackServerAsync(
t *testing.T, ctx context.Context, //nolint:revive // t before ctx in test helpers
port int, state string,
exchangeFn func(context.Context, string) (*credstore.Token, error),
) chan callbackServerResult {
t.Helper()
ch := make(chan callbackServerResult, 1)
go func() {
storage, err := startCallbackServer(ctx, port, state, defaultCallbackTimeout, exchangeFn)
ch <- callbackServerResult{storage, err}
}()
// Give the server a moment to bind.
time.Sleep(50 * time.Millisecond)
return ch
}
// noExchangeFn returns an exchange function that fails the test if called.
func noExchangeFn(t *testing.T) func(context.Context, string) (*credstore.Token, error) {
t.Helper()
return func(_ context.Context, _ string) (*credstore.Token, error) {
t.Error("exchangeFn should not be called")
return nil, errors.New("should not be called")
}
}
// stubExchangeFn returns an exchange function that validates the received code
// and returns a minimal token on success.
func stubExchangeFn(wantCode string) func(context.Context, string) (*credstore.Token, error) {
return func(_ context.Context, gotCode string) (*credstore.Token, error) {
if gotCode != wantCode {
return nil, fmt.Errorf("unexpected code: got %q, want %q", gotCode, wantCode)
}
return &credstore.Token{AccessToken: "test-token"}, nil
}
}
func TestCallbackServer_Success(t *testing.T) {
const port = 19101
state := "test-state-success"
ch := startCallbackServerAsync(
t,
context.Background(),
port,
state,
stubExchangeFn("mycode123"),
)
callbackURL := fmt.Sprintf(
"http://127.0.0.1:%d/callback?code=mycode123&state=%s",
port, state,
)
resp, err := http.Get(callbackURL) //nolint:noctx,gosec // test-only HTTP call to local server
if err != nil {
t.Fatalf("GET callback failed: %v", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
t.Errorf("unexpected status %d", resp.StatusCode)
}
if !strings.Contains(string(body), "Authorization Successful") {
t.Errorf("expected success page, got: %s", string(body))
}
select {
case result := <-ch:
if result.err != nil {
t.Errorf("expected success, got error: %v", result.err)
}
if result.storage == nil {
t.Error("expected non-nil storage")
}
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for callback result")
}
}
func TestCallbackServer_StateMismatch(t *testing.T) {
const port = 19102
state := "expected-state"
ch := startCallbackServerAsync(t, context.Background(), port, state, noExchangeFn(t))
callbackURL := fmt.Sprintf(
"http://127.0.0.1:%d/callback?code=mycode&state=wrong-state",
port,
)
resp, err := http.Get(callbackURL) //nolint:noctx,gosec // test-only HTTP call to local server
if err != nil {
t.Fatalf("GET callback failed: %v", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
bodyStr := string(body)
// Verify browser shows sanitized message
if !strings.Contains(bodyStr, "Authorization Failed") {
t.Errorf("expected failure page for state mismatch, got: %s", bodyStr)
}
if !strings.Contains(bodyStr, "security issue") {
t.Errorf("expected sanitized security message in browser, got: %s", bodyStr)
}
// Verify browser does NOT show CSRF attack details
if strings.Contains(bodyStr, "CSRF") {
t.Errorf("browser should not mention CSRF attack details, got: %s", bodyStr)
}
select {
case result := <-ch:
if result.err == nil {
t.Error("expected error for state mismatch, got nil")
}
// Terminal error should contain state_mismatch
if !strings.Contains(result.err.Error(), "state_mismatch") {
t.Errorf("expected terminal error to mention state_mismatch, got: %v", result.err)
}
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for callback result")
}
}
func TestCallbackServer_OAuthError(t *testing.T) {
const port = 19103
state := "state-for-error"
ch := startCallbackServerAsync(t, context.Background(), port, state, noExchangeFn(t))
callbackURL := fmt.Sprintf(
"http://127.0.0.1:%d/callback?error=access_denied&error_description=User+denied&state=%s",
port, state,
)
resp, err := http.Get(callbackURL) //nolint:noctx,gosec // test-only HTTP call to local server
if err != nil {
t.Fatalf("GET callback failed: %v", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
bodyStr := string(body)
// Verify browser shows sanitized message
if !strings.Contains(bodyStr, "Authorization Failed") {
t.Errorf("expected failure page for access_denied, got: %s", bodyStr)
}
if !strings.Contains(bodyStr, "Authorization was denied") {
t.Errorf("expected sanitized message in browser, got: %s", bodyStr)
}
// Verify browser does NOT show detailed description
if strings.Contains(bodyStr, "User denied") {
t.Errorf("browser should not contain detailed error description, got: %s", bodyStr)
}
select {
case result := <-ch:
if result.err == nil {
t.Error("expected error for access_denied, got nil")
}
// Verify terminal error still contains full details
if !strings.Contains(result.err.Error(), "access_denied") {
t.Errorf("expected terminal error to mention access_denied, got: %v", result.err)
}
if !strings.Contains(result.err.Error(), "User denied") {
t.Errorf("expected terminal error to contain detailed description, got: %v", result.err)
}
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for callback result")
}
}
func TestCallbackServer_ExchangeFailure(t *testing.T) {
const port = 19106
state := "state-for-exchange-failure"
ch := startCallbackServerAsync(t, context.Background(), port, state,
func(_ context.Context, _ string) (*credstore.Token, error) {
return nil, errors.New("unauthorized_client: backend service authentication failed")
})
callbackURL := fmt.Sprintf(
"http://127.0.0.1:%d/callback?code=mycode&state=%s",
port, state,
)
resp, err := http.Get(callbackURL) //nolint:noctx,gosec // test-only HTTP call to local server
if err != nil {
t.Fatalf("GET callback failed: %v", err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
bodyStr := string(body)
// Verify browser shows generic message
if !strings.Contains(bodyStr, "Authorization Failed") {
t.Errorf("expected failure page for exchange error, got: %s", bodyStr)
}
if !strings.Contains(bodyStr, "Token exchange failed") {
t.Errorf("expected sanitized message in browser, got: %s", bodyStr)
}
// Verify browser does NOT show backend error details
if strings.Contains(bodyStr, "unauthorized_client") {
t.Errorf("browser should not contain backend error code, got: %s", bodyStr)
}
if strings.Contains(bodyStr, "backend service") {
t.Errorf("browser should not contain backend error details, got: %s", bodyStr)
}
select {
case result := <-ch:
if result.err == nil {
t.Error("expected error for exchange failure, got nil")
}
// Verify terminal error still contains full backend error
if !strings.Contains(result.err.Error(), "unauthorized_client") {
t.Errorf("expected terminal error to mention unauthorized_client, got: %v", result.err)
}
if !strings.Contains(result.err.Error(), "backend service") {
t.Errorf("expected terminal error to contain full details, got: %v", result.err)
}
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for callback result")
}
}
func TestCallbackServer_DoubleCallback(t *testing.T) {
const port = 19105
state := "test-state-double"
ch := startCallbackServerAsync(t, context.Background(), port, state, stubExchangeFn("mycode"))
url := fmt.Sprintf("http://127.0.0.1:%d/callback?code=mycode&state=%s", port, state)
done := make(chan error, 2)
for range 2 {
go func() {
resp, err := http.Get(url) //nolint:noctx,gosec // test-only HTTP call to local server
if err == nil {
resp.Body.Close()
}
done <- err
}()
}
for range 2 {
select {
case <-done:
case <-time.After(3 * time.Second):
t.Fatal("a callback handler goroutine hung on channel send")
}
}
select {
case result := <-ch:
if result.err != nil {
t.Errorf("expected success, got error: %v", result.err)
}
if result.storage == nil {
t.Error("expected non-nil storage")
}
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for callback result")
}
}
func TestCallbackServer_MissingCode(t *testing.T) {
const port = 19104
state := "state-for-missing-code"
ch := startCallbackServerAsync(t, context.Background(), port, state, noExchangeFn(t))
callbackURL := fmt.Sprintf(
"http://127.0.0.1:%d/callback?state=%s",
port, state,
)
resp, err := http.Get(callbackURL) //nolint:noctx,gosec // test-only HTTP call to local server
if err != nil {
t.Fatalf("GET callback failed: %v", err)
}
defer resp.Body.Close()
select {
case result := <-ch:
if result.err == nil {
t.Error("expected error for missing code, got nil")
}
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for callback result")
}
}
func TestCallbackServer_RejectsNonGetMethod(t *testing.T) {
const port = 19107
state := "test-state-method"
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
ch := startCallbackServerAsync(t, ctx, port, state, noExchangeFn(t))
callbackURL := fmt.Sprintf(
"http://127.0.0.1:%d/callback?code=mycode&state=%s",
port, state,
)
resp, err := http.Post(
callbackURL,
"",
nil,
) //nolint:noctx,gosec // test-only HTTP call to local server
if err != nil {
t.Fatalf("POST callback failed: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusMethodNotAllowed {
t.Errorf("expected 405 Method Not Allowed, got %d", resp.StatusCode)
}
cancel()
select {
case result := <-ch:
if result.err == nil {
t.Error("expected context cancellation error")
}
case <-time.After(3 * time.Second):
t.Fatal("timed out waiting for callback result")
}
}
func TestWriteCallbackPage_SecurityHeaders(t *testing.T) {
tests := []struct {
name string
success bool
message string
}{
{"success_page", true, ""},
{"failure_page", false, "Something went wrong"},
}
wantHeaders := map[string]string{
"Content-Type": "text/html; charset=utf-8",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"Content-Security-Policy": "default-src 'none'; style-src 'unsafe-inline'",
"Cache-Control": "no-store",
"Referrer-Policy": "no-referrer",
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := httptest.NewRecorder()
writeCallbackPage(w, tt.success, tt.message)
resp := w.Result()
defer resp.Body.Close()
for name, want := range wantHeaders {
if got := resp.Header.Get(name); got != want {
t.Errorf("header %s = %q, want %q", name, got, want)
}
}
})
}
}