Skip to content

Commit a489c4f

Browse files
improvement(auth): layer disposable-email-domains into signup email validation
Compose the disposable-email-domains list (exact + wildcard) into better-auth-harmony's validator alongside its bundled Mailchecker list, so signup rejects an email if either flags it. Server-only module to keep the dataset out of the client bundle.
1 parent 2626482 commit a489c4f

6 files changed

Lines changed: 74 additions & 1 deletion

File tree

apps/sim/lib/auth/auth.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
organization,
2222
} from 'better-auth/plugins'
2323
import { emailHarmony } from 'better-auth-harmony'
24+
import { validateEmail as validateEmailWithMailchecker } from 'better-auth-harmony/email'
2425
import { and, count, eq, inArray, sql } from 'drizzle-orm'
2526
import { headers } from 'next/headers'
2627
import Stripe from 'stripe'
@@ -78,6 +79,7 @@ import {
7879
import { PlatformEvents } from '@/lib/core/telemetry'
7980
import { getBaseUrl, isLocalhostUrl, parseOriginList } from '@/lib/core/utils/urls'
8081
import { processCredentialDraft } from '@/lib/credentials/draft-processor'
82+
import { isDisposableEmailDomain } from '@/lib/messaging/email/disposable-domains.server'
8183
import { sendEmail } from '@/lib/messaging/email/mailer'
8284
import { getFromEmailAddress, getPersonalEmailFrom } from '@/lib/messaging/email/utils'
8385
import { quickValidateEmail } from '@/lib/messaging/email/validation'
@@ -930,7 +932,14 @@ export const auth = betterAuth({
930932
}),
931933
},
932934
plugins: [
933-
...(isSignupEmailValidationEnabled ? [emailHarmony()] : []),
935+
...(isSignupEmailValidationEnabled
936+
? [
937+
emailHarmony({
938+
validator: (email) =>
939+
validateEmailWithMailchecker(email) && !isDisposableEmailDomain(email),
940+
}),
941+
]
942+
: []),
934943
...(env.TURNSTILE_SECRET_KEY
935944
? [
936945
captcha({
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** Ambient types for `disposable-email-domains` — ships JSON arrays with no bundled types. */
2+
declare module 'disposable-email-domains' {
3+
const domains: string[]
4+
export default domains
5+
}
6+
7+
declare module 'disposable-email-domains/wildcard.json' {
8+
const baseDomains: string[]
9+
export default baseDomains
10+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @vitest-environment node
3+
*/
4+
import { describe, expect, it } from 'vitest'
5+
import { isDisposableEmailDomain } from '@/lib/messaging/email/disposable-domains.server'
6+
7+
describe('isDisposableEmailDomain', () => {
8+
it('flags a known disposable domain', () => {
9+
expect(isDisposableEmailDomain('someone@mailinator.com')).toBe(true)
10+
})
11+
12+
it('flags a subdomain of a wildcard base domain', () => {
13+
expect(isDisposableEmailDomain('someone@inbox.10mail.org')).toBe(true)
14+
})
15+
16+
it('is case-insensitive on the domain', () => {
17+
expect(isDisposableEmailDomain('Someone@MailInator.com')).toBe(true)
18+
})
19+
20+
it('allows a normal provider domain', () => {
21+
expect(isDisposableEmailDomain('someone@gmail.com')).toBe(false)
22+
})
23+
24+
it('allows a custom catch-all domain that is not on the list', () => {
25+
expect(isDisposableEmailDomain('sim6dc088f506@lordfortescue.org.uk')).toBe(false)
26+
})
27+
28+
it('returns false for malformed input with no domain', () => {
29+
expect(isDisposableEmailDomain('not-an-email')).toBe(false)
30+
})
31+
})
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import disposableDomains from 'disposable-email-domains'
2+
import wildcardBaseDomains from 'disposable-email-domains/wildcard.json'
3+
4+
const exactDomains = new Set(disposableDomains)
5+
6+
/**
7+
* Server-only disposable-email-domain check backed by the `disposable-email-domains`
8+
* package (~120K exact domains plus wildcard base domains). Layered alongside
9+
* better-auth-harmony's bundled Mailchecker list at the signup gate.
10+
*
11+
* Never import from client code — the dataset would bloat the browser bundle.
12+
* Matches exact domains and any subdomain of a wildcard base domain.
13+
*/
14+
export function isDisposableEmailDomain(email: string): boolean {
15+
const domain = email.split('@')[1]?.toLowerCase()
16+
if (!domain) return false
17+
if (exactDomains.has(domain)) return true
18+
return wildcardBaseDomains.some((base) => domain === base || domain.endsWith(`.${base}`))
19+
}

apps/sim/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
"csv-parse": "6.1.0",
127127
"date-fns": "4.1.0",
128128
"decimal.js": "10.6.0",
129+
"disposable-email-domains": "1.0.62",
129130
"docx": "^9.6.1",
130131
"docx-preview": "^0.3.7",
131132
"drizzle-orm": "^0.45.2",

bun.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)