Skip to content

Commit 882a59c

Browse files
authored
fix: preserve channel permission for cyber policy 403 errors (#615)
1 parent 52bcf12 commit 882a59c

5 files changed

Lines changed: 78 additions & 1 deletion

File tree

core/relay/adaptor/interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ type Error interface {
121121
StatusCode() int
122122
}
123123

124+
type ErrorCodeProvider interface {
125+
ErrorCode() any
126+
}
127+
124128
var ErrGetBalanceNotImplemented = errors.New("get balance not implemented")
125129

126130
type Balancer interface {

core/relay/adaptor/utils.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ func (e BasicError[T]) StatusCode() int {
2222
return e.statusCode
2323
}
2424

25+
func (e BasicError[T]) ErrorCode() any {
26+
provider, ok := any(e.error).(ErrorCodeProvider)
27+
if !ok {
28+
return nil
29+
}
30+
31+
return provider.ErrorCode()
32+
}
33+
2534
func (e BasicError[T]) Error() string {
2635
return fmt.Sprintf("status code: %d, error: %v", e.statusCode, e.error)
2736
}

core/relay/model/chat.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ type OpenAIErrorResponse struct {
192192
Error OpenAIError `json:"error"`
193193
}
194194

195+
func (e OpenAIErrorResponse) ErrorCode() any {
196+
return e.Error.Code
197+
}
198+
195199
type OpenAIError struct {
196200
Code any `json:"code,omitempty"`
197201
Message string `json:"message,omitempty"`

core/relay/plugin/monitor/monitor.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,37 @@ var channelNoPermissionStatusCodesMap = map[int]struct{}{
5050
http.StatusNotFound: {},
5151
}
5252

53+
var forbiddenHasPermissionErrorCodesMap = map[string]struct{}{
54+
"session_blocked_by_cyber_policy": {},
55+
}
56+
5357
func ChannelStatusHasPermission(statusCode int) bool {
5458
_, ok := channelNoPermissionStatusCodesMap[statusCode]
5559
return !ok
5660
}
5761

5862
func ChannelHasPermission(relayErr adaptor.Error) bool {
59-
return ChannelStatusHasPermission(relayErr.StatusCode())
63+
if ChannelStatusHasPermission(relayErr.StatusCode()) {
64+
return true
65+
}
66+
67+
if relayErr.StatusCode() != http.StatusForbidden {
68+
return false
69+
}
70+
71+
provider, ok := relayErr.(adaptor.ErrorCodeProvider)
72+
if !ok {
73+
return false
74+
}
75+
76+
errorCode, ok := provider.ErrorCode().(string)
77+
if !ok {
78+
return false
79+
}
80+
81+
_, ok = forbiddenHasPermissionErrorCodesMap[errorCode]
82+
83+
return ok
6084
}
6185

6286
func getRequestDuration(meta *meta.Meta) time.Duration {

core/relay/plugin/monitor/monitor_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/labring/aiproxy/core/common/config"
99
relaymeta "github.com/labring/aiproxy/core/relay/meta"
10+
relaymodel "github.com/labring/aiproxy/core/relay/model"
1011
"github.com/stretchr/testify/require"
1112
)
1213

@@ -73,3 +74,38 @@ func TestChannelStatusHasPermission(t *testing.T) {
7374

7475
require.True(t, ChannelStatusHasPermission(http.StatusBadRequest))
7576
}
77+
78+
func TestChannelHasPermissionForForbiddenErrorCode(t *testing.T) {
79+
t.Parallel()
80+
81+
tests := []struct {
82+
name string
83+
code string
84+
want bool
85+
}{
86+
{
87+
name: "request blocked by cyber policy",
88+
code: "session_blocked_by_cyber_policy",
89+
want: true,
90+
},
91+
{
92+
name: "channel permission failure",
93+
code: "insufficient_user_quota",
94+
want: false,
95+
},
96+
}
97+
98+
for _, tt := range tests {
99+
t.Run(tt.name, func(t *testing.T) {
100+
t.Parallel()
101+
102+
relayErr := relaymodel.NewOpenAIError(http.StatusForbidden, relaymodel.OpenAIError{
103+
Code: tt.code,
104+
Message: "forbidden",
105+
Type: relaymodel.ErrorTypeUpstream,
106+
})
107+
108+
require.Equal(t, tt.want, ChannelHasPermission(relayErr))
109+
})
110+
}
111+
}

0 commit comments

Comments
 (0)