Skip to content

Commit 9c4b371

Browse files
authored
Merge pull request #106 from DanielYang59/fix-mailto-multi-email
Fix `mailto` handling of multiple recipients
2 parents 3263c3b + 22b7567 commit 9c4b371

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

lib/proto/mailto.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,22 @@ module.exports.check = (link, opts, callback) => {
66
import('node-email-verifier').then((mod) => {
77
const emailValidator = mod.default;
88
const address = link
9-
.substr(7) // strip "mailto:"
10-
.split('?')[0]; // trim ?subject=blah hfields
9+
.replace(/^mailto:/i, '')
10+
.split('?')[0]; // trim ?subject=blah hfields
1111

1212
/* per RFC6068, the '?' is a reserved delimiter and email addresses containing '?' must be encoded,
1313
* so it's safe to split on '?' and pick [0].
1414
*/
1515

16-
emailValidator(address, { checkMx: true, timeout: opts.timeout || '10s' }).then((emailValid) => {
17-
if (!emailValid) {
16+
// multiple addresses separated by commas
17+
const addresses = address.split(',');
18+
19+
Promise.all(
20+
addresses.map(addr =>
21+
emailValidator(addr.trim(), { checkMx: true, timeout: opts.timeout || '10s' })
22+
)
23+
).then((results) => {
24+
if (results.some(valid => !valid)) {
1825
return callback(null, new LinkCheckResult(opts, link, 400, null));
1926
}
2027
return callback(null, new LinkCheckResult(opts, link, 200, null));

test/link-check.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,18 @@ describe('link-check', function () {
268268
});
269269
});
270270

271+
it('should handle valid mailto with multiple recipients', function (done) {
272+
linkCheck(
273+
'MAILTO:[email protected],[email protected]?subject=Test',
274+
function (err, result) {
275+
expect(err).to.be(null);
276+
expect(result.link).to.be('MAILTO:[email protected],[email protected]?subject=Test');
277+
expect(result.status).to.be('alive');
278+
done();
279+
}
280+
);
281+
});
282+
271283
it('should handle valid mailto with encoded characters in address', function (done) {
272284
linkCheck('mailto:foo%[email protected]', function (err, result) {
273285
expect(err).to.be(null);
@@ -304,6 +316,18 @@ describe('link-check', function () {
304316
});
305317
});
306318

319+
it('should handle invalid mailto with multiple recipients', function (done) {
320+
linkCheck(
321+
'mailto:foo@@bar@@baz,[email protected]?subject=Test',
322+
function (err, result) {
323+
expect(err).to.be(null);
324+
expect(result.link).to.be('mailto:foo@@bar@@baz,[email protected]?subject=Test');
325+
expect(result.status).to.be('dead');
326+
done();
327+
}
328+
);
329+
});
330+
307331
it('should handle file protocol', function(done) {
308332
linkCheck('fixtures/file.md', { baseUrl: 'file://' + __dirname }, function(err, result) {
309333
expect(err).to.be(null);

0 commit comments

Comments
 (0)