Skip to content

Commit 07ffe8c

Browse files
andris9claude
andcommitted
fix: return the promise from every resolveContent branch
When called without a callback, only the plain string/Buffer path reached the final return statement; the stream, URL, file path, data uri, and empty content branches returned early, so awaiting resolveContent resolved to undefined immediately for those content types. The branching body now lives in an inner function and the promisified wrapper has a single exit. Also replaces an unreachable-intent Buffer.from(0) call, which throws a TypeError, with Buffer.alloc(0) for data uris that contain no comma. Reported in nodemailer-upstream-bugs-2026-06-10 (issue 5). Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent cb4f904 commit 07ffe8c

2 files changed

Lines changed: 92 additions & 7 deletions

File tree

‎lib/shared/index.js‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,12 @@ module.exports.resolveContent = (data, key, options, callback) => {
530530
});
531531
}
532532

533+
resolveContentValue(data, key, options, callback);
534+
535+
return promise;
536+
};
537+
538+
function resolveContentValue(data, key, options, callback) {
533539
let content = (data && data[key] && data[key].content) || data[key];
534540
const encoding = ((typeof data[key] === 'object' && data[key].encoding) || 'utf8')
535541
.toString()
@@ -567,10 +573,7 @@ module.exports.resolveContent = (data, key, options, callback) => {
567573
} else if (/^data:/i.test(content.path || content.href)) {
568574
const parsedDataUri = module.exports.parseDataURI(content.path || content.href);
569575

570-
if (!parsedDataUri || !parsedDataUri.data) {
571-
return callback(null, Buffer.from(0));
572-
}
573-
return callback(null, parsedDataUri.data);
576+
return callback(null, parsedDataUri && parsedDataUri.data ? parsedDataUri.data : Buffer.alloc(0));
574577
} else if (content.path) {
575578
if (options.disableFileAccess) {
576579
return setImmediate(() => {
@@ -589,9 +592,7 @@ module.exports.resolveContent = (data, key, options, callback) => {
589592

590593
// default action, return as is
591594
setImmediate(() => callback(null, content));
592-
593-
return promise;
594-
};
595+
}
595596

596597
/**
597598
* Copies properties from source objects to target objects

‎test/shared/shared-test.js‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,90 @@ describe('Shared Funcs Tests', { timeout: 100 * 1000 }, () => {
380380
done();
381381
});
382382
});
383+
384+
it('should return an empty buffer for a data uri without a comma', (t, done) => {
385+
let mail = {
386+
data: {
387+
attachment: {
388+
path: 'data:no-comma-here'
389+
}
390+
}
391+
};
392+
shared.resolveContent(mail.data, 'attachment', (err, value) => {
393+
assert.ok(!err);
394+
assert.deepStrictEqual(value, Buffer.alloc(0));
395+
done();
396+
});
397+
});
398+
});
399+
400+
// Regression tests: every resolveContent branch must return the generated
401+
// promise; stream/url/file/data-uri/empty branches used to return early and
402+
// resolve the awaited value to undefined
403+
describe('promise form', () => {
404+
const expected = Buffer.from('<p>Tere, tere</p><p>vana kere!</p>\n');
405+
406+
it('should resolve string content', async () => {
407+
let mail = { data: { html: '<p>Tere, tere</p><p>vana kere!</p>\n' } };
408+
const value = await shared.resolveContent(mail.data, 'html');
409+
assert.strictEqual(value, '<p>Tere, tere</p><p>vana kere!</p>\n');
410+
});
411+
412+
it('should resolve empty content', async () => {
413+
let mail = { data: { html: '' } };
414+
const value = await shared.resolveContent(mail.data, 'html');
415+
assert.strictEqual(value, '');
416+
});
417+
418+
it('should resolve a stream', async () => {
419+
let mail = { data: { html: fs.createReadStream(__dirname + '/fixtures/message.html') } };
420+
const value = await shared.resolveContent(mail.data, 'html');
421+
assert.deepStrictEqual(value, expected);
422+
});
423+
424+
it('should resolve a file path', async () => {
425+
let mail = { data: { html: { path: __dirname + '/fixtures/message.html' } } };
426+
const value = await shared.resolveContent(mail.data, 'html');
427+
assert.deepStrictEqual(value, expected);
428+
});
429+
430+
it('should resolve an url', async () => {
431+
let mail = { data: { html: { path: 'http://localhost:' + port + '/message.html' } } };
432+
const value = await shared.resolveContent(mail.data, 'html');
433+
assert.deepStrictEqual(value, expected);
434+
});
435+
436+
it('should resolve a data uri', async () => {
437+
let mail = { data: { attachment: { path: 'data:image/png,tere%20tere' } } };
438+
const value = await shared.resolveContent(mail.data, 'attachment');
439+
assert.deepStrictEqual(value, Buffer.from('tere tere'));
440+
});
441+
442+
it('should resolve an empty buffer for a data uri without a comma', async () => {
443+
let mail = { data: { attachment: { path: 'data:no-comma-here' } } };
444+
const value = await shared.resolveContent(mail.data, 'attachment');
445+
assert.deepStrictEqual(value, Buffer.alloc(0));
446+
});
447+
448+
it('should reject a missing file', async () => {
449+
let mail = { data: { html: { path: __dirname + '/fixtures/no-such-file.html' } } };
450+
await assert.rejects(shared.resolveContent(mail.data, 'html'));
451+
});
452+
453+
it('should reject an unreachable url', async () => {
454+
let mail = { data: { html: { path: 'http://localhost:' + (port + 1000) + '/message.html' } } };
455+
await assert.rejects(shared.resolveContent(mail.data, 'html'));
456+
});
457+
458+
it('should reject when file access is disabled', async () => {
459+
let mail = { data: { html: { path: __dirname + '/fixtures/message.html' } } };
460+
await assert.rejects(shared.resolveContent(mail.data, 'html', { disableFileAccess: true }), { code: 'EFILEACCESS' });
461+
});
462+
463+
it('should reject when url access is disabled', async () => {
464+
let mail = { data: { html: { path: 'http://localhost:' + port + '/message.html' } } };
465+
await assert.rejects(shared.resolveContent(mail.data, 'html', { disableUrlAccess: true }), { code: 'EURLACCESS' });
466+
});
383467
});
384468
});
385469

0 commit comments

Comments
 (0)