fix(rbac): close org-tenant gate gaps from Plan 3 review

Three follow-ups from #1350 review:

1. DELETE /organizations/:id/members/:tenant_id was un-gated, so a
   tenant Viewer could remove their own tenant from an org via the
   members endpoint and bypass the Admin+ requirement that POST
   /:id/leave already enforces. Gate it with g.Admin() — symmetric
   with /leave.

2. isOwnerTenant() returned (false, err) when the owner user lookup
   failed, and the two call sites only checked the bool. A deleted /
   unreadable owner user would silently fall through and let admins
   remove or re-role the would-be owner tenant. Make the helper
   fail-closed: any lookup error now returns (true, err), and both
   call sites refuse the destructive op on error.

3. POST /:id/request-upgrade was un-gated. Approval of the request
   changes the whole tenant's org role, so it is in the same trust
   tier as join/leave/create. Add g.Admin().
This commit is contained in:
wizardchen
2026-05-15 22:25:31 +08:00
committed by lyingbug
parent 928f5abe3f
commit eda6b1011a
2 changed files with 37 additions and 11 deletions
+9 -4
View File
@@ -823,8 +823,10 @@ func RegisterOrganizationRoutes(r *gin.RouterGroup, orgHandler *handler.Organiza
orgs.DELETE("/:id", orgHandler.DeleteOrganization)
// Leave organization (Admin+ in caller's tenant only)
orgs.POST("/:id/leave", g.Admin(), orgHandler.LeaveOrganization)
// Request role upgrade (for existing members)
orgs.POST("/:id/request-upgrade", orgHandler.RequestRoleUpgrade)
// Request role upgrade (Admin+ in caller's tenant only).
// An upgrade approval changes the whole tenant's org role, so it
// must not be initiated by a tenant Viewer/Contributor.
orgs.POST("/:id/request-upgrade", g.Admin(), orgHandler.RequestRoleUpgrade)
// Generate invite code
orgs.POST("/:id/invite-code", orgHandler.GenerateInviteCode)
// Search users for invite (admin only)
@@ -835,8 +837,11 @@ func RegisterOrganizationRoutes(r *gin.RouterGroup, orgHandler *handler.Organiza
orgs.GET("/:id/members", orgHandler.ListMembers)
// Update member role (path parameter is the member tenant_id)
orgs.PUT("/:id/members/:tenant_id", orgHandler.UpdateMemberRole)
// Remove member (path parameter is the member tenant_id)
orgs.DELETE("/:id/members/:tenant_id", orgHandler.RemoveMember)
// Remove member (path parameter is the member tenant_id).
// Both self-removal (caller's own tenant) and admin-removal-of-other
// take a whole tenant out of the org, so the route must be Admin+
// in the caller's tenant — symmetric with POST /:id/leave above.
orgs.DELETE("/:id/members/:tenant_id", g.Admin(), orgHandler.RemoveMember)
// List join requests (admin only)
orgs.GET("/:id/join-requests", orgHandler.ListJoinRequests)
// Review join request (admin only)