|
| 1 | +// Copyright 2025 The Go MCP SDK Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by an MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package oauthex |
| 6 | + |
| 7 | +import ( |
| 8 | + "net/http" |
| 9 | + "net/http/httptest" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCheckHTTPSOrLoopback(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + addr string |
| 18 | + wantErr bool |
| 19 | + }{ |
| 20 | + {name: "empty", addr: "", wantErr: false}, |
| 21 | + {name: "https dns allowed", addr: "https://example.com/prm", wantErr: false}, |
| 22 | + {name: "cleartext dns disallowed", addr: "http://example.com/prm", wantErr: true}, |
| 23 | + {name: "https loopback ip allowed", addr: "http://127.0.0.1:8080/prm", wantErr: false}, |
| 24 | + {name: "http localhost allowed", addr: "http://localhost/prm", wantErr: false}, |
| 25 | + {name: "https private ip disallowed", addr: "https://10.0.0.1/prm", wantErr: true}, |
| 26 | + {name: "link local metadata disallowed", addr: "https://169.254.169.254/", wantErr: true}, |
| 27 | + {name: "mapped private address disallowed", addr: "https://[::ffff:10.0.0.1]/prm", wantErr: true}, |
| 28 | + {name: "https public ip allowed", addr: "https://8.8.8.8/prm", wantErr: false}, |
| 29 | + } |
| 30 | + for _, tt := range tests { |
| 31 | + t.Run(tt.name, func(t *testing.T) { |
| 32 | + err := checkHTTPSOrLoopback(tt.addr) |
| 33 | + if (err != nil) != tt.wantErr { |
| 34 | + t.Errorf("checkHTTPSOrLoopback(%q) error = %v, wantErr %v", tt.addr, err, tt.wantErr) |
| 35 | + } |
| 36 | + }) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestDiscoveryClientRedirect(t *testing.T) { |
| 41 | + newRequestTo := func(u string) *http.Request { |
| 42 | + t.Helper() |
| 43 | + r, err := http.NewRequest(http.MethodGet, u, nil) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("NewRequest(%q): %v", u, err) |
| 46 | + } |
| 47 | + return r |
| 48 | + } |
| 49 | + testCases := []struct { |
| 50 | + name string |
| 51 | + custom *http.Client |
| 52 | + req *http.Request |
| 53 | + via []*http.Request |
| 54 | + wantBlocked bool |
| 55 | + }{ |
| 56 | + { |
| 57 | + name: "redirect to public allowed", |
| 58 | + req: newRequestTo("https://8.8.8.8/"), |
| 59 | + via: []*http.Request{newRequestTo("https://93.184.216.34/")}, |
| 60 | + wantBlocked: false, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "downgrade blocked", |
| 64 | + req: newRequestTo("http://93.184.216.34/"), |
| 65 | + via: []*http.Request{newRequestTo("https://93.184.216.34/")}, |
| 66 | + wantBlocked: true, |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "redirect to loopback blocked", |
| 70 | + req: newRequestTo("https://127.0.0.1/"), |
| 71 | + via: []*http.Request{newRequestTo("https://93.184.216.34/")}, |
| 72 | + wantBlocked: true, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "redirect to hostname resolving to loopback blocked", |
| 76 | + req: newRequestTo("https://localhost/"), |
| 77 | + via: []*http.Request{newRequestTo("https://93.184.216.34/")}, |
| 78 | + wantBlocked: true, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "custom client with no custom check", |
| 82 | + custom: &http.Client{}, |
| 83 | + req: newRequestTo("https://127.0.0.1/"), |
| 84 | + via: []*http.Request{newRequestTo("https://93.184.216.34/")}, |
| 85 | + wantBlocked: true, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "custom client check disable", |
| 89 | + custom: &http.Client{ |
| 90 | + CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 91 | + return nil |
| 92 | + }, |
| 93 | + }, |
| 94 | + req: newRequestTo("https://127.0.0.1/"), |
| 95 | + via: []*http.Request{newRequestTo("https://93.184.216.34/")}, |
| 96 | + wantBlocked: false, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "too many redirects", |
| 100 | + req: newRequestTo("https://8.8.8.8/"), |
| 101 | + via: func() []*http.Request { |
| 102 | + via := make([]*http.Request, maxDiscoveryRedirects) |
| 103 | + for i := range via { |
| 104 | + via[i] = newRequestTo("https://8.8.8.8/") |
| 105 | + } |
| 106 | + return via |
| 107 | + }(), |
| 108 | + wantBlocked: true, |
| 109 | + }, |
| 110 | + } |
| 111 | + for _, tc := range testCases { |
| 112 | + t.Run(tc.name, func(t *testing.T) { |
| 113 | + client := newDiscoveryClient(tc.custom) |
| 114 | + gotErr := client.CheckRedirect(tc.req, tc.via) |
| 115 | + if gotErr != nil && !tc.wantBlocked { |
| 116 | + t.Fatalf("client.CheckRedirect() error = %v", gotErr) |
| 117 | + } |
| 118 | + if gotErr == nil && tc.wantBlocked { |
| 119 | + t.Fatal("client.CheckRedirect() error = nil, want blocked") |
| 120 | + } |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestDiscoveryBlocksInternalTarget(t *testing.T) { |
| 126 | + ctx := t.Context() |
| 127 | + for _, target := range []string{ |
| 128 | + "https://169.254.169.254/latest/meta-data/", |
| 129 | + "https://10.0.0.1/.well-known/oauth-protected-resource", |
| 130 | + } { |
| 131 | + t.Run(target, func(t *testing.T) { |
| 132 | + _, err := GetProtectedResourceMetadata(ctx, target, target, nil) |
| 133 | + if err == nil { |
| 134 | + t.Fatalf("GetProtectedResourceMetadata(%q) succeeded, want SSRF rejection", target) |
| 135 | + } |
| 136 | + if !strings.Contains(err.Error(), "non-public") { |
| 137 | + t.Errorf("error = %v, want it to mention a non-public address", err) |
| 138 | + } |
| 139 | + }) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestDiscoveryAllowsLoopbackInitial(t *testing.T) { |
| 144 | + ctx := t.Context() |
| 145 | + h := &fakeResourceHandler{} |
| 146 | + server := httptest.NewTLSServer(h) |
| 147 | + defer server.Close() |
| 148 | + h.installHandlers(server.URL) |
| 149 | + |
| 150 | + metadataURL := server.URL + "/.well-known/oauth-protected-resource" |
| 151 | + prm, err := GetProtectedResourceMetadata(ctx, metadataURL, server.URL, server.Client()) |
| 152 | + if err != nil { |
| 153 | + t.Fatalf("GetProtectedResourceMetadata on loopback failed: %v", err) |
| 154 | + } |
| 155 | + if prm == nil { |
| 156 | + t.Fatal("nil prm") |
| 157 | + } |
| 158 | +} |
0 commit comments