feat(plugin-auth): cap self-created orgs per user (OS_ORG_LIMIT)#2357
Merged
Conversation
Forward better-auth's `organizationLimit` in FUNCTION form, counting only the caller's `role=owner` memberships (orgs they created) — never orgs they were invited into — read via `withSystemReadContext` so the cross-tenant `sys_member` count isn't filtered to empty by org-scoping. Threshold from `OS_ORG_LIMIT` (new `resolveOrgLimit()`); unset → unlimited (self-host default). Fail-open if the count can't be taken, so an infra hiccup never blocks a legitimate user. Why owner-scoped, not a flat count: better-auth's number form counts ALL of a user's memberships, which would block a collaborator invited into many orgs from creating their own. We only want to bound scripted org/free-env spam (each new org auto-provisions a free environment on the cloud control plane), so capping self-created orgs is the right axis. No-op in single-org mode (create is already denied by beforeCreateOrganization). Verified on the local rig: a user owning 4 orgs but a member of 5 is allowed to create at limit=5 (the invited org is not counted), then blocked once they own 5. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
📓 Docs Drift CheckThis PR changes 2 package(s): 10 hand-written doc(s) reference the affected code and may need an implementation-accuracy re-verification:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Adds a configurable per-user cap on self-created organizations, forwarded to better-auth's
organizationLimit.resolveOrgLimit()(@objectstack/types) readsOS_ORG_LIMIT; unset → unlimited (self-host default).plugin-authpassesorganizationLimitin function form, counting only the caller'srole=ownermemberships (orgs they created) — read viawithSystemReadContextso the cross-tenantsys_membercount isn't filtered to empty by org-scoping. Fail-open if the count can't be taken.Why
Enabling multi-org (
OS_MULTI_ORG_ENABLED) lets a user create orgs, and on the cloud control plane every new org auto-provisions a free environment (a real DB). Without a cap, one account could scriptcreateOrganizationto spawn unbounded free DBs.better-auth's number form counts all of a user's memberships, which would wrongly block a collaborator invited into many orgs from creating their own. Owner-scoped counting is the correct axis. No-op in single-org mode (create is already denied by
beforeCreateOrganization).Verified (local rig)
OS_ORG_LIMIT=5(the invited org is not counted).🤖 Generated with Claude Code