-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_security_test.go
More file actions
262 lines (239 loc) · 8.03 KB
/
Copy pathapp_security_test.go
File metadata and controls
262 lines (239 loc) · 8.03 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
package bast
import (
"context"
"errors"
"net/http/httptest"
"strings"
"testing"
)
func ipApp(trusted ...string) *App {
return newTestApp(Config{TrustedProxies: trusted},
GET("/ip", func(ctx *Ctx) Response {
return ctx.Raw(200, "text/plain", []byte(ctx.IP()))
}),
)
}
func serveIP(t *testing.T, app *App, remoteAddr string, headers map[string]string) string {
t.Helper()
req := httptest.NewRequest("GET", "/ip", nil)
req.RemoteAddr = remoteAddr
for k, v := range headers {
req.Header.Set(k, v)
}
w := httptest.NewRecorder()
app.ServeHTTP(w, req)
return w.Body.String()
}
func TestIP_XFF_SpoofedLeftmostIgnored(t *testing.T) {
app := ipApp("10.0.0.0/8")
// Client forged "6.6.6.6"; the trusted proxy appended the real client on the right.
got := serveIP(t, app, "10.0.0.9:4433", map[string]string{
"X-Forwarded-For": "6.6.6.6, 203.0.113.5",
})
if got != "203.0.113.5" {
t.Errorf("IP() = %q, want rightmost untrusted 203.0.113.5 (leftmost is attacker-controlled)", got)
}
}
func TestIP_XFF_SkipsTrustedHops(t *testing.T) {
app := ipApp("10.0.0.0/8")
// Two internal hops appended after the client — both must be skipped.
got := serveIP(t, app, "10.0.0.9:4433", map[string]string{
"X-Forwarded-For": "203.0.113.5, 10.0.0.7, 10.0.0.8",
})
if got != "203.0.113.5" {
t.Errorf("IP() = %q, want 203.0.113.5 after skipping trusted hops", got)
}
}
func TestIP_XFF_AllTrusted_ReturnsLeftmost(t *testing.T) {
app := ipApp("10.0.0.0/8")
got := serveIP(t, app, "10.0.0.9:4433", map[string]string{
"X-Forwarded-For": "10.0.0.5, 10.0.0.6",
})
if got != "10.0.0.5" {
t.Errorf("IP() = %q, want 10.0.0.5 when every hop is trusted", got)
}
}
func TestIP_UntrustedPeer_IgnoresXFF(t *testing.T) {
app := ipApp("10.0.0.0/8")
got := serveIP(t, app, "203.0.113.9:1234", map[string]string{
"X-Forwarded-For": "6.6.6.6",
})
if got != "203.0.113.9" {
t.Errorf("IP() = %q, want the peer address when it is not a trusted proxy", got)
}
}
func TestReadiness_DoesNotLeakErrorDetail(t *testing.T) {
secret := "postgres://user:hunter2@10.1.2.3:5432 connection refused"
app := New(Config{
Logger: &recLogger{},
Health: &HealthConfig{
ReadyPath: "/ready",
Checks: []HealthCheck{
CustomCheck("db", func(ctx context.Context) error { return errors.New(secret) }),
},
},
})
resp := app.ReadinessHandler()(NewTestCtxWithPath("/ready"))
body := string(resp.Body())
if resp.Status() != 503 {
t.Errorf("status = %d, want 503", resp.Status())
}
if !strings.Contains(body, "degraded") {
t.Errorf("body should mark the check degraded, got %s", body)
}
for _, leak := range []string{"postgres", "hunter2", "10.1.2.3"} {
if strings.Contains(body, leak) {
t.Errorf("readiness body leaks %q — dependency errors must be logged, not returned: %s", leak, body)
}
}
}
func TestStreamCtx_Send_MultilineDataCannotInjectFrames(t *testing.T) {
app := newTestApp(Config{},
STREAM("/sse", func(s *StreamCtx) {
_ = s.Send("tick", "hello\nevent: forged\ndata: injected")
_ = s.Flush()
}),
)
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest("GET", "/sse", nil))
body := w.Body.String()
if strings.Contains(body, "\nevent: forged\n") {
t.Errorf("payload newline forged a new event frame:\n%s", body)
}
// Every payload line must be carried as its own data: line.
want := "event: tick\ndata: hello\ndata: event: forged\ndata: data: injected\n\n"
if body != want {
t.Errorf("Send framing:\ngot %q\nwant %q", body, want)
}
}
func TestStreamCtx_Send_EventNameStripped(t *testing.T) {
app := newTestApp(Config{},
STREAM("/sse", func(s *StreamCtx) {
_ = s.Send("tick\ndata: forged", "x")
_ = s.Flush()
}),
)
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest("GET", "/sse", nil))
if got, want := w.Body.String(), "event: tickdata: forged\ndata: x\n\n"; got != want {
t.Errorf("event name must have CR/LF stripped:\ngot %q\nwant %q", got, want)
}
}
func TestWithValue_DoesNotAliasStore(t *testing.T) {
c := newTestCtx()
c.Set("shared", "orig")
cp := c.WithValue("k", "v")
cp.Set("copy-only", true)
if _, ok := c.Get("copy-only"); ok {
t.Error("Set on the WithValue copy leaked into the original's pooled store")
}
if v, _ := cp.Get("shared"); v != "orig" {
t.Errorf("copy should inherit existing store values, got %v", v)
}
c.Set("orig-only", true)
if _, ok := cp.Get("orig-only"); ok {
t.Error("Set on the original leaked into the copy")
}
}
func TestWithValue_ParamsPointIntoOwnStorage(t *testing.T) {
c := newTestCtx()
SetTestParam(c, "id", "42")
cp := c.WithValue("k", "v")
if got := cp.Param("id"); got != "42" {
t.Fatalf("copy lost params: Param(id) = %q", got)
}
// Mutating the original's storage must not affect the copy.
c.paramStorage[0].Value = "mutated"
if got := cp.Param("id"); got != "42" {
t.Errorf("copy params alias the original's paramStorage: got %q after mutation", got)
}
}
func TestDocs_CustomAssetsBaseURL(t *testing.T) {
app := New(Config{
Logger: &recLogger{},
Docs: &DocsConfig{
Enabled: true,
Path: "/docs",
JSONPath: "/openapi.json",
Title: "T",
AssetsBaseURL: "/static/swagger",
},
})
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest("GET", "/docs", nil))
body := w.Body.String()
if !strings.Contains(body, `"/static/swagger/swagger-ui.css"`) {
t.Errorf("custom AssetsBaseURL not used for css:\n%s", body)
}
if strings.Contains(body, "unpkg.com") {
t.Errorf("custom AssetsBaseURL still references unpkg:\n%s", body)
}
}
func TestAutoOptions_PathExists_Returns204WithAllow(t *testing.T) {
app := newTestApp(Config{},
GET("/thing", func(ctx *Ctx) Response { return ctx.OK(nil) }),
POST("/thing", func(ctx *Ctx) Response { return ctx.OK(nil) }),
)
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest("OPTIONS", "/thing", nil))
if w.Code != 204 {
t.Fatalf("status = %d, want 204 for auto-OPTIONS", w.Code)
}
allow := w.Header().Get("Allow")
if !strings.Contains(allow, "GET") || !strings.Contains(allow, "POST") {
t.Errorf("Allow = %q, want GET and POST", allow)
}
}
func TestAutoOptions_RunsGlobalMiddleware(t *testing.T) {
// CORS is registered as global middleware; it must see auto-OPTIONS requests
// or browser preflight breaks for every route without an explicit OPTIONS handler.
mw := func(next HandlerFunc) HandlerFunc {
return func(ctx *Ctx) Response {
return next(ctx).WithHeader("X-Global-MW", "ran")
}
}
app := New(Config{Logger: &recLogger{}})
app.Use(mw)
app.Register(Module{Controller: &routesCtrl{[]Route{
GET("/thing", func(ctx *Ctx) Response { return ctx.OK(nil) }),
}}})
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest("OPTIONS", "/thing", nil))
if w.Header().Get("X-Global-MW") != "ran" {
t.Error("global middleware did not run on the auto-OPTIONS path")
}
}
func TestAutoOptions_UnknownPathStays404(t *testing.T) {
app := newTestApp(Config{},
GET("/thing", func(ctx *Ctx) Response { return ctx.OK(nil) }),
)
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest("OPTIONS", "/nope", nil))
if w.Code != 404 {
t.Errorf("status = %d, want 404 for OPTIONS on unregistered path", w.Code)
}
}
func TestAutoOptions_ExplicitRouteWins(t *testing.T) {
app := newTestApp(Config{},
GET("/thing", func(ctx *Ctx) Response { return ctx.OK(nil) }),
Route{Method: "OPTIONS", Pattern: "/thing", Handler: func(ctx *Ctx) Response {
return ctx.Raw(200, "text/plain", []byte("custom"))
}},
)
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest("OPTIONS", "/thing", nil))
if w.Code != 200 || w.Body.String() != "custom" {
t.Errorf("explicit OPTIONS route must take precedence, got %d %q", w.Code, w.Body.String())
}
}
func TestDocs_DefaultAssetsBaseURL(t *testing.T) {
app := New(Config{
Logger: &recLogger{},
Docs: &DocsConfig{Enabled: true, Path: "/docs", JSONPath: "/openapi.json"},
})
w := httptest.NewRecorder()
app.ServeHTTP(w, httptest.NewRequest("GET", "/docs", nil))
if !strings.Contains(w.Body.String(), "unpkg.com/swagger-ui-dist") {
t.Errorf("default should remain the unpkg CDN:\n%s", w.Body.String())
}
}