forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannel_authz_test.go
More file actions
204 lines (175 loc) · 6.31 KB
/
Copy pathchannel_authz_test.go
File metadata and controls
204 lines (175 loc) · 6.31 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
package controller
import (
"bytes"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"github.com/QuantumNous/new-api/common"
"github.com/QuantumNous/new-api/model"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestChannelHasSensitiveChanges(t *testing.T) {
baseURL := "https://api.example.com"
headerOverride := `{"Authorization":"Bearer {api_key}"}`
origin := &model.Channel{
Type: 1,
Key: "old-key",
BaseURL: &baseURL,
HeaderOverride: &headerOverride,
Models: "gpt-4o",
Group: "default",
}
t.Run("non-sensitive routing fields", func(t *testing.T) {
updated := PatchChannel{Channel: *origin}
updated.Models = "gpt-4o,gpt-4o-mini"
updated.Group = "vip"
assert.False(t, channelHasSensitiveChanges(&updated, origin, map[string]any{
"models": updated.Models,
"group": updated.Group,
}))
})
t.Run("key change", func(t *testing.T) {
updated := PatchChannel{Channel: *origin}
updated.Key = "new-key"
assert.True(t, channelHasSensitiveChanges(&updated, origin, map[string]any{"key": updated.Key}))
})
t.Run("base url change", func(t *testing.T) {
updated := PatchChannel{Channel: *origin}
newBaseURL := "https://leak.example.com"
updated.BaseURL = &newBaseURL
assert.True(t, channelHasSensitiveChanges(&updated, origin, map[string]any{"base_url": newBaseURL}))
})
t.Run("header override change", func(t *testing.T) {
updated := PatchChannel{Channel: *origin}
newHeaderOverride := `{"X-Key":"{api_key}"}`
updated.HeaderOverride = &newHeaderOverride
assert.True(t, channelHasSensitiveChanges(&updated, origin, map[string]any{"header_override": newHeaderOverride}))
})
t.Run("omitted sensitive fields do not use zero values", func(t *testing.T) {
updated := PatchChannel{}
updated.Id = origin.Id
updated.Priority = origin.Priority
assert.False(t, channelHasSensitiveChanges(&updated, origin, map[string]any{"priority": 10}))
})
t.Run("unknown field fails closed", func(t *testing.T) {
updated := PatchChannel{Channel: *origin}
assert.True(t, channelHasSensitiveChanges(&updated, origin, map[string]any{"future_secret_field": "x"}))
})
t.Run("status is operational", func(t *testing.T) {
updated := PatchChannel{Channel: *origin}
updated.Status = common.ChannelStatusManuallyDisabled
assert.False(t, channelHasSensitiveChanges(&updated, origin, map[string]any{"status": updated.Status}))
})
t.Run("read-only fields are ignored by sensitivity check", func(t *testing.T) {
updated := PatchChannel{Channel: *origin}
updated.Balance = 99
updated.UsedQuota = 100
updated.ResponseTime = 200
assert.False(t, channelHasSensitiveChanges(&updated, origin, map[string]any{
"balance": updated.Balance,
"used_quota": updated.UsedQuota,
"response_time": updated.ResponseTime,
}))
})
}
func TestClearChannelReadOnlyFields(t *testing.T) {
channel := PatchChannel{Channel: model.Channel{
CreatedTime: 11,
TestTime: 22,
ResponseTime: 33,
Balance: 44.5,
BalanceUpdatedTime: 55,
UsedQuota: 66,
Models: "gpt-4o",
Group: "default",
}}
clearChannelReadOnlyFields(&channel, map[string]any{
"created_time": channel.CreatedTime,
"test_time": channel.TestTime,
"response_time": channel.ResponseTime,
"balance": channel.Balance,
"balance_updated_time": channel.BalanceUpdatedTime,
"used_quota": channel.UsedQuota,
"models": channel.Models,
"group": channel.Group,
})
assert.Zero(t, channel.CreatedTime)
assert.Zero(t, channel.TestTime)
assert.Zero(t, channel.ResponseTime)
assert.Zero(t, channel.Balance)
assert.Zero(t, channel.BalanceUpdatedTime)
assert.Zero(t, channel.UsedQuota)
assert.Equal(t, "gpt-4o", channel.Models)
assert.Equal(t, "default", channel.Group)
}
func TestUpdateChannelRejectsStatusField(t *testing.T) {
gin.SetMode(gin.TestMode)
recorder := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(recorder)
ctx.Request = httptest.NewRequest(
http.MethodPut,
"/api/channel/",
bytes.NewBufferString(`{"id":1,"status":2}`),
)
ctx.Request.Header.Set("Content-Type", "application/json")
UpdateChannel(ctx)
require.Equal(t, http.StatusOK, recorder.Code)
var response struct {
Success bool `json:"success"`
Message string `json:"message"`
}
require.NoError(t, common.Unmarshal(recorder.Body.Bytes(), &response))
assert.False(t, response.Success)
}
func TestChannelStatusValidation(t *testing.T) {
assert.True(t, isManageableChannelStatus(common.ChannelStatusEnabled))
assert.True(t, isManageableChannelStatus(common.ChannelStatusManuallyDisabled))
assert.False(t, isManageableChannelStatus(common.ChannelStatusAutoDisabled))
assert.False(t, isManageableChannelStatus(0))
}
// TestChannelFieldsAreClassified guards the fail-closed sensitivity check: every
// JSON field of PatchChannel (including the embedded model.Channel) must be listed
// in channelSensitiveFields, channelNonSensitiveFields, or
// channelOperationalFields. A newly added field that is left unclassified will
// fail this test, forcing a conscious permission decision instead of silently
// defaulting either way.
func TestChannelFieldsAreClassified(t *testing.T) {
classified := func(name string) bool {
if _, ok := channelSensitiveFields[name]; ok {
return true
}
if _, ok := channelNonSensitiveFields[name]; ok {
return true
}
if _, ok := channelOperationalFields[name]; ok {
return true
}
_, ok := channelReadOnlyFields[name]
return ok
}
var collect func(rt reflect.Type) []string
collect = func(rt reflect.Type) []string {
var names []string
for i := 0; i < rt.NumField(); i++ {
field := rt.Field(i)
if field.Anonymous && field.Type.Kind() == reflect.Struct {
names = append(names, collect(field.Type)...)
continue
}
name := strings.Split(field.Tag.Get("json"), ",")[0]
if name == "" || name == "-" {
continue
}
names = append(names, name)
}
return names
}
for _, name := range collect(reflect.TypeOf(PatchChannel{})) {
assert.Truef(t, classified(name),
"channel field %q is not classified; add it to channelSensitiveFields, channelNonSensitiveFields, channelOperationalFields, or channelReadOnlyFields in channel_authz.go", name)
}
}