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.
This commit is contained in:
@@ -16,7 +16,12 @@ func CanViewIntegrationSecrets(ctx context.Context) bool {
|
|||||||
return RoleFromContext(ctx).HasPermission(types.TenantRoleAdmin)
|
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.
|
// CanViewTenantAPIKey is true for Owner+ only.
|
||||||
func CanViewTenantAPIKey(ctx context.Context) bool {
|
func CanViewTenantAPIKey(ctx context.Context) bool {
|
||||||
return RoleFromContext(ctx).HasPermission(types.TenantRoleOwner)
|
return RoleCanViewTenantAPIKey(RoleFromContext(ctx))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func NewTenantResponseWithRole(tenant *types.Tenant, role types.TenantRole) *Ten
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
includeSecrets := role.HasPermission(types.TenantRoleAdmin)
|
includeSecrets := role.HasPermission(types.TenantRoleAdmin)
|
||||||
includeAPIKey := role.HasPermission(types.TenantRoleOwner)
|
includeAPIKey := RoleCanViewTenantAPIKey(role)
|
||||||
|
|
||||||
resp := &TenantResponse{
|
resp := &TenantResponse{
|
||||||
ID: tenant.ID,
|
ID: tenant.ID,
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import (
|
|||||||
"github.com/Tencent/WeKnora/internal/assets"
|
"github.com/Tencent/WeKnora/internal/assets"
|
||||||
"github.com/Tencent/WeKnora/internal/config"
|
"github.com/Tencent/WeKnora/internal/config"
|
||||||
"github.com/Tencent/WeKnora/internal/errors"
|
"github.com/Tencent/WeKnora/internal/errors"
|
||||||
|
"github.com/Tencent/WeKnora/internal/handler/dto"
|
||||||
"github.com/Tencent/WeKnora/internal/logger"
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
"github.com/Tencent/WeKnora/internal/models/asr"
|
"github.com/Tencent/WeKnora/internal/models/asr"
|
||||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
"github.com/Tencent/WeKnora/internal/models/chat"
|
||||||
@@ -1377,15 +1378,16 @@ func (h *InitializationHandler) buildConfigResponse(ctx context.Context, models
|
|||||||
config := map[string]interface{}{
|
config := map[string]interface{}{
|
||||||
"hasFiles": hasFiles,
|
"hasFiles": hasFiles,
|
||||||
}
|
}
|
||||||
|
includeIntegrationDetail := dto.CanViewIntegrationSecrets(ctx)
|
||||||
|
|
||||||
// 按类型分组模型
|
// 按类型分组模型
|
||||||
for _, model := range models {
|
for _, model := range models {
|
||||||
if model == nil {
|
if model == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Hide sensitive information for builtin models
|
// Hide sensitive information for builtin models and viewers.
|
||||||
baseURL := model.Parameters.BaseURL
|
baseURL := model.Parameters.BaseURL
|
||||||
if model.IsBuiltin {
|
if model.IsBuiltin || !includeIntegrationDetail {
|
||||||
baseURL = ""
|
baseURL = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1415,7 +1417,7 @@ func (h *InitializationHandler) buildConfigResponse(ctx context.Context, models
|
|||||||
"modelName": model.Name,
|
"modelName": model.Name,
|
||||||
"baseUrl": baseURL,
|
"baseUrl": baseURL,
|
||||||
"credentials": map[string]bool{
|
"credentials": map[string]bool{
|
||||||
"apiKey": model.Parameters.APIKey != "",
|
"apiKey": model.Parameters.APIKey != "" && !model.IsBuiltin,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
case types.ModelTypeVLLM:
|
case types.ModelTypeVLLM:
|
||||||
|
|||||||
@@ -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")
|
||||||
|
}
|
||||||
@@ -326,15 +326,22 @@ func (h *SystemHandler) CheckParserEngines(c *gin.Context) {
|
|||||||
c.JSON(400, gin.H{"code": 1, "msg": "请求体格式错误"})
|
c.JSON(400, gin.H{"code": 1, "msg": "请求体格式错误"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
overrides := body.ToOverridesMap()
|
var existing *types.ParserEngineConfig
|
||||||
|
var tenant *types.Tenant
|
||||||
if v, exists := c.Get(types.TenantInfoContextKey.String()); exists {
|
if v, exists := c.Get(types.TenantInfoContextKey.String()); exists {
|
||||||
if tenant, ok := v.(*types.Tenant); ok && tenant != nil {
|
if t, ok := v.(*types.Tenant); ok && t != nil {
|
||||||
if creds := tenant.Credentials.GetWeKnoraCloud(); creds != nil {
|
tenant = t
|
||||||
if overrides == nil {
|
existing = t.ParserEngineConfig
|
||||||
overrides = make(map[string]string)
|
}
|
||||||
}
|
}
|
||||||
overrides["weknoracloud_app_id"] = creds.AppID
|
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)
|
reader, docreaderAddr, docreaderTransport := h.resolveDocReader(c.Request.Context(), overrides)
|
||||||
|
|||||||
@@ -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"])
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user