-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy patherror_leakage_test.go
More file actions
148 lines (125 loc) · 4.58 KB
/
Copy patherror_leakage_test.go
File metadata and controls
148 lines (125 loc) · 4.58 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
package oauth
import (
"context"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"time"
"github.com/tuannvm/oauth-mcp-proxy/provider"
)
func TestErrorMessagesDoNotLeakSensitiveInfo(t *testing.T) {
// Test that error messages don't leak redirect URIs or OAuth provider details
// Test 1: Invalid redirect URI error doesn't leak the URI
t.Run("InvalidRedirectURIErrorDoesNotLeakURI", func(t *testing.T) {
handler := &OAuth2Handler{
config: &OAuth2Config{
Mode: "proxy",
FixedRedirectURI: "https://server.com/callback",
AllowedClientRedirectDomains: "example.com",
},
logger: &defaultLogger{},
}
// Try to use an evil redirect URI
params := url.Values{}
params.Set("redirect_uri", "https://evil.com/steal-tokens")
params.Set("state", "test-state")
req := httptest.NewRequest("GET", "/oauth/authorize?"+params.Encode(), nil)
recorder := httptest.NewRecorder()
handler.HandleAuthorize(recorder, req)
body := recorder.Body.String()
// Error should not contain the evil redirect URI
if strings.Contains(body, "evil.com") {
t.Errorf("Error message leaks redirect URI: %s", body)
}
if strings.Contains(body, "steal-tokens") {
t.Errorf("Error message leaks redirect URI path: %s", body)
}
})
// Test 2: Authorization error doesn't leak OAuth provider details
t.Run("AuthorizationErrorDoesNotLeakProviderDetails", func(t *testing.T) {
handler := &OAuth2Handler{
config: &OAuth2Config{
Mode: "proxy",
FixedRedirectURI: "https://server.com/callback",
Issuer: "https://secret-internal-idp.company.com",
ClientID: "secret-client-id",
},
logger: &defaultLogger{},
}
params := url.Values{}
params.Set("redirect_uri", "http://localhost:3000/callback")
params.Set("state", "test-state")
params.Set("error", "access_denied")
params.Set("error_description", "The user denied access to scope: profile email internal_admin_role")
req := httptest.NewRequest("GET", "/oauth/callback?"+params.Encode(), nil)
recorder := httptest.NewRecorder()
handler.HandleCallback(recorder, req)
body := recorder.Body.String()
// Error should not leak the detailed error description from the OAuth provider
if strings.Contains(body, "internal_admin_role") {
t.Errorf("Error message leaks OAuth provider details: %s", body)
}
if strings.Contains(body, "secret-internal-idp") {
t.Errorf("Error message leaks internal issuer: %s", body)
}
})
}
func TestGenericErrorMessagesForSecurity(t *testing.T) {
// Test that sensitive operations return generic error messages
t.Run("InvalidStateReturnsGenericError", func(t *testing.T) {
handler := &OAuth2Handler{
config: &OAuth2Config{
Mode: "proxy",
FixedRedirectURI: "https://server.com/callback",
stateSigningKey: []byte("test-key-32-bytes-long!"),
},
logger: &defaultLogger{},
seenNonces: make(map[string]time.Time),
}
// Use a tampered state
params := url.Values{}
params.Set("code", "valid-code")
params.Set("state", "tampered-state-without-signature")
req := httptest.NewRequest("GET", "/oauth/callback?"+params.Encode(), nil)
recorder := httptest.NewRecorder()
handler.HandleCallback(recorder, req)
// Should return generic error, not reveal anything about state format
if recorder.Code != http.StatusBadRequest {
t.Errorf("Expected 400, got %d", recorder.Code)
}
body := recorder.Body.String()
// Error should be generic
if !strings.Contains(body, "Invalid state") {
t.Errorf("Expected generic 'Invalid state' error, got: %s", body)
}
// Should not contain internal implementation details
if strings.Contains(body, "HMAC") || strings.Contains(body, "signature") {
t.Errorf("Error message reveals implementation details: %s", body)
}
})
}
func TestTokenValidationErrorMessages(t *testing.T) {
// Test that token validation errors don't leak token details
t.Run("TokenErrorsDoNotLeakTokenContent", func(t *testing.T) {
validator := &provider.HMACValidator{}
err := validator.Initialize(&provider.Config{
Audience: "test-audience",
JWTSecret: []byte("test-secret-key-32-bytes-long"),
})
if err != nil {
t.Fatalf("Failed to initialize validator: %v", err)
}
// Try to validate an invalid token
_, err = validator.ValidateToken(context.TODO(), "invalid.token.here")
if err == nil {
t.Error("Expected error for invalid token")
} else {
// Error should not contain the actual token content
if strings.Contains(err.Error(), "invalid.token.here") {
t.Errorf("Error message leaks token content: %v", err)
}
}
})
}