From fc64751f4ea36a7f62c734f52a96751068d22040 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Thu, 2 Jul 2026 14:38:41 +0800 Subject: [PATCH] fix(security): close init config and parser-check redaction gaps Strip model base URLs from viewer-accessible initialization config responses and merge redacted parser secrets before engine check overrides so admin test flows keep working after masked GET responses. --- internal/handler/dto/role.go | 7 ++- internal/handler/dto/tenant.go | 2 +- internal/handler/initialization.go | 8 ++- .../initialization_config_redaction_test.go | 58 +++++++++++++++++++ internal/handler/system.go | 21 ++++--- internal/handler/system_parser_check_test.go | 29 ++++++++++ 6 files changed, 113 insertions(+), 12 deletions(-) create mode 100644 internal/handler/initialization_config_redaction_test.go create mode 100644 internal/handler/system_parser_check_test.go diff --git a/internal/handler/dto/role.go b/internal/handler/dto/role.go index 8877aa1c..cf31c8e6 100644 --- a/internal/handler/dto/role.go +++ b/internal/handler/dto/role.go @@ -16,7 +16,12 @@ func CanViewIntegrationSecrets(ctx context.Context) bool { return RoleFromContext(ctx).HasPermission(types.TenantRoleAdmin) } +// RoleCanViewTenantAPIKey is true for Owner+ only. +func RoleCanViewTenantAPIKey(role types.TenantRole) bool { + return role.HasPermission(types.TenantRoleOwner) +} + // CanViewTenantAPIKey is true for Owner+ only. func CanViewTenantAPIKey(ctx context.Context) bool { - return RoleFromContext(ctx).HasPermission(types.TenantRoleOwner) + return RoleCanViewTenantAPIKey(RoleFromContext(ctx)) } diff --git a/internal/handler/dto/tenant.go b/internal/handler/dto/tenant.go index cc5d7a26..123d5589 100644 --- a/internal/handler/dto/tenant.go +++ b/internal/handler/dto/tenant.go @@ -45,7 +45,7 @@ func NewTenantResponseWithRole(tenant *types.Tenant, role types.TenantRole) *Ten return nil } includeSecrets := role.HasPermission(types.TenantRoleAdmin) - includeAPIKey := role.HasPermission(types.TenantRoleOwner) + includeAPIKey := RoleCanViewTenantAPIKey(role) resp := &TenantResponse{ ID: tenant.ID, diff --git a/internal/handler/initialization.go b/internal/handler/initialization.go index f550a0c0..ad8c9d3d 100644 --- a/internal/handler/initialization.go +++ b/internal/handler/initialization.go @@ -19,6 +19,7 @@ import ( "github.com/Tencent/WeKnora/internal/assets" "github.com/Tencent/WeKnora/internal/config" "github.com/Tencent/WeKnora/internal/errors" + "github.com/Tencent/WeKnora/internal/handler/dto" "github.com/Tencent/WeKnora/internal/logger" "github.com/Tencent/WeKnora/internal/models/asr" "github.com/Tencent/WeKnora/internal/models/chat" @@ -1377,15 +1378,16 @@ func (h *InitializationHandler) buildConfigResponse(ctx context.Context, models config := map[string]interface{}{ "hasFiles": hasFiles, } + includeIntegrationDetail := dto.CanViewIntegrationSecrets(ctx) // 按类型分组模型 for _, model := range models { if model == nil { continue } - // Hide sensitive information for builtin models + // Hide sensitive information for builtin models and viewers. baseURL := model.Parameters.BaseURL - if model.IsBuiltin { + if model.IsBuiltin || !includeIntegrationDetail { baseURL = "" } @@ -1415,7 +1417,7 @@ func (h *InitializationHandler) buildConfigResponse(ctx context.Context, models "modelName": model.Name, "baseUrl": baseURL, "credentials": map[string]bool{ - "apiKey": model.Parameters.APIKey != "", + "apiKey": model.Parameters.APIKey != "" && !model.IsBuiltin, }, } case types.ModelTypeVLLM: diff --git a/internal/handler/initialization_config_redaction_test.go b/internal/handler/initialization_config_redaction_test.go new file mode 100644 index 00000000..bafc250c --- /dev/null +++ b/internal/handler/initialization_config_redaction_test.go @@ -0,0 +1,58 @@ +package handler + +import ( + "context" + "encoding/json" + "testing" + + "github.com/Tencent/WeKnora/internal/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestBuildConfigResponse_ViewerOmitsModelBaseURL(t *testing.T) { + h := &InitializationHandler{} + ctx := context.WithValue(context.Background(), types.TenantRoleContextKey, types.TenantRoleViewer) + models := []*types.Model{{ + Type: types.ModelTypeKnowledgeQA, + Name: "custom-llm", + Parameters: types.ModelParameters{ + BaseURL: "https://tenant-private.example.com", + APIKey: "sk-secret-do-not-leak", + }, + }} + kb := &types.KnowledgeBase{} + + config := h.buildConfigResponse(ctx, models, kb, false) + llm, ok := config["llm"].(map[string]interface{}) + require.True(t, ok) + assert.Empty(t, llm["baseUrl"]) + + body, err := json.Marshal(config) + require.NoError(t, err) + assert.NotContains(t, string(body), "tenant-private.example.com") + assert.NotContains(t, string(body), "sk-secret-do-not-leak") +} + +func TestBuildConfigResponse_AdminKeepsModelBaseURL(t *testing.T) { + h := &InitializationHandler{} + ctx := context.WithValue(context.Background(), types.TenantRoleContextKey, types.TenantRoleAdmin) + models := []*types.Model{{ + Type: types.ModelTypeKnowledgeQA, + Name: "custom-llm", + Parameters: types.ModelParameters{ + BaseURL: "https://tenant-private.example.com", + APIKey: "sk-secret-do-not-leak", + }, + }} + kb := &types.KnowledgeBase{} + + config := h.buildConfigResponse(ctx, models, kb, false) + llm, ok := config["llm"].(map[string]interface{}) + require.True(t, ok) + assert.Equal(t, "https://tenant-private.example.com", llm["baseUrl"]) + + body, err := json.Marshal(config) + require.NoError(t, err) + assert.NotContains(t, string(body), "sk-secret-do-not-leak") +} diff --git a/internal/handler/system.go b/internal/handler/system.go index 9b719d7f..bb3763bf 100644 --- a/internal/handler/system.go +++ b/internal/handler/system.go @@ -326,15 +326,22 @@ func (h *SystemHandler) CheckParserEngines(c *gin.Context) { c.JSON(400, gin.H{"code": 1, "msg": "请求体格式错误"}) return } - overrides := body.ToOverridesMap() + var existing *types.ParserEngineConfig + var tenant *types.Tenant if v, exists := c.Get(types.TenantInfoContextKey.String()); exists { - if tenant, ok := v.(*types.Tenant); ok && tenant != nil { - if creds := tenant.Credentials.GetWeKnoraCloud(); creds != nil { - if overrides == nil { - overrides = make(map[string]string) - } - overrides["weknoracloud_app_id"] = creds.AppID + if t, ok := v.(*types.Tenant); ok && t != nil { + tenant = t + existing = t.ParserEngineConfig + } + } + merged := types.MergeParserEngineConfigForUpdate(&body, existing) + overrides := merged.ToOverridesMap() + if tenant != nil { + if creds := tenant.Credentials.GetWeKnoraCloud(); creds != nil { + if overrides == nil { + overrides = make(map[string]string) } + overrides["weknoracloud_app_id"] = creds.AppID } } reader, docreaderAddr, docreaderTransport := h.resolveDocReader(c.Request.Context(), overrides) diff --git a/internal/handler/system_parser_check_test.go b/internal/handler/system_parser_check_test.go new file mode 100644 index 00000000..268b872c --- /dev/null +++ b/internal/handler/system_parser_check_test.go @@ -0,0 +1,29 @@ +package handler + +import ( + "testing" + + "github.com/Tencent/WeKnora/internal/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +func TestParserEngineCheckOverrides_PreservesStoredSecrets(t *testing.T) { + body := types.ParserEngineConfig{ + MinerUAPIKey: types.RedactedSecretPlaceholder, + PaddleOCRVLCloudToken: types.RedactedSecretPlaceholder, + MinerUEndpoint: "http://mineru-new", + } + existing := &types.ParserEngineConfig{ + MinerUAPIKey: "mineru-secret", + PaddleOCRVLCloudToken: "paddle-secret", + MinerUEndpoint: "http://mineru-old", + } + + merged := types.MergeParserEngineConfigForUpdate(&body, existing) + require.NotNil(t, merged) + overrides := merged.ToOverridesMap() + assert.Equal(t, "mineru-secret", overrides["mineru_api_key"]) + assert.Equal(t, "paddle-secret", overrides["paddleocr_vl_cloud_token"]) + assert.Equal(t, "http://mineru-new", overrides["mineru_endpoint"]) +}