test(rbac): cover review gaps from PR 2 self-review

- Add TestRequireRole_CrossTenantSuperuserBypass: pin the
  CanAccessAllTenants -> Owner-gate bypass behaviour for RequireRole,
  matching the existing coverage on RequireOwnershipOrRole. Catches
  reorderings of the fast paths that would re-lock org-level
  superusers out of foreign-tenant Owner-only routes.

- Add TestAgentCreatorLookup_CrossTenantIsHiddenAsNotFound: lock down
  the defence-in-depth tenant re-check inside AgentCreatorLookup so a
  future refactor that loosens the service-layer scope (or wires a
  cross-tenant variant by mistake) cannot leak access via the
  ownership shortcut.

- Reword RequireOwnershipOrRole step 7-8 doc comment so "role check
  already failed" is unambiguous (it refers to step 1 above).

- Annotate the synthetic-system-user construction in middleware/auth.go
  with a back-pointer to types.IsSyntheticUserID so anyone changing
  the prefix or numeric suffix knows to update the helper too.
This commit is contained in:
wizardchen
2026-05-13 21:49:40 +08:00
committed by lyingbug
parent 8b71def60b
commit 0f0f7e4339
4 changed files with 63 additions and 2 deletions
+21
View File
@@ -148,3 +148,24 @@ func TestAgentCreatorLookup_AgentNotFoundMapsToSentinel(t *testing.T) {
t.Fatalf("expected ErrResourceNotFound, got %v", err)
}
}
func TestAgentCreatorLookup_CrossTenantIsHiddenAsNotFound(t *testing.T) {
// Defensive: service.GetAgentByID already scopes by tenant, but
// AgentCreatorLookup re-checks the row's TenantID anyway. If a
// future refactor loosens the service-layer scope (or adds a
// cross-tenant variant that gets wired here by mistake), a
// foreign-tenant row must NEVER leak through the ownership
// shortcut. Pinning the contract here so the defensive branch
// survives refactors.
h := &CustomAgentHandler{service: &stubAgentService{
get: func(_ context.Context, _ string) (*types.CustomAgent, error) {
return &types.CustomAgent{
ID: "agent-1", TenantID: 999, CreatedBy: "u1",
}, nil
},
}}
_, err := h.AgentCreatorLookup(newKBLookupCtx(t, 1, "agent-1"))
if !errors.Is(err, middleware.ErrResourceNotFound) {
t.Fatalf("cross-tenant agent must surface as not-found, got %v", err)
}
}