| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- import assert from 'node:assert/strict';
- import { mkdtempSync } from 'node:fs';
- import net from 'node:net';
- import { tmpdir } from 'node:os';
- import path from 'node:path';
- import { test } from 'node:test';
- import {
- createDomain,
- createInboundMailbox,
- createUser,
- initDatabase,
- listInboundMessages
- } from '../src/db.js';
- import { sendViaSmtp } from '../src/mailer.js';
- import { startSubmissionServer } from '../src/submission.js';
- test('SMTP accepts unauthenticated inbound mail for local mailboxes', async () => {
- initDatabase(mkdtempSync(path.join(tmpdir(), 'mailhub-submission-inbound-')), 'inbound-secret');
- const user = createUser({ username: 'inbound-smtp', email: 'inbound-smtp@example.com', password: 'password123' });
- createDomain(user.id, {
- domain: 'inbound.example',
- selector: 'mh',
- verificationToken: 'verify',
- dkimPublic: 'public',
- dkimPrivate: 'private',
- senderHost: 'mail.inbound.example',
- sendingIp: '192.0.2.10',
- spfExtra: '',
- dmarcPolicy: 'none',
- dmarcRua: ''
- });
- createInboundMailbox(user.id, { address: 'support@inbound.example', displayName: 'Support' });
- const [server] = startSubmissionServer({
- enabled: true,
- listeners: [{ port: 0, protocol: 'smtp' }],
- hostname: 'mx.inbound.example',
- allowInsecureAuth: true,
- inboundEnabled: true,
- relayHost: '',
- relayPort: 25,
- relaySecure: false,
- relayUsername: '',
- relayPassword: '',
- relayHelo: 'mx.inbound.example'
- });
- await waitForListening(server);
- try {
- const rawMessage = [
- 'From: Alice <alice@example.net>',
- 'To: Support <support@inbound.example>',
- 'Subject: Hello inbound SMTP',
- 'Message-ID: <hello-inbound@example.net>',
- 'Content-Type: text/plain; charset=UTF-8',
- '',
- 'Hello through SMTP.',
- ''
- ].join('\r\n');
- const response = await sendViaSmtp({
- host: '127.0.0.1',
- port: server.address().port,
- secure: false,
- username: '',
- password: '',
- helo: 'sender.example.net',
- mailFrom: 'alice@example.net',
- recipients: ['support@inbound.example'],
- rawMessage
- });
- assert.match(response.message, /Message accepted/i);
- const [message] = listInboundMessages(user.id);
- assert.equal(message.sender, 'alice@example.net');
- assert.deepEqual(message.recipients, ['support@inbound.example']);
- assert.equal(message.subject, 'Hello inbound SMTP');
- assert.equal(message.preview, 'Hello through SMTP.');
- } finally {
- await closeServer(server);
- }
- });
- test('SMTP rejects unauthenticated inbound mail for unknown recipients', async () => {
- initDatabase(mkdtempSync(path.join(tmpdir(), 'mailhub-submission-inbound-reject-')), 'inbound-secret');
- const [server] = startSubmissionServer({
- enabled: true,
- listeners: [{ port: 0, protocol: 'smtp' }],
- hostname: 'mx.inbound.example',
- allowInsecureAuth: true,
- inboundEnabled: true
- });
- await waitForListening(server);
- try {
- const transcript = await smtpTranscript(server.address().port, [
- 'EHLO sender.example.net',
- 'MAIL FROM:<alice@example.net>',
- 'RCPT TO:<nobody@external.example>'
- ]);
- assert.match(transcript.at(-1), /^550 /);
- assert.equal(listInboundMessages(1).length, 0);
- } finally {
- await closeServer(server);
- }
- });
- test('SMTP stores each inbound recipient without exposing other envelope recipients', async () => {
- initDatabase(mkdtempSync(path.join(tmpdir(), 'mailhub-submission-inbound-multi-')), 'inbound-secret');
- const supportUser = createUser({ username: 'support-user', email: 'support@example.com', password: 'password123' });
- const privateUser = createUser({ username: 'private-user', email: 'private@example.com', password: 'password123' });
- createDomain(supportUser.id, {
- domain: 'support.example',
- selector: 'mh',
- verificationToken: 'verify',
- dkimPublic: 'public',
- dkimPrivate: 'private',
- senderHost: 'mail.support.example',
- sendingIp: '192.0.2.12',
- spfExtra: '',
- dmarcPolicy: 'none',
- dmarcRua: ''
- });
- createDomain(privateUser.id, {
- domain: 'private.example',
- selector: 'mh',
- verificationToken: 'verify',
- dkimPublic: 'public',
- dkimPrivate: 'private',
- senderHost: 'mail.private.example',
- sendingIp: '192.0.2.13',
- spfExtra: '',
- dmarcPolicy: 'none',
- dmarcRua: ''
- });
- createInboundMailbox(supportUser.id, { address: 'support@support.example' });
- createInboundMailbox(privateUser.id, { address: 'private@private.example' });
- const [server] = startSubmissionServer({
- enabled: true,
- listeners: [{ port: 0, protocol: 'smtp' }],
- hostname: 'mx.inbound.example',
- allowInsecureAuth: true,
- inboundEnabled: true
- });
- await waitForListening(server);
- try {
- const rawMessage = [
- 'From: Alice <alice@example.net>',
- 'To: Support <support@support.example>',
- 'Subject: Multi recipient',
- '',
- 'Hello both.',
- ''
- ].join('\r\n');
- await sendViaSmtp({
- host: '127.0.0.1',
- port: server.address().port,
- secure: false,
- username: '',
- password: '',
- helo: 'sender.example.net',
- mailFrom: 'alice@example.net',
- recipients: ['support@support.example', 'private@private.example'],
- rawMessage
- });
- assert.deepEqual(listInboundMessages(supportUser.id)[0].recipients, ['support@support.example']);
- assert.deepEqual(listInboundMessages(privateUser.id)[0].recipients, ['private@private.example']);
- } finally {
- await closeServer(server);
- }
- });
- test('SMTP accepts unauthenticated inbound bounces with an empty envelope sender', async () => {
- initDatabase(mkdtempSync(path.join(tmpdir(), 'mailhub-submission-inbound-bounce-')), 'inbound-secret');
- const user = createUser({ username: 'bounce-user', email: 'bounce@example.com', password: 'password123' });
- createDomain(user.id, {
- domain: 'bounce.example',
- selector: 'mh',
- verificationToken: 'verify',
- dkimPublic: 'public',
- dkimPrivate: 'private',
- senderHost: 'mail.bounce.example',
- sendingIp: '192.0.2.14',
- spfExtra: '',
- dmarcPolicy: 'none',
- dmarcRua: ''
- });
- createInboundMailbox(user.id, { address: 'postmaster@bounce.example' });
- const [server] = startSubmissionServer({
- enabled: true,
- listeners: [{ port: 0, protocol: 'smtp' }],
- hostname: 'mx.bounce.example',
- allowInsecureAuth: true,
- inboundEnabled: true
- });
- await waitForListening(server);
- try {
- await sendViaSmtp({
- host: '127.0.0.1',
- port: server.address().port,
- secure: false,
- username: '',
- password: '',
- helo: 'sender.example.net',
- mailFrom: '',
- recipients: ['postmaster@bounce.example'],
- rawMessage: 'From: MAILER-DAEMON <>\r\nSubject: Delivery status\r\n\r\nBounced.'
- });
- const [message] = listInboundMessages(user.id);
- assert.equal(message.sender, '');
- assert.deepEqual(message.recipients, ['postmaster@bounce.example']);
- } finally {
- await closeServer(server);
- }
- });
- test('SMTP rejects oversized unauthenticated inbound messages without storing them', async () => {
- initDatabase(mkdtempSync(path.join(tmpdir(), 'mailhub-submission-inbound-size-')), 'inbound-secret');
- const user = createUser({ username: 'size-user', email: 'size@example.com', password: 'password123' });
- createDomain(user.id, {
- domain: 'size.example',
- selector: 'mh',
- verificationToken: 'verify',
- dkimPublic: 'public',
- dkimPrivate: 'private',
- senderHost: 'mail.size.example',
- sendingIp: '192.0.2.15',
- spfExtra: '',
- dmarcPolicy: 'none',
- dmarcRua: ''
- });
- createInboundMailbox(user.id, { address: 'support@size.example' });
- const [server] = startSubmissionServer({
- enabled: true,
- listeners: [{ port: 0, protocol: 'smtp' }],
- hostname: 'mx.size.example',
- allowInsecureAuth: true,
- inboundEnabled: true,
- maxMessageBytes: 64
- });
- await waitForListening(server);
- try {
- const transcript = await smtpTranscript(server.address().port, [
- 'EHLO sender.example.net',
- 'MAIL FROM:<alice@example.net>',
- 'RCPT TO:<support@size.example>',
- 'DATA',
- [
- 'Subject: Oversized inbound',
- '',
- 'This body is intentionally longer than the configured inbound message size limit.',
- '.'
- ].join('\r\n')
- ]);
- assert.match(transcript.at(-1), /^552 /);
- assert.equal(listInboundMessages(user.id).length, 0);
- } finally {
- await closeServer(server);
- }
- });
- function waitForListening(server) {
- if (server.listening) return Promise.resolve();
- return new Promise((resolve) => server.once('listening', resolve));
- }
- function closeServer(server) {
- return new Promise((resolve, reject) => {
- server.close((error) => error ? reject(error) : resolve());
- });
- }
- async function smtpTranscript(port, commands) {
- return await new Promise((resolve, reject) => {
- const socket = net.createConnection({ host: '127.0.0.1', port });
- socket.setEncoding('utf8');
- socket.setTimeout(3000);
- const responses = [];
- let buffer = '';
- let index = -1;
- socket.on('data', (chunk) => {
- buffer += chunk;
- let lineEnd;
- while ((lineEnd = buffer.indexOf('\n')) !== -1) {
- const line = buffer.slice(0, lineEnd).replace(/\r$/, '');
- buffer = buffer.slice(lineEnd + 1);
- if (!/^\d{3}[ -]/.test(line)) continue;
- responses.push(line);
- if (/^\d{3} /.test(line)) {
- index += 1;
- if (index >= commands.length) {
- socket.end('QUIT\r\n');
- resolve(responses);
- return;
- }
- socket.write(`${commands[index]}\r\n`);
- }
- }
- });
- socket.once('error', reject);
- socket.once('timeout', () => reject(new Error('SMTP transcript timed out')));
- });
- }
|