|
| 1 | +import { createServer, type Server } from 'node:net' |
| 2 | +import { |
| 3 | + inputValidationMock, |
| 4 | + inputValidationMockFns, |
| 5 | +} from '@sim/testing/mocks/input-validation.mock' |
| 6 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock) |
| 9 | +vi.mock('@/lib/messaging/email/ehlo', () => ({ getSmtpEhloName: () => 'sim.example.com' })) |
| 10 | + |
| 11 | +import { executeSmtpSend } from '@/lib/internal/smtp/operations' |
| 12 | + |
| 13 | +const { mockValidateDatabaseHost } = inputValidationMockFns |
| 14 | + |
| 15 | +/** A plaintext SMTP server that never offers STARTTLS and records every command it receives. */ |
| 16 | +function startPlaintextSmtpServer(commands: string[]): Promise<{ server: Server; port: number }> { |
| 17 | + const server = createServer((socket) => { |
| 18 | + socket.on('error', () => {}) |
| 19 | + let buffered = '' |
| 20 | + let inData = false |
| 21 | + socket.write('220 test ESMTP\r\n') |
| 22 | + socket.on('data', (chunk) => { |
| 23 | + buffered += chunk.toString() |
| 24 | + let lineEnd = buffered.indexOf('\r\n') |
| 25 | + while (lineEnd !== -1) { |
| 26 | + const line = buffered.slice(0, lineEnd) |
| 27 | + buffered = buffered.slice(lineEnd + 2) |
| 28 | + lineEnd = buffered.indexOf('\r\n') |
| 29 | + if (inData) { |
| 30 | + if (line === '.') { |
| 31 | + inData = false |
| 32 | + socket.write('250 queued\r\n') |
| 33 | + } |
| 34 | + continue |
| 35 | + } |
| 36 | + const command = line.split(' ')[0].toUpperCase() |
| 37 | + commands.push(command) |
| 38 | + if (command === 'EHLO') socket.write('250-test\r\n250 AUTH PLAIN LOGIN\r\n') |
| 39 | + else if (command === 'STARTTLS') socket.write('502 command not implemented\r\n') |
| 40 | + else if (command === 'AUTH') socket.write('235 authenticated\r\n') |
| 41 | + else if (command === 'DATA') { |
| 42 | + inData = true |
| 43 | + socket.write('354 go ahead\r\n') |
| 44 | + } else if (command === 'QUIT') socket.end('221 bye\r\n') |
| 45 | + else socket.write('250 ok\r\n') |
| 46 | + } |
| 47 | + }) |
| 48 | + }) |
| 49 | + return new Promise((resolve) => { |
| 50 | + server.listen(0, '127.0.0.1', () => { |
| 51 | + const address = server.address() |
| 52 | + resolve({ server, port: typeof address === 'object' && address ? address.port : 0 }) |
| 53 | + }) |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +describe('SMTP security modes against a server without STARTTLS', () => { |
| 58 | + let server: Server | undefined |
| 59 | + const commands: string[] = [] |
| 60 | + |
| 61 | + beforeEach(() => { |
| 62 | + commands.length = 0 |
| 63 | + mockValidateDatabaseHost.mockResolvedValue({ isValid: true, resolvedIP: '127.0.0.1' }) |
| 64 | + }) |
| 65 | + |
| 66 | + afterEach(async () => { |
| 67 | + await new Promise((resolve) => server?.close(resolve) ?? resolve(undefined)) |
| 68 | + }) |
| 69 | + |
| 70 | + const send = async (smtpSecure: 'TLS' | 'None') => { |
| 71 | + const started = await startPlaintextSmtpServer(commands) |
| 72 | + server = started.server |
| 73 | + return executeSmtpSend( |
| 74 | + { |
| 75 | + smtpHost: 'smtp.example.com', |
| 76 | + smtpPort: started.port, |
| 77 | + smtpUsername: 'user', |
| 78 | + smtpPassword: 'password', |
| 79 | + smtpSecure, |
| 80 | + from: 'from@example.com', |
| 81 | + to: 'to@example.com', |
| 82 | + subject: 'Hello', |
| 83 | + body: 'Hello', |
| 84 | + }, |
| 85 | + { requestId: 'request-1', userId: 'user-1' } |
| 86 | + ) |
| 87 | + } |
| 88 | + |
| 89 | + it('fails in TLS mode before sending credentials', async () => { |
| 90 | + await expect(send('TLS')).rejects.toMatchObject({ status: 500 }) |
| 91 | + expect(commands).toContain('EHLO') |
| 92 | + expect(commands).not.toContain('AUTH') |
| 93 | + expect(commands).not.toContain('MAIL') |
| 94 | + }) |
| 95 | + |
| 96 | + it('still sends in None mode, which allows a plaintext session', async () => { |
| 97 | + await expect(send('None')).resolves.toMatchObject({ success: true }) |
| 98 | + expect(commands).toContain('AUTH') |
| 99 | + }) |
| 100 | +}) |
0 commit comments