Repository navigation
Expand file tree
/
Copy pathmain_test.go
More file actions
1265 lines (1203 loc) · 47.3 KB
/
Copy pathmain_test.go
File metadata and controls
1265 lines (1203 loc) · 47.3 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"go/types"
"io/fs"
"net"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"regexp"
"slices"
"strconv"
"strings"
"testing"
"time"
"github.com/go-chi/chi/v5"
"github.com/prometheus/client_golang/prometheus/testutil"
"go.uber.org/zap"
"go.uber.org/zap/zaptest/observer"
"github.com/babs/mcp-auth-proxy/handlers"
"github.com/babs/mcp-auth-proxy/metrics"
"github.com/babs/mcp-auth-proxy/middleware"
)
// TestCIDRAwareKey covers R2-H1: keying walks XFF right-to-left from
// the trusted ingress, stopping at the first hop NOT in the trusted
// CIDR set. That stops a client behind a typical k8s ingress from
// minting an unbounded rate-limit bucket per request by appending
// arbitrary leftmost values.
func TestCIDRAwareKey(t *testing.T) {
_, podCIDR, err := net.ParseCIDR("10.0.0.0/8")
if err != nil {
t.Fatalf("parse cidr: %v", err)
}
keyFn := cidrAwareKey([]*net.IPNet{podCIDR}, "")
t.Run("trusted_peer_uses_rightmost_untrusted_xff_hop", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/x", nil)
req.RemoteAddr = "10.1.2.3:12345"
// nginx-ingress style: client IP is rightmost-trusted-1.
req.Header.Set("X-Forwarded-For", "203.0.113.99, 10.42.0.7")
key, err := keyFn(req)
if err != nil {
t.Fatalf("keyFn: %v", err)
}
if key != "203.0.113.99" {
t.Errorf("want client IP from rightmost-trusted-1, got %q", key)
}
})
t.Run("trusted_peer_ignores_attacker_appended_leftmost_xff", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/x", nil)
req.RemoteAddr = "10.1.2.3:12345"
// Attacker behind an ingress that APPENDS XFF: their value
// is the leftmost entry. With a right-to-left walk we land
// on the ingress' own view of the immediate peer
// (203.0.113.99), not on whatever the attacker forged.
req.Header.Set("X-Forwarded-For", "evil-spoof, 203.0.113.99, 10.42.0.7")
key, err := keyFn(req)
if err != nil {
t.Fatalf("keyFn: %v", err)
}
if key == "evil-spoof" {
t.Errorf("attacker spoof leaked into bucket key: %q", key)
}
if key != "203.0.113.99" {
t.Errorf("want client IP from rightmost-trusted-1, got %q", key)
}
})
t.Run("trusted_peer_ignores_true_client_ip_by_default", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/x", nil)
req.RemoteAddr = "10.1.2.3:12345"
// Default header is X-Forwarded-For; True-Client-IP must
// NOT influence the bucket without an explicit opt-in.
req.Header.Set("True-Client-IP", "evil-spoof")
key, err := keyFn(req)
if err != nil {
t.Fatalf("keyFn: %v", err)
}
if key == "evil-spoof" {
t.Errorf("True-Client-IP leaked into bucket key without TRUSTED_PROXY_HEADER opt-in: %q", key)
}
})
t.Run("trusted_peer_uses_pinned_header_when_opted_in", func(t *testing.T) {
fn := cidrAwareKey([]*net.IPNet{podCIDR}, "X-Real-Ip")
req := httptest.NewRequest(http.MethodGet, "/x", nil)
req.RemoteAddr = "10.1.2.3:12345"
req.Header.Set("X-Real-Ip", "203.0.113.42")
// XFF should be ignored when the operator pinned a different header.
req.Header.Set("X-Forwarded-For", "203.0.113.99")
key, err := fn(req)
if err != nil {
t.Fatalf("keyFn: %v", err)
}
if key != "203.0.113.42" {
t.Errorf("want pinned X-Real-Ip value, got %q", key)
}
})
t.Run("untrusted_peer_uses_remote_addr", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/x", nil)
req.RemoteAddr = "203.0.113.50:443"
req.Header.Set("X-Forwarded-For", "10.1.2.3")
key, err := keyFn(req)
if err != nil {
t.Fatalf("keyFn: %v", err)
}
if key == "10.1.2.3" {
t.Errorf("untrusted peer must NOT honor XFF; got spoofed key %q", key)
}
if key != "203.0.113.50" {
t.Errorf("want RemoteAddr-derived key %q, got %q", "203.0.113.50", key)
}
})
t.Run("trusted_peer_no_xff_falls_back_to_remote_addr", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/x", nil)
req.RemoteAddr = "10.1.2.3:12345"
key, err := keyFn(req)
if err != nil {
t.Fatalf("keyFn: %v", err)
}
if key != "10.1.2.3" {
t.Errorf("want fallback to RemoteAddr, got %q", key)
}
})
t.Run("trusted_peer_all_hops_trusted_falls_back", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/x", nil)
req.RemoteAddr = "10.1.2.3:12345"
// Pathological: every XFF entry is itself a trusted hop.
// No untrusted origin to bucket on — fall back to RemoteAddr.
req.Header.Set("X-Forwarded-For", "10.42.0.1, 10.42.0.2, 10.42.0.3")
key, err := keyFn(req)
if err != nil {
t.Fatalf("keyFn: %v", err)
}
if key != "10.1.2.3" {
t.Errorf("want fallback to RemoteAddr when every XFF hop is trusted, got %q", key)
}
})
t.Run("malformed_xff_hop_falls_back", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/x", nil)
req.RemoteAddr = "10.1.2.3:12345"
req.Header.Set("X-Forwarded-For", "not-an-ip")
key, err := keyFn(req)
if err != nil {
t.Fatalf("keyFn: %v", err)
}
if key != "10.1.2.3" {
t.Errorf("want fallback to RemoteAddr on malformed hop, got %q", key)
}
})
t.Run("bad_remote_addr_falls_back", func(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/x", nil)
req.RemoteAddr = "garbage"
if _, err := keyFn(req); err != nil {
t.Errorf("fallback should not error, got %v", err)
}
})
}
// TestZapMiddleware_SkipRE verifies ACCESS_LOG_SKIP_RE suppresses the
// access-log line for matching paths while leaving non-matching paths
// and the handler response untouched. A nil regex is the
// log-everything default. The handler MUST always run regardless of
// match — skipping the log line must never skip the handler.
func TestZapMiddleware_SkipRE(t *testing.T) {
healthz := regexp.MustCompile(`^/healthz$`)
probes := regexp.MustCompile(`^/(healthz|readyz)$`)
cases := []struct {
name string
re *regexp.Regexp
path string
wantLogs int
}{
{"nil_logs_healthz", nil, "/healthz", 1},
{"healthz_re_skips_healthz", healthz, "/healthz", 0},
{"healthz_re_skips_with_query", healthz, "/healthz?verbose=1", 0},
{"healthz_re_logs_other", healthz, "/mcp", 1},
{"healthz_re_logs_trailing_slash", healthz, "/healthz/", 1},
{"probes_re_skips_readyz", probes, "/readyz", 0},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var called bool
next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
called = true
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
})
core, logs := observer.New(zap.InfoLevel)
h := zapMiddleware(zap.New(core), tc.re, nil)(next) // nil rpcMetrics — RPC observer not under test here
rec := httptest.NewRecorder()
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, tc.path, nil))
if !called {
t.Fatal("next handler was not invoked — skip branch must always call next")
}
if rec.Code != http.StatusOK {
t.Errorf("status = %d, want 200", rec.Code)
}
if got := logs.FilterMessage("request").Len(); got != tc.wantLogs {
t.Errorf("access-log count = %d, want %d", got, tc.wantLogs)
}
// resp_content_type is the runbooks' fingerprint for telling
// the HTML error page from the JSON body — it must survive
// refactors.
if tc.wantLogs == 1 {
fields := logs.FilterMessage("request").All()[0].ContextMap()
if got, ok := fields["resp_content_type"]; !ok || got != "application/json" {
t.Errorf("resp_content_type = %v (present=%v), want application/json", got, ok)
}
}
})
}
}
// TestZapMiddleware_RPCMetrics_GateAndFanOut pins both axes of the
// rpc-metrics observer:
//
// - per-tool counter: invoked once per tools/call entry (single
// request OR per-entry inside a batch). Tool name passes through
// verbatim (downstream cardinality guard maps "" / overflow).
// - batch counter: invoked exactly once per HTTP request that
// decoded as a batch with at least one tools/call entry, AFTER
// all per-tool fan-outs land. Carries the request's actual bytes.
//
// Skipped cases (no per-tool, no batch invocation): protocol-level
// methods (initialize / notifications/* / tools/list / prompts/*),
// empty requests, batches with zero tools/call entries.
func TestZapMiddleware_RPCMetrics_GateAndFanOut(t *testing.T) {
cases := []struct {
name string
method string
tool string
batch []middleware.RPCCall
wantTools []string // perTool invocations in order
wantBatches int // batch invocations
}{
{name: "tools_call_invokes", method: "tools/call", tool: "weather", wantTools: []string{"weather"}},
{name: "tools_call_unknown_tool", method: "tools/call", tool: "", wantTools: []string{""}},
{name: "initialize_skipped", method: "initialize"},
{name: "notifications_skipped", method: "notifications/initialized"},
{name: "tools_list_skipped", method: "tools/list"},
{name: "empty_skipped"},
{
name: "batch_two_tools_calls_fans_out_and_counts_batch",
method: "tools/call,tools/call",
batch: []middleware.RPCCall{
{Method: "tools/call", Tool: "weather"},
{Method: "tools/call", Tool: "search"},
},
wantTools: []string{"weather", "search"},
wantBatches: 1,
},
{
name: "batch_mixed_only_tools_call_counts_and_batch_fires_once",
method: "tools/call,initialize,tools/call",
batch: []middleware.RPCCall{
{Method: "tools/call", Tool: "weather"},
{Method: "initialize"},
{Method: "tools/call", Tool: "search"},
},
wantTools: []string{"weather", "search"},
wantBatches: 1,
},
{
name: "batch_no_tools_calls_skipped_entirely",
method: "initialize,tools/list",
batch: []middleware.RPCCall{
{Method: "initialize"},
{Method: "tools/list"},
},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var gotTools []string
var gotBatches int
obs := &rpcMetrics{
perTool: func(tool string, _ int, _ int64, _ int) {
gotTools = append(gotTools, tool)
},
batch: func(_ int, _ int64, _ int) {
gotBatches++
},
}
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if rec := middleware.LogRecordFromContext(r.Context()); rec != nil {
rec.RPCMethod = tc.method
rec.RPCTool = tc.tool
rec.RPCBatch = tc.batch
}
w.WriteHeader(http.StatusOK)
})
h := zapMiddleware(zap.NewNop(), nil, obs)(next)
rr := httptest.NewRecorder()
h.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, "/mcp", nil))
if len(gotTools) != len(tc.wantTools) {
t.Fatalf("perTool count = %d (%v), want %d (%v)", len(gotTools), gotTools, len(tc.wantTools), tc.wantTools)
}
for i, w := range tc.wantTools {
if gotTools[i] != w {
t.Errorf("perTool call %d: got tool=%q, want %q", i, gotTools[i], w)
}
}
if gotBatches != tc.wantBatches {
t.Errorf("batch invocations = %d, want %d", gotBatches, tc.wantBatches)
}
})
}
}
// TestRegisterOAuthRoutes_BrowserFacingWiring pins the wiring itself,
// not just the middleware's behaviour: every handler-level test installs
// BrowserFacing by hand, so all of them stay green if the router stops
// installing it. This asserts the routes as mounted.
//
// Each route gets its OWN stub that names itself in a header before
// deferring to the negotiated 429 sink: one shared stub would stay
// green with two handlers swapped between paths, and the sink is what
// reads which representation a route produces.
func TestRegisterOAuthRoutes_BrowserFacingWiring(t *testing.T) {
stub := func(name string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Handler", name)
handlers.RateLimitExceeded(w, r)
}
}
r := chi.NewRouter()
registerOAuthRoutes(r, oauthRoutes{
Register: stub("register"), Authorize: stub("authorize"), Consent: stub("consent"),
Callback: stub("callback"), Token: stub("token"),
RegisterLimit: passthrough, AuthorizeLimit: passthrough, ConsentLimit: passthrough,
CallbackLimit: passthrough, TokenLimit: passthrough,
})
cases := []struct {
method, path, wantHandler, wantCT string
}{
// Browser-terminated: the human reads the response.
{http.MethodGet, "/authorize", "authorize", "text/html; charset=utf-8"},
{http.MethodPost, "/consent", "consent", "text/html; charset=utf-8"},
{http.MethodGet, "/callback", "callback", "text/html; charset=utf-8"},
// Machine endpoints: RFC 6749 §5.2 / RFC 7591 §3.2.2 require JSON
// whatever the caller asks for.
{http.MethodPost, "/register", "register", "application/json"},
{http.MethodPost, "/token", "token", "application/json"},
}
for _, tc := range cases {
t.Run(tc.path, func(t *testing.T) {
req := httptest.NewRequest(tc.method, tc.path, nil)
req.Header.Set("Accept", "text/html,application/xhtml+xml,*/*;q=0.8")
req.Header.Set("Sec-Fetch-Dest", "document")
rr := httptest.NewRecorder()
r.ServeHTTP(rr, req)
if got := rr.Result().Header.Get("X-Handler"); got != tc.wantHandler {
t.Errorf("%s %s served by handler %q, want %q", tc.method, tc.path, got, tc.wantHandler)
}
if got := rr.Result().Header.Get("Content-Type"); got != tc.wantCT {
t.Errorf("%s %s: Content-Type = %q, want %q", tc.method, tc.path, got, tc.wantCT)
}
})
}
}
// TestRegisterOAuthRoutes_MarkerOutsideLimiter pins the ordering
// constraint on EVERY browser-facing route: BrowserFacing must wrap the
// limiter, not the reverse. Inverted, the throttle response never sees
// the marker and a throttled human is back to the JSON dead end — with
// every other test still green, since they all install the middleware
// by hand instead of going through the router.
func TestRegisterOAuthRoutes_MarkerOutsideLimiter(t *testing.T) {
reached := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) })
cases := []struct {
name, method, path, wantCT string
limited func(*oauthRoutes, func(http.Handler) http.Handler)
}{
{"authorize", http.MethodGet, "/authorize", "text/html; charset=utf-8", func(rt *oauthRoutes, l func(http.Handler) http.Handler) { rt.AuthorizeLimit = l }},
{"consent", http.MethodPost, "/consent", "text/html; charset=utf-8", func(rt *oauthRoutes, l func(http.Handler) http.Handler) { rt.ConsentLimit = l }},
{"callback", http.MethodGet, "/callback", "text/html; charset=utf-8", func(rt *oauthRoutes, l func(http.Handler) http.Handler) { rt.CallbackLimit = l }},
// The two machine endpoints carry different budgets from the
// browser ones: their binding must be pinned too, or the router
// can swap limiters (and so budgets) with every test green.
{"register", http.MethodPost, "/register", "application/json", func(rt *oauthRoutes, l func(http.Handler) http.Handler) { rt.RegisterLimit = l }},
{"token", http.MethodPost, "/token", "application/json", func(rt *oauthRoutes, l func(http.Handler) http.Handler) { rt.TokenLimit = l }},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
rt := oauthRoutes{
Register: reached, Authorize: reached, Consent: reached, Callback: reached, Token: reached,
RegisterLimit: passthrough, AuthorizeLimit: passthrough, ConsentLimit: passthrough,
CallbackLimit: passthrough, TokenLimit: passthrough,
}
tc.limited(&rt, rateLimiter(1, time.Minute, "wiring-"+tc.name))
r := chi.NewRouter()
registerOAuthRoutes(r, rt)
send := func() *httptest.ResponseRecorder {
req := httptest.NewRequest(tc.method, tc.path, nil)
req.Header.Set("Accept", "text/html,application/xhtml+xml,*/*;q=0.8")
req.Header.Set("Sec-Fetch-Dest", "document")
rr := httptest.NewRecorder()
r.ServeHTTP(rr, req)
return rr
}
if first := send(); first.Result().StatusCode != http.StatusOK {
t.Fatalf("first request: want 200, got %d", first.Result().StatusCode)
}
throttled := send()
if throttled.Result().StatusCode != http.StatusTooManyRequests {
t.Fatalf("second request: want 429, got %d", throttled.Result().StatusCode)
}
if got := throttled.Result().Header.Get("Content-Type"); got != tc.wantCT {
t.Errorf("throttled Content-Type = %q, want %q on %s — wrong limiter binding or BrowserFacing inside the limiter", got, tc.wantCT, tc.path)
}
})
}
}
// A nil limiter would leave the route unthrottled. Wiring must fail
// loudly rather than serve it, and the message must name the route.
func TestRegisterOAuthRoutes_NilLimiterPanics(t *testing.T) {
stub := http.HandlerFunc(handlers.RateLimitExceeded)
defer func() {
r := recover()
if r == nil {
t.Fatal("a nil limiter was accepted — the route would serve unthrottled")
}
if msg, _ := r.(string); !strings.Contains(msg, "/callback") {
t.Errorf("panic %q does not name the offending route", r)
}
}()
registerOAuthRoutes(chi.NewRouter(), oauthRoutes{
Register: stub, Authorize: stub, Consent: stub, Callback: stub, Token: stub,
RegisterLimit: passthrough, AuthorizeLimit: passthrough, ConsentLimit: passthrough,
CallbackLimit: nil, TokenLimit: passthrough,
})
}
// TestRateLimiter_ThrottleNegotiates pins that a throttled human on a
// browser-facing route gets the error page, not a JSON dead end — the
// 429 is the throttle a user is most likely to meet, and it used to be
// the one response that bypassed content negotiation entirely.
func TestRateLimiter_ThrottleNegotiates(t *testing.T) {
limited := handlers.BrowserFacing(rateLimiter(1, time.Minute, "negotiate-test")(
http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) })))
send := func(accept string) *httptest.ResponseRecorder {
req := httptest.NewRequest(http.MethodGet, "/callback", nil)
req.Header.Set("Accept", accept)
// A real top-level navigation: the 429 page arm requires the
// navigation signal on top of Accept (anti-amplification).
req.Header.Set("Sec-Fetch-Dest", "document")
rr := httptest.NewRecorder()
limited.ServeHTTP(rr, req)
return rr
}
browserAccept := "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
if first := send(browserAccept); first.Code != http.StatusOK {
t.Fatalf("first request: want 200, got %d", first.Code)
}
throttled := send(browserAccept)
if throttled.Code != http.StatusTooManyRequests {
t.Fatalf("second request: want 429, got %d", throttled.Code)
}
if ct := throttled.Result().Header.Get("Content-Type"); ct != "text/html; charset=utf-8" {
t.Errorf("Content-Type = %q, want text/html; charset=utf-8 (body: %s)", ct, throttled.Body.String())
}
if body := throttled.Body.String(); !strings.Contains(body, "Temporarily unavailable") {
t.Errorf("throttle page missing its title:\n%s", body)
}
if got := throttled.Result().Header.Get("Vary"); got != "Accept, Sec-Fetch-Dest" {
t.Errorf("Vary = %q, want \"Accept, Sec-Fetch-Dest\"", got)
}
// The page tells the user to wait and retry; the response has to
// say how long, and the value has to be the window rather than a
// default that happens to be non-empty.
if got := throttled.Result().Header.Get("Retry-After"); got != "60" {
t.Errorf("Retry-After = %q, want 60 (the limiter window)", got)
}
}
// TestRateLimiter_StripsXRateLimitHeaders verifies the wrapper around
// httprate suppresses the X-RateLimit-Limit / -Remaining / -Reset
// headers on both the success and the 429 paths. Production MCP
// servers don't surface these; we match that posture so an attacker
// cannot pace just-under-the-limit floods.
func TestRateLimiter_StripsXRateLimitHeaders(t *testing.T) {
mw := rateLimiter(2, time.Minute, "test")
next := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
h := mw(next)
probe := func(req *http.Request) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
return rr
}
// First request: should be allowed (200).
first := probe(httptest.NewRequest(http.MethodGet, "/x", nil))
if first.Code != http.StatusOK {
t.Fatalf("first request: want 200, got %d", first.Code)
}
for _, k := range []string{"X-Ratelimit-Limit", "X-Ratelimit-Remaining", "X-Ratelimit-Reset"} {
if v := first.Header().Get(k); v != "" {
t.Errorf("first response leaked %s = %q", k, v)
}
}
// Trip the limit: second + third requests, third should 429.
for range 2 {
probe(httptest.NewRequest(http.MethodGet, "/x", nil))
}
throttled := probe(httptest.NewRequest(http.MethodGet, "/x", nil))
if throttled.Code != http.StatusTooManyRequests {
t.Fatalf("throttled request: want 429, got %d", throttled.Code)
}
for _, k := range []string{"X-Ratelimit-Limit", "X-Ratelimit-Remaining", "X-Ratelimit-Reset"} {
if v := throttled.Header().Get(k); v != "" {
t.Errorf("429 response leaked %s = %q", k, v)
}
}
}
// TestRateLimiter_AuthorizeAndConsent_IndependentBuckets pins the
// wiring: /authorize and /consent each get their own httprate bucket
// instance, so a flood that exhausts one path does not poison the
// other. A regression that re-shared the limiter (the earlier shape
// before the dedicated consentLimit) is caught by triggering one
// label's 429 and verifying the other label's counter stays flat.
func TestRateLimiter_AuthorizeAndConsent_IndependentBuckets(t *testing.T) {
authorizeMW := rateLimiter(2, time.Minute, "authorize")
consentMW := rateLimiter(2, time.Minute, "consent")
// Sanity — they must be distinct middleware instances (i.e. the
// caller in main.go must construct two separate rateLimiter calls,
// not reuse one).
authorizeBefore := testutil.ToFloat64(metrics.RateLimited.WithLabelValues("authorize"))
consentBefore := testutil.ToFloat64(metrics.RateLimited.WithLabelValues("consent"))
noop := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) })
authH := authorizeMW(noop)
consentH := consentMW(noop)
send := func(h http.Handler) int {
req := httptest.NewRequest(http.MethodPost, "/x", nil)
rr := httptest.NewRecorder()
h.ServeHTTP(rr, req)
return rr.Code
}
// Burn /authorize's bucket: 2 allowed, 3rd should 429.
send(authH)
send(authH)
if got := send(authH); got != http.StatusTooManyRequests {
t.Fatalf("authorize 3rd request: want 429, got %d", got)
}
// /consent's bucket should be untouched: 2 allowed, 3rd 429s on
// its own counter, NOT on the authorize one.
send(consentH)
send(consentH)
if got := send(consentH); got != http.StatusTooManyRequests {
t.Fatalf("consent 3rd request: want 429, got %d", got)
}
// Each label's counter incremented exactly once (the 3rd
// request on each path).
if delta := testutil.ToFloat64(metrics.RateLimited.WithLabelValues("authorize")) - authorizeBefore; delta != 1 {
t.Errorf("authorize RateLimited delta = %v, want 1", delta)
}
if delta := testutil.ToFloat64(metrics.RateLimited.WithLabelValues("consent")) - consentBefore; delta != 1 {
t.Errorf("consent RateLimited delta = %v, want 1 (regression: /consent likely sharing the /authorize bucket)", delta)
}
}
// TestSecurityHeaders pins the public-listener security-headers
// baseline. Every response (regardless of handler outcome — 200, 401,
// 404, 500) MUST carry the five headers. Verified by routing a no-op
// handler, an error handler, and a chain that calls writeOAuthError.
func TestSecurityHeaders(t *testing.T) {
wantHeaders := map[string]string{
"Strict-Transport-Security": "max-age=63072000; includeSubDomains",
"X-Content-Type-Options": "nosniff",
"X-Frame-Options": "DENY",
"Referrer-Policy": "no-referrer",
"Content-Security-Policy": "default-src 'none'; frame-ancestors 'none'",
}
cases := []struct {
name string
handler http.HandlerFunc
status int
}{
{
name: "ok_200",
handler: func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
},
status: http.StatusOK,
},
{
name: "json_400",
handler: func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{"error":"invalid_request"}`))
},
status: http.StatusBadRequest,
},
{
name: "redirect_302",
handler: func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Location", "https://example/cb?error=x")
w.WriteHeader(http.StatusFound)
},
status: http.StatusFound,
},
{
name: "panic_recovered",
handler: func(_ http.ResponseWriter, _ *http.Request) {
// Recoverer would normally wrap; for this test we
// just ensure the headers were set BEFORE the panic.
panic("boom")
},
status: http.StatusInternalServerError, // synthesized by deferred recover
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
h := securityHeaders(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rec := recover(); rec != nil {
w.WriteHeader(http.StatusInternalServerError)
}
}()
tc.handler(w, r)
}))
rr := httptest.NewRecorder()
h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/", nil))
for k, want := range wantHeaders {
if got := rr.Header().Get(k); got != want {
t.Errorf("%s: header %q = %q, want %q", tc.name, k, got, want)
}
}
})
}
}
// Every http.Server this binary builds must cap MaxHeaderBytes: Go's
// 1 MB default lets a single request drag a megabyte of headers through
// the pre-auth path (the Accept negotiation scan among others), which
// the rate limiter by construction does not cover. Asserted over the
// AST because the servers are built inside main() and are not reachable
// from a test.
func TestServers_CapMaxHeaderBytes(t *testing.T) {
fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "main.go", nil, 0)
if err != nil {
t.Fatalf("parse main.go: %v", err)
}
found := 0
ast.Inspect(file, func(n ast.Node) bool {
lit, ok := n.(*ast.CompositeLit)
if !ok {
return true
}
sel, ok := lit.Type.(*ast.SelectorExpr)
if !ok || sel.Sel.Name != "Server" {
return true
}
if pkg, ok := sel.X.(*ast.Ident); !ok || pkg.Name != "http" {
return true
}
found++
for _, elt := range lit.Elts {
kv, ok := elt.(*ast.KeyValueExpr)
if !ok {
continue
}
key, ok := kv.Key.(*ast.Ident)
if !ok || key.Name != "MaxHeaderBytes" {
continue
}
// Value, not just presence: the field exists to shrink the
// pre-auth header budget, so 1<<20 (net/http's own default,
// the value this test exists to reject) must not pass.
n, err := strconv.Atoi(types.ExprString(kv.Value))
if err != nil {
// Shift expressions do not constant-fold in ExprString.
var base, shift int
if _, e := fmt.Sscanf(types.ExprString(kv.Value), "%d << %d", &base, &shift); e != nil {
t.Errorf("main.go:%d: MaxHeaderBytes value %q is not a plain integer or shift — cannot verify the budget",
fset.Position(kv.Pos()).Line, types.ExprString(kv.Value))
return true
}
n = base << shift
}
if n > 64<<10 {
t.Errorf("main.go:%d: MaxHeaderBytes = %d, want <= %d — the point of the field is a budget smaller than net/http's 1 MB default",
fset.Position(kv.Pos()).Line, n, 64<<10)
}
return true
}
t.Errorf("main.go:%d: http.Server literal does not set MaxHeaderBytes — it inherits net/http's 1 MB default",
fset.Position(lit.Pos()).Line)
return true
})
if found < 2 {
t.Fatalf("found %d http.Server literals in main.go, want >=2 (public + metrics) — this test went blind", found)
}
}
// A wrong method on a browser-facing route must answer through the
// error sink, not chi's default empty-bodied 405: /consent is a URL
// users bookmark, and a blank page is exactly what this feature exists
// to remove.
func TestRegisterOAuthRoutes_MethodNotAllowedNegotiates(t *testing.T) {
r := routerWith405(t)
// GET /consent — the bookmark case. POST-only route, so 405.
// Sec-Fetch-Dest marks it a real top-level navigation, which the
// unbounded 405 path requires before it will ship the page.
req := httptest.NewRequest(http.MethodGet, "/consent", nil)
req.Header.Set("Accept", "text/html,application/xhtml+xml,*/*;q=0.8")
req.Header.Set("Sec-Fetch-Dest", "document")
rr := httptest.NewRecorder()
r.ServeHTTP(rr, req)
if rr.Result().StatusCode != http.StatusMethodNotAllowed {
t.Fatalf("status = %d, want 405", rr.Result().StatusCode)
}
if ct := rr.Result().Header.Get("Content-Type"); ct != "text/html; charset=utf-8" {
t.Errorf("Content-Type = %q, want the negotiated page", ct)
}
if body := rr.Body.String(); !strings.Contains(body, "method_not_allowed") {
t.Errorf("405 page carries no support code:\n%s", body)
}
// A machine caller on the same wrong method stays on JSON.
jsonReq := httptest.NewRequest(http.MethodGet, "/consent", nil)
jsonRR := httptest.NewRecorder()
r.ServeHTTP(jsonRR, jsonReq)
if ct := jsonRR.Result().Header.Get("Content-Type"); ct != "application/json" {
t.Errorf("machine caller Content-Type = %q, want application/json", ct)
}
}
// routerWith405 builds the production route shape plus the 405
// responder, which main installs only once every route is registered.
func routerWith405(t *testing.T) chi.Router {
t.Helper()
stub := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) })
r := chi.NewRouter()
r.Get("/healthz", stub)
// Production route shapes the OAuth five do not cover, and which
// allowedMethods has a branch for each of: a multi-method route
// (sorting + ", " joining), an all-methods Handle route (chi's "*"
// pseudo-method), and a wildcard pattern (skipped entirely).
r.Get("/multi", stub)
r.Put("/multi", stub)
r.Post("/multi", stub)
r.Handle("/mcp", stub)
r.Handle("/mcp/*", stub)
registerOAuthRoutes(r, oauthRoutes{
Register: stub, Authorize: stub, Consent: stub, Callback: stub, Token: stub,
RegisterLimit: passthrough, AuthorizeLimit: passthrough, ConsentLimit: passthrough,
CallbackLimit: passthrough, TokenLimit: passthrough,
})
installMethodNotAllowed(r)
installNotFound(r)
return r
}
// The 405 responder is router-wide, so its negotiation has to be scoped
// per path or it re-opens on /token and /register the HTML arm the
// BrowserFacing marker exists to keep shut (RFC 6749 §5.2, RFC 7591
// §3.2.2). Every non-browser-facing route on the router is checked, not
// just the OAuth ones — the handler answers for all of them.
func TestMethodNotAllowed_NegotiatesOnlyOnBrowserRoutes(t *testing.T) {
r := routerWith405(t)
for _, tc := range []struct {
method, path, wantCT string
}{
{http.MethodPost, "/authorize", "text/html; charset=utf-8"},
{http.MethodGet, "/consent", "text/html; charset=utf-8"},
{http.MethodPost, "/callback", "text/html; charset=utf-8"},
{http.MethodGet, "/token", "application/json"},
{http.MethodGet, "/register", "application/json"},
{http.MethodPost, "/healthz", "application/json"},
} {
req := httptest.NewRequest(tc.method, tc.path, nil)
req.Header.Set("Accept", "text/html,application/xhtml+xml,*/*;q=0.8")
req.Header.Set("Sec-Fetch-Dest", "document")
rr := httptest.NewRecorder()
r.ServeHTTP(rr, req)
if rr.Result().StatusCode != http.StatusMethodNotAllowed {
t.Errorf("%s %s: status = %d, want 405", tc.method, tc.path, rr.Result().StatusCode)
continue
}
if ct := rr.Result().Header.Get("Content-Type"); ct != tc.wantCT {
t.Errorf("%s %s: Content-Type = %q, want %q", tc.method, tc.path, ct, tc.wantCT)
}
}
}
// RFC 9110 §15.5.6 makes Allow a MUST on 405. chi's default responder
// sets it; a replacement that forgets to drops a mandatory header on
// every route at once.
func TestMethodNotAllowed_SetsAllow(t *testing.T) {
r := routerWith405(t)
for _, tc := range []struct{ method, path, want string }{
{http.MethodGet, "/consent", "POST"},
{http.MethodPost, "/callback", "GET"},
{http.MethodGet, "/token", "POST"},
{http.MethodPost, "/healthz", "GET"},
// Multi-method: pins both the sort (map iteration is random, so
// an unsorted Allow differs across restarts) and the ", " join.
{http.MethodDelete, "/multi", "GET, POST, PUT"},
} {
rr := httptest.NewRecorder()
r.ServeHTTP(rr, httptest.NewRequest(tc.method, tc.path, nil))
if got := rr.Result().Header.Get("Allow"); got != tc.want {
t.Errorf("%s %s: Allow = %q, want %q", tc.method, tc.path, got, tc.want)
}
}
}
// The proxy-rendered pages allow their inline <style> by sha256 hash,
// never by 'unsafe-inline' (handlers/pages.go). Two documents kept
// describing the pre-hash policy long after the code changed, so the
// class of defect — prose asserting a CSP directive the code does not
// emit — gets a guard instead of a promise to remember. Matches the
// directive form only: naming 'unsafe-inline' to say it is NOT used is
// exactly what several of these documents legitimately do.
func TestDocs_DoNotClaimUnsafeInlineStyles(t *testing.T) {
// Regexp, not a fixed substring: a real CSP names a source before
// the keyword (`style-src 'self' 'unsafe-inline'`), and the fixed
// form saw none of those.
banned := regexp.MustCompile(`style-src[^;\n]*'unsafe-inline'`)
negated := regexp.MustCompile(`(?i)\b(not|never|rather than|instead of|no longer|without|must NOT)\b`)
root, err := os.Getwd()
if err != nil {
t.Fatalf("getwd: %v", err)
}
// Getwd echoes $PWD, which may be a symlink to the checkout; WalkDir
// does not follow a symlinked root and would visit one entry.
if root, err = filepath.EvalSymlinks(root); err != nil {
t.Fatalf("evalsymlinks: %v", err)
}
scanned := 0
seen := map[string]bool{}
err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
if d.Name() == ".git" || d.Name() == "node_modules" {
return fs.SkipDir
}
return nil
}
// .go too: the claim also rots in code comments, and one did —
// handlers/consent.go described the pre-hash policy for a whole
// review round with this guard green.
switch filepath.Ext(path) {
case ".md", ".go":
default:
return nil
}
// This file states the pattern in order to search for it.
if strings.HasSuffix(path, "main_test.go") {
return nil
}
scanned++
if rel, e := filepath.Rel(root, path); e == nil {
seen[rel] = true
}
body, err := os.ReadFile(path)
if err != nil {
return err
}
// Whitespace-normalised over a two-line window: every doc here
// is hard-wrapped at ~70 columns and the claim is 24 chars, so
// a reflow splitting it across lines is the likely shape, not a
// contrived one.
inFence, fenceLine := false, 0
lines := strings.Split(string(body), "\n")
joined := func(i int) string {
if i+1 < len(lines) {
return strings.Join(strings.Fields(lines[i]+" "+lines[i+1]), " ")
}
return strings.Join(strings.Fields(lines[i]), " ")
}
for i, line := range lines {
// Fenced blocks quote code, including code as it stood at
// the time of a dated incident note — that is a record, not
// a claim about today. Prose is the claim.
if strings.HasPrefix(strings.TrimSpace(line), "```") {
inFence = !inFence
fenceLine = i + 1
continue
}
// A negated mention is the correct thing to write — every
// doc here explains what the CSP uses *instead of*
// 'unsafe-inline'. Only an assertion that it IS used is a
// defect.
text := joined(i)
if !inFence && banned.MatchString(text) && !negated.MatchString(text) {
rel, _ := filepath.Rel(root, path)
t.Errorf("%s:%d claims style-src 'unsafe-inline', but every proxy-rendered page allows its style by sha256 hash:\n\t%s",
rel, i+1, strings.TrimSpace(joined(i)))
}
}
// An unbalanced fence would latch inFence true to EOF and blind
// the rest of the file — itself a doc defect, so fail on it.
if inFence {
rel, _ := filepath.Rel(root, path)
t.Errorf("%s:%d opens a code fence that is never closed — everything after it escapes this guard", rel, fenceLine)
}
return nil
})
if err != nil {
t.Fatalf("walk: %v", err)
}
// A walk that reaches nothing must fail loudly rather than pass.
// A count alone does not prove the files that make CSP claims were
// among those scanned; name them.
for _, must := range []string{"README.md", "specs.md", "docs/threat-model.md", "manifests/overlays/production/README.md", "handlers/pages.go"} {
if !seen[must] {
t.Errorf("%s was never scanned — the walk missed a file that makes CSP claims", must)
}
}
if scanned < 30 {
t.Fatalf("scanned only %d files, want >=30 — the walk went blind", scanned)
}
}
// browserFacingPaths and the BrowserFacing wiring in
// registerOAuthRoutes are two statements of the same fact, and the 405
// responder trusts the list. Derive the marked set from the live router
// rather than restating it: markedness is observed through
// RateLimitExceeded, whose arm choice IS the marker the responder
// reads, so a route marked on the handler and missing from the list
// fails here instead of silently answering JSON on its 405.
func TestBrowserFacingPaths_MatchesTheWiring(t *testing.T) {
stub := http.HandlerFunc(handlers.RateLimitExceeded)
r := chi.NewRouter()
registerOAuthRoutes(r, oauthRoutes{
Register: stub, Authorize: stub, Consent: stub, Callback: stub, Token: stub,
RegisterLimit: passthrough, AuthorizeLimit: passthrough, ConsentLimit: passthrough,
CallbackLimit: passthrough, TokenLimit: passthrough,
})
var marked []string
probed := 0
for _, route := range r.Routes() {
for method := range route.Handlers {
if method == "*" {
continue
}
req := httptest.NewRequest(method, route.Pattern, nil)
req.Header.Set("Accept", "text/html")
req.Header.Set("Sec-Fetch-Dest", "document")
rr := httptest.NewRecorder()
r.ServeHTTP(rr, req)
probed++
if strings.HasPrefix(rr.Result().Header.Get("Content-Type"), "text/html") {
marked = append(marked, route.Pattern)
}
}
}
if probed < 5 {
t.Fatalf("probed only %d routes, want >=5 — the walk went blind", probed)
}
want := slices.Clone(browserFacingPaths)
slices.Sort(want)
slices.Sort(marked)
if !slices.Equal(marked, want) {
t.Errorf("routes wired with BrowserFacing = %v, browserFacingPaths = %v — installMethodNotAllowed reads the list, so the two must agree", marked, want)