Repository navigation
fix(auth): block only curated disposable domains, and never gate SSO or SCIM provisioning #9529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import { betterAuth } from 'better-auth' | ||
| import { memoryAdapter } from 'better-auth/adapters/memory' | ||
| import { admin } from 'better-auth/plugins' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { DISPOSABLE_EMAIL_ERROR_CODE } from '@/lib/auth/constants' | ||
| import { refuseDisposableAccountEmail } from '@/lib/auth/disposable-email-gate' | ||
| import { configuredSsoPlugin } from '@/lib/auth/sso/plugin' | ||
|
|
||
| /** | ||
| * A real Better Auth instance wired the way `auth.ts` wires the gate, so the endpoint path each | ||
| * creation path reports to `databaseHooks.user.create.before` is Better Auth's, not an assumption. | ||
| */ | ||
| function createAuth() { | ||
| return betterAuth({ | ||
| baseURL: 'http://localhost:3000', | ||
| secret: 'disposable-email-gate-test-secret-0123456789', | ||
| database: memoryAdapter({ user: [], session: [], account: [], verification: [] }), | ||
| emailAndPassword: { enabled: true }, | ||
| plugins: [admin()], | ||
| databaseHooks: { | ||
| user: { | ||
| create: { | ||
| before: async (user, context) => { | ||
| await refuseDisposableAccountEmail(user.email, context?.path) | ||
| return { data: user } | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| const password = 'correct-horse-battery-staple' | ||
|
|
||
| describe('refuseDisposableAccountEmail', () => { | ||
| it('refuses a self-service email signup from a disposable domain', async () => { | ||
| const auth = createAuth() | ||
| await expect( | ||
| auth.api.signUpEmail({ body: { email: 'jane@mailinator.com', password, name: 'Jane' } }) | ||
| ).rejects.toMatchObject({ body: { code: DISPOSABLE_EMAIL_ERROR_CODE } }) | ||
| }) | ||
|
|
||
| it('admits a self-service email signup from a company under a public suffix', async () => { | ||
| const auth = createAuth() | ||
| const result = await auth.api.signUpEmail({ | ||
| body: { email: 'jane@company.msk.ru', password, name: 'Jane' }, | ||
| }) | ||
| expect(result.user.email).toBe('jane@company.msk.ru') | ||
| }) | ||
|
|
||
| /** SCIM provisioning creates accounts through the admin plugin's server-side `createUser`. */ | ||
| it('admits a directory-provisioned account whatever its domain', async () => { | ||
| const auth = createAuth() | ||
| const result = await auth.api.createUser({ | ||
| body: { email: 'jane@mailinator.com', name: 'Jane' }, | ||
| }) | ||
| expect(result.user.email).toBe('jane@mailinator.com') | ||
| }) | ||
|
|
||
| /** | ||
| * Better Auth dispatches with an endpoint's declared path, so the SSO plugin's own callback | ||
| * endpoints name the paths a just-in-time creation reports. No IdP is driven here. | ||
| */ | ||
| const { callbackSSO, callbackSSOShared, callbackSSOSAML, acsEndpoint } = | ||
| configuredSsoPlugin.endpoints | ||
| it.each([callbackSSO.path, callbackSSOShared.path, callbackSSOSAML.path, acsEndpoint.path])( | ||
| 'admits any domain at the SSO plugin callback path %s', | ||
| async (path) => { | ||
| await expect( | ||
| refuseDisposableAccountEmail('jane@mailinator.com', path) | ||
| ).resolves.toBeUndefined() | ||
| } | ||
| ) | ||
|
|
||
| it.each([undefined, '/sign-up/email', '/callback/:id', '/oauth2/callback/:providerId'])( | ||
| 'refuses a disposable domain created at %s', | ||
| async (path) => { | ||
| await expect(refuseDisposableAccountEmail('jane@mailinator.com', path)).rejects.toMatchObject( | ||
| { body: { code: DISPOSABLE_EMAIL_ERROR_CODE } } | ||
| ) | ||
| } | ||
| ) | ||
| }) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { APIError } from 'better-auth/api' | ||
| import { DISPOSABLE_EMAIL_ERROR_CODE, DISPOSABLE_EMAIL_MESSAGE } from '@/lib/auth/constants' | ||
| import { isSsoCallbackPath } from '@/lib/auth/sso/callback-provider' | ||
| import { emailDomain, isDisposableEmail } from '@/lib/messaging/email/disposable' | ||
|
|
||
| const logger = createLogger('DisposableEmailGate') | ||
|
|
||
| /** | ||
| * The admin plugin's endpoint, which SCIM provisioning calls server-side. Over HTTP it requires a | ||
| * platform admin's session, so either way an account created here was not self-service. | ||
| */ | ||
| const ADMIN_CREATE_USER_PATH = '/admin/create-user' | ||
|
|
||
| /** | ||
| * Whether the account is being created on behalf of an organization: SCIM provisioning, or | ||
| * just-in-time creation at an SSO callback (Sim admits SSO only through a provider whose domain | ||
| * the organization verified). The organization vouches for the identity, so a throwaway inbox is | ||
| * not the risk, while a false positive breaks provisioning (a refused SCIM create is retried by | ||
| * the IdP indefinitely). | ||
| */ | ||
| function isOrganizationProvisioned(path: string | undefined): boolean { | ||
| return path !== undefined && (path === ADMIN_CREATE_USER_PATH || isSsoCallbackPath(path)) | ||
| } | ||
|
|
||
| /** | ||
| * Refuses self-service account creation from a disposable email domain, throwing the | ||
| * `DISPOSABLE_EMAIL` error the signup form and OAuth error page recognize. | ||
| * | ||
| * @param path - the Better Auth endpoint the creation runs under (`context?.path` in | ||
| * `databaseHooks.user.create.before`), which tells organization provisioning apart from signup. | ||
| */ | ||
| export async function refuseDisposableAccountEmail( | ||
| email: string, | ||
| path: string | undefined | ||
| ): Promise<void> { | ||
| if (isOrganizationProvisioned(path)) return | ||
| if (!(await isDisposableEmail(email))) return | ||
|
|
||
| logger.warn('Refusing account creation for a disposable email domain', { | ||
| domain: emailDomain(email), | ||
| path, | ||
| }) | ||
| throw new APIError('FORBIDDEN', { | ||
| code: DISPOSABLE_EMAIL_ERROR_CODE, | ||
| message: DISPOSABLE_EMAIL_MESSAGE, | ||
| }) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.