Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/sim/lib/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ import { isSsoCallbackPath, resolveSsoCallbackProviderId } from '@/lib/auth/sso/
import { configuredSsoPlugin } from '@/lib/auth/sso/plugin'
import {
assertPersonalCheckoutAllowed,
assertSelfServeCheckoutPlan,
authorizeSubscriptionReference,
isPersonalCheckoutRequest,
} from '@/lib/billing/authorization'
Expand Down Expand Up @@ -1212,6 +1213,7 @@ export const auth = betterAuth({
const sessionUserId = session?.user?.id
if (sessionUserId) {
const requestBody = ctx.body ?? {}
assertSelfServeCheckoutPlan(requestBody.plan)
const referenceId = resolveCheckoutReferenceId(
requestBody,
sessionUserId,
Expand Down
14 changes: 14 additions & 0 deletions apps/sim/lib/billing/authorization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ vi.mock('@/lib/billing/enterprise-outbox', () => {

import {
assertPersonalCheckoutAllowed,
assertSelfServeCheckoutPlan,
authorizeSubscriptionReference,
isPersonalCheckoutRequest,
} from '@/lib/billing/authorization'
Expand Down Expand Up @@ -115,6 +116,19 @@ describe('authorizeSubscriptionReference', () => {
})
})

describe('assertSelfServeCheckoutPlan', () => {
it('refuses a self-serve Enterprise checkout with a client error', () => {
expect(() => assertSelfServeCheckoutPlan('enterprise')).toThrow(
expect.objectContaining({ statusCode: 400 })
)
})

it('admits self-serve Pro and Team checkouts', () => {
expect(() => assertSelfServeCheckoutPlan('pro_6000')).not.toThrow()
expect(() => assertSelfServeCheckoutPlan('team_25000')).not.toThrow()
})
})

describe('assertPersonalCheckoutAllowed', () => {
beforeEach(() => {
mockGetOrganizationCoverageForMember.mockResolvedValue({ status: 'not-covered' })
Expand Down
15 changes: 14 additions & 1 deletion apps/sim/lib/billing/authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
assertNoUnresolvedEnterpriseIssuance,
EnterpriseIssuanceInProgressError,
} from '@/lib/billing/enterprise-outbox'
import { isOrgPlan, isPro } from '@/lib/billing/plan-helpers'
import { isEnterprise, isOrgPlan, isPro } from '@/lib/billing/plan-helpers'
import { isOrgScopedSubscription } from '@/lib/billing/subscriptions/utils'

const logger = createLogger('BillingAuthorization')
Expand Down Expand Up @@ -74,6 +74,19 @@ async function assertNotBilledThroughMarketplace(referenceId: string): Promise<v
})
}

/**
* Refuse a self-serve checkout for the Enterprise plan. Enterprise is issued by the Sim team with
* its own Stripe price and metadata, so the Stripe plugin's placeholder price can never check out,
* and its write to a pending subscription row fails the Enterprise metadata constraint.
*/
export function assertSelfServeCheckoutPlan(plan: unknown): void {
if (typeof plan !== 'string' || !isEnterprise(plan)) return

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Mixed-case Enterprise bypasses refusal

assertSelfServeCheckoutPlan only rejects lowercase enterprise because isEnterprise uses exact equality. A personal checkout with plan: 'Enterprise' or 'ENTERPRISE' passes this guard, but the Stripe plugin accepts either spelling. Enterprise remains in getPlans(), so the pending subscription write fails the database's Enterprise metadata check and returns a server error instead of the intended 400.

Normalize the name before checking it and add mixed-case regression tests.

Suggested change
if (typeof plan !== 'string' || !isEnterprise(plan)) return
if (typeof plan !== 'string' || !isEnterprise(plan.trim().toLowerCase())) return

logger.warn('Blocking checkout - Enterprise is not a self-serve plan')
throw new APIError('BAD_REQUEST', {
message: 'Enterprise plans are set up by our team. Contact sales to get started.',
})
}

/** Refuse an organization-plan upgrade while the account's own Pro plan is still in its free trial. */
async function assertNoPersonalTrial(userId: string): Promise<void> {
const trialing = await db
Expand Down
Loading