Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:mailer): migrate to Nodemailer 10, harden TLS and align SMTP logging

johaven29062f7f7b12
diff --git a/backend/package.json b/backend/package.json
index 98f895c8..ab2da0ed 100755
--- a/backend/package.json
+++ b/backend/package.json
@@ -71,7 +71,7 @@
"mime-types": "^3.0.2",
"mysql2": "^3.23.1",
"nestjs-pino": "^4.6.1",
- "nodemailer": "^9.0.1",
+ "nodemailer": "^10.0.15",
"openid-client": "^6.8.4",
"passport": "^0.7.0",
"passport-jwt": "^4.0.1",
@@ -105,7 +105,6 @@
"@types/html-to-text": "^9.0.4",
"@types/mime-types": "^3.0.1",
"@types/node": "^24.13.0",
- "@types/nodemailer": "^8.0.1",
"@types/passport-http": "^0.3.11",
"@types/passport-jwt": "^4.0.1",
"@types/passport-local": "^1.0.38",
diff --git a/backend/src/infrastructure/mailer/interfaces/mail.interface.ts b/backend/src/infrastructure/mailer/interfaces/mail.interface.ts
index 3ab6e066..effd6396 100644
--- a/backend/src/infrastructure/mailer/interfaces/mail.interface.ts
+++ b/backend/src/infrastructure/mailer/interfaces/mail.interface.ts
@@ -1,23 +1,3 @@
-export interface MailTransport {
- host: string
- port: number
- secure: boolean
- ignoreTLS: boolean
- auth: {
- user: string
- pass: string
- }
- debug?: boolean
- logger?: any
-}
-
-export interface MailDefaultsTransport {
- from: string
- tls: {
- rejectUnauthorized: boolean
- }
-}
-
export interface MailProps {
to: string
subject: string
diff --git a/backend/src/infrastructure/mailer/mailer.config.ts b/backend/src/infrastructure/mailer/mailer.config.ts
index 0c72c902..d7d7402c 100644
--- a/backend/src/infrastructure/mailer/mailer.config.ts
+++ b/backend/src/infrastructure/mailer/mailer.config.ts
@@ -31,7 +31,7 @@ export class MailerConfig {
@IsOptional()
@IsBoolean()
- rejectUnauthorized?: boolean = false
+ rejectUnauthorized?: boolean = true
@IsOptional()
@IsObject()
@@ -43,10 +43,6 @@ export class MailerConfig {
@IsString()
sender?: string = 'Sync-in<[email protected]>'
- @IsOptional()
- @IsBoolean()
- debug?: boolean = false
-
@IsOptional()
@IsBoolean()
logger?: boolean = false
diff --git a/backend/src/infrastructure/mailer/mailer.service.spec.ts b/backend/src/infrastructure/mailer/mailer.service.spec.ts
index 61e39c7e..8606ffdf 100644
--- a/backend/src/infrastructure/mailer/mailer.service.spec.ts
+++ b/backend/src/infrastructure/mailer/mailer.service.spec.ts
@@ -1,7 +1,7 @@
import { ConfigService } from '@nestjs/config'
import { Test, TestingModule } from '@nestjs/testing'
import { PinoLogger } from 'nestjs-pino'
-import nodemailer from 'nodemailer'
+import nodemailer, { type Transporter } from 'nodemailer'
import { MailerConfig } from './mailer.config'
import { Mailer } from './mailer.service'
@@ -11,7 +11,7 @@ const createTransportMock = {
verify: vi.fn().mockResolvedValue(true),
sendMail: vi.fn().mockResolvedValue(true)
}
-vi.mocked(nodemailer.createTransport).mockReturnValue(createTransportMock)
+vi.mocked(nodemailer.createTransport).mockReturnValue(createTransportMock as unknown as Transporter)
describe(Mailer.name, () => {
let module: TestingModule
@@ -24,12 +24,13 @@ describe(Mailer.name, () => {
port: 587,
auth: { user: 'user', pass: 'pass' },
secure: false,
+ ignoreTLS: false,
+ rejectUnauthorized: true,
sender: '[email protected]',
- debug: false,
logger: false
}
- const initModule = async (config: MailerConfig | undefined) => {
+ const initModule = async (config: MailerConfig | undefined, loggerLevel: string = 'info') => {
module = await Test.createTestingModule({
providers: [
Mailer,
@@ -42,7 +43,9 @@ describe(Mailer.name, () => {
error: vi.fn(),
info: vi.fn(),
assign: vi.fn(),
- logger: { level: 'info' }
+ logger: {
+ isLevelEnabled: vi.fn().mockImplementation((level: string) => level === 'debug' && ['trace', 'debug'].includes(loggerLevel))
+ }
}
}
]
@@ -65,14 +68,14 @@ describe(Mailer.name, () => {
})
it('should not initialize transporter if config is absent', () => {
- expect(mailer['transport']).toBeUndefined()
+ expect(mailer['transporter']).toBeUndefined()
expect(nodemailer.createTransport).not.toHaveBeenCalled()
})
it('should initialize secure transport with no secure port', async () => {
await initModule({ ...mailerConfig, secure: true })
expect(mailer['configuration'].secure).toBe(false)
- await initModule({ ...mailerConfig, port: 25, secure: true, logger: true, debug: true })
+ await initModule({ ...mailerConfig, port: 25, secure: true, logger: true })
expect(mailer['configuration'].secure).toBe(false)
const loggerWarnSpy = vi.spyOn(logger, 'warn')
expect(loggerWarnSpy).toHaveBeenCalledWith(expect.stringMatching(/has been disabled/i))
@@ -96,7 +99,29 @@ describe(Mailer.name, () => {
host: mailerConfig.host,
port: mailerConfig.port,
auth: mailerConfig.auth,
- secure: mailerConfig.secure
+ secure: mailerConfig.secure,
+ ignoreTLS: mailerConfig.ignoreTLS,
+ tls: { rejectUnauthorized: mailerConfig.rejectUnauthorized },
+ debug: false,
+ logger: false
+ }),
+ expect.objectContaining({
+ from: mailerConfig.sender
+ })
+ )
+ })
+
+ it.each([
+ { loggerLevel: 'info', smtpDebug: false },
+ { loggerLevel: 'debug', smtpDebug: true },
+ { loggerLevel: 'trace', smtpDebug: true }
+ ])('should set SMTP debug to $smtpDebug when the general logger level is $loggerLevel', async ({ loggerLevel, smtpDebug }) => {
+ await initModule({ ...mailerConfig, logger: true }, loggerLevel)
+
+ expect(nodemailer.createTransport).toHaveBeenLastCalledWith(
+ expect.objectContaining({
+ debug: smtpDebug,
+ logger
}),
expect.objectContaining({
from: mailerConfig.sender
diff --git a/backend/src/infrastructure/mailer/mailer.service.ts b/backend/src/infrastructure/mailer/mailer.service.ts
index 6db06e97..a2ec6e06 100644
--- a/backend/src/infrastructure/mailer/mailer.service.ts
+++ b/backend/src/infrastructure/mailer/mailer.service.ts
@@ -1,15 +1,14 @@
import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { PinoLogger } from 'nestjs-pino'
-import nodemailer from 'nodemailer'
-import type { Logger as NodeMailLogger } from 'nodemailer/lib/shared'
-import { MailDefaultsTransport, MailProps, MailTransport } from './interfaces/mail.interface'
+import nodemailer, { type SendMailOptions, type SMTPTransportOptions, type Transporter } from 'nodemailer'
+import type { MailProps } from './interfaces/mail.interface'
import { MailerConfig } from './mailer.config'
@Injectable()
export class Mailer {
public available: boolean = false
- private readonly transporter: nodemailer.Transporter
+ private readonly transporter: Transporter
private readonly configuration: MailerConfig
constructor(
@@ -21,11 +20,11 @@ export class Mailer {
if (!this.configuration) {
return
}
- this.logger.logger.level = this.configuration.debug ? 'debug' : 'info'
if (this.configuration.secure && (this.configuration.port === 587 || this.configuration.port === 25)) {
this.logger.warn(`Secure transport has been disabled due to use of port : ${this.configuration.port}`)
this.configuration.secure = false
}
+ const smtpLoggerEnabled = this.configuration.logger === true
this.transporter = nodemailer.createTransport(
{
host: this.configuration.host,
@@ -33,27 +32,30 @@ export class Mailer {
auth: this.configuration.auth,
secure: this.configuration.secure,
ignoreTLS: this.configuration.ignoreTLS,
- logger: this.configuration.logger ? (this.logger as NodeMailLogger & any) : false
- } satisfies MailTransport,
- { from: this.configuration.sender, tls: { rejectUnauthorized: this.configuration.rejectUnauthorized } } satisfies MailDefaultsTransport
+ tls: { rejectUnauthorized: this.configuration.rejectUnauthorized },
+ debug: smtpLoggerEnabled && this.logger.logger.isLevelEnabled('debug'),
+ logger: smtpLoggerEnabled ? (this.logger as SMTPTransportOptions['logger']) : false
+ } satisfies SMTPTransportOptions,
+ { from: this.configuration.sender } satisfies SendMailOptions
)
- this.verify().catch(this.logger.error)
+ void this.verify()
}
- async sendMails(mails: MailProps[]) {
+ async sendMails(mails: MailProps[]): Promise<void> {
if (!this.available) {
return
}
- for (const m of mails) {
- this.transporter
- .sendMail(m)
- .then(() => {
+
+ await Promise.all(
+ mails.map(async (m) => {
+ try {
+ await this.transporter.sendMail(m)
this.logger.info(`Mail sent to '${m.to}' with subject '${m.subject}'`)
- })
- .catch((e) => {
+ } catch (e) {
this.logger.error(`Mail was not sent to '${m.to}' with subject '${m.subject}' : ${e}`)
- })
- }
+ }
+ })
+ )
}
private async verify(): Promise<void> {
diff --git a/environment/environment.dist.yaml b/environment/environment.dist.yaml
index 750c0197..6e2a0ca5 100644
--- a/environment/environment.dist.yaml
+++ b/environment/environment.dist.yaml
@@ -75,14 +75,12 @@ mail:
# default: false
ignoreTLS: false
# rejectUnauthorized: reject the connection if the server's TLS certificate is invalid
- # default: false
- rejectUnauthorized: false
- # Enable logger
+ # default: true
+ rejectUnauthorized: true
+ # Enable Nodemailer internal SMTP logs
+ # Their verbosity follows the general logger.level; debug and trace enable detailed SMTP traffic
# default: `false`
logger: false
- # Set log level to debug
- # default: `false`
- debug: false
auth:
# provider : `mysql` | `ldap` | `oidc`
# default: `mysql`