Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/mail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ export class MailError extends Error {
}
}

export function assertEmailSenderAllowed(...addresses: string[]): void {
const blocked = new Set(
(process.env.EMAIL_QUEUE_BLOCKED_SENDERS ?? '')
.split(',')
.map((address) => address.trim().toLowerCase())
.filter(Boolean),
);
const senders = addresses.map((value) => (/<([^>]+)>/.exec(value)?.[1] ?? value).trim().toLowerCase());
if (senders.some((address) => blocked.has(address))) {
throw new MailError('Outbound sending from this mailbox is paused for administrator review.');
}
}

// ---------------------------------------------------------------------------
// Providers
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -1619,6 +1632,7 @@ export function smtpTransport(account: Account) {
}

export const sendViaSmtp: SmtpSender = async (account, outgoing) => {
assertEmailSenderAllowed(account.email, account.user, outgoing.from);
const transporter = smtpTransport(account);
const info = await transporter.sendMail({
from: outgoing.from,
Expand Down Expand Up @@ -1703,6 +1717,7 @@ export async function sendMail(
isBlocked?: HostBlockCheck;
} = {},
): Promise<SendResult> {
assertEmailSenderAllowed(account.email, account.user, outgoing.from);
const choice = chooseTransport(account, options.resendKey, options.via);
const smtp = options.smtp ?? sendViaSmtp;
const resend = options.resend ?? resendSender();
Expand Down
20 changes: 20 additions & 0 deletions test/mail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,26 @@ describe('sendMail', () => {
expect(isTransportFailure('550 5.1.1 recipient rejected')).toBe(false);
expect(isTransportFailure('Resend refused the message: domain is not verified')).toBe(false);
});

it('stops a blocked mailbox before SMTP or Resend fallback', async () => {
const previous = process.env.EMAIL_QUEUE_BLOCKED_SENDERS;
process.env.EMAIL_QUEUE_BLOCKED_SENDERS = 'ettinger@gmail.com';
const calls: string[] = [];
try {
await expect(sendMail(account({ email: 'ettinger@gmail.com', user: 'ettinger@gmail.com' }), {
...outgoing,
from: 'Me <ettinger@gmail.com>',
}, {
resendKey: 'key',
smtp: async () => { calls.push('smtp'); return 'must-not-send'; },
resend: async () => { calls.push('resend'); return 'must-not-send'; },
})).rejects.toThrow(/paused for administrator review/);
expect(calls).toEqual([]);
} finally {
if (previous === undefined) delete process.env.EMAIL_QUEUE_BLOCKED_SENDERS;
else process.env.EMAIL_QUEUE_BLOCKED_SENDERS = previous;
}
});
});

describe('Resend', () => {
Expand Down
Loading