|
7 | 7 | "encoding/json" |
8 | 8 | "fmt" |
9 | 9 | "io" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
10 | 12 | "strings" |
11 | 13 | "testing" |
12 | 14 | ) |
@@ -95,3 +97,80 @@ func TestServeStdioUsesContentLengthFraming(t *testing.T) { |
95 | 97 | func bufioReader(r io.Reader) *bufio.Reader { |
96 | 98 | return bufio.NewReader(r) |
97 | 99 | } |
| 100 | + |
| 101 | +func TestOriginAllowed(t *testing.T) { |
| 102 | + cases := []struct { |
| 103 | + origin string |
| 104 | + want bool |
| 105 | + }{ |
| 106 | + {"", true}, // real MCP clients and curl send no Origin |
| 107 | + {"http://127.0.0.1:8765", true}, |
| 108 | + {"http://localhost:8765", true}, |
| 109 | + {"http://[::1]:8765", true}, |
| 110 | + {"https://localhost", true}, |
| 111 | + {"http://evil.example", false}, // a page the user happens to visit |
| 112 | + {"https://evil.example", false}, // ...over TLS |
| 113 | + {"http://127.0.0.1.evil.test", false}, |
| 114 | + {"http://localhost.evil.test", false}, |
| 115 | + {"null", false}, // sandboxed iframe / some redirects |
| 116 | + {"://", false}, // unparseable |
| 117 | + // userinfo must not be mistaken for the host |
| 118 | + {"http://localhost@evil.example", false}, |
| 119 | + {"http://127.0.0.1@evil.example/x", false}, |
| 120 | + // a host that merely contains a loopback name |
| 121 | + {"http://myLocalhost", false}, |
| 122 | + {"http://localhost.", false}, |
| 123 | + {"http://127.0.0.2", false}, |
| 124 | + // non-http schemes still carry a host that must match |
| 125 | + {"file://localhost", true}, |
| 126 | + {"https://evil.example:80", false}, |
| 127 | + } |
| 128 | + for _, c := range cases { |
| 129 | + r := httptest.NewRequest(http.MethodPost, "/mcp", strings.NewReader("{}")) |
| 130 | + if c.origin != "" { |
| 131 | + r.Header.Set("Origin", c.origin) |
| 132 | + } |
| 133 | + if got := originAllowed(r); got != c.want { |
| 134 | + t.Errorf("originAllowed(Origin: %q) = %v, want %v", c.origin, got, c.want) |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestHTTPHandlerRejectsForeignOrigin(t *testing.T) { |
| 140 | + srv := httptest.NewServer(Server{}.httpHandler()) |
| 141 | + defer srv.Close() |
| 142 | + |
| 143 | + body := `{"jsonrpc":"2.0","id":1,"method":"tools/list"}` |
| 144 | + |
| 145 | + // a browser on an attacker's page: must be refused before any tool runs |
| 146 | + req, err := http.NewRequest(http.MethodPost, srv.URL+"/mcp", strings.NewReader(body)) |
| 147 | + if err != nil { |
| 148 | + t.Fatal(err) |
| 149 | + } |
| 150 | + req.Header.Set("Origin", "http://evil.example") |
| 151 | + res, err := srv.Client().Do(req) |
| 152 | + if err != nil { |
| 153 | + t.Fatal(err) |
| 154 | + } |
| 155 | + defer res.Body.Close() |
| 156 | + if res.StatusCode != http.StatusForbidden { |
| 157 | + t.Fatalf("cross-origin request: status = %d, want %d", res.StatusCode, http.StatusForbidden) |
| 158 | + } |
| 159 | + |
| 160 | + // a non-browser client (no Origin) must still work |
| 161 | + res2, err := srv.Client().Post(srv.URL+"/mcp", "application/json", strings.NewReader(body)) |
| 162 | + if err != nil { |
| 163 | + t.Fatal(err) |
| 164 | + } |
| 165 | + defer res2.Body.Close() |
| 166 | + if res2.StatusCode != http.StatusOK { |
| 167 | + t.Fatalf("no-Origin request: status = %d, want %d", res2.StatusCode, http.StatusOK) |
| 168 | + } |
| 169 | + var parsed response |
| 170 | + if err := json.NewDecoder(res2.Body).Decode(&parsed); err != nil { |
| 171 | + t.Fatal(err) |
| 172 | + } |
| 173 | + if parsed.Result == nil { |
| 174 | + t.Fatal("expected a tools/list result for a request without Origin") |
| 175 | + } |
| 176 | +} |
0 commit comments