fix(rbac): address PR 2 review feedback
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.
This commit is contained in:
@@ -54,6 +54,33 @@ func UserIDFromContext(ctx context.Context) (string, bool) {
|
||||
return v, ok && v != ""
|
||||
}
|
||||
|
||||
// IsSyntheticUserID reports whether id refers to the synthetic system
|
||||
// user that the X-API-Key auth path attaches to each tenant
|
||||
// (User.ID = "system-<tenantID>"). These users have no real human
|
||||
// behind them and no tenant_members row, so RBAC ownership matching
|
||||
// against them is never meaningful — service-layer code that records
|
||||
// "the creator" should skip storing this ID and treat the resource as
|
||||
// tenant-owned instead.
|
||||
//
|
||||
// Kept minimal on purpose: the prefix and "all digits afterwards"
|
||||
// invariant comes from middleware/auth.go's User construction for the
|
||||
// API-key branch. If that prefix changes, update both sides.
|
||||
func IsSyntheticUserID(id string) bool {
|
||||
const prefix = "system-"
|
||||
if len(id) <= len(prefix) {
|
||||
return false
|
||||
}
|
||||
if id[:len(prefix)] != prefix {
|
||||
return false
|
||||
}
|
||||
for _, r := range id[len(prefix):] {
|
||||
if r < '0' || r > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// TenantRoleFromContext extracts the caller's TenantRole in the currently
|
||||
// active tenant. Returns TenantRoleViewer when the key is absent so that
|
||||
// callers fail closed (least privilege) if the auth middleware did not
|
||||
|
||||
@@ -4,6 +4,35 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestIsSyntheticUserID(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
id string
|
||||
want bool
|
||||
}{
|
||||
{"matches system-<digits>", "system-1", true},
|
||||
{"matches large tenant id", "system-1234567890", true},
|
||||
{"empty string", "", false},
|
||||
{"prefix only", "system-", false},
|
||||
{"missing prefix", "1", false},
|
||||
{"non-digit suffix", "system-abc", false},
|
||||
{"mixed suffix", "system-1a2", false},
|
||||
{"prefix with space", "system- 1", false},
|
||||
{"uppercase prefix", "SYSTEM-1", false},
|
||||
{"normal uuid user", "550e8400-e29b-41d4-a716-446655440000", false},
|
||||
{"system uuid trap", "system-550e8400", false}, // contains '-'
|
||||
}
|
||||
for _, c := range cases {
|
||||
c := c
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
got := IsSyntheticUserID(c.id)
|
||||
if got != c.want {
|
||||
t.Fatalf("IsSyntheticUserID(%q) = %v, want %v", c.id, got, c.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLanguageLocaleName(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -76,9 +76,15 @@ type CustomAgent struct {
|
||||
// Created by user ID
|
||||
CreatedBy string `yaml:"created_by" json:"created_by" gorm:"type:varchar(36)"`
|
||||
// RunnableByViewer controls whether users with TenantRoleViewer can
|
||||
// start chat sessions against this agent. Defaults to true so viewers
|
||||
// can still consume published agents; admins toggle it off for agents
|
||||
// whose tools should be restricted to contributors and above.
|
||||
// run this agent IN AGENT MODE (i.e. with tools, MCP, sandboxed code
|
||||
// execution, etc.). Plain RAG / Wiki QA sessions against the same
|
||||
// agent are unaffected — they don't invoke tools, so restricting
|
||||
// them buys no security and would just block Viewer consumption of
|
||||
// published agents. Defaults to true; admins toggle it off for
|
||||
// agents whose tools should be restricted to contributors and above.
|
||||
//
|
||||
// Enforcement lives in handler/session/qa.go's AgentQA, gated behind
|
||||
// cfg.Tenant.EnableRBAC like every other PR2 check.
|
||||
//
|
||||
// Note: no GORM `default:true` tag here. With a plain `bool` field that
|
||||
// tag causes GORM to treat Go's zero value (false) as "unspecified" and
|
||||
|
||||
Reference in New Issue
Block a user