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:
@@ -89,6 +89,39 @@ func TestRequireRole_NilConfigFailsOpen(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireRole_CrossTenantSuperuserBypass(t *testing.T) {
|
||||
// Org-level superusers (User.CanAccessAllTenants) bypass tenant role
|
||||
// gates — see auth.go's resolveTenantRole, which gives them a
|
||||
// transient Admin in foreign tenants. RequireRole has to honour the
|
||||
// same bypass for Owner-only gates, otherwise a superuser would be
|
||||
// locked out of DELETE /tenants/:id once enforcement turns on.
|
||||
//
|
||||
// Pinned as a regression test: if anyone reorders the fast paths so
|
||||
// the superuser check ends up after the enforcement branch, this
|
||||
// test fails before the change ships.
|
||||
router := gin.New()
|
||||
router.Use(func(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
ctx = context.WithValue(ctx, types.TenantRoleContextKey, types.TenantRoleViewer)
|
||||
ctx = context.WithValue(ctx, types.UserIDContextKey, "su1")
|
||||
ctx = context.WithValue(ctx, types.UserContextKey, &types.User{
|
||||
ID: "su1", CanAccessAllTenants: true,
|
||||
})
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
c.Next()
|
||||
})
|
||||
router.GET("/protected",
|
||||
RequireRole(types.TenantRoleOwner, cfgRBAC(true)),
|
||||
func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{}) },
|
||||
)
|
||||
req := httptest.NewRequest(http.MethodGet, "/protected", nil)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, req)
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("superuser must bypass Owner role gate, got %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// ---------- RequireOwnershipOrRole ----------
|
||||
|
||||
func TestRequireOwnershipOrRole_AdminBypassesLookup(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user