fix(security): redact integration secrets from viewer-accessible endpoints

Prevent low-privileged tenant members from reading raw API keys and
infrastructure credentials via tenant profile, KV config, model/MCP/web
search listings, and KB initialization config responses.
This commit is contained in:
wizardchen
2026-07-02 12:31:57 +08:00
committed by lyingbug
parent 8325dac738
commit 0b73080bb8
18 changed files with 649 additions and 61 deletions
+2 -1
View File
@@ -13,6 +13,7 @@ import (
"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/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
@@ -522,7 +523,7 @@ func (h *AuthHandler) GetCurrentUser(c *gin.Context) {
"success": true,
"data": gin.H{
"user": userInfo,
"tenant": tenant,
"tenant": dto.NewTenantResponse(ctx, tenant),
"memberships": memberships,
},
})
+14 -4
View File
@@ -10,6 +10,7 @@
package dto
import (
"context"
"time"
"github.com/Tencent/WeKnora/internal/types"
@@ -68,10 +69,11 @@ type CredentialFieldMetadata struct {
// Headers, EnvVars, StdioConfig) stripped — these reveal how the tenant
// configured an upstream provider and must not be visible to other tenants
// that see the same builtin row via the cross-tenant list.
func NewMCPServiceResponse(svc *types.MCPService) *MCPServiceResponse {
func NewMCPServiceResponse(ctx context.Context, svc *types.MCPService) *MCPServiceResponse {
if svc == nil {
return nil
}
includeDetail := CanViewIntegrationSecrets(ctx)
resp := &MCPServiceResponse{
ID: svc.ID,
TenantID: svc.TenantID,
@@ -88,14 +90,22 @@ func NewMCPServiceResponse(svc *types.MCPService) *MCPServiceResponse {
CreatedAt: svc.CreatedAt,
UpdatedAt: svc.UpdatedAt,
}
if !includeDetail {
resp.Headers = nil
resp.EnvVars = nil
}
if svc.AuthConfig != nil {
resp.AuthConfig = &MCPAuthConfigResponse{
auth := &MCPAuthConfigResponse{
AuthType: svc.AuthConfig.AuthType,
APIKeyHeader: svc.AuthConfig.APIKeyHeader,
CustomHeaders: svc.AuthConfig.CustomHeaders,
Scopes: svc.AuthConfig.Scopes,
AuthServerMetadataURL: svc.AuthConfig.AuthServerMetadataURL,
}
if !includeDetail {
auth.CustomHeaders = nil
}
resp.AuthConfig = auth
}
if svc.IsBuiltin {
// Builtin services are shared across tenants — strip everything that
@@ -115,10 +125,10 @@ func NewMCPServiceResponse(svc *types.MCPService) *MCPServiceResponse {
}
// NewMCPServiceResponses is the slice convenience wrapper used by ListMCPServices.
func NewMCPServiceResponses(svcs []*types.MCPService) []*MCPServiceResponse {
func NewMCPServiceResponses(ctx context.Context, svcs []*types.MCPService) []*MCPServiceResponse {
out := make([]*MCPServiceResponse, 0, len(svcs))
for _, s := range svcs {
out = append(out, NewMCPServiceResponse(s))
out = append(out, NewMCPServiceResponse(ctx, s))
}
return out
}
+21 -5
View File
@@ -25,7 +25,7 @@ func TestMCPServiceResponse_OmitsSecrets(t *testing.T) {
CustomHeaders: map[string]string{"X-Trace": "abc"},
},
}
body, err := json.Marshal(NewMCPServiceResponse(svc))
body, err := json.Marshal(NewMCPServiceResponse(adminContext(), svc))
assert.NoError(t, err)
s := string(body)
assert.NotContains(t, s, "sk-real-api-key-do-not-leak",
@@ -63,7 +63,7 @@ func TestMCPServiceResponse_BuiltinStripsTenantConfig(t *testing.T) {
APIKey: "should-not-leak-via-builtin",
},
}
resp := NewMCPServiceResponse(svc)
resp := NewMCPServiceResponse(adminContext(), svc)
assert.Nil(t, resp.URL, "builtin must not leak per-tenant URL")
assert.Nil(t, resp.Headers, "builtin must not leak per-tenant headers")
assert.Nil(t, resp.AuthConfig, "builtin must not leak auth config")
@@ -73,7 +73,23 @@ func TestMCPServiceResponse_BuiltinStripsTenantConfig(t *testing.T) {
assert.False(t, strings.Contains(string(body), "X-Tenant-Secret"))
}
func TestMCPServiceResponse_NilSafe(t *testing.T) {
assert.Nil(t, NewMCPServiceResponse(nil))
assert.Equal(t, []*MCPServiceResponse{}, NewMCPServiceResponses(nil))
func TestMCPServiceResponse_ViewerStripsIntegrationDetail(t *testing.T) {
svc := &types.MCPService{
ID: "svc-2",
Headers: types.MCPHeaders{"Authorization": "Bearer secret"},
EnvVars: types.MCPEnvVars{"TOKEN": "secret"},
AuthConfig: &types.MCPAuthConfig{
CustomHeaders: map[string]string{"X-Auth": "secret"},
},
}
resp := NewMCPServiceResponse(viewerContext(), svc)
assert.Nil(t, resp.Headers)
assert.Nil(t, resp.EnvVars)
assert.NotNil(t, resp.AuthConfig)
assert.Nil(t, resp.AuthConfig.CustomHeaders)
}
func TestMCPServiceResponse_NilSafe(t *testing.T) {
assert.Nil(t, NewMCPServiceResponse(adminContext(), nil))
assert.Equal(t, []*MCPServiceResponse{}, NewMCPServiceResponses(adminContext(), nil))
}
+8 -3
View File
@@ -1,6 +1,7 @@
package dto
import (
"context"
"time"
"github.com/Tencent/WeKnora/internal/types"
@@ -53,7 +54,7 @@ type ModelParametersDTO struct {
//
// Builtin models are shared across tenants — strip BaseURL (which can leak
// the tenant's private endpoint) and any non-shared parameters.
func NewModelResponse(m *types.Model) *ModelResponse {
func NewModelResponse(ctx context.Context, m *types.Model) *ModelResponse {
if m == nil {
return nil
}
@@ -68,6 +69,10 @@ func NewModelResponse(m *types.Model) *ModelResponse {
SupportsVision: m.Parameters.SupportsVision,
AppID: m.Parameters.AppID,
}
if !CanViewIntegrationSecrets(ctx) {
params.ExtraConfig = nil
params.CustomHeaders = nil
}
if m.IsBuiltin {
// Builtin: strip everything that could reveal per-tenant config.
// EmbeddingParameters and ParameterSize / Provider / InterfaceType /
@@ -104,10 +109,10 @@ func NewModelResponse(m *types.Model) *ModelResponse {
}
// NewModelResponses is the slice convenience wrapper.
func NewModelResponses(models []*types.Model) []*ModelResponse {
func NewModelResponses(ctx context.Context, models []*types.Model) []*ModelResponse {
out := make([]*ModelResponse, 0, len(models))
for _, m := range models {
out = append(out, NewModelResponse(m))
out = append(out, NewModelResponse(ctx, m))
}
return out
}
+18 -5
View File
@@ -22,7 +22,7 @@ func TestModelResponse_OmitsSecrets(t *testing.T) {
Provider: "openai",
},
}
body, err := json.Marshal(NewModelResponse(m))
body, err := json.Marshal(NewModelResponse(adminContext(), m))
assert.NoError(t, err)
s := string(body)
assert.NotContains(t, s, "sk-real-api-key-do-not-leak")
@@ -55,7 +55,7 @@ func TestModelResponse_BuiltinStripsTenantConfig(t *testing.T) {
ExtraConfig: map[string]string{"region": "cn-hangzhou"},
},
}
resp := NewModelResponse(m)
resp := NewModelResponse(adminContext(), m)
assert.Empty(t, resp.Parameters.BaseURL,
"builtin must not leak per-tenant base URL")
assert.Empty(t, resp.Parameters.AppID,
@@ -70,7 +70,20 @@ func TestModelResponse_BuiltinStripsTenantConfig(t *testing.T) {
assert.False(t, strings.Contains(string(body), "tenant-private.example.com"))
}
func TestModelResponse_NilSafe(t *testing.T) {
assert.Nil(t, NewModelResponse(nil))
assert.Equal(t, []*ModelResponse{}, NewModelResponses(nil))
func TestModelResponse_ViewerStripsIntegrationDetail(t *testing.T) {
m := &types.Model{
ID: "m-2",
Parameters: types.ModelParameters{
CustomHeaders: map[string]string{"Authorization": "Bearer secret"},
ExtraConfig: map[string]string{"region": "cn-hangzhou"},
},
}
resp := NewModelResponse(viewerContext(), m)
assert.Nil(t, resp.Parameters.CustomHeaders)
assert.Nil(t, resp.Parameters.ExtraConfig)
}
func TestModelResponse_NilSafe(t *testing.T) {
assert.Nil(t, NewModelResponse(adminContext(), nil))
assert.Equal(t, []*ModelResponse{}, NewModelResponses(adminContext(), nil))
}
+22
View File
@@ -0,0 +1,22 @@
package dto
import (
"context"
"github.com/Tencent/WeKnora/internal/types"
)
// RoleFromContext returns the caller's tenant role from ctx.
func RoleFromContext(ctx context.Context) types.TenantRole {
return types.TenantRoleFromContext(ctx)
}
// CanViewIntegrationSecrets is true for Admin+ (includes Owner).
func CanViewIntegrationSecrets(ctx context.Context) bool {
return RoleFromContext(ctx).HasPermission(types.TenantRoleAdmin)
}
// CanViewTenantAPIKey is true for Owner+ only.
func CanViewTenantAPIKey(ctx context.Context) bool {
return RoleFromContext(ctx).HasPermission(types.TenantRoleOwner)
}
+79
View File
@@ -0,0 +1,79 @@
package dto
import (
"context"
"time"
"github.com/Tencent/WeKnora/internal/types"
"gorm.io/gorm"
)
// TenantResponse is the viewer-safe tenant profile shape. Secret-bearing
// columns are omitted or redacted unless the caller has Admin+ (Owner for
// api_key).
type TenantResponse struct {
ID uint64 `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
APIKey string `json:"api_key,omitempty"`
Status string `json:"status"`
RetrieverEngines types.RetrieverEngines `json:"retriever_engines"`
Business string `json:"business"`
StorageQuota int64 `json:"storage_quota"`
StorageUsed int64 `json:"storage_used"`
ContextConfig *types.ContextConfig `json:"context_config,omitempty"`
WebSearchConfig *types.WebSearchConfig `json:"web_search_config,omitempty"`
ParserEngineConfig *types.ParserEngineConfig `json:"parser_engine_config,omitempty"`
Credentials *types.CredentialsConfig `json:"credentials,omitempty"`
StorageEngineConfig *types.StorageEngineConfig `json:"storage_engine_config,omitempty"`
ChatHistoryConfig *types.ChatHistoryConfig `json:"chat_history_config,omitempty"`
RetrievalConfig *types.RetrievalConfig `json:"retrieval_config,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `json:"deleted_at"`
}
// NewTenantResponse converts a stored tenant into its HTTP response shape.
func NewTenantResponse(ctx context.Context, tenant *types.Tenant) *TenantResponse {
if tenant == nil {
return nil
}
includeSecrets := CanViewIntegrationSecrets(ctx)
includeAPIKey := CanViewTenantAPIKey(ctx)
resp := &TenantResponse{
ID: tenant.ID,
Name: tenant.Name,
Description: tenant.Description,
Status: tenant.Status,
RetrieverEngines: tenant.RetrieverEngines,
Business: tenant.Business,
StorageQuota: tenant.StorageQuota,
StorageUsed: tenant.StorageUsed,
ContextConfig: tenant.ContextConfig,
ChatHistoryConfig: tenant.ChatHistoryConfig,
RetrievalConfig: tenant.RetrievalConfig,
CreatedAt: tenant.CreatedAt,
UpdatedAt: tenant.UpdatedAt,
DeletedAt: tenant.DeletedAt,
}
if includeAPIKey {
resp.APIKey = tenant.APIKey
}
if includeSecrets {
resp.WebSearchConfig = types.WebSearchConfigForResponse(tenant.WebSearchConfig, true)
resp.ParserEngineConfig = types.ParserEngineConfigForResponse(tenant.ParserEngineConfig, true)
resp.Credentials = types.CredentialsConfigForResponse(tenant.Credentials, true)
resp.StorageEngineConfig = types.StorageEngineConfigForResponse(tenant.StorageEngineConfig, true)
}
return resp
}
// NewTenantResponses is the slice convenience wrapper.
func NewTenantResponses(ctx context.Context, tenants []*types.Tenant) []*TenantResponse {
out := make([]*TenantResponse, 0, len(tenants))
for _, t := range tenants {
out = append(out, NewTenantResponse(ctx, t))
}
return out
}
+77
View File
@@ -0,0 +1,77 @@
package dto
import (
"encoding/json"
"testing"
"github.com/Tencent/WeKnora/internal/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTenantResponse_ViewerOmitsSecrets(t *testing.T) {
tenant := sampleSecretTenant()
body, err := json.Marshal(NewTenantResponse(viewerContext(), tenant))
require.NoError(t, err)
s := string(body)
assert.NotContains(t, s, "tenant-api-key-123")
assert.NotContains(t, s, "legacy-search-secret-999")
assert.NotContains(t, s, "wk-app-secret-def")
assert.NotContains(t, s, "parser-secret-123")
assert.NotContains(t, s, "minio-secret-789")
assert.NotContains(t, s, "web_search_config")
assert.NotContains(t, s, "parser_engine_config")
assert.NotContains(t, s, "storage_engine_config")
assert.NotContains(t, s, "credentials")
}
func TestTenantResponse_OwnerGetsAPIKeyOnly(t *testing.T) {
tenant := sampleSecretTenant()
body, err := json.Marshal(NewTenantResponse(ownerContext(), tenant))
require.NoError(t, err)
s := string(body)
assert.Contains(t, s, "tenant-api-key-123")
assert.NotContains(t, s, "legacy-search-secret-999")
assert.NotContains(t, s, "parser-secret-123")
}
func TestTenantResponse_AdminGetsRedactedIntegrationConfigs(t *testing.T) {
tenant := sampleSecretTenant()
resp := NewTenantResponse(adminContext(), tenant)
require.NotNil(t, resp.WebSearchConfig)
assert.Equal(t, types.RedactedSecretPlaceholder, resp.WebSearchConfig.ProxyURL)
assert.Empty(t, resp.WebSearchConfig.APIKey)
require.NotNil(t, resp.ParserEngineConfig)
assert.Equal(t, types.RedactedSecretPlaceholder, resp.ParserEngineConfig.MinerUAPIKey)
require.NotNil(t, resp.StorageEngineConfig.MinIO)
assert.Equal(t, types.RedactedSecretPlaceholder, resp.StorageEngineConfig.MinIO.SecretAccessKey)
}
func sampleSecretTenant() *types.Tenant {
return &types.Tenant{
ID: 42,
Name: "tenant",
APIKey: "tenant-api-key-123",
WebSearchConfig: &types.WebSearchConfig{
APIKey: "legacy-search-secret-999",
ProxyURL: "http://proxy.internal:8080",
},
Credentials: &types.CredentialsConfig{
WeKnoraCloud: &types.WeKnoraCloudCredentials{
AppID: "wk-app-id-abc",
AppSecret: "wk-app-secret-def",
},
},
ParserEngineConfig: &types.ParserEngineConfig{
MinerUAPIKey: "parser-secret-123",
PaddleOCRVLCloudToken: "paddle-secret-456",
},
StorageEngineConfig: &types.StorageEngineConfig{
DefaultProvider: "minio",
MinIO: &types.MinIOEngineConfig{
AccessKeyID: "minio-access-id",
SecretAccessKey: "minio-secret-789",
},
},
}
}
+22
View File
@@ -0,0 +1,22 @@
package dto
import (
"context"
"github.com/Tencent/WeKnora/internal/types"
)
func adminContext() context.Context {
ctx := context.Background()
return context.WithValue(ctx, types.TenantRoleContextKey, types.TenantRoleAdmin)
}
func viewerContext() context.Context {
ctx := context.Background()
return context.WithValue(ctx, types.TenantRoleContextKey, types.TenantRoleViewer)
}
func ownerContext() context.Context {
ctx := context.Background()
return context.WithValue(ctx, types.TenantRoleContextKey, types.TenantRoleOwner)
}
+18 -12
View File
@@ -1,6 +1,7 @@
package dto
import (
"context"
"time"
"github.com/Tencent/WeKnora/internal/types"
@@ -34,35 +35,40 @@ type WebSearchProviderParametersDTO struct {
}
// NewWebSearchProviderResponse converts a stored entity into its response shape.
func NewWebSearchProviderResponse(e *types.WebSearchProviderEntity) *WebSearchProviderResponse {
func NewWebSearchProviderResponse(ctx context.Context, e *types.WebSearchProviderEntity) *WebSearchProviderResponse {
if e == nil {
return nil
}
params := WebSearchProviderParametersDTO{
EngineID: e.Parameters.EngineID,
BaseURL: e.Parameters.BaseURL,
ProxyURL: e.Parameters.ProxyURL,
ExtraConfig: e.Parameters.ExtraConfig,
}
if !CanViewIntegrationSecrets(ctx) {
params.ProxyURL = ""
params.ExtraConfig = nil
}
return &WebSearchProviderResponse{
ID: e.ID,
TenantID: e.TenantID,
Name: e.Name,
Provider: e.Provider,
Description: e.Description,
Parameters: WebSearchProviderParametersDTO{
EngineID: e.Parameters.EngineID,
BaseURL: e.Parameters.BaseURL,
ProxyURL: e.Parameters.ProxyURL,
ExtraConfig: e.Parameters.ExtraConfig,
},
IsDefault: e.IsDefault,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
Parameters: params,
IsDefault: e.IsDefault,
CreatedAt: e.CreatedAt,
UpdatedAt: e.UpdatedAt,
Credentials: map[string]CredentialFieldMetadata{
"api_key": {Configured: e.Parameters.APIKey != ""},
},
}
}
func NewWebSearchProviderResponses(es []*types.WebSearchProviderEntity) []*WebSearchProviderResponse {
func NewWebSearchProviderResponses(ctx context.Context, es []*types.WebSearchProviderEntity) []*WebSearchProviderResponse {
out := make([]*WebSearchProviderResponse, 0, len(es))
for _, e := range es {
out = append(out, NewWebSearchProviderResponse(e))
out = append(out, NewWebSearchProviderResponse(ctx, e))
}
return out
}
@@ -19,7 +19,7 @@ func TestWebSearchProviderResponse_OmitsSecrets(t *testing.T) {
BaseURL: "https://example.com",
},
}
body, err := json.Marshal(NewWebSearchProviderResponse(e))
body, err := json.Marshal(NewWebSearchProviderResponse(adminContext(), e))
assert.NoError(t, err)
s := string(body)
assert.NotContains(t, s, "bing-secret-do-not-leak")
@@ -35,7 +35,20 @@ func TestWebSearchProviderResponse_OmitsSecrets(t *testing.T) {
assert.Contains(t, s, "example.com")
}
func TestWebSearchProviderResponse_NilSafe(t *testing.T) {
assert.Nil(t, NewWebSearchProviderResponse(nil))
assert.Equal(t, []*WebSearchProviderResponse{}, NewWebSearchProviderResponses(nil))
func TestWebSearchProviderResponse_ViewerStripsIntegrationDetail(t *testing.T) {
e := &types.WebSearchProviderEntity{
ID: "wsp-2",
Parameters: types.WebSearchProviderParameters{
ProxyURL: "http://proxy:8080",
ExtraConfig: map[string]string{"token": "secret"},
},
}
resp := NewWebSearchProviderResponse(viewerContext(), e)
assert.Empty(t, resp.Parameters.ProxyURL)
assert.Nil(t, resp.Parameters.ExtraConfig)
}
func TestWebSearchProviderResponse_NilSafe(t *testing.T) {
assert.Nil(t, NewWebSearchProviderResponse(adminContext(), nil))
assert.Equal(t, []*WebSearchProviderResponse{}, NewWebSearchProviderResponses(adminContext(), nil))
}
+19 -9
View File
@@ -1385,10 +1385,8 @@ func (h *InitializationHandler) buildConfigResponse(ctx context.Context, models
}
// Hide sensitive information for builtin models
baseURL := model.Parameters.BaseURL
apiKey := model.Parameters.APIKey
if model.IsBuiltin {
baseURL = ""
apiKey = ""
}
switch model.Type {
@@ -1397,22 +1395,28 @@ func (h *InitializationHandler) buildConfigResponse(ctx context.Context, models
"source": string(model.Source),
"modelName": model.Name,
"baseUrl": baseURL,
"apiKey": apiKey,
"credentials": map[string]bool{
"apiKey": model.Parameters.APIKey != "" && !model.IsBuiltin,
},
}
case types.ModelTypeEmbedding:
config["embedding"] = map[string]interface{}{
"source": string(model.Source),
"modelName": model.Name,
"baseUrl": baseURL,
"apiKey": apiKey,
"dimension": model.Parameters.EmbeddingParameters.Dimension,
"credentials": map[string]bool{
"apiKey": model.Parameters.APIKey != "" && !model.IsBuiltin,
},
}
case types.ModelTypeRerank:
config["rerank"] = map[string]interface{}{
"enabled": true,
"modelName": model.Name,
"baseUrl": baseURL,
"apiKey": apiKey,
"credentials": map[string]bool{
"apiKey": model.Parameters.APIKey != "",
},
}
case types.ModelTypeVLLM:
if config["multimodal"] == nil {
@@ -1424,9 +1428,11 @@ func (h *InitializationHandler) buildConfigResponse(ctx context.Context, models
multimodal["vlm"] = map[string]interface{}{
"modelName": model.Name,
"baseUrl": baseURL,
"apiKey": apiKey,
"interfaceType": model.Parameters.InterfaceType,
"modelId": model.ID,
"credentials": map[string]bool{
"apiKey": model.Parameters.APIKey != "" && !model.IsBuiltin,
},
}
}
}
@@ -1450,7 +1456,9 @@ func (h *InitializationHandler) buildConfigResponse(ctx context.Context, models
"enabled": false,
"modelName": "",
"baseUrl": "",
"apiKey": "",
"credentials": map[string]bool{
"apiKey": false,
},
}
}
@@ -1485,12 +1493,14 @@ func (h *InitializationHandler) buildConfigResponse(ctx context.Context, models
switch effectiveProvider {
case "cos":
multimodal["cos"] = map[string]interface{}{
"secretId": kb.StorageConfig.SecretID,
"secretKey": kb.StorageConfig.SecretKey,
"region": kb.StorageConfig.Region,
"bucketName": kb.StorageConfig.BucketName,
"appId": kb.StorageConfig.AppID,
"pathPrefix": kb.StorageConfig.PathPrefix,
"credentials": map[string]bool{
"secretId": kb.StorageConfig.SecretID != "",
"secretKey": kb.StorageConfig.SecretKey != "",
},
}
case "minio":
multimodal["minio"] = map[string]interface{}{
+4 -4
View File
@@ -85,7 +85,7 @@ func (h *MCPServiceHandler) CreateMCPService(c *gin.Context) {
// construction — no runtime redaction needed.
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": dto.NewMCPServiceResponse(&service),
"data": dto.NewMCPServiceResponse(ctx,&service),
})
}
@@ -119,7 +119,7 @@ func (h *MCPServiceHandler) ListMCPServices(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": dto.NewMCPServiceResponses(services),
"data": dto.NewMCPServiceResponses(ctx, services),
})
}
@@ -158,7 +158,7 @@ func (h *MCPServiceHandler) GetMCPService(c *gin.Context) {
// so the cross-tenant builtin list does not leak per-tenant config.
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": dto.NewMCPServiceResponse(service),
"data": dto.NewMCPServiceResponse(ctx,service),
})
}
@@ -351,7 +351,7 @@ func (h *MCPServiceHandler) UpdateMCPService(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": dto.NewMCPServiceResponse(stored),
"data": dto.NewMCPServiceResponse(ctx,stored),
})
}
+4 -4
View File
@@ -118,7 +118,7 @@ func (h *ModelHandler) CreateModel(c *gin.Context) {
c.JSON(http.StatusCreated, gin.H{
"success": true,
"data": dto.NewModelResponse(model),
"data": dto.NewModelResponse(ctx,model),
})
}
@@ -163,7 +163,7 @@ func (h *ModelHandler) GetModel(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": dto.NewModelResponse(model),
"data": dto.NewModelResponse(ctx,model),
})
}
@@ -201,7 +201,7 @@ func (h *ModelHandler) ListModels(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": dto.NewModelResponses(models),
"data": dto.NewModelResponses(ctx, models),
})
}
@@ -634,7 +634,7 @@ func (h *ModelHandler) UpdateModel(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": dto.NewModelResponse(model),
"data": dto.NewModelResponse(ctx,model),
})
}
+15 -6
View File
@@ -12,6 +12,7 @@ import (
"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/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
@@ -411,7 +412,7 @@ func (h *TenantHandler) GetTenant(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": tenant,
"data": dto.NewTenantResponse(ctx, tenant),
})
}
@@ -502,7 +503,7 @@ func (h *TenantHandler) UpdateTenant(c *gin.Context) {
)
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": updatedTenant,
"data": dto.NewTenantResponse(ctx, updatedTenant),
})
}
@@ -852,7 +853,7 @@ func (h *TenantHandler) ListTenants(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": gin.H{
"items": []*types.Tenant{tenant},
"items": []*dto.TenantResponse{dto.NewTenantResponse(ctx, tenant)},
},
})
}
@@ -983,6 +984,14 @@ func (h *TenantHandler) GetTenantKV(c *gin.Context) {
ctx := c.Request.Context()
key := secutils.SanitizeForLog(c.Param("key"))
switch key {
case "web-search-config", "parser-engine-config", "storage-engine-config":
if !dto.CanViewIntegrationSecrets(ctx) {
c.Error(errors.NewForbiddenError("integration configuration requires admin access"))
return
}
}
switch key {
case "web-search-config":
h.GetTenantWebSearchConfig(c)
@@ -1120,7 +1129,7 @@ func (h *TenantHandler) GetTenantWebSearchConfig(c *gin.Context) {
logger.Infof(ctx, "Tenant web search config retrieved successfully, Tenant ID: %d", tenant.ID)
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": types.EffectiveWebSearchConfig(tenant.WebSearchConfig),
"data": types.WebSearchConfigForResponse(tenant.WebSearchConfig, true),
})
}
@@ -1133,7 +1142,7 @@ func (h *TenantHandler) GetTenantParserEngineConfig(c *gin.Context) {
c.Error(errors.NewBadRequestError("Tenant is empty"))
return
}
data := tenant.ParserEngineConfig
data := types.ParserEngineConfigForResponse(tenant.ParserEngineConfig, true)
if data == nil {
data = &types.ParserEngineConfig{}
}
@@ -1185,7 +1194,7 @@ func (h *TenantHandler) GetTenantStorageEngineConfig(c *gin.Context) {
c.Error(errors.NewBadRequestError("Tenant is empty"))
return
}
data := tenant.StorageEngineConfig
data := types.StorageEngineConfigForResponse(tenant.StorageEngineConfig, true)
if data == nil {
data = &types.StorageEngineConfig{}
}
@@ -0,0 +1,162 @@
package handler
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/Tencent/WeKnora/internal/middleware"
"github.com/Tencent/WeKnora/internal/types"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type stubTenantService struct {
tenant *types.Tenant
}
func (s *stubTenantService) CreateTenant(context.Context, *types.Tenant) (*types.Tenant, error) {
return nil, nil
}
func (s *stubTenantService) GetTenantByID(context.Context, uint64) (*types.Tenant, error) {
return s.tenant, nil
}
func (s *stubTenantService) GetTenantsByIDs(context.Context, []uint64) (map[uint64]*types.Tenant, error) {
return map[uint64]*types.Tenant{s.tenant.ID: s.tenant}, nil
}
func (s *stubTenantService) UpdateTenant(context.Context, *types.Tenant) (*types.Tenant, error) {
return s.tenant, nil
}
func (s *stubTenantService) DeleteTenant(context.Context, uint64) error { return nil }
func (s *stubTenantService) ListTenants(context.Context) ([]*types.Tenant, error) {
return []*types.Tenant{s.tenant}, nil
}
func (s *stubTenantService) ListAllTenants(context.Context) ([]*types.Tenant, error) {
return nil, nil
}
func (s *stubTenantService) SearchTenants(context.Context, string, uint64, int, int) ([]*types.Tenant, int64, error) {
return nil, 0, nil
}
func (s *stubTenantService) UpdateAPIKey(context.Context, uint64) (string, error) { return "", nil }
func (s *stubTenantService) ExtractTenantIDFromAPIKey(string) (uint64, error) { return 0, nil }
func (s *stubTenantService) BulkSetStorageQuota(context.Context, int64) (int64, error) {
return 0, nil
}
func (s *stubTenantService) GetTenantByIDForUser(context.Context, uint64, string) (*types.Tenant, error) {
return s.tenant, nil
}
func (s *stubTenantService) GetWeKnoraCloudCredentials(context.Context) *types.WeKnoraCloudCredentials {
return nil
}
func newTenantHandlerTestEngine(t *testing.T, role types.TenantRole, tenant *types.Tenant) *gin.Engine {
t.Helper()
gin.SetMode(gin.TestMode)
h := &TenantHandler{service: &stubTenantService{tenant: tenant}}
r := gin.New()
r.Use(middleware.ErrorHandler())
r.Use(func(c *gin.Context) {
ctx := c.Request.Context()
ctx = context.WithValue(ctx, types.TenantIDContextKey, tenant.ID)
ctx = context.WithValue(ctx, types.TenantRoleContextKey, role)
ctx = context.WithValue(ctx, types.TenantInfoContextKey, tenant)
c.Request = c.Request.WithContext(ctx)
c.Set(types.TenantIDContextKey.String(), tenant.ID)
c.Next()
})
r.GET("/tenants", h.ListTenants)
r.GET("/tenants/:id", h.GetTenant)
r.GET("/tenants/kv/:key", h.GetTenantKV)
return r
}
func TestListTenantsViewerDoesNotLeakSecrets(t *testing.T) {
tenant := secretTenantFixture()
engine := newTenantHandlerTestEngine(t, types.TenantRoleViewer, tenant)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/tenants", nil)
engine.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
body := rec.Body.String()
assert.NotContains(t, body, "tenant-api-key-123")
assert.NotContains(t, body, "parser-secret-123")
}
func TestGetTenantViewerDoesNotLeakSecrets(t *testing.T) {
tenant := secretTenantFixture()
engine := newTenantHandlerTestEngine(t, types.TenantRoleViewer, tenant)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/tenants/42", nil)
engine.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
assert.NotContains(t, rec.Body.String(), "tenant-api-key-123")
}
func TestGetTenantKVViewerForbiddenForSecretKeys(t *testing.T) {
tenant := secretTenantFixture()
engine := newTenantHandlerTestEngine(t, types.TenantRoleViewer, tenant)
for _, key := range []string{"web-search-config", "parser-engine-config", "storage-engine-config"} {
t.Run(key, func(t *testing.T) {
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/tenants/kv/"+key, nil)
engine.ServeHTTP(rec, req)
require.Equal(t, http.StatusForbidden, rec.Code)
})
}
}
func TestGetTenantKVAdminReturnsRedactedSecrets(t *testing.T) {
tenant := secretTenantFixture()
engine := newTenantHandlerTestEngine(t, types.TenantRoleAdmin, tenant)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/tenants/kv/parser-engine-config", nil)
engine.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
var payload struct {
Success bool `json:"success"`
Data types.ParserEngineConfig `json:"data"`
}
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &payload))
assert.Equal(t, types.RedactedSecretPlaceholder, payload.Data.MinerUAPIKey)
assert.NotContains(t, rec.Body.String(), "parser-secret-123")
}
func secretTenantFixture() *types.Tenant {
return &types.Tenant{
ID: 42,
Name: "tenant",
APIKey: "tenant-api-key-123",
WebSearchConfig: &types.WebSearchConfig{
APIKey: "legacy-search-secret-999",
},
ParserEngineConfig: &types.ParserEngineConfig{
MinerUAPIKey: "parser-secret-123",
},
StorageEngineConfig: &types.StorageEngineConfig{
MinIO: &types.MinIOEngineConfig{
SecretAccessKey: "minio-secret-789",
},
},
}
}
func TestGetTenantKVViewerAllowedForNonSecretKey(t *testing.T) {
tenant := secretTenantFixture()
tenant.RetrievalConfig = &types.RetrievalConfig{}
engine := newTenantHandlerTestEngine(t, types.TenantRoleViewer, tenant)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/tenants/kv/retrieval-config", nil)
engine.ServeHTTP(rec, req)
require.Equal(t, http.StatusOK, rec.Code)
}
+4 -4
View File
@@ -111,7 +111,7 @@ func (h *WebSearchProviderHandler) CreateProvider(c *gin.Context) {
c.JSON(http.StatusCreated, gin.H{
"success": true,
"data": dto.NewWebSearchProviderResponse(provider),
"data": dto.NewWebSearchProviderResponse(ctx,provider),
})
}
@@ -134,7 +134,7 @@ func (h *WebSearchProviderHandler) ListProviders(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": dto.NewWebSearchProviderResponses(providers),
"data": dto.NewWebSearchProviderResponses(ctx, providers),
})
}
@@ -169,7 +169,7 @@ func (h *WebSearchProviderHandler) GetProvider(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"success": true,
"data": dto.NewWebSearchProviderResponse(provider),
"data": dto.NewWebSearchProviderResponse(ctx,provider),
})
}
@@ -263,7 +263,7 @@ func (h *WebSearchProviderHandler) UpdateProvider(c *gin.Context) {
// Re-fetch to get the full stored state
updated, _ := h.repo.GetByID(ctx, tenantID, id)
if updated != nil {
c.JSON(http.StatusOK, gin.H{"success": true, "data": dto.NewWebSearchProviderResponse(updated)})
c.JSON(http.StatusOK, gin.H{"success": true, "data": dto.NewWebSearchProviderResponse(ctx,updated)})
} else {
c.JSON(http.StatusOK, gin.H{"success": true})
}
+143
View File
@@ -0,0 +1,143 @@
package types
import "strings"
// WebSearchConfigForResponse returns a copy safe for HTTP responses.
// When maskSecrets is true, api_key is omitted and a configured proxy_url
// is replaced with RedactedSecretPlaceholder.
func WebSearchConfigForResponse(cfg *WebSearchConfig, maskSecrets bool) *WebSearchConfig {
if cfg == nil {
return nil
}
out := *EffectiveWebSearchConfig(cfg)
if !maskSecrets {
return &out
}
out.APIKey = ""
if strings.TrimSpace(out.ProxyURL) != "" {
out.ProxyURL = RedactedSecretPlaceholder
}
return &out
}
// ParserEngineConfigForResponse returns a copy with secret fields redacted
// when maskSecrets is true.
func ParserEngineConfigForResponse(cfg *ParserEngineConfig, maskSecrets bool) *ParserEngineConfig {
if cfg == nil {
return nil
}
out := *cfg
if !maskSecrets {
return &out
}
if out.MinerUAPIKey != "" {
out.MinerUAPIKey = RedactedSecretPlaceholder
}
if out.PaddleOCRVLCloudToken != "" {
out.PaddleOCRVLCloudToken = RedactedSecretPlaceholder
}
return &out
}
// StorageEngineConfigForResponse returns a copy with provider secret fields
// redacted when maskSecrets is true.
func StorageEngineConfigForResponse(cfg *StorageEngineConfig, maskSecrets bool) *StorageEngineConfig {
if cfg == nil {
return nil
}
out := *cfg
if !maskSecrets {
return &out
}
if out.MinIO != nil {
minio := *out.MinIO
if minio.AccessKeyID != "" {
minio.AccessKeyID = RedactedSecretPlaceholder
}
if minio.SecretAccessKey != "" {
minio.SecretAccessKey = RedactedSecretPlaceholder
}
out.MinIO = &minio
}
if out.COS != nil {
cos := *out.COS
if cos.SecretID != "" {
cos.SecretID = RedactedSecretPlaceholder
}
if cos.SecretKey != "" {
cos.SecretKey = RedactedSecretPlaceholder
}
out.COS = &cos
}
if out.TOS != nil {
tos := *out.TOS
if tos.AccessKey != "" {
tos.AccessKey = RedactedSecretPlaceholder
}
if tos.SecretKey != "" {
tos.SecretKey = RedactedSecretPlaceholder
}
out.TOS = &tos
}
if out.S3 != nil {
s3 := *out.S3
if s3.AccessKey != "" {
s3.AccessKey = RedactedSecretPlaceholder
}
if s3.SecretKey != "" {
s3.SecretKey = RedactedSecretPlaceholder
}
out.S3 = &s3
}
if out.OSS != nil {
oss := *out.OSS
if oss.AccessKey != "" {
oss.AccessKey = RedactedSecretPlaceholder
}
if oss.SecretKey != "" {
oss.SecretKey = RedactedSecretPlaceholder
}
out.OSS = &oss
}
if out.KS3 != nil {
ks3 := *out.KS3
if ks3.AccessKey != "" {
ks3.AccessKey = RedactedSecretPlaceholder
}
if ks3.SecretKey != "" {
ks3.SecretKey = RedactedSecretPlaceholder
}
out.KS3 = &ks3
}
if out.OBS != nil {
obs := *out.OBS
if obs.AccessKey != "" {
obs.AccessKey = RedactedSecretPlaceholder
}
if obs.SecretKey != "" {
obs.SecretKey = RedactedSecretPlaceholder
}
out.OBS = &obs
}
return &out
}
// CredentialsConfigForResponse returns a copy with app_secret redacted when
// maskSecrets is true.
func CredentialsConfigForResponse(cfg *CredentialsConfig, maskSecrets bool) *CredentialsConfig {
if cfg == nil {
return nil
}
out := *cfg
if !maskSecrets {
return &out
}
if out.WeKnoraCloud != nil {
cloud := *out.WeKnoraCloud
if cloud.AppSecret != "" {
cloud.AppSecret = RedactedSecretPlaceholder
}
out.WeKnoraCloud = &cloud
}
return &out
}