fix(email): respect hosts file for SMTP connections#3082
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughUpdates Next.js env route type import to ChangesNext.js Type Definitions Path
SMTP Custom Socket for /etc/hosts Support
Sequence DiagramsequenceDiagram
participant Nodemailer
participant getSocket
participant net_createConnection as net.createConnection
participant SMTPServer as SMTP Server
Nodemailer->>getSocket: request socket (host not IP)
getSocket->>net_createConnection: create TCP connection to host:port
net_createConnection->>SMTPServer: establish TCP connection
SMTPServer-->>net_createConnection: socket connected
net_createConnection-->>getSocket: socket ready
getSocket-->>Nodemailer: return { connection: socket }
Nodemailer->>SMTPServer: use returned socket for SMTP session
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
server/lib/email/index.ts (1)
49-49: Verify nodemailergetSocketintegration and IP/hostname branching
nodemaileris pinned to8.0.5, and the transportgetSocketcallback wiring matches the expected contract (getSocket(options, callback)->callback(err)/callback(null, { connection })).- The conditional socket injection correctly disables
getSocketfor literal IPs and uses it for hostnames:getSocket: net.isIP(settings.options.smtpHost) ? undefined : getSocket,- Consider explicitly guarding against empty/undefined
smtpHostso you don’t accidentally route throughgetSocketwith an empty host value.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@server/lib/email/index.ts` at line 49, The transport wiring currently chooses getSocket based on net.isIP(settings.options.smtpHost) but can mistakenly pass getSocket when smtpHost is empty/undefined; update the condition used where getSocket is assigned (the getSocket property for the nodemailer transport creation) to explicitly check that settings.options.smtpHost is a non-empty string and not an IP (e.g., ensure settings.options.smtpHost && !net.isIP(settings.options.smtpHost)) so getSocket is only set for valid hostnames; keep the existing getSocket callback implementation and nodemailer compatibility unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@server/lib/email/index.ts`:
- Around line 10-24: The getSocket function lacks a connection timeout; update
getSocket to call socket.setTimeout(...) (e.g., 10000 ms) after net.connect,
listen for the 'timeout' event and in that handler remove the 'error'/'connect'
listeners, destroy the socket, and invoke callback with a timeout Error; also
ensure you clear the timeout or remove the 'timeout' listener inside onConnect
and onError so you don't leak listeners or sockets. Target the getSocket
function and its onError/onConnect handlers to add the timeout setup and cleanup
logic.
---
Nitpick comments:
In `@server/lib/email/index.ts`:
- Line 49: The transport wiring currently chooses getSocket based on
net.isIP(settings.options.smtpHost) but can mistakenly pass getSocket when
smtpHost is empty/undefined; update the condition used where getSocket is
assigned (the getSocket property for the nodemailer transport creation) to
explicitly check that settings.options.smtpHost is a non-empty string and not an
IP (e.g., ensure settings.options.smtpHost &&
!net.isIP(settings.options.smtpHost)) so getSocket is only set for valid
hostnames; keep the existing getSocket callback implementation and nodemailer
compatibility unchanged.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 9fb9ce4a-2a75-428b-9da7-af6fd800e70c
📒 Files selected for processing (2)
next-env.d.tsserver/lib/email/index.ts
044967e to
18c0d9b
Compare
Description
This PR fixes an issue where SMTP hostnames defined through Docker
extra_hostsor/etc/hostswere not respected when sending email notifications.Previously, nodemailer could resolve the SMTP hostname through DNS before opening the connection, which could bypass
/etc/hosts.How Has This Been Tested?
Tested locally with Docker:
getent hosts example.cominside the container resolved to the host gateway./etc/hostsentry mappedexample.comto the host gateway, but the SMTPclient still followed nodemailer's DNS resolution path instead of the hosts
entry.
Screenshots / Logs (if applicable)
Checklist:
pnpm buildpnpm i18n:extractSummary by CodeRabbit