8b71def60b
Middleware - RequireOwnershipOrRole now skips the creator lookup entirely when EnableRBAC is off, eliminating a hidden per-request DB roundtrip in the rollout window. - New ErrResourceNotFound sentinel lets lookups signal "not visible to this tenant"; the middleware passes the request through so handlers can return a real 404 instead of a misleading 403. - Transient lookup failures now surface as 503 (was 403) so monitoring can tell denied-by-policy from cannot-verify-right-now. - Cross-tenant superusers (CanAccessAllTenants) bypass tenant role gates, restoring DELETE /tenants/:id and ResetAPIKey for org operators that resolve to Admin in foreign tenants. - Construction-time warning fires once when cfg is nil/incomplete, so a wiring bug that disables enforcement globally is visible at boot. Route coverage - Add role guards to FAQ, KnowledgeTag, WikiPage, Skill, WebSearch, WebSearchProvider, WeKnoraCloud, Session, Chat, and Message route groups so the matrix actually matches the PR description. Service layer - KB and agent creators are no longer recorded as the synthetic system-<tenantID> user used by the X-API-Key path; resources stay tenant-owned. New types.IsSyntheticUserID helper centralises the prefix check. - KBCreatorLookup and AgentCreatorLookup re-check tenant scope so a same-ID resource in another tenant cannot piggyback through the ownership shortcut. Docs - CustomAgent.RunnableByViewer comment now matches enforcement: it only blocks Viewer-driven agent-mode runs, not plain RAG/Wiki QA. Tests - New cases for the 503/404 paths, lookup-skip behaviour, cross-tenant superuser bypass, the lookup helpers (not-found, cross-tenant, built-in agent, missing tenant context), and IsSyntheticUserID.
109 lines
4.0 KiB
Go
109 lines
4.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
|
|
apprepo "github.com/Tencent/WeKnora/internal/application/repository"
|
|
"github.com/Tencent/WeKnora/internal/application/service"
|
|
"github.com/Tencent/WeKnora/internal/middleware"
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Per-resource creator-id resolvers used by middleware.RequireOwnershipOrRole.
|
|
// The route registration wires these as closures so they capture the handler's
|
|
// service dependencies; the middleware just calls them with the gin.Context
|
|
// and gets back one of:
|
|
//
|
|
// - (creatorID, nil) : ownership match check decides
|
|
// - ("", nil) : tenant-owned (legacy or built-in)
|
|
// - ("", ErrResourceNotFound) : :id doesn't resolve in this tenant;
|
|
// middleware passes through so the
|
|
// handler can issue a real 404
|
|
// - ("", <other err>) : transient/unexpected failure;
|
|
// middleware will 503 the request
|
|
//
|
|
// Tenant scoping is enforced INSIDE the lookup: even if the underlying
|
|
// `Get*ByID` repo call is unscoped, the lookup re-checks `tenant_id`
|
|
// against the caller's context. This stops a creator-match shortcut
|
|
// from leaking access to a same-ID resource in a different tenant.
|
|
|
|
// KBCreatorLookup resolves :id -> KnowledgeBase.CreatorID, scoped to
|
|
// the caller's tenant. Used by all per-KB mutating routes.
|
|
func (h *KnowledgeBaseHandler) KBCreatorLookup(c *gin.Context) (string, error) {
|
|
id := c.Param("id")
|
|
if id == "" {
|
|
return "", errors.New("missing :id param for KB creator lookup")
|
|
}
|
|
ctx := c.Request.Context()
|
|
tenantID, ok := types.TenantIDFromContext(ctx)
|
|
if !ok {
|
|
// 没有租户上下文意味着 auth 中间件未完成;当作 lookup 失败让上层 503,
|
|
// 而不是 silently 走 fail-open 给一个不该有的访问。
|
|
return "", errors.New("tenant context missing")
|
|
}
|
|
kb, err := h.service.GetKnowledgeBaseByID(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, apprepo.ErrKnowledgeBaseNotFound) {
|
|
return "", middleware.ErrResourceNotFound
|
|
}
|
|
return "", err
|
|
}
|
|
if kb == nil {
|
|
return "", middleware.ErrResourceNotFound
|
|
}
|
|
// 显式重校验租户:repo.GetKnowledgeBaseByID 不带 tenant 过滤,
|
|
// 万一未来 :id 被攻击者从他人租户 UUID 试探到,也不会借由
|
|
// "ownership match" 通过中间件。
|
|
if kb.TenantID != tenantID {
|
|
return "", middleware.ErrResourceNotFound
|
|
}
|
|
return kb.CreatorID, nil
|
|
}
|
|
|
|
// AgentCreatorLookup resolves :id -> CustomAgent.CreatedBy. Built-in
|
|
// agents (IsBuiltin == true) are tenant-owned across the board: they
|
|
// belong to the tenant rather than to any one user, so we return
|
|
// ("", nil) and let the role check decide. The same holds for legacy
|
|
// rows whose CreatedBy was never populated.
|
|
//
|
|
// The underlying GetAgentByID already scopes to the caller's tenant,
|
|
// but we keep an explicit defence-in-depth check here in case future
|
|
// refactors loosen the service-layer scope.
|
|
func (h *CustomAgentHandler) AgentCreatorLookup(c *gin.Context) (string, error) {
|
|
id := c.Param("id")
|
|
if id == "" {
|
|
return "", errors.New("missing :id param for agent creator lookup")
|
|
}
|
|
ctx := c.Request.Context()
|
|
tenantID, ok := types.TenantIDFromContext(ctx)
|
|
if !ok {
|
|
return "", errors.New("tenant context missing")
|
|
}
|
|
agent, err := h.service.GetAgentByID(ctx, id)
|
|
if err != nil {
|
|
if errors.Is(err, service.ErrAgentNotFound) ||
|
|
errors.Is(err, apprepo.ErrCustomAgentNotFound) {
|
|
return "", middleware.ErrResourceNotFound
|
|
}
|
|
return "", err
|
|
}
|
|
if agent == nil {
|
|
return "", middleware.ErrResourceNotFound
|
|
}
|
|
if agent.TenantID != tenantID {
|
|
return "", middleware.ErrResourceNotFound
|
|
}
|
|
if agent.IsBuiltin {
|
|
return "", nil
|
|
}
|
|
return agent.CreatedBy, nil
|
|
}
|
|
|
|
// Compile-time guards: the methods must satisfy middleware.CreatorLookup
|
|
// so route wiring stays type-safe even if a signature drifts.
|
|
var (
|
|
_ middleware.CreatorLookup = (*KnowledgeBaseHandler)(nil).KBCreatorLookup
|
|
_ middleware.CreatorLookup = (*CustomAgentHandler)(nil).AgentCreatorLookup
|
|
)
|