fix(security): redact secrets in login and tenant KV update responses
Apply the same tenant/integration secret policy to auth login, OIDC callback, switch-tenant, and invite-register payloads, and return masked config objects from Admin-only tenant KV PUT handlers.
This commit is contained in:
@@ -211,14 +211,14 @@ func (h *AuthHandler) Login(c *gin.Context) {
|
|||||||
// Check if login was successful
|
// Check if login was successful
|
||||||
if !response.Success {
|
if !response.Success {
|
||||||
logger.Warnf(ctx, "Login failed: %s", response.Message)
|
logger.Warnf(ctx, "Login failed: %s", response.Message)
|
||||||
c.JSON(http.StatusUnauthorized, response)
|
c.JSON(http.StatusUnauthorized, dto.NewAuthLoginResponse(response))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// User is already in the correct format from service
|
// User is already in the correct format from service
|
||||||
|
|
||||||
logger.Infof(ctx, "User logged in successfully, email: %s", email)
|
logger.Infof(ctx, "User logged in successfully, email: %s", email)
|
||||||
c.JSON(http.StatusOK, response)
|
c.JSON(http.StatusOK, dto.NewAuthLoginResponse(response))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOIDCAuthorizationURL godoc
|
// GetOIDCAuthorizationURL godoc
|
||||||
@@ -336,7 +336,7 @@ func (h *AuthHandler) OIDCRedirectCallback(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func encodeOIDCCallbackPayload(resp *types.OIDCCallbackResponse) (string, error) {
|
func encodeOIDCCallbackPayload(resp *types.OIDCCallbackResponse) (string, error) {
|
||||||
payload, err := json.Marshal(resp)
|
payload, err := json.Marshal(dto.NewAuthOIDCCallbackResponse(resp))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@@ -711,10 +711,8 @@ func (h *AuthHandler) SwitchTenant(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, resp)
|
c.JSON(http.StatusOK, dto.NewAuthLoginResponse(resp))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AutoSetup godoc
|
|
||||||
// @Summary 自动初始化(Lite 桌面版)
|
// @Summary 自动初始化(Lite 桌面版)
|
||||||
// @Description Lite 版专用:首次启动时自动创建默认用户和租户并返回令牌,后续启动直接签发令牌,免除手动注册/登录流程
|
// @Description Lite 版专用:首次启动时自动创建默认用户和租户并返回令牌,后续启动直接签发令牌,免除手动注册/登录流程
|
||||||
// @Tags 认证
|
// @Tags 认证
|
||||||
@@ -777,7 +775,7 @@ func (h *AuthHandler) AutoSetup(c *gin.Context) {
|
|||||||
tenant, _ := h.tenantService.GetTenantByID(ctx, user.TenantID)
|
tenant, _ := h.tenantService.GetTenantByID(ctx, user.TenantID)
|
||||||
|
|
||||||
logger.Info(ctx, "Auto-setup: completed successfully")
|
logger.Info(ctx, "Auto-setup: completed successfully")
|
||||||
c.JSON(http.StatusOK, &types.LoginResponse{
|
c.JSON(http.StatusOK, dto.NewAuthLoginResponse(&types.LoginResponse{
|
||||||
Success: true,
|
Success: true,
|
||||||
Message: "Auto-setup successful",
|
Message: "Auto-setup successful",
|
||||||
User: user,
|
User: user,
|
||||||
@@ -789,7 +787,7 @@ func (h *AuthHandler) AutoSetup(c *gin.Context) {
|
|||||||
}},
|
}},
|
||||||
Token: accessToken,
|
Token: accessToken,
|
||||||
RefreshToken: refreshToken,
|
RefreshToken: refreshToken,
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
// tenantNameOrEmpty returns t.Name when t is non-nil, "" otherwise.
|
// tenantNameOrEmpty returns t.Name when t is non-nil, "" otherwise.
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
|
||||||
apperrors "github.com/Tencent/WeKnora/internal/errors"
|
apperrors "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/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
secutils "github.com/Tencent/WeKnora/internal/utils"
|
secutils "github.com/Tencent/WeKnora/internal/utils"
|
||||||
@@ -200,7 +201,7 @@ func (h *AuthHandler) RegisterByInvite(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tenant, _ := h.tenantService.GetTenantByID(ctx, inv.TenantID)
|
tenant, _ := h.tenantService.GetTenantByID(ctx, inv.TenantID)
|
||||||
c.JSON(http.StatusCreated, &types.LoginResponse{
|
c.JSON(http.StatusCreated, dto.NewAuthLoginResponse(&types.LoginResponse{
|
||||||
Success: true,
|
Success: true,
|
||||||
Message: "Registration successful",
|
Message: "Registration successful",
|
||||||
User: user,
|
User: user,
|
||||||
@@ -212,5 +213,5 @@ func (h *AuthHandler) RegisterByInvite(c *gin.Context) {
|
|||||||
}},
|
}},
|
||||||
Token: accessToken,
|
Token: accessToken,
|
||||||
RefreshToken: refreshToken,
|
RefreshToken: refreshToken,
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
import "github.com/Tencent/WeKnora/internal/types"
|
||||||
|
|
||||||
|
// AuthLoginResponse is the HTTP-safe login / switch-tenant response shape.
|
||||||
|
type AuthLoginResponse struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
User *types.User `json:"user,omitempty"`
|
||||||
|
ActiveTenant *TenantResponse `json:"active_tenant,omitempty"`
|
||||||
|
Memberships []types.Membership `json:"memberships"`
|
||||||
|
Token string `json:"token,omitempty"`
|
||||||
|
RefreshToken string `json:"refresh_token,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// AuthOIDCCallbackResponse is the HTTP-safe OIDC callback payload shape.
|
||||||
|
type AuthOIDCCallbackResponse struct {
|
||||||
|
Success bool `json:"success"`
|
||||||
|
Message string `json:"message,omitempty"`
|
||||||
|
User *types.User `json:"user,omitempty"`
|
||||||
|
Tenant *TenantResponse `json:"tenant,omitempty"`
|
||||||
|
Memberships []types.Membership `json:"memberships"`
|
||||||
|
Token string `json:"token,omitempty"`
|
||||||
|
RefreshToken string `json:"refresh_token,omitempty"`
|
||||||
|
IsNewUser bool `json:"is_new_user,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAuthLoginResponse converts a service-layer login response for HTTP output.
|
||||||
|
func NewAuthLoginResponse(resp *types.LoginResponse) *AuthLoginResponse {
|
||||||
|
if resp == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var role types.TenantRole
|
||||||
|
if resp.ActiveTenant != nil {
|
||||||
|
role = membershipRoleForTenant(resp.Memberships, resp.ActiveTenant.ID)
|
||||||
|
}
|
||||||
|
return &AuthLoginResponse{
|
||||||
|
Success: resp.Success,
|
||||||
|
Message: resp.Message,
|
||||||
|
User: resp.User,
|
||||||
|
ActiveTenant: NewTenantResponseWithRole(resp.ActiveTenant, role),
|
||||||
|
Memberships: resp.Memberships,
|
||||||
|
Token: resp.Token,
|
||||||
|
RefreshToken: resp.RefreshToken,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewAuthOIDCCallbackResponse converts an OIDC callback response for HTTP output.
|
||||||
|
func NewAuthOIDCCallbackResponse(resp *types.OIDCCallbackResponse) *AuthOIDCCallbackResponse {
|
||||||
|
if resp == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
var role types.TenantRole
|
||||||
|
if resp.Tenant != nil {
|
||||||
|
role = membershipRoleForTenant(resp.Memberships, resp.Tenant.ID)
|
||||||
|
}
|
||||||
|
return &AuthOIDCCallbackResponse{
|
||||||
|
Success: resp.Success,
|
||||||
|
Message: resp.Message,
|
||||||
|
User: resp.User,
|
||||||
|
Tenant: NewTenantResponseWithRole(resp.Tenant, role),
|
||||||
|
Memberships: resp.Memberships,
|
||||||
|
Token: resp.Token,
|
||||||
|
RefreshToken: resp.RefreshToken,
|
||||||
|
IsNewUser: resp.IsNewUser,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func membershipRoleForTenant(memberships []types.Membership, tenantID uint64) types.TenantRole {
|
||||||
|
for _, m := range memberships {
|
||||||
|
if m.TenantID == tenantID && m.Role.IsValid() {
|
||||||
|
return m.Role
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package dto
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAuthLoginResponse_ViewerOmitsActiveTenantSecrets(t *testing.T) {
|
||||||
|
tenant := sampleSecretTenant()
|
||||||
|
resp := NewAuthLoginResponse(&types.LoginResponse{
|
||||||
|
Success: true,
|
||||||
|
ActiveTenant: tenant,
|
||||||
|
Memberships: []types.Membership{{
|
||||||
|
TenantID: tenant.ID,
|
||||||
|
Role: types.TenantRoleViewer,
|
||||||
|
}},
|
||||||
|
})
|
||||||
|
body, err := json.Marshal(resp)
|
||||||
|
require.NoError(t, err)
|
||||||
|
s := string(body)
|
||||||
|
assert.NotContains(t, s, "tenant-api-key-123")
|
||||||
|
assert.NotContains(t, s, "parser-secret-123")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAuthLoginResponse_OwnerGetsAPIKey(t *testing.T) {
|
||||||
|
tenant := sampleSecretTenant()
|
||||||
|
resp := NewAuthLoginResponse(&types.LoginResponse{
|
||||||
|
Success: true,
|
||||||
|
ActiveTenant: tenant,
|
||||||
|
Memberships: []types.Membership{{
|
||||||
|
TenantID: tenant.ID,
|
||||||
|
Role: types.TenantRoleOwner,
|
||||||
|
}},
|
||||||
|
})
|
||||||
|
body, err := json.Marshal(resp)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Contains(t, string(body), "tenant-api-key-123")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAuthOIDCCallbackResponse_ViewerOmitsTenantSecrets(t *testing.T) {
|
||||||
|
tenant := sampleSecretTenant()
|
||||||
|
resp := NewAuthOIDCCallbackResponse(&types.OIDCCallbackResponse{
|
||||||
|
Success: true,
|
||||||
|
Tenant: tenant,
|
||||||
|
Memberships: []types.Membership{{
|
||||||
|
TenantID: tenant.ID,
|
||||||
|
Role: types.TenantRoleViewer,
|
||||||
|
}},
|
||||||
|
})
|
||||||
|
body, err := json.Marshal(resp)
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.NotContains(t, string(body), "tenant-api-key-123")
|
||||||
|
}
|
||||||
@@ -35,27 +35,33 @@ type TenantResponse struct {
|
|||||||
|
|
||||||
// NewTenantResponse converts a stored tenant into its HTTP response shape.
|
// NewTenantResponse converts a stored tenant into its HTTP response shape.
|
||||||
func NewTenantResponse(ctx context.Context, tenant *types.Tenant) *TenantResponse {
|
func NewTenantResponse(ctx context.Context, tenant *types.Tenant) *TenantResponse {
|
||||||
|
return NewTenantResponseWithRole(tenant, RoleFromContext(ctx))
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewTenantResponseWithRole converts a stored tenant using an explicit role
|
||||||
|
// (for auth flows where tenant role is not yet in request context).
|
||||||
|
func NewTenantResponseWithRole(tenant *types.Tenant, role types.TenantRole) *TenantResponse {
|
||||||
if tenant == nil {
|
if tenant == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
includeSecrets := CanViewIntegrationSecrets(ctx)
|
includeSecrets := role.HasPermission(types.TenantRoleAdmin)
|
||||||
includeAPIKey := CanViewTenantAPIKey(ctx)
|
includeAPIKey := role.HasPermission(types.TenantRoleOwner)
|
||||||
|
|
||||||
resp := &TenantResponse{
|
resp := &TenantResponse{
|
||||||
ID: tenant.ID,
|
ID: tenant.ID,
|
||||||
Name: tenant.Name,
|
Name: tenant.Name,
|
||||||
Description: tenant.Description,
|
Description: tenant.Description,
|
||||||
Status: tenant.Status,
|
Status: tenant.Status,
|
||||||
RetrieverEngines: tenant.RetrieverEngines,
|
RetrieverEngines: tenant.RetrieverEngines,
|
||||||
Business: tenant.Business,
|
Business: tenant.Business,
|
||||||
StorageQuota: tenant.StorageQuota,
|
StorageQuota: tenant.StorageQuota,
|
||||||
StorageUsed: tenant.StorageUsed,
|
StorageUsed: tenant.StorageUsed,
|
||||||
ContextConfig: tenant.ContextConfig,
|
ContextConfig: tenant.ContextConfig,
|
||||||
ChatHistoryConfig: tenant.ChatHistoryConfig,
|
ChatHistoryConfig: tenant.ChatHistoryConfig,
|
||||||
RetrievalConfig: tenant.RetrievalConfig,
|
RetrievalConfig: tenant.RetrievalConfig,
|
||||||
CreatedAt: tenant.CreatedAt,
|
CreatedAt: tenant.CreatedAt,
|
||||||
UpdatedAt: tenant.UpdatedAt,
|
UpdatedAt: tenant.UpdatedAt,
|
||||||
DeletedAt: tenant.DeletedAt,
|
DeletedAt: tenant.DeletedAt,
|
||||||
}
|
}
|
||||||
if includeAPIKey {
|
if includeAPIKey {
|
||||||
resp.APIKey = tenant.APIKey
|
resp.APIKey = tenant.APIKey
|
||||||
|
|||||||
@@ -1099,7 +1099,7 @@ func (h *TenantHandler) updateTenantWebSearchConfigInternal(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": true,
|
"success": true,
|
||||||
"data": types.EffectiveWebSearchConfig(updatedTenant.WebSearchConfig),
|
"data": types.WebSearchConfigForResponse(updatedTenant.WebSearchConfig, true),
|
||||||
"message": "Web search configuration updated successfully",
|
"message": "Web search configuration updated successfully",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -1180,7 +1180,7 @@ func (h *TenantHandler) updateTenantParserEngineConfigInternal(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": true,
|
"success": true,
|
||||||
"data": updatedTenant.ParserEngineConfig,
|
"data": types.ParserEngineConfigForResponse(updatedTenant.ParserEngineConfig, true),
|
||||||
"message": "解析引擎配置已更新",
|
"message": "解析引擎配置已更新",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -1245,7 +1245,7 @@ func (h *TenantHandler) updateTenantStorageEngineConfigInternal(c *gin.Context)
|
|||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": true,
|
"success": true,
|
||||||
"data": updatedTenant.StorageEngineConfig,
|
"data": types.StorageEngineConfigForResponse(updatedTenant.StorageEngineConfig, true),
|
||||||
"message": "存储引擎配置已更新",
|
"message": "存储引擎配置已更新",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user