Skip to content

Commit 5f69497

Browse files
andris9claude
andauthored
fix: two pending security advisories (jsonTransport access bypass, List-* CRLF injection) (#1820)
* fix: enforce disableFileAccess/disableUrlAccess in content resolution shared.resolveContent() read local files and fetched HTTP(S) URLs without consulting disableFileAccess/disableUrlAccess. The MIME streaming path in mime-node enforces those flags, but jsonTransport (mail.normalize -> resolveAll) and the attachDataUrls pre-plugin (_convertDataImages) resolve content through shared.resolveContent() before streaming, bypassing the policy. An attacker controlling a message field such as an attachment `path` or `text.href` could read local files into the JSON output or force outbound HTTP requests despite the flags being set. resolveContent() now accepts an optional access-policy object (the legacy (data, key, callback) signature still works) and rejects blocked file/URL content with EFILEACCESS/EURLACCESS, matching mime-node. resolveAll() and _convertDataImages() pass the message's flags through. Behavior is unchanged when the flags are unset. Refs: GHSA-wqvq-jvpq-h66f Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> * fix: strip CRLF from List-* header comments _getListHeaders() inserted list.*.comment values into prepared List-* headers without neutralizing CR/LF. mimeFuncs.isPlainText() does not treat CR or LF as disallowed, so a comment passed the plain-text check and was emitted verbatim through foldLines(), bypassing the CRLF normalization that ordinary (unprepared) header values get in _encodeHeaderValue(). An attacker influencing any list.*.comment field (help, unsubscribe, subscribe, post, owner, archive, id) could inject arbitrary additional message headers. CR/LF in the comment are now collapsed to a space before the prepared header is built, matching the normalization used for ordinary header values. The url field was already sanitized by _formatListUrl(). Refs: GHSA-268h-hp4c-crq3 Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 15138a8 commit 5f69497

5 files changed

Lines changed: 261 additions & 45 deletions

File tree

‎lib/mailer/index.js‎

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -407,31 +407,39 @@ class Mail extends EventEmitter {
407407
if ((!this.options.attachDataUrls && !mail.data.attachDataUrls) || !mail.data.html) {
408408
return callback();
409409
}
410-
mail.resolveContent(mail.data, 'html', (err, html) => {
411-
if (err) {
412-
return callback(err);
410+
mail.resolveContent(
411+
mail.data,
412+
'html',
413+
{ disableFileAccess: mail.data.disableFileAccess, disableUrlAccess: mail.data.disableUrlAccess },
414+
(err, html) => {
415+
if (err) {
416+
return callback(err);
417+
}
418+
let cidCounter = 0;
419+
html = (html || '')
420+
.toString()
421+
.replace(
422+
/(<img\b[^<>]{0,1024} src\s{0,20}=[\s"']{0,20})(data:([^;]+);[^"'>\s]+)/gi,
423+
(match, prefix, dataUri, mimeType) => {
424+
const cid = crypto.randomBytes(10).toString('hex') + '@localhost';
425+
if (!mail.data.attachments) {
426+
mail.data.attachments = [];
427+
}
428+
if (!Array.isArray(mail.data.attachments)) {
429+
mail.data.attachments = [].concat(mail.data.attachments || []);
430+
}
431+
mail.data.attachments.push({
432+
path: dataUri,
433+
cid,
434+
filename: 'image-' + ++cidCounter + '.' + mimeTypes.detectExtension(mimeType)
435+
});
436+
return prefix + 'cid:' + cid;
437+
}
438+
);
439+
mail.data.html = html;
440+
callback();
413441
}
414-
let cidCounter = 0;
415-
html = (html || '')
416-
.toString()
417-
.replace(/(<img\b[^<>]{0,1024} src\s{0,20}=[\s"']{0,20})(data:([^;]+);[^"'>\s]+)/gi, (match, prefix, dataUri, mimeType) => {
418-
const cid = crypto.randomBytes(10).toString('hex') + '@localhost';
419-
if (!mail.data.attachments) {
420-
mail.data.attachments = [];
421-
}
422-
if (!Array.isArray(mail.data.attachments)) {
423-
mail.data.attachments = [].concat(mail.data.attachments || []);
424-
}
425-
mail.data.attachments.push({
426-
path: dataUri,
427-
cid,
428-
filename: 'image-' + ++cidCounter + '.' + mimeTypes.detectExtension(mimeType)
429-
});
430-
return prefix + 'cid:' + cid;
431-
});
432-
mail.data.html = html;
433-
callback();
434-
});
442+
);
435443
}
436444

437445
set(key, value) {

‎lib/mailer/mail-message.js‎

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,25 +111,29 @@ class MailMessage {
111111
if (!args[0] || !args[0][args[1]]) {
112112
return resolveNext();
113113
}
114-
shared.resolveContent(...args, (err, value) => {
115-
if (err) {
116-
return callback(err);
117-
}
114+
shared.resolveContent(
115+
...args,
116+
{ disableFileAccess: this.data.disableFileAccess, disableUrlAccess: this.data.disableUrlAccess },
117+
(err, value) => {
118+
if (err) {
119+
return callback(err);
120+
}
118121

119-
const node = {
120-
content: value
121-
};
122-
if (args[0][args[1]] && typeof args[0][args[1]] === 'object' && !Buffer.isBuffer(args[0][args[1]])) {
123-
Object.keys(args[0][args[1]]).forEach(key => {
124-
if (!(key in node) && !['content', 'path', 'href', 'raw'].includes(key)) {
125-
node[key] = args[0][args[1]][key];
126-
}
127-
});
128-
}
122+
const node = {
123+
content: value
124+
};
125+
if (args[0][args[1]] && typeof args[0][args[1]] === 'object' && !Buffer.isBuffer(args[0][args[1]])) {
126+
Object.keys(args[0][args[1]]).forEach(key => {
127+
if (!(key in node) && !['content', 'path', 'href', 'raw'].includes(key)) {
128+
node[key] = args[0][args[1]][key];
129+
}
130+
});
131+
}
129132

130-
args[0][args[1]] = node;
131-
resolveNext();
132-
});
133+
args[0][args[1]] = node;
134+
resolveNext();
135+
}
136+
);
133137
};
134138

135139
setImmediate(() => resolveNext());
@@ -269,7 +273,8 @@ class MailMessage {
269273
if (value && value.url) {
270274
if (key.toLowerCase().trim() === 'id') {
271275
// List-ID: "comment" <domain>
272-
let comment = value.comment || '';
276+
// strip CR/LF so a comment can't inject extra header lines
277+
let comment = (value.comment || '').toString().replace(/\r?\n|\r/g, ' ');
273278
if (mimeFuncs.isPlainText(comment)) {
274279
comment = '"' + comment + '"';
275280
} else {
@@ -280,7 +285,8 @@ class MailMessage {
280285
}
281286

282287
// List-*: <http://domain> (comment)
283-
let comment = value.comment || '';
288+
// strip CR/LF so a comment can't inject extra header lines
289+
let comment = (value.comment || '').toString().replace(/\r?\n|\r/g, ' ');
284290
if (!mimeFuncs.isPlainText(comment)) {
285291
comment = mimeFuncs.encodeWord(comment);
286292
}

‎lib/shared/index.js‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const urllib = require('url');
66
const util = require('util');
77
const fs = require('fs');
88
const nmfetch = require('../fetch');
9+
const errors = require('../errors');
910
const dns = require('dns');
1011
const net = require('net');
1112
const os = require('os');
@@ -501,9 +502,17 @@ module.exports.parseDataURI = uri => {
501502
*
502503
* @param {Object} data An object or an Array you want to resolve an element for
503504
* @param {String|Number} key Property name or an Array index
505+
* @param {Object} [options] Optional access policy: { disableFileAccess, disableUrlAccess }
504506
* @param {Function} callback Callback function with (err, value)
505507
*/
506-
module.exports.resolveContent = (data, key, callback) => {
508+
module.exports.resolveContent = (data, key, options, callback) => {
509+
// options is optional; support the legacy resolveContent(data, key, callback) signature
510+
if (!callback && typeof options === 'function') {
511+
callback = options;
512+
options = false;
513+
}
514+
options = options || {};
515+
507516
let promise;
508517

509518
if (!callback) {
@@ -538,6 +547,13 @@ module.exports.resolveContent = (data, key, callback) => {
538547
callback(null, value);
539548
});
540549
} else if (/^https?:\/\//i.test(content.path || content.href)) {
550+
if (options.disableUrlAccess) {
551+
return setImmediate(() => {
552+
const err = new Error('Url access rejected for ' + (content.path || content.href));
553+
err.code = errors.EURLACCESS;
554+
callback(err);
555+
});
556+
}
541557
return resolveStream(nmfetch(content.path || content.href), callback);
542558
} else if (/^data:/i.test(content.path || content.href)) {
543559
const parsedDataUri = module.exports.parseDataURI(content.path || content.href);
@@ -547,6 +563,13 @@ module.exports.resolveContent = (data, key, callback) => {
547563
}
548564
return callback(null, parsedDataUri.data);
549565
} else if (content.path) {
566+
if (options.disableFileAccess) {
567+
return setImmediate(() => {
568+
const err = new Error('File access rejected for ' + content.path);
569+
err.code = errors.EFILEACCESS;
570+
callback(err);
571+
});
572+
}
550573
return resolveStream(fs.createReadStream(content.path), callback);
551574
}
552575
}

‎test/json-transport/json-transport-test.js‎

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
44

5-
const { describe, it } = require('node:test');
5+
const { describe, it, before, after } = require('node:test');
66
const assert = require('node:assert/strict');
7+
const http = require('http');
78
const nodemailer = require('../../lib/nodemailer');
89

910
describe('JSON Transport Tests', () => {
@@ -146,3 +147,127 @@ describe('JSON Transport Tests', () => {
146147
});
147148
});
148149
});
150+
151+
describe('JSON Transport access control (disableFileAccess / disableUrlAccess)', () => {
152+
let port = 10399;
153+
let server;
154+
155+
before((t, done) => {
156+
server = http.createServer((req, res) => {
157+
res.writeHead(200, { 'Content-Type': 'text/plain' });
158+
res.end('SECRET_URL_BODY');
159+
});
160+
server.listen(port, done);
161+
});
162+
163+
after((t, done) => {
164+
server.close(done);
165+
});
166+
167+
it('should reject attachment file access when disableFileAccess is set', (t, done) => {
168+
let transport = nodemailer.createTransport({
169+
jsonTransport: true,
170+
disableFileAccess: true
171+
});
172+
173+
transport.sendMail(
174+
{
175+
176+
177+
subject: 'file',
178+
text: 'body',
179+
attachments: [{ filename: 'secret.txt', path: __dirname + '/fixtures/body.html' }]
180+
},
181+
err => {
182+
assert.ok(err);
183+
assert.strictEqual(err.code, 'EFILEACCESS');
184+
done();
185+
}
186+
);
187+
});
188+
189+
it('should reject URL content access when disableUrlAccess is set', (t, done) => {
190+
let transport = nodemailer.createTransport({
191+
jsonTransport: true,
192+
disableUrlAccess: true
193+
});
194+
195+
transport.sendMail(
196+
{
197+
198+
199+
subject: 'url',
200+
text: { href: 'http://127.0.0.1:' + port + '/private' }
201+
},
202+
err => {
203+
assert.ok(err);
204+
assert.strictEqual(err.code, 'EURLACCESS');
205+
done();
206+
}
207+
);
208+
});
209+
210+
it('should reject attachDataUrls html file access when disableFileAccess is set', (t, done) => {
211+
let transport = nodemailer.createTransport({
212+
jsonTransport: true,
213+
attachDataUrls: true,
214+
disableFileAccess: true
215+
});
216+
217+
transport.sendMail(
218+
{
219+
220+
221+
subject: 'datauri',
222+
html: { path: __dirname + '/fixtures/body.html' }
223+
},
224+
err => {
225+
assert.ok(err);
226+
assert.strictEqual(err.code, 'EFILEACCESS');
227+
done();
228+
}
229+
);
230+
});
231+
232+
it('should still resolve file attachments when the flags are not set', (t, done) => {
233+
let transport = nodemailer.createTransport({
234+
jsonTransport: true
235+
});
236+
237+
transport.sendMail(
238+
{
239+
240+
241+
subject: 'file',
242+
text: 'body',
243+
attachments: [{ filename: 'body.html', path: __dirname + '/fixtures/body.html' }]
244+
},
245+
(err, info) => {
246+
assert.ok(!err);
247+
let message = JSON.parse(info.message);
248+
assert.ok(message.attachments[0].content);
249+
done();
250+
}
251+
);
252+
});
253+
254+
it('should still resolve URL content when the flags are not set', (t, done) => {
255+
let transport = nodemailer.createTransport({
256+
jsonTransport: true
257+
});
258+
259+
transport.sendMail(
260+
{
261+
262+
263+
subject: 'url',
264+
text: { href: 'http://127.0.0.1:' + port + '/private' }
265+
},
266+
(err, info) => {
267+
assert.ok(!err);
268+
assert.strictEqual(JSON.parse(info.message).text, 'SECRET_URL_BODY');
269+
done();
270+
}
271+
);
272+
});
273+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
const { describe, it } = require('node:test');
4+
const assert = require('node:assert/strict');
5+
const nodemailer = require('../../lib/nodemailer');
6+
7+
// Regression tests for GHSA-268h-hp4c-crq3: CRLF in a `list.*.comment` field
8+
// must not be emitted as a real header boundary in the generated message.
9+
describe('List-* header comment CRLF injection', () => {
10+
const send = data => {
11+
const transport = nodemailer.createTransport({ streamTransport: true, buffer: true });
12+
return new Promise((resolve, reject) => {
13+
transport.sendMail(data, (err, info) => (err ? reject(err) : resolve(info.message.toString('utf8'))));
14+
});
15+
};
16+
17+
const listKeys = ['help', 'unsubscribe', 'subscribe', 'post', 'owner', 'archive', 'id'];
18+
19+
listKeys.forEach(key => {
20+
it('should not allow header injection via list.' + key + '.comment', async () => {
21+
const raw = await send({
22+
23+
24+
subject: 'list ' + key,
25+
list: {
26+
[key]: {
27+
url: key === 'id' ? 'example.test' : 'https://example.test/' + key,
28+
comment: 'comment\r\nX-Injected-' + key + ': yes'
29+
}
30+
},
31+
text: 'body'
32+
});
33+
34+
// the injected marker must not appear at the start of its own header line
35+
assert.ok(!new RegExp('\\r\\nX-Injected-' + key + ': yes').test(raw), 'CRLF injected a standalone header for list.' + key);
36+
// and the (sanitized) comment text is still carried inside the List-* header
37+
assert.ok(new RegExp('^List-' + key + ':', 'im').test(raw));
38+
});
39+
});
40+
41+
it('should keep a benign comment intact in the List-* header', async () => {
42+
const raw = await send({
43+
44+
45+
subject: 'benign',
46+
list: { unsubscribe: { url: 'https://example.test/u', comment: 'Unsubscribe here' } },
47+
text: 'body'
48+
});
49+
50+
const line = raw.split('\r\n').find(l => /^List-Unsubscribe:/.test(l));
51+
assert.ok(line);
52+
assert.ok(line.includes('(Unsubscribe here)'));
53+
});
54+
});

0 commit comments

Comments
 (0)