-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy patherrors_test.go
More file actions
301 lines (259 loc) · 8.62 KB
/
Copy patherrors_test.go
File metadata and controls
301 lines (259 loc) · 8.62 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
package server
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// TestErrorMessagesDoNotLeakInternalDetails verifies that error responses
// do not contain internal error details that could be exploited by attackers.
func TestErrorMessagesDoNotLeakInternalDetails(t *testing.T) {
// List of sensitive patterns that should never appear in user-facing errors
sensitivePatterns := []string{
"panic",
"runtime error",
"nil pointer",
"stack trace",
"goroutine",
".go:", // file paths like "server.go:123"
"sql:", // SQL errors
"connection", // Connection errors
"timeout", // Unless it's a user-friendly timeout message
"ECONNREFUSED",
"EOF",
"broken pipe",
}
tests := []struct {
name string
path string
method string
body string
contentType string
setupFunc func(t *testing.T, s *Server)
checkFunc func(t *testing.T, resp *http.Response, body string)
}{
{
name: "Invalid authorization request parse error",
path: "/auth",
method: "POST",
body: "invalid%body",
contentType: "application/x-www-form-urlencoded",
checkFunc: func(t *testing.T, resp *http.Response, body string) {
// Should return a safe error message, not the parse error details
for _, pattern := range sensitivePatterns {
require.NotContains(t, body, pattern,
"Response should not contain sensitive pattern: %s", pattern)
}
},
},
{
name: "Invalid callback state",
path: "/callback?state=invalid_state",
method: "GET",
checkFunc: func(t *testing.T, resp *http.Response, body string) {
require.Equal(t, http.StatusBadRequest, resp.StatusCode)
// Should not leak storage error details
require.NotContains(t, body, "storage")
require.NotContains(t, body, "not found")
},
},
{
name: "Invalid token request",
path: "/token",
method: "POST",
body: "grant_type=authorization_code&code=invalid",
contentType: "application/x-www-form-urlencoded",
checkFunc: func(t *testing.T, resp *http.Response, body string) {
// Token endpoint returns JSON errors which is correct OAuth2 behavior
// Just verify no internal details leak
for _, pattern := range sensitivePatterns {
require.NotContains(t, body, pattern,
"Response should not contain sensitive pattern: %s", pattern)
}
},
},
{
name: "Invalid introspection request - no token",
path: "/token/introspect",
method: "POST",
body: "",
contentType: "application/x-www-form-urlencoded",
checkFunc: func(t *testing.T, resp *http.Response, body string) {
for _, pattern := range sensitivePatterns {
require.NotContains(t, body, pattern,
"Response should not contain sensitive pattern: %s", pattern)
}
},
},
{
name: "Device flow invalid user code",
path: "/device/auth/verify_code",
method: "POST",
body: "user_code=INVALID",
checkFunc: func(t *testing.T, resp *http.Response, body string) {
for _, pattern := range sensitivePatterns {
require.NotContains(t, body, pattern,
"Response should not contain sensitive pattern: %s", pattern)
}
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
httpServer, s := newTestServer(t, nil)
defer httpServer.Close()
if tc.setupFunc != nil {
tc.setupFunc(t, s)
}
var reqBody io.Reader
if tc.body != "" {
reqBody = strings.NewReader(tc.body)
}
req := httptest.NewRequest(tc.method, tc.path, reqBody)
if tc.contentType != "" {
req.Header.Set("Content-Type", tc.contentType)
}
rr := httptest.NewRecorder()
s.ServeHTTP(rr, req)
resp := rr.Result()
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
require.NoError(t, err)
body := string(bodyBytes)
if tc.checkFunc != nil {
tc.checkFunc(t, resp, body)
}
})
}
}
// TestLoginErrorMessageIsSafe verifies that the login error page
// shows a safe, user-friendly message.
func TestLoginErrorMessageIsSafe(t *testing.T) {
httpServer, s := newTestServer(t, nil)
defer httpServer.Close()
// Create a request that will trigger a login error
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/auth/nonexistent/login?state=test", nil)
s.ServeHTTP(rr, req)
resp := rr.Result()
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
bodyStr := string(body)
// Should not contain error stack traces or internal details
require.NotContains(t, bodyStr, "panic")
require.NotContains(t, bodyStr, ".go:")
require.NotContains(t, bodyStr, "goroutine")
}
// TestCallbackErrorMessageIsSafe verifies that callback errors
// do not leak internal details.
func TestCallbackErrorMessageIsSafe(t *testing.T) {
httpServer, s := newTestServer(t, nil)
defer httpServer.Close()
// Test OAuth2 callback with invalid state
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/callback?code=test&state=invalid", nil)
s.ServeHTTP(rr, req)
resp := rr.Result()
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
bodyStr := string(body)
// Should not contain storage error details
require.NotContains(t, bodyStr, "storage.ErrNotFound")
require.NotContains(t, bodyStr, "database")
}
// TestDeviceCallbackMethodError verifies that unsupported methods
// return safe error messages.
func TestDeviceCallbackMethodError(t *testing.T) {
httpServer, s := newTestServer(t, nil)
defer httpServer.Close()
// Test with unsupported method
rr := httptest.NewRecorder()
req := httptest.NewRequest("PUT", "/device/callback", nil)
s.ServeHTTP(rr, req)
resp := rr.Result()
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
bodyStr := string(body)
// The router now rejects the wrong method with the shared 405 handler, which
// must not expose the method name in the error.
require.Equal(t, http.StatusMethodNotAllowed, resp.StatusCode)
require.NotContains(t, bodyStr, "PUT")
require.NotContains(t, bodyStr, "method not implemented")
}
// TestRenderErrorSafeMessages tests that renderError uses safe messages
func TestRenderErrorSafeMessages(t *testing.T) {
tests := []struct {
name string
statusCode int
message string
expectedInBody []string
notInBody []string
}{
{
name: "Login error message",
statusCode: http.StatusInternalServerError,
message: ErrMsgLoginError,
expectedInBody: []string{"Login error", "administrator"},
notInBody: []string{"stack", "panic", ".go:"},
},
{
name: "Authentication failed message",
statusCode: http.StatusInternalServerError,
message: ErrMsgAuthenticationFailed,
expectedInBody: []string{"Authentication failed", "administrator"},
notInBody: []string{"stack", "panic", ".go:"},
},
{
name: "Database error message",
statusCode: http.StatusInternalServerError,
message: ErrMsgDatabaseError,
expectedInBody: []string{"database error"},
notInBody: []string{"sql:", "connection", "timeout"},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
httpServer, s := newTestServer(t, nil)
defer httpServer.Close()
rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/", nil)
s.renderError(req, rr, tc.statusCode, tc.message)
resp := rr.Result()
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
bodyStr := string(body)
require.Equal(t, tc.statusCode, resp.StatusCode)
for _, expected := range tc.expectedInBody {
require.Contains(t, bodyStr, expected,
"Response should contain: %s", expected)
}
for _, notExpected := range tc.notInBody {
require.NotContains(t, bodyStr, notExpected,
"Response should not contain: %s", notExpected)
}
})
}
}
// TestTokenErrorDoesNotLeakDetails tests that token errors don't leak internal details
func TestTokenErrorDoesNotLeakDetails(t *testing.T) {
httpServer, s := newTestServer(t, nil)
defer httpServer.Close()
// Create a token request with invalid credentials
body := bytes.NewBufferString("grant_type=authorization_code&code=invalid_code")
req := httptest.NewRequest("POST", "/token", body)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth("invalid_client", "invalid_secret")
rr := httptest.NewRecorder()
s.ServeHTTP(rr, req)
resp := rr.Result()
defer resp.Body.Close()
respBody, _ := io.ReadAll(resp.Body)
bodyStr := string(respBody)
// Should not contain internal error details
require.NotContains(t, bodyStr, "storage")
require.NotContains(t, bodyStr, "not found")
require.NotContains(t, bodyStr, ".go:")
}