forked from forwardemail/free-email-forwarding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
1166 lines (1040 loc) · 37.5 KB
/
Copy pathindex.js
File metadata and controls
1166 lines (1040 loc) · 37.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const crypto = require('crypto');
const dns = require('dns');
const fs = require('fs');
const os = require('os');
const tls = require('tls');
const util = require('util');
const Limiter = require('ratelimiter');
const Redis = require('@ladjs/redis');
const _ = require('lodash');
const addressParser = require('nodemailer/lib/addressparser');
const arrayJoinConjunction = require('array-join-conjunction');
const bytes = require('bytes');
const dkimVerify = require('python-dkim-verify');
const dnsbl = require('dnsbl');
const domains = require('disposable-email-domains');
const getFQDN = require('get-fqdn');
const got = require('got');
const ip = require('ip');
const isSANB = require('is-string-and-not-blank');
const mailUtilities = require('mailin/lib/mailUtilities.js');
const ms = require('ms');
const nodemailer = require('nodemailer');
const punycode = require('punycode/');
const sharedConfig = require('@ladjs/shared-config');
const spfCheck2 = require('python-spfcheck2');
const validator = require('validator');
const wildcards = require('disposable-email-domains/wildcard.json');
const { SMTPServer } = require('smtp-server');
const { SRS } = require('sender-rewriting-scheme');
const { boolean } = require('boolean');
const { oneLine } = require('common-tags');
const { CustomError, MessageSplitter, env, logger } = require('./helpers');
const lookupAsync = util.promisify(dns.lookup);
const resolveTxtAsync = util.promisify(dns.resolveTxt);
const resolveMxAsync = util.promisify(dns.resolveMx);
const computeSpamScoreAsync = util.promisify(mailUtilities.computeSpamScore);
const CODES_TO_RESPONSE_CODES = {
ETIMEDOUT: 420,
ECONNRESET: 442,
EADDRINUSE: 421,
ECONNREFUSED: 421,
EPIPE: 421,
ENOTFOUND: 421,
ENETUNREACH: 421,
EAI_AGAIN: 421
};
const RETRY_CODES = _.keys(CODES_TO_RESPONSE_CODES);
const transporterConfig = {
debug: !env.IS_SILENT,
logger,
direct: true,
opportunisticTLS: true,
port: 25,
tls: {
rejectUnauthorized: env.NODE_ENV !== 'test'
},
connectionTimeout: ms('5s'),
greetingTimeout: ms('5s'),
socketTimeout: 0
};
class ForwardEmail {
constructor(config = {}) {
this.config = {
...sharedConfig('SMTP'),
// TODO: eventually set 127.0.0.1 as DNS server
// BUT we would need to set up an API endpoint/functionality
// on our service at https://forwardemail.net that would clear
// the local dns cache at 127.0.0.1 after purging Cloudflare/Google
// for both `dnsbl` and `dns` usage
// <https://gist.github.com/zhurui1008/48130439a079a3c23920>
// <https://github.com/niftylettuce/forward-email/issues/131#issuecomment-490484052>
//
// <https://blog.cloudflare.com/announcing-1111/>
dns: env.DNS_PROVIDERS,
noReply: env.EMAIL_NOREPLY,
logger,
smtp: {
size: bytes(env.SMTP_MESSAGE_MAX_SIZE),
onConnect: this.onConnect.bind(this),
onData: this.onData.bind(this),
onMailFrom: this.onMailFrom.bind(this),
onRcptTo: this.onRcptTo.bind(this),
disabledCommands: ['AUTH'],
logInfo: !env.IS_SILENT,
logger,
...config.smtp
},
spamScoreThreshold: env.SPAM_SCORE_THRESHOLD,
blacklist: env.BLACKLIST,
blacklistedStr:
"Your mail server's IP address of %s is listed on the %s DNS blacklist (visit %s to submit a removal request and try again).",
dnsbl: {
domains: env.DNSBL_DOMAINS,
removals: env.DNSBL_REMOVALS,
...config.dnsbl
},
rateLimit: {
duration: env.RATELIMIT_DURATION
? parseInt(env.RATELIMIT_DURATION, 10)
: 60000,
max: env.RATELIMIT_MAX ? parseInt(env.RATELIMIT_MAX, 10) : 100,
prefix: env.RATELIMIT_PREFIX
? env.RATELIMIT_PREFIX
: `limit_${env.NODE_ENV.toLowerCase()}`
},
exchanges: env.SMTP_EXCHANGE_DOMAINS,
dkim: {
domainName: env.DKIM_DOMAIN_NAME,
keySelector: env.DKIM_KEY_SELECTOR,
privateKey: isSANB(env.DKIM_PRIVATE_KEY_PATH)
? fs.readFileSync(env.DKIM_PRIVATE_KEY_PATH, 'utf8')
: undefined,
cacheDir: os.tmpdir(),
...config.dkim
},
maxForwardedAddresses: env.MAX_FORWARDED_ADDRESSES,
email: env.EMAIL_SUPPORT,
website: env.WEBSITE_URL,
recordPrefix: env.TXT_RECORD_PREFIX,
whitelistedDisposableDomains: env.VANITY_DOMAINS,
lookupEndpoint: env.LOOKUP_ENDPOINT,
lookupSecrets: env.LOOKUP_SECRETS,
srs: {
separator: '=',
secret: env.SRS_SECRET,
maxAge: 30
},
srsDomain: env.SRS_DOMAIN,
...config
};
if (
this.dnsbl &&
Array.isArray(this.dnsbl.domains) &&
Array.isArray(this.dnsbl.removals) &&
this.dnsbl.domains.length !== this.dnsbl.removals.length
)
throw new Error('DNSBL_DOMAINS length must be equal to DNSBL_REMOVALS');
if (this.config.ssl) {
this.config.ssl.minVersion = 'TLSv1';
this.config.ssl.ciphers = tls
.getCiphers()
.map(cipher => cipher.toUpperCase())
.join(':');
this.config.ssl.secureOptions =
crypto.constants.SSL_OP_NO_SSLv3 | crypto.constants.SSL_OP_NO_SSLv2;
delete this.config.ssl.allowHTTP1;
if (boolean(process.env.IS_NOT_SECURE)) this.config.ssl.secure = false;
else this.config.ssl.secure = true;
}
// Sender Rewriting Schema ("SRS")
this.srs = new SRS(this.config.srs);
// SMTP Server
this.config.smtp = {
...this.config.smtp,
...this.config.ssl
};
// initialize redis
const client = new Redis(
this.config.redis,
logger,
this.config.redisMonitor
);
// setup rate limiting with redis
if (this.config.rateLimit) {
this.limiter = {
db: client,
...this.config.rateLimit
};
}
// expose client
this.client = client;
// setup our smtp server which listens for incoming email
this.server = new SMTPServer(this.config.smtp);
// kind of hacky but I filed a GH issue
// <https://github.com/nodemailer/smtp-server/issues/135>
this.server.address = this.server.server.address.bind(this.server.server);
this.server.on('error', err => {
logger.error(err);
});
dns.setServers(this.config.dns);
this.listen = this.listen.bind(this);
this.close = this.close.bind(this);
this.processRecipient = this.processRecipient.bind(this);
this.processAddress = this.processAddress.bind(this);
this.sendEmail = this.sendEmail.bind(this);
this.parseUsername = this.parseUsername.bind(this);
this.parseFilter = this.parseFilter.bind(this);
this.parseDomain = this.parseDomain.bind(this);
this.onConnect = this.onConnect.bind(this);
this.checkBlacklists = this.checkBlacklists.bind(this);
this.onData = this.onData.bind(this);
this.validateSPF = this.validateSPF.bind(this);
this.validateDKIM = this.validateDKIM.bind(this);
this.validateMX = this.validateMX.bind(this);
this.validateRateLimit = this.validateRateLimit.bind(this);
this.isBlacklisted = this.isBlacklisted.bind(this);
this.isDisposable = this.isDisposable.bind(this);
this.onMailFrom = this.onMailFrom.bind(this);
this.getForwardingAddresses = this.getForwardingAddresses.bind(this);
this.onRcptTo = this.onRcptTo.bind(this);
}
async listen(port) {
await util.promisify(this.server.listen).bind(this.server)(
port || this.config.port
);
}
async close() {
await util.promisify(this.server.close).bind(this.server);
}
processRecipient(options) {
const { recipient, name, from, raw } = options;
const { address, addresses } = recipient;
return Promise.all(
addresses.map(({ to, host }) => {
return this.processAddress(address, {
host,
name,
envelope: {
from: this.srs.forward(from, this.parseDomain(to, false)),
to
},
raw
});
})
);
}
async processAddress(address, options) {
try {
const info = await this.sendEmail(options);
logger.log(info);
return info;
} catch (err) {
// here we do some magic so that we push an error message
// that has the end-recipient's email masked with the
// original to address that we were trying to send to
err.message = err.message.replace(
new RegExp(options.envelope.to, 'gi'),
address
);
logger.error(err);
return {
accepted: [],
rejected: [address],
rejectedErrors: [err]
};
}
}
// TODO: eventually we can combine multiple recipients
// that have the same MX records in the same envelope `to`
sendEmail(options) {
const { host, name, envelope, raw } = options;
const transporter = nodemailer.createTransport({
...transporterConfig,
...this.config.ssl,
host,
name
});
return transporter.sendMail({ envelope, raw });
}
parseUsername(address) {
({ address } = addressParser(address)[0]);
let username = address.includes('+')
? address.split('+')[0]
: address.split('@')[0];
username = punycode.toASCII(username).toLowerCase();
return username;
}
parseFilter(address) {
({ address } = addressParser(address)[0]);
return address.includes('+') ? address.split('+')[1].split('@')[0] : '';
}
parseDomain(address, isSender = true) {
let domain = addressParser(address)[0].address.split('@')[1];
domain = punycode.toASCII(domain);
// check against blacklist
if (this.isBlacklisted(domain))
throw new CustomError(
`The domain ${domain} is blacklisted by ${this.config.website}`
);
// ensure fully qualified domain name
/*
if (!validator.isFQDN(domain))
throw new CustomError(
`${domain} is not a fully qualified domain name ("FQDN")`
);
*/
// prevent disposable email addresses from being used
if (isSender && this.isDisposable(domain))
throw new CustomError(
`Disposable email address domain of ${domain} is not permitted`
);
return domain;
}
async checkBlacklists(ip) {
// if no blacklists are provided then return early
if (
!this.config.dnsbl ||
!this.config.dnsbl.domains ||
(Array.isArray(this.config.dnsbl.domains) &&
this.config.dnsbl.domains.length === 0)
) {
logger.warn('No DNS blacklists were provided');
return false;
}
if (Array.isArray(this.config.dnsbl.domains)) {
const results = await dnsbl.batch(ip, this.config.dnsbl.domains, {
servers: this.config.dns
});
if (!Array.isArray(results) || results.length === 0) return false;
const blacklistedResults = results.filter(result => result.listed);
if (blacklistedResults.length === 0) return false;
return blacklistedResults
.map(result =>
util.format(
this.config.blacklistedStr,
ip,
result.blacklist,
this.config.dnsbl.removals[
this.config.dnsbl.domains.indexOf(result.blacklist)
]
)
)
.join(' ');
}
const result = await dnsbl.lookup(ip, this.config.dnsbl.domains, {
servers: this.config.dns
});
if (!result) return false;
return util.format(
this.config.blacklistedStr,
ip,
this.config.dnsbl.domains,
this.config.dnsbl.removals
);
}
async onConnect(session, fn) {
if (env.NODE_ENV === 'test') return fn();
// TODO: implement stricter spam checking to alleviate this
/*
// ensure it's a fully qualififed domain name
if (!validator.isFQDN(session.clientHostname))
return fn(
new CustomError(
`${session.clientHostname} is not a fully qualified domain name ("FQDN")`
)
);
*/
try {
// check against blacklist
if (
validator.isFQDN(session.clientHostname) &&
this.isBlacklisted(session.clientHostname)
)
throw new CustomError(
`The domain ${session.clientHostname} is blacklisted by ${this.config.website}`
);
if (this.isBlacklisted(session.remoteAddress))
throw new CustomError(
`The IP address ${session.remoteAddress} is blacklisted by ${this.config.website}`
);
// ensure that it's not on the DNS blacklist
// Spamhaus = zen.spamhaus.org
// SpamCop = bl.spamcop.net
// Barracuda = b.barracudacentral.org
// Lashback = ubl.unsubscore.com
// PSBL = psbl.surriel.com
const message = await this.checkBlacklists(session.remoteAddress);
if (!message) return fn();
const err = new CustomError(message, 554);
logger.error(err);
fn(err);
} catch (err) {
logger.error(err);
fn();
}
}
async onData(stream, session, fn) {
//
// debugging
//
let originalRaw;
//
// store an object of email addresses that bounced
// with their associated error that occurred
//
const bounces = [];
//
// store an array of chunks of the message
//
const chunks = [];
//
// read the message headers and message itself
//
const messageSplitter = new MessageSplitter({
size: this.config.smtp.size
});
messageSplitter.on('readable', () => {
let chunk;
while ((chunk = messageSplitter.read()) !== null) {
chunks.push(chunk);
}
});
//
// if an error occurs we have to continue reading the stream
//
messageSplitter.once('error', err => {
stream.unpipe(messageSplitter);
stream.on('readable', () => {
stream.read();
});
stream.once('end', () => fn(err));
});
messageSplitter.once('end', async () => {
//
// we need to check the following:
//
// 1) X if email file size exceeds the limit (no bottleneck)
// 2) X ensure all email headers were parsed
// 3) X prevent replies to no-reply@forwardemail.net (no bottleneck)
// 4) X check for spam (score must be < 5) (child process spam daemon)
// 5) X if DKIM signature passed and was valid (child process python)
// 6) X if SPF is valid (child process python)
// 7) X send email
//
try {
//
// 1) if email file size exceeds the limit
//
if (stream.sizeExceeded)
throw new CustomError(
`Maximum allowed message size ${bytes(
this.config.smtp.size
)} exceeded`,
552
);
//
// 2) ensure all email headers were parsed
//
if (!messageSplitter.headersParsed)
throw new CustomError(
'Headers were unable to be parsed, please try again',
421
);
//
// 3) prevent replies to no-reply@forwardemail.net
//
if (
_.every(
session.envelope.rcptTo,
to => to.address === this.config.noReply
)
)
throw new CustomError(
oneLine`You need to reply to the "Reply-To" email address on the email; do not send messages to <${this.config.noReply}>`
);
//
// store variables for use later
//
// headers object (includes the \r\n\r\n header and body separator)
const { headers } = messageSplitter;
const originalFrom = headers.getFirst('from');
if (!originalFrom)
throw new CustomError(
'Your message is not RFC 5322 compliant, please include a valid "From" header.'
);
// message body as a single Buffer (everything after the \r\n\r\n separator)
originalRaw = Buffer.concat([headers.build(), ...chunks]);
//
// 4) check for spam (score must be < 5)
//
// TODO: we need to replace the spam block below with implementation
// // of `pdf-spamc-stream` from https://github.com/streamtOtO/spamc-stream
// // note that this package name is published with several key updates
//
// TODO: we may also want to add clamav for attachment scanning
let spamScore = 0;
try {
spamScore = await computeSpamScoreAsync(originalRaw);
} catch (err) {
logger.error(err);
}
if (spamScore >= this.config.spamScoreThreshold)
throw new CustomError(
`Message detected as spam (spam score of ${spamScore} exceeds threshold of ${this.config.spamScoreThreshold})`,
554
);
//
// 5) if DKIM signature passed and was valid
//
//
// if and only if there was a `dkim-signature` or `x-google-dkim-signature` header
//
const dkim =
headers.getFirst('dkim-signature') === ''
? true
: await this.validateDKIM(originalRaw);
if (!dkim)
throw new CustomError(
'Your email contained an invalid DKIM signature. For more information visit https://en.wikipedia.org/wiki/DomainKeys_Identified_Mail. You can also reach out to us for help analyzing this issue.'
);
// get the fully qualified domain name ("FQDN") of this server
let ipAddress;
if (env.NODE_ENV === 'test') {
const obj = await lookupAsync(this.config.exchanges[0]);
ipAddress = obj.address;
} else {
ipAddress = ip.address();
}
const name = await getFQDN(ipAddress);
//
// 6) if SPF is valid
//
const spf = await this.validateSPF(
env.NODE_ENV === 'test' ? ipAddress : session.remoteAddress,
session.envelope.mailFrom.address,
env.NODE_ENV === 'test' ? name : session.clientHostname
);
if (!['pass', 'neutral', 'none', 'softfail'].includes(spf))
throw new CustomError(
`The email you sent has failed SPF validation with a result of "${spf}"`
);
let recipients = await Promise.all(
session.envelope.rcptTo.map(async to => {
try {
// bounce message if it was sent to no-reply@
if (to.address === this.config.noReply)
throw new CustomError(
oneLine`You need to reply to the "Reply-To" email address on the email; do not send messages to <${this.config.noReply}>`
);
// get all forwarding addresses for this individual address
const addresses = await this.getForwardingAddresses(to.address);
if (addresses === false)
return { address: to.address, addresses: [], ignored: true };
return { address: to.address, addresses };
} catch (err) {
logger.error(err);
bounces.push({
address: to.address,
err
});
}
})
);
// flatten the recipients and make them unique
recipients = _.uniqBy(_.compact(_.flatten(recipients)), 'address');
// go through recipients and if we have a user+xyz@domain
// AND we also have user@domain then honor the user@domain only
// (helps to alleviate bulk spam with services like Gmail)
recipients = recipients.map(recipient => {
recipient.addresses = recipient.addresses.filter(address => {
if (!address.includes('+')) return true;
return !recipient.addresses.includes(
`${this.parseUsername(address)}@${this.parseDomain(
address,
false
)}`
);
});
return recipient;
});
recipients = await Promise.all(
recipients.map(async recipient => {
try {
const errors = [];
const { addresses } = recipient;
recipient.addresses = await Promise.all(
addresses.map(async address => {
try {
const addresses = await this.validateMX(address);
// `addresses` are already pre-sorted by lowest priority
return { to: address, host: addresses[0].exchange };
} catch (err) {
// e.g. if the MX servers don't exist for recipient
// then obviously there should be an error
logger.error(err);
errors.push({
address,
err
});
}
})
);
recipient.addresses = _.compact(recipient.addresses);
if (!_.isEmpty(recipient.addresses)) return recipient;
if (errors.length === 0) return recipient;
throw new Error(
errors.map(error => `${error.address}: ${error.err.message}`)
);
} catch (err) {
logger.error(err);
bounces.push({
address: recipient.address,
err
});
}
})
);
recipients = _.compact(recipients);
// if no recipients return early with bounces joined together
if (_.isEmpty(recipients)) {
if (_.isEmpty(bounces)) throw new CustomError('Invalid recipients');
throw new CustomError(
bounces
.map(
bounce =>
`Error for ${bounce.address} of "${bounce.err.message}"`
)
.join(', ')
);
}
// join headers object and body into a full rfc822 formatted email
// headers.build() compiles headers into a Buffer with the \r\n\r\n separator
// (eventually we call `dkim.sign(raw)` and pass it to nodemailer's `raw` option)
const raw = Buffer.concat([headers.build(), ...chunks]);
// set from address using SRS
const from = this.srs.forward(
session.envelope.mailFrom.address,
this.config.srsDomain
);
//
// 11) send email
//
try {
const accepted = [];
await Promise.all(
recipients.map(async recipient => {
// return early if recipient is ignored
if (recipient.ignored) return;
const results = await this.processRecipient({
recipient,
name,
from,
raw
});
for (const element of results) {
// TODO: a@a.com -> b@b.com + c@c.com when c@c.com fails
// it will still say a@a.com is successful
// but it will be confusing because the b@b.com will be
// masked to a@a.com and the end user will see that there
// was both a success and a failure for the same address
// (perhaps we indicate this user has email forwarded?)
if (element.accepted.length > 0)
accepted.push(recipient.address);
if (element.rejected.length === 0) continue;
for (let x = 0; x < element.rejected.length; x++) {
const err = element.rejectedErrors[x];
bounces.push({
address: recipient.address,
err
});
}
}
})
);
if (bounces.length === 0) return fn();
const codes = bounces.map(bounce => {
if (_.isNumber(bounce.err.responseCode))
return bounce.err.responseCode;
if (
_.isString(bounce.err.code) &&
RETRY_CODES.includes(bounce.err.code)
)
return CODES_TO_RESPONSE_CODES[bounce.err.code];
return 550;
});
// sort the codes and get the lowest one
// (that way 4xx retries are attempted)
const [code] = codes.sort();
const messages = [];
if (accepted.length > 0)
messages.push(
`Message was sent successfully to ${arrayJoinConjunction(
accepted
)}`
);
for (const element of bounces) {
messages.push(
`Error for ${element.address} of "${element.err.message}"`
);
}
// join the messages together and make them unique
const err = new CustomError(_.uniq(messages).join(', '), code);
logger.error(err);
fn(err);
} catch (err) {
stream.destroy(err);
}
} catch (err) {
stream.destroy(err);
}
});
stream.once('error', err => {
// parse SMTP code and message
if (err.message && err.message.startsWith('SMTP code:')) {
err.responseCode = err.message.split('SMTP code:')[1].split(' ')[0];
err.message = err.message.split('msg:')[1];
}
err.message += ` - if you need help please forward this email to ${this.config.email} or visit ${this.config.website}`;
const log = { session };
if (originalRaw) log.email = originalRaw.toString();
logger.error(err, log);
fn(err);
});
stream.pipe(messageSplitter);
}
//
// basically we have to check if the domain has an SPF record
// if it does, then we need to check if the sender's domain is included
//
// if any errors occur, we should respond with this:
// err.message = 'SPF validation error';
// err.responseCode = 451;
//
// however if it's something like a network error
// we should respond with a `421` code as we do below
//
async validateSPF(remoteAddress, from, clientHostname) {
try {
const [result, explanation] = await spfCheck2(
remoteAddress,
from,
clientHostname
);
if (['permerror', 'temperror'].includes(result))
throw new CustomError(
`SPF validation failed with result "${result}" and explanation "${explanation}"`
);
return result;
} catch (err) {
err.responseCode = 421;
throw err;
}
}
async validateDKIM(raw) {
try {
const pass = await dkimVerify(raw);
return pass;
} catch (err) {
logger.error(err);
err.message = `Your email contained an invalid DKIM signature. For more information visit https://en.wikipedia.org/wiki/DomainKeys_Identified_Mail. You can also reach out to us for help analyzing this issue. Original error message: ${err.message}`;
err.responseCode = 421;
throw err;
}
}
async validateMX(address) {
try {
const domain = this.parseDomain(address);
const addresses = await resolveMxAsync(domain);
if (!addresses || addresses.length === 0)
throw new CustomError(
`DNS lookup for ${domain} did not return any valid MX records`
);
return _.sortBy(addresses, 'priority');
} catch (err) {
if (/queryMx ENODATA/.test(err)) {
err.message = `DNS lookup for ${address} did not return a valid MX record`;
err.responseCode = 550;
} else if (/queryTxt ENOTFOUND/.test(err)) {
err.message = `DNS lookup for ${address} did not return a valid TXT record`;
err.responseCode = 550;
} else if (!err.responseCode) {
err.responseCode = 421;
}
throw err;
}
}
validateRateLimit(email) {
// if SPF TXT record exists for the domain name
// then ensure that `session.remoteAddress` resolves
// to either the IP address or the domain name value for the SPF
return new Promise((resolve, reject) => {
if (email === this.config.noReply || !this.limiter) return resolve();
const id = email;
const limit = new Limiter({ ...this.limiter, id });
limit.get((err, limit) => {
if (err) {
err.responseCode = 421;
return reject(err);
}
if (limit.remaining) {
logger.info(
`Rate limit for ${email} is now ${limit.remaining - 1}/${
limit.total
}`
);
return resolve();
}
const delta = (limit.reset * 1000 - Date.now()) | 0;
reject(
new CustomError(
`Rate limit exceeded for ${id}, retry in ${ms(delta, {
long: true
})}`,
451
)
);
});
});
}
isBlacklisted(domain) {
return Array.isArray(this.config.blacklist)
? this.config.blacklist.includes(domain)
: false;
}
isDisposable(domain) {
if (this.config.whitelistedDisposableDomains.includes(domain)) return false;
for (const d of domains) {
if (d === domain) return true;
}
for (const w of wildcards) {
if (w === domain || domain.endsWith(`.${w}`)) return true;
}
return false;
}
async onMailFrom(address, session, fn) {
try {
await Promise.all([
this.validateRateLimit(address.address),
this.validateMX(address.address)
]);
fn();
} catch (err) {
fn(err);
}
}
// this returns the forwarding address for a given email address
// eslint-disable-next-line complexity
async getForwardingAddresses(address, recursive = []) {
const domain = this.parseDomain(address, false);
const records = await resolveTxtAsync(domain);
// dns TXT record must contain `forward-email=` prefix
const validRecords = [];
// verifications must start with `forward-email-site-verification=` prefix
const verifications = [];
// add support for multi-line TXT records
for (let i = 0; i < records.length; i++) {
records[i] = records[i].join(''); // join chunks together
if (records[i].startsWith(`${this.config.recordPrefix}=`))
validRecords.push(
records[i].replace(`${this.config.recordPrefix}=`, '')
);
if (
records[i].startsWith(`${this.config.recordPrefix}-site-verification=`)
)
verifications.push(
records[i].replace(
`${this.config.recordPrefix}-site-verification=`,
''
)
);
}
if (verifications.length > 0) {
if (verifications.length > 1)
throw new CustomError(
`Domain ${domain} has multiple verification TXT records of "${this.config.recordPrefix}-site-verification" and should only have one`
);
// if there was a verification record then perform lookup
const { body } = await got.get(
`${this.config.lookupEndpoint}?verification_record=${verifications[0]}`,
{
responseType: 'json',
username: this.config.lookupSecrets[0]
}
);
// body is an Array of records that are formatted like TXT records
if (Array.isArray(body)) {
// combine with any existing TXT records (ensures graceful DNS propagation)
for (const element of body) {
validRecords.push(element);
}
}
}
// join multi-line TXT records together and replace double w/single commas
const record = validRecords
.join(',')
.replace(/,+/g, ',')
.trim();
// if the record was blank then throw an error
if (!isSANB(record))
throw new CustomError(
`${address} domain of ${domain} has a blank "${this.config.recordPrefix}" TXT record or has zero aliases configured`
);
// e.g. hello@niftylettuce.com => niftylettuce@gmail.com
// record = "forward-email=hello:niftylettuce@gmail.com"
// e.g. hello+test@niftylettuce.com => niftylettuce+test@gmail.com
// record = "forward-email=hello:niftylettuce@gmail.com"
// e.g. *@niftylettuce.com => niftylettuce@gmail.com
// record = "forward-email=niftylettuce@gmail.com"
// e.g. *+test@niftylettuce.com => niftylettuce@gmail.com
// record = "forward-email=niftylettuce@gmail.com"
// remove trailing whitespaces from each address listed
const addresses = record.split(',').map(a => a.trim());
if (addresses.length === 0)
throw new CustomError(
`${address} domain of ${domain} has zero forwarded addresses configured in the TXT record with "${this.config.recordPrefix}"`
);
// store if address is ignored or not