feat(mailer): say so when SMTP certificate verification is off

SMTP_SKIP_TLS_VERIFY (and the matching admin setting) turns off certificate
checking for outgoing mail. That escape hatch stays — an install behind an
internal relay with a self-signed certificate depends on it — but until now it
downgraded the connection without a word, and a transport is built per send, so
whoever flipped it had nothing to go on afterwards.

It now warns once per process, naming the host it applies to. Once, not per
mail: a line on every notification is a line nobody reads.
This commit is contained in:
Maurice
2026-08-25 10:45:14 +02:00
committed by Maurice
parent 23196aa038
commit fb76074e7c
2 changed files with 169 additions and 1 deletions
@@ -2,7 +2,7 @@ import { Injectable } from '@nestjs/common';
import nodemailer from 'nodemailer';
import { PASSWORD_RESET_I18N } from '@trek/shared/i18n/externalNotifications';
import { readEnv } from '../../../app-config';
import { logError, logInfo, logDebug } from '../../audit/audit-log.logger';
import { logError, logInfo, logDebug, logWarn } from '../../audit/audit-log.logger';
import { decrypt_api_key } from '../../common/crypto/apiKeyCrypto';
import { DatabaseService } from '../../database/database.service';
import { buildEmailHtml, buildPasswordResetHtml } from './email-html';
@@ -28,6 +28,8 @@ interface SmtpConfig {
*/
@Injectable()
export class MailerService {
private skippedTlsWarned = false;
constructor(private readonly db: DatabaseService) {}
private getAppSetting(key: string): string | null {
@@ -61,15 +63,35 @@ export class MailerService {
*/
private createTransport(config: SmtpConfig) {
const skipTls = readEnv().smtp.skipTlsVerify || this.getAppSetting('smtp_skip_tls_verify') === 'true';
if (skipTls) this.warnOnceAboutSkippedTls(config);
return nodemailer.createTransport({
host: config.host,
port: config.port,
secure: config.secure,
auth: config.user ? { user: config.user, pass: config.pass } : undefined,
// The operator's opt-out for a relay with a self-signed certificate, off by
// default and reachable only through SMTP_SKIP_TLS_VERIFY or the matching
// admin setting. It stays because self-hosted installs behind an internal
// relay depend on it; warnOnceAboutSkippedTls is what keeps it from being a
// silent downgrade.
...(skipTls ? { tls: { rejectUnauthorized: false } } : {}),
});
}
/**
* Once per process, not once per mail — a transport is built on every send, and
* a line on every notification would train the operator to scroll past it.
*/
private warnOnceAboutSkippedTls(config: SmtpConfig): void {
if (this.skippedTlsWarned) return;
this.skippedTlsWarned = true;
logWarn(
`SECURITY: SMTP certificate verification is disabled for ${config.host}:${config.port}. ` +
'Mail — including password-reset links and the SMTP credentials — travels over a channel an ' +
'active attacker can impersonate. Keep this on only for a relay you control on a trusted network.',
);
}
/** Is SMTP configured at the instance level? (Independent of any one user's address.) */
isSmtpConfigured(): boolean {
return !!(readEnv().smtp.host || this.getAppSetting('smtp_host'));