forked from james-6-23/codex2api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
208 lines (177 loc) · 6.1 KB
/
Copy pathmain_test.go
File metadata and controls
208 lines (177 loc) · 6.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
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
package main
import (
"bytes"
"log"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/codex2api/proxy"
"github.com/gin-gonic/gin"
)
func TestConfigureTrustedProxiesRejectsForwardedForSpoofing(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
if err := configureTrustedProxies(r, nil); err != nil {
t.Fatalf("configureTrustedProxies() error = %v", err)
}
r.GET("/client-ip", func(c *gin.Context) {
c.String(http.StatusOK, c.ClientIP())
})
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/client-ip", nil)
req.RemoteAddr = "203.0.113.10:12345"
req.Header.Set("X-Forwarded-For", "127.0.0.1")
req.Header.Set("X-Real-IP", "127.0.0.1")
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", w.Code, http.StatusOK)
}
if got := strings.TrimSpace(w.Body.String()); got != "203.0.113.10" {
t.Fatalf("ClientIP() = %q, want remote addr and not spoofed loopback", got)
}
}
func TestConfigureTrustedProxiesHonorsTrustedProxyList(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
if err := configureTrustedProxies(r, []string{"10.0.0.0/8", "192.168.1.1"}); err != nil {
t.Fatalf("configureTrustedProxies() error = %v", err)
}
r.GET("/client-ip", func(c *gin.Context) {
c.String(http.StatusOK, c.ClientIP())
})
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/client-ip", nil)
req.RemoteAddr = "10.1.2.3:12345"
req.Header.Set("X-Forwarded-For", "198.51.100.7")
r.ServeHTTP(w, req)
if got := strings.TrimSpace(w.Body.String()); got != "198.51.100.7" {
t.Fatalf("trusted proxy ClientIP() = %q, want forwarded client 198.51.100.7", got)
}
w = httptest.NewRecorder()
req = httptest.NewRequest(http.MethodGet, "/client-ip", nil)
req.RemoteAddr = "203.0.113.10:12345"
req.Header.Set("X-Forwarded-For", "198.51.100.7")
r.ServeHTTP(w, req)
if got := strings.TrimSpace(w.Body.String()); got != "203.0.113.10" {
t.Fatalf("untrusted source ClientIP() = %q, want remote addr 203.0.113.10", got)
}
}
func TestConfigureTrustedProxiesRejectsInvalidEntry(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
if err := configureTrustedProxies(r, []string{"not-an-ip"}); err == nil {
t.Fatal("configureTrustedProxies() error = nil, want error for invalid proxy entry")
}
}
func TestConfigureTrustedProxiesAllowsLoopbackWAF(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
if err := configureTrustedProxies(r, []string{"127.0.0.1", "::1"}); err != nil {
t.Fatalf("configureTrustedProxies() error = %v", err)
}
r.GET("/client-ip", func(c *gin.Context) {
c.String(http.StatusOK, c.ClientIP())
})
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/client-ip", nil)
req.RemoteAddr = "127.0.0.1:12345"
req.Header.Set("X-Forwarded-For", "203.0.113.42")
req.Header.Set("X-Real-IP", "127.0.0.1")
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", w.Code, http.StatusOK)
}
if got := strings.TrimSpace(w.Body.String()); got != "203.0.113.42" {
t.Fatalf("ClientIP() = %q, want forwarded client IP from trusted loopback proxy", got)
}
}
func TestConfigureTrustedProxiesAllowsDockerWAF(t *testing.T) {
gin.SetMode(gin.TestMode)
r := gin.New()
if err := configureTrustedProxies(r, []string{"127.0.0.1", "::1", "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"}); err != nil {
t.Fatalf("configureTrustedProxies() error = %v", err)
}
r.GET("/client-ip", func(c *gin.Context) {
c.String(http.StatusOK, c.ClientIP())
})
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/client-ip", nil)
req.RemoteAddr = "172.18.0.2:12345"
req.Header.Set("X-Forwarded-For", "203.0.113.42")
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, want %d", w.Code, http.StatusOK)
}
if got := strings.TrimSpace(w.Body.String()); got != "203.0.113.42" {
t.Fatalf("ClientIP() = %q, want forwarded client IP from trusted Docker proxy", got)
}
}
func TestLoggerMiddlewareRedactsSensitiveContext(t *testing.T) {
gin.SetMode(gin.TestMode)
var logs bytes.Buffer
previousOutput := log.Writer()
previousFlags := log.Flags()
log.SetOutput(&logs)
log.SetFlags(0)
t.Cleanup(func() {
log.SetOutput(previousOutput)
log.SetFlags(previousFlags)
})
r := gin.New()
r.Use(loggerMiddleware())
r.GET("/probe", func(c *gin.Context) {
c.Set("x-account-email", "alice@example.com")
c.Set("x-account-proxy", "http://user:secret@proxy.example:8080")
c.Set("x-model", "gpt-5.5")
c.Set("x-reasoning-effort", "medium")
c.Set("x-service-tier", "fast")
c.Status(http.StatusAccepted)
})
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/probe", nil)
r.ServeHTTP(w, req)
if w.Code != http.StatusAccepted {
t.Fatalf("status = %d, want %d", w.Code, http.StatusAccepted)
}
got := logs.String()
for _, forbidden := range []string{"alice@example.com", "secret"} {
if strings.Contains(got, forbidden) {
t.Fatalf("log output leaked %q: %s", forbidden, got)
}
}
for _, expected := range []string{"GET /probe 202", "gpt-5.5", "effort=medium", "fast"} {
if !strings.Contains(got, expected) {
t.Fatalf("log output missing %q: %s", expected, got)
}
}
}
func TestLoggerMiddlewareUsesStreamingOutcomeOverride(t *testing.T) {
gin.SetMode(gin.TestMode)
var logs bytes.Buffer
previousOutput := log.Writer()
previousFlags := log.Flags()
log.SetOutput(&logs)
log.SetFlags(0)
t.Cleanup(func() {
log.SetOutput(previousOutput)
log.SetFlags(previousFlags)
})
r := gin.New()
r.Use(loggerMiddleware())
r.POST("/v1/responses", func(c *gin.Context) {
// 模拟 SSE 已提交 200 后才发现下游断开;真实 HTTP 状态无法回写,
// 但访问日志必须记录最终内部结果 499。
c.Status(http.StatusOK)
c.Set(proxy.AccessLogStatusContextKey, 499)
})
w := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil)
r.ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("wire status = %d, want %d", w.Code, http.StatusOK)
}
if got := logs.String(); !strings.Contains(got, "POST /v1/responses 499") {
t.Fatalf("access log must use stream outcome override: %s", got)
}
}