|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +const { mockResolveMx, envRef } = vi.hoisted(() => ({ |
| 7 | + mockResolveMx: vi.fn(), |
| 8 | + envRef: { |
| 9 | + BLOCKED_EMAIL_MX_HOSTS: undefined as string | undefined, |
| 10 | + }, |
| 11 | +})) |
| 12 | + |
| 13 | +vi.mock('dns/promises', () => ({ |
| 14 | + default: { resolveMx: mockResolveMx }, |
| 15 | +})) |
| 16 | + |
| 17 | +vi.mock('@/lib/core/config/env', () => ({ |
| 18 | + get env() { |
| 19 | + return envRef |
| 20 | + }, |
| 21 | +})) |
| 22 | + |
| 23 | +import { validateSignupEmailMx } from '@/lib/messaging/email/validation.server' |
| 24 | + |
| 25 | +const mx = (...hosts: string[]) => |
| 26 | + hosts.map((exchange, i) => ({ exchange, priority: (i + 1) * 10 })) |
| 27 | + |
| 28 | +describe('validateSignupEmailMx', () => { |
| 29 | + beforeEach(() => { |
| 30 | + vi.clearAllMocks() |
| 31 | + envRef.BLOCKED_EMAIL_MX_HOSTS = undefined |
| 32 | + }) |
| 33 | + |
| 34 | + it('blocks a domain whose MX backend is on the configured denylist', async () => { |
| 35 | + envRef.BLOCKED_EMAIL_MX_HOSTS = 'blocked-backend.example' |
| 36 | + mockResolveMx.mockResolvedValue(mx('smtp.blocked-backend.example')) |
| 37 | + const result = await validateSignupEmailMx('user@rotated-domain.test') |
| 38 | + expect(result.allowed).toBe(false) |
| 39 | + expect(result.reason).toBe('blocked_mx_backend') |
| 40 | + }) |
| 41 | + |
| 42 | + it('matches the denylist as a case-insensitive substring of the MX exchange', async () => { |
| 43 | + envRef.BLOCKED_EMAIL_MX_HOSTS = 'Blocked-Backend.Example' |
| 44 | + mockResolveMx.mockResolvedValue(mx('mx1.blocked-backend.example')) |
| 45 | + const result = await validateSignupEmailMx('user@another-domain.test') |
| 46 | + expect(result.allowed).toBe(false) |
| 47 | + expect(result.reason).toBe('blocked_mx_backend') |
| 48 | + }) |
| 49 | + |
| 50 | + it('does not block any backend when the denylist is empty (no hardcoded defaults)', async () => { |
| 51 | + envRef.BLOCKED_EMAIL_MX_HOSTS = undefined |
| 52 | + mockResolveMx.mockResolvedValue(mx('smtp.blocked-backend.example')) |
| 53 | + const result = await validateSignupEmailMx('user@rotated-domain.test') |
| 54 | + expect(result.allowed).toBe(true) |
| 55 | + }) |
| 56 | + |
| 57 | + it('allows a legitimate domain (gmail)', async () => { |
| 58 | + mockResolveMx.mockResolvedValue( |
| 59 | + mx('gmail-smtp-in.l.google.com', 'alt1.gmail-smtp-in.l.google.com') |
| 60 | + ) |
| 61 | + const result = await validateSignupEmailMx('real.person@gmail.com') |
| 62 | + expect(result.allowed).toBe(true) |
| 63 | + }) |
| 64 | + |
| 65 | + it('blocks a domain with no MX records (ENOTFOUND)', async () => { |
| 66 | + mockResolveMx.mockRejectedValue(Object.assign(new Error('not found'), { code: 'ENOTFOUND' })) |
| 67 | + const result = await validateSignupEmailMx('x@no-such-domain.invalid') |
| 68 | + expect(result.allowed).toBe(false) |
| 69 | + expect(result.reason).toBe('no_mx') |
| 70 | + }) |
| 71 | + |
| 72 | + it('blocks a domain that resolves to an empty MX set', async () => { |
| 73 | + mockResolveMx.mockResolvedValue([]) |
| 74 | + const result = await validateSignupEmailMx('x@empty.example') |
| 75 | + expect(result.allowed).toBe(false) |
| 76 | + expect(result.reason).toBe('no_mx') |
| 77 | + }) |
| 78 | + |
| 79 | + it('fails open on a transient DNS error (does not block legit users)', async () => { |
| 80 | + mockResolveMx.mockRejectedValue(Object.assign(new Error('timeout'), { code: 'ETIMEOUT' })) |
| 81 | + const result = await validateSignupEmailMx('user@some-real-domain.com') |
| 82 | + expect(result.allowed).toBe(true) |
| 83 | + }) |
| 84 | + |
| 85 | + it('allows when the email has no domain (defers to other validation)', async () => { |
| 86 | + const result = await validateSignupEmailMx('not-an-email') |
| 87 | + expect(result.allowed).toBe(true) |
| 88 | + expect(mockResolveMx).not.toHaveBeenCalled() |
| 89 | + }) |
| 90 | +}) |
0 commit comments