Skip to content

Commit 057f71c

Browse files
committed
fix(logs): isolate privileged metadata
1 parent 73afad5 commit 057f71c

25 files changed

Lines changed: 989 additions & 326 deletions

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ upload
88
*.db
99
build
1010
*.db-journal
11+
*.db-shm
12+
*.db-wal
1113
logs
1214
web/dist
1315
web/node_modules

‎controller/channel-test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ func settleTestQuota(info *relaycommon.RelayInfo, priceData hosttypes.PriceData,
559559
return common.QuotaFromFloat(priceData.ModelPrice * common.QuotaPerUnit), nil
560560
}
561561

562-
func buildTestLogOther(c *gin.Context, info *relaycommon.RelayInfo, priceData hosttypes.PriceData, usage *dto.Usage, tieredResult *billingexpr.TieredResult) map[string]interface{} {
562+
func buildTestLogOther(c *gin.Context, info *relaycommon.RelayInfo, priceData hosttypes.PriceData, usage *dto.Usage, tieredResult *billingexpr.TieredResult) *model.LogOther {
563563
other := service.GenerateTextOtherInfo(c, info, priceData.ModelRatio, priceData.GroupRatioInfo.GroupRatio, priceData.CompletionRatio,
564564
usage.PromptTokensDetails.CachedTokens, priceData.CacheRatio, priceData.ModelPrice, priceData.GroupRatioInfo.GroupSpecialRatio)
565565
if tieredResult != nil {

‎controller/channel_test_internal_test.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,10 +279,11 @@ func TestBuildTestLogOtherInjectsTieredInfo(t *testing.T) {
279279
RequestRules: requestRules,
280280
})
281281

282-
require.Equal(t, "tiered_expr", other["billing_mode"])
283-
require.Equal(t, "base", other["matched_tier"])
284-
require.Equal(t, requestRules, other["request_rules"])
285-
require.NotEmpty(t, other["expr_b64"])
282+
fields := other.Snapshot()
283+
require.Equal(t, "tiered_expr", fields["billing_mode"])
284+
require.Equal(t, "base", fields["matched_tier"])
285+
require.Equal(t, requestRules, fields["request_rules"])
286+
require.NotEmpty(t, fields["expr_b64"])
286287
}
287288

288289
func TestResolveChannelTestUserIDUsesRequestUser(t *testing.T) {

‎controller/log.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ func GetAllLogs(c *gin.Context) {
2929
}
3030
if c.GetInt("role") < common.RoleRootUser {
3131
model.FormatAdminLogs(logs)
32+
} else {
33+
model.FormatRootLogs(logs)
3234
}
3335
pageInfo.SetTotal(int(total))
3436
pageInfo.SetItems(logs)

‎controller/relay.go‎

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -411,41 +411,21 @@ func processChannelError(c *gin.Context, channelError types.ChannelError, err *t
411411
modelName := c.GetString("original_model")
412412
tokenId := c.GetInt("token_id")
413413
userGroup := c.GetString("group")
414-
channelId := c.GetInt("channel_id")
415-
other := make(map[string]interface{})
414+
other := model.NewLogOther()
416415
if c.Request != nil && c.Request.URL != nil {
417-
other["request_path"] = c.Request.URL.Path
418-
}
419-
other["error_type"] = err.GetErrorType()
420-
other["error_code"] = err.GetErrorCode()
421-
other["status_code"] = err.StatusCode
422-
other["channel_id"] = channelId
423-
other["channel_name"] = c.GetString("channel_name")
424-
other["channel_type"] = c.GetInt("channel_type")
425-
adminInfo := make(map[string]interface{})
426-
adminInfo["use_channel"] = c.GetStringSlice("use_channel")
427-
if relayInfo != nil {
428-
if diagnostics := relayInfo.ConversionDiagnostics(); len(diagnostics) > 0 {
429-
adminInfo["conversion_diagnostics"] = diagnostics
430-
}
431-
if relayInfo.ConversionDiagnosticsTruncated() {
432-
adminInfo["conversion_diagnostics_truncated"] = true
433-
}
434-
}
435-
isMultiKey := common.GetContextKeyBool(c, constant.ContextKeyChannelIsMultiKey)
436-
if isMultiKey {
437-
adminInfo["is_multi_key"] = true
438-
adminInfo["multi_key_index"] = common.GetContextKeyInt(c, constant.ContextKeyChannelMultiKeyIndex)
416+
other.SetPublic("request_path", c.Request.URL.Path)
439417
}
440-
service.AppendChannelAffinityAdminInfo(c, adminInfo)
441-
other["admin_info"] = adminInfo
418+
other.SetPublic("error_type", err.GetErrorType())
419+
other.SetPublic("error_code", err.GetErrorCode())
420+
other.SetPublic("status_code", err.StatusCode)
421+
service.AppendRelayLogAdminInfo(c, relayInfo, other)
442422
service.AppendTaskPluginContextAuditInfo(c, other)
443423
startTime := common.GetContextKeyTime(c, constant.ContextKeyRequestStartTime)
444424
if startTime.IsZero() {
445425
startTime = time.Now()
446426
}
447427
useTimeSeconds := int(time.Since(startTime).Seconds())
448-
model.RecordErrorLog(c, userId, channelId, modelName, tokenName, err.MaskSensitiveErrorWithStatusCode(), tokenId, useTimeSeconds, common.GetContextKeyBool(c, constant.ContextKeyIsStream), userGroup, other)
428+
model.RecordErrorLog(c, userId, channelError.ChannelId, modelName, tokenName, err.MaskSensitiveErrorWithStatusCode(), tokenId, useTimeSeconds, common.GetContextKeyBool(c, constant.ContextKeyIsStream), userGroup, other)
449429
}
450430

451431
}

‎controller/relay_error_log_test.go‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package controller
2+
3+
import (
4+
"errors"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
"time"
9+
10+
"github.com/QuantumNous/new-api/common"
11+
"github.com/QuantumNous/new-api/constant"
12+
"github.com/QuantumNous/new-api/model"
13+
"github.com/QuantumNous/new-api/relaykit/types"
14+
15+
"github.com/gin-gonic/gin"
16+
"github.com/glebarez/sqlite"
17+
"github.com/stretchr/testify/assert"
18+
"github.com/stretchr/testify/require"
19+
"gorm.io/gorm"
20+
)
21+
22+
func TestProcessChannelErrorUsesSnapshotWithoutLeakingChannelMetadata(t *testing.T) {
23+
gin.SetMode(gin.TestMode)
24+
previousDB, previousLogDB := model.DB, model.LOG_DB
25+
previousRedisEnabled := common.RedisEnabled
26+
previousMainDatabaseType := common.MainDatabaseType()
27+
previousLogDatabaseType := common.LogDatabaseType()
28+
previousErrorLogEnabled := constant.ErrorLogEnabled
29+
30+
database, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
31+
require.NoError(t, err)
32+
sqlDB, err := database.DB()
33+
require.NoError(t, err)
34+
sqlDB.SetMaxOpenConns(1)
35+
require.NoError(t, database.AutoMigrate(&model.User{}, &model.Log{}))
36+
model.DB, model.LOG_DB = database, database
37+
common.RedisEnabled = false
38+
common.SetDatabaseTypes(common.DatabaseTypeSQLite, common.DatabaseTypeSQLite)
39+
constant.ErrorLogEnabled = true
40+
t.Cleanup(func() {
41+
model.DB, model.LOG_DB = previousDB, previousLogDB
42+
common.RedisEnabled = previousRedisEnabled
43+
common.SetDatabaseTypes(previousMainDatabaseType, previousLogDatabaseType)
44+
constant.ErrorLogEnabled = previousErrorLogEnabled
45+
require.NoError(t, sqlDB.Close())
46+
})
47+
48+
require.NoError(t, database.Create(&model.User{Id: 7, Username: "log-owner", Group: "default"}).Error)
49+
recorder := httptest.NewRecorder()
50+
ctx, _ := gin.CreateTestContext(recorder)
51+
ctx.Request = httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil)
52+
ctx.Set("id", 7)
53+
ctx.Set("username", "log-owner")
54+
ctx.Set("token_name", "test-token")
55+
ctx.Set("token_id", 11)
56+
ctx.Set("original_model", "gpt-test")
57+
ctx.Set("group", "default")
58+
ctx.Set("channel_id", 202)
59+
ctx.Set("channel_name", "mutable-context-channel")
60+
ctx.Set("channel_type", 9)
61+
ctx.Set("use_channel", []string{"101"})
62+
common.SetContextKey(ctx, constant.ContextKeyRequestStartTime, time.Now().Add(-time.Second))
63+
64+
channelSnapshot := types.ChannelError{
65+
ChannelId: 101,
66+
ChannelType: 1,
67+
ChannelName: "snapshot-channel",
68+
AutoBan: false,
69+
}
70+
apiErr := types.NewOpenAIError(errors.New("upstream failed"), types.ErrorCodeBadResponseStatusCode, http.StatusBadGateway)
71+
72+
processChannelError(ctx, channelSnapshot, apiErr, nil)
73+
74+
var stored model.Log
75+
require.NoError(t, database.First(&stored).Error)
76+
assert.Equal(t, channelSnapshot.ChannelId, stored.ChannelId)
77+
storedOther, err := common.StrToMap(stored.Other)
78+
require.NoError(t, err)
79+
assert.Equal(t, float64(http.StatusBadGateway), storedOther["status_code"])
80+
for _, key := range []string{"channel_id", "channel_name", "channel_type"} {
81+
assert.NotContains(t, storedOther, key)
82+
}
83+
adminInfo, ok := storedOther["admin_info"].(map[string]interface{})
84+
require.True(t, ok)
85+
assert.Equal(t, []interface{}{"101"}, adminInfo["use_channel"])
86+
87+
logs, total, err := model.GetUserLogs(7, model.LogTypeError, 0, 0, "", "", 0, 10, "", "", "")
88+
require.NoError(t, err)
89+
require.Equal(t, int64(1), total)
90+
require.Len(t, logs, 1)
91+
assert.Equal(t, channelSnapshot.ChannelId, logs[0].ChannelId)
92+
assert.Empty(t, logs[0].ChannelName)
93+
userOther, err := common.StrToMap(logs[0].Other)
94+
require.NoError(t, err)
95+
assert.NotContains(t, userOther, "admin_info")
96+
for _, key := range []string{"channel_id", "channel_name", "channel_type"} {
97+
assert.NotContains(t, userOther, key)
98+
}
99+
}

0 commit comments

Comments
 (0)