Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(security): close init config and parser-check redaction gaps

wizardchenfc64751f4ea3
diff --git a/internal/handler/dto/role.go b/internal/handler/dto/role.go
index 8877aa1c9..cf31c8e6c 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 cc5d7a26b..123d55895 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 f550a0c09..ad8c9d3dc 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 000000000..bafc250c0
--- /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 9b719d7f8..bb3763bf4 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 000000000..268b872c5
--- /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"])
+}