Skip to content

Commit 203c8ec

Browse files
andris9claude
andcommitted
fix: resolve oauth2_provision_cb at send time for non-pooled SMTP transports
The transport-level auth object is built in the SMTPTransport constructor, before Mail assigns transporter.mailer, so a provision callback registered with transporter.set('oauth2_provision_cb', ...) was never picked up when sendMail() was called without a per-message auth value and the send failed with EAUTH. Re-check the mailer for the callback when the cached auth is returned from getAuth(). Pooled transports, verify(), and per-message auth were unaffected. Reported in nodemailer-upstream-bugs-2026-06-10 (issue 1), dates back to v3.0.0. Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent a6bcbf6 commit 203c8ec

2 files changed

Lines changed: 133 additions & 0 deletions

File tree

‎lib/smtp-transport/index.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ class SMTPTransport extends EventEmitter {
7171

7272
getAuth(authOpts) {
7373
if (!authOpts) {
74+
if (this.auth && this.auth.oauth2 && this.mailer) {
75+
// Transport-level auth is resolved in the constructor, before the Mail wrapper
76+
// assigns `this.mailer`, so a provision callback registered with
77+
// `transporter.set('oauth2_provision_cb', ...)` has to be re-checked here
78+
this.auth.oauth2.provisionCallback = this.mailer.get('oauth2_provision_cb') || this.auth.oauth2.provisionCallback;
79+
}
7480
return this.auth;
7581
}
7682

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
'use strict';
2+
3+
const { describe, it, beforeEach, afterEach } = require('node:test');
4+
const assert = require('node:assert/strict');
5+
const nodemailer = require('../../lib/nodemailer');
6+
const SMTPServer = require('smtp-server').SMTPServer;
7+
8+
// Regression tests: the transport-level auth object is resolved in the SMTPTransport
9+
// constructor, before Mail assigns `transporter.mailer`, so a provision callback
10+
// registered with `transporter.set('oauth2_provision_cb', ...)` was never picked up
11+
// for non-pooled sendMail() calls without a per-message `auth` value.
12+
describe('SMTP transport OAuth2 provision callback', { timeout: 10000 }, () => {
13+
let server;
14+
15+
beforeEach((t, done) => {
16+
server = new SMTPServer({
17+
authMethods: ['XOAUTH2'],
18+
disabledCommands: ['STARTTLS'],
19+
20+
onData(stream, session, callback) {
21+
stream.on('data', () => {});
22+
stream.on('end', callback);
23+
},
24+
25+
onAuth(auth, session, callback) {
26+
if (auth.method !== 'XOAUTH2' || auth.username !== '[email protected]' || auth.accessToken !== 'provisioned-token') {
27+
return callback(null, {
28+
data: {
29+
status: '401',
30+
schemes: 'bearer mac',
31+
scope: 'my_smtp_access_scope_name'
32+
}
33+
});
34+
}
35+
callback(null, {
36+
user: 123
37+
});
38+
},
39+
logger: false
40+
});
41+
42+
server.listen(0, done);
43+
});
44+
45+
afterEach((t, done) => {
46+
server.close(done);
47+
});
48+
49+
it('should use oauth2_provision_cb for non-pooled sendMail without per-message auth', (t, done) => {
50+
let provisionCount = 0;
51+
52+
const transporter = nodemailer.createTransport({
53+
host: 'localhost',
54+
port: server.server.address().port,
55+
ignoreTLS: true,
56+
auth: {
57+
type: 'OAuth2',
58+
59+
},
60+
logger: false
61+
});
62+
63+
transporter.set('oauth2_provision_cb', (user, renew, callback) => {
64+
provisionCount++;
65+
assert.strictEqual(user, '[email protected]');
66+
callback(null, 'provisioned-token');
67+
});
68+
69+
const mailData = {
70+
71+
72+
subject: 'test',
73+
text: 'test'
74+
};
75+
76+
transporter.sendMail(mailData, err => {
77+
try {
78+
assert.ok(!err, err && err.message);
79+
assert.strictEqual(provisionCount, 1);
80+
} catch (E) {
81+
return done(E);
82+
}
83+
84+
// a second message must reuse the cached access token instead of provisioning again
85+
transporter.sendMail(mailData, err => {
86+
try {
87+
assert.ok(!err, err && err.message);
88+
assert.strictEqual(provisionCount, 1);
89+
} catch (E) {
90+
return done(E);
91+
}
92+
done();
93+
});
94+
});
95+
});
96+
97+
it('should fail with EAUTH when no provision callback is registered', (t, done) => {
98+
const transporter = nodemailer.createTransport({
99+
host: 'localhost',
100+
port: server.server.address().port,
101+
ignoreTLS: true,
102+
auth: {
103+
type: 'OAuth2',
104+
105+
},
106+
logger: false
107+
});
108+
109+
transporter.sendMail(
110+
{
111+
112+
113+
subject: 'test',
114+
text: 'test'
115+
},
116+
err => {
117+
try {
118+
assert.ok(err);
119+
assert.strictEqual(err.code, 'EAUTH');
120+
} catch (E) {
121+
return done(E);
122+
}
123+
done();
124+
}
125+
);
126+
});
127+
});

0 commit comments

Comments
 (0)