forked from grafana/mcp-grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_security_test.go
More file actions
150 lines (133 loc) · 5.1 KB
/
Copy pathhttp_security_test.go
File metadata and controls
150 lines (133 loc) · 5.1 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
package mcpgrafana
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// okHandler is the inner handler the middleware wraps. We assert that the
// middleware either calls it (allow) or short-circuits before it (deny).
func okHandler(t *testing.T) (http.Handler, *bool) {
t.Helper()
called := false
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
w.WriteHeader(http.StatusOK)
}), &called
}
func TestDNSRebindingProtectionMiddleware_Host(t *testing.T) {
cases := []struct {
name string
allowed []string
host string
wantStatus int
wantCalled bool
}{
{"matching host passes", []string{"localhost:8000"}, "localhost:8000", http.StatusOK, true},
{"case-insensitive host match passes", []string{"localhost:8000"}, "LOCALHOST:8000", http.StatusOK, true},
{"mismatched host blocked", []string{"localhost:8000"}, "evil.example:8000", http.StatusForbidden, false},
{"loopback IP variant blocked when not allowlisted", []string{"localhost:8000"}, "127.0.0.1:8000", http.StatusForbidden, false},
{"empty allowlist permits everything (Host check disabled)", nil, "anything.example", http.StatusOK, true},
{"wildcard disables host validation", []string{"*"}, "rebinding.attacker.example", http.StatusOK, true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
inner, called := okHandler(t)
mw := DNSRebindingProtectionMiddleware(HostOriginPolicy{AllowedHosts: tc.allowed})
req := httptest.NewRequest(http.MethodGet, "/sse", nil)
req.Host = tc.host
rr := httptest.NewRecorder()
mw(inner).ServeHTTP(rr, req)
assert.Equal(t, tc.wantStatus, rr.Code)
assert.Equal(t, tc.wantCalled, *called)
})
}
}
func TestDNSRebindingProtectionMiddleware_Origin(t *testing.T) {
cases := []struct {
name string
allowed []string
origin string
wantStatus int
wantCalled bool
}{
{"no Origin header passes (CLI client)", nil, "", http.StatusOK, true},
{"empty allowlist rejects any Origin", nil, "http://evil.example", http.StatusForbidden, false},
{"matching Origin passes", []string{"http://localhost:3000"}, "http://localhost:3000", http.StatusOK, true},
{"case-insensitive Origin match passes", []string{"http://localhost:3000"}, "HTTP://LOCALHOST:3000", http.StatusOK, true},
{"non-matching Origin blocked", []string{"http://localhost:3000"}, "http://evil.example", http.StatusForbidden, false},
{"wildcard disables Origin validation even with Origin present", []string{"*"}, "http://evil.example", http.StatusOK, true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
inner, called := okHandler(t)
// AllowedHosts is left empty so only Origin is exercised here.
mw := DNSRebindingProtectionMiddleware(HostOriginPolicy{AllowedOrigins: tc.allowed})
req := httptest.NewRequest(http.MethodGet, "/sse", nil)
if tc.origin != "" {
req.Header.Set("Origin", tc.origin)
}
rr := httptest.NewRecorder()
mw(inner).ServeHTTP(rr, req)
assert.Equal(t, tc.wantStatus, rr.Code)
assert.Equal(t, tc.wantCalled, *called)
})
}
}
// TestDNSRebindingProtectionMiddleware_RebindingScenario simulates the exact
// DNS-rebinding case from the customer report: a browser hits 127.0.0.1:8000
// but the URL bar (and therefore the Host header) is the attacker's domain.
// With the default Host allowlist derived from the bind address, the request
// must be rejected before reaching the SSE handler.
func TestDNSRebindingProtectionMiddleware_RebindingScenario(t *testing.T) {
inner, called := okHandler(t)
policy := HostOriginPolicy{AllowedHosts: DefaultAllowedHosts("localhost:8000")}
mw := DNSRebindingProtectionMiddleware(policy)
req := httptest.NewRequest(http.MethodGet, "/sse", nil)
req.Host = "rebinding.attacker.example:8000"
req.Header.Set("Origin", "http://rebinding.attacker.example:8000")
rr := httptest.NewRecorder()
mw(inner).ServeHTTP(rr, req)
assert.Equal(t, http.StatusForbidden, rr.Code)
assert.False(t, *called, "inner handler must not be reached for a rebinding Host")
}
func TestDefaultAllowedHosts(t *testing.T) {
cases := []struct {
name string
address string
want []string
}{
{
name: "localhost bind allows IPv4 and IPv6 loopback too",
address: "localhost:8000",
want: []string{"localhost:8000", "127.0.0.1:8000", "[::1]:8000"},
},
{
name: "wildcard IPv4 bind allows all loopback variants",
address: "0.0.0.0:8000",
want: []string{"localhost:8000", "127.0.0.1:8000", "[::1]:8000"},
},
{
name: "empty host bind allows all loopback variants",
address: ":8000",
want: []string{"localhost:8000", "127.0.0.1:8000", "[::1]:8000"},
},
{
name: "explicit hostname binds only that hostname",
address: "mcp.internal:8000",
want: []string{"mcp.internal:8000"},
},
{
name: "explicit IPv4 binds only that IPv4",
address: "10.0.0.5:8000",
want: []string{"10.0.0.5:8000"},
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
got := DefaultAllowedHosts(tc.address)
require.Equal(t, tc.want, got)
})
}
}