forked from forwardemail/free-email-forwarding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
623 lines (547 loc) · 19.3 KB
/
index.js
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
const fs = require('fs');
const { promisify } = require('util');
const path = require('path');
const dns = require('dns');
const autoBind = require('auto-bind');
const { SMTPServer } = require('smtp-server');
const bytes = require('bytes');
const { MailParser } = require('mailparser');
const nodemailer = require('nodemailer');
const redis = require('redis');
const Limiter = require('ratelimiter');
const ms = require('ms');
const domains = require('disposable-email-domains');
const wildcards = require('disposable-email-domains/wildcard.json');
const validator = require('validator');
const bluebird = require('bluebird');
const isObject = require('lodash/isObject');
const isString = require('lodash/isString');
const uniq = require('lodash/uniq');
const addressParser = require('nodemailer/lib/addressparser');
let mailUtilities = require('mailin/lib/mailUtilities.js');
mailUtilities = bluebird.promisifyAll(mailUtilities);
const invalidTXTError = new Error('Invalid forward-email TXT record');
invalidTXTError.responseCode = 550;
const invalidMXError = new Error('Sender has invalid MX records');
invalidMXError.responseCode = 550;
const headers = [
'subject',
'references',
'date',
'to',
'from',
'to',
'cc',
'bcc',
'message-id',
'in-reply-to',
'reply-to'
];
const log = process.env.NODE_ENV !== 'production';
class ForwardEmail {
constructor(config = {}) {
config = Object.assign(
{
smtp: {},
limiter: {},
exchanges: ['mx1.forwardemail.net', 'mx2.forwardemail.net']
},
config
);
const ssl = {};
if (process.env.NODE_ENV === 'production') {
ssl.secure = process.env.SECURE === 'true';
// ssl.needsUpgrade = true;
ssl.key = fs.readFileSync(
'/home/deploy/mx1.forwardemail.net.key',
'utf8'
);
ssl.cert = fs.readFileSync(
'/home/deploy/mx1.forwardemail.net.cert',
'utf8'
);
ssl.ca = fs.readFileSync('/home/deploy/mx1.forwardemail.net.ca', 'utf8');
}
this.ssl = ssl;
this.config = {
smtp: Object.assign(
{
size: bytes('25mb'),
onConnect: this.onConnect.bind(this),
onData: this.onData.bind(this),
onMailFrom: this.onMailFrom.bind(this),
onRcptTo: this.onRcptTo.bind(this),
disabledCommands: ['AUTH'],
...ssl,
logInfo: log,
logger: log
},
config.smtp
),
limiter: Object.assign({}, config.limiter),
exchanges: config.exchanges
};
// setup rate limiting with redis
this.limiter = Object.assign(
{
db: redis.createClient(),
max: 100, // max requests within duration
duration: ms('1h')
},
this.config.limiter
);
// setup our smtp server which listens for incoming email
this.server = new SMTPServer(this.config.smtp);
autoBind(this);
}
parseUsername(address) {
address = addressParser(address)[0].address;
return address.indexOf('+') === -1
? address.split('@')[0]
: address.split('+')[0];
}
parseFilter(address) {
address = addressParser(address)[0].address;
return address.indexOf('+') === -1
? ''
: address.split('+')[1].split('@')[0];
}
parseDomain(address) {
const domain = addressParser(address)[0].address.split('@')[1];
// ensure fully qualified domain name
if (!validator.isFQDN(domain)) {
const err = new Error(`${domain} is not a FQDN`);
err.responseCode = 550;
throw err;
}
// prevent disposable email addresses from being used
if (this.isDisposable(domain)) {
const err = new Error('Disposable email addresses are not permitted');
err.responseCode = 550;
throw err;
}
return domain;
}
onConnect(session, fn) {
// TODO: this needs tested in production
// or we need to come up with a better way to do this
if (process.env.NODE_ENV === 'test') return fn();
if (validator.isFQDN(session.clientHostname)) return fn();
const err = new Error(`${session.clientHostname} is not a FQDN`);
err.responseCode = 550;
fn(err);
}
async onData(stream, session, fn) {
// <https://github.com/nodemailer/mailparser/blob/master/examples/pipe.js>
const parser = new MailParser();
const mail = { attachments: [] };
let rawEmail = '';
stream.on('error', fn);
parser.on('error', err => {
stream.emit('error', err);
parser.end();
});
parser.on('end', async () => {
try {
headers.forEach(key => {
if (mail.headers.has(key)) {
const formatted = key.replace(/-([a-z])/g, (m, c) =>
c.toUpperCase()
);
mail[formatted] = mail.headers.get(key);
mail.headers.delete(key);
if (['to', 'from', 'cc', 'bcc'].includes(key)) {
mail[formatted] = mail[formatted].text;
}
}
});
session.envelope.rcptTo = await Promise.all(
session.envelope.rcptTo.map(to => {
return new Promise(async (resolve, reject) => {
try {
const address = await this.getForwardingAddress(to.address);
resolve({
...to,
address
});
} catch (err) {
reject(err);
}
});
})
);
session.envelope = {
from: session.envelope.mailFrom.address,
// make sure it's unique so we don't send dups
to: uniq(session.envelope.rcptTo.map(to => to.address))
};
mail.headers = Array.from(mail.headers).reduce((obj, [key, value]) => {
if (isObject(value)) {
if (isString(value.value)) obj[key] = value.value;
if (isObject(value.params))
Object.keys(value.params).forEach(k => {
obj[key] += `; ${k}=${value.params[k]}`;
});
} else {
obj[key] = value;
}
return obj;
}, {});
const obj = {
...mail
// envelope: session.envelope
};
console.dir(obj);
// TODO: not sure if we need to change this
// obj.to = await this.getForwardingAddress(obj.to);
const spf = await this.validateSPF(
session.remoteAddress,
mail.from,
session.clientHostname
);
const dkim = await this.validateDKIM(rawEmail);
// basically if there was no valid SPF record found for this sender
// AND if there was no valid DKIM signature on the message
// then we must refuse sending this email along because it
// literally has on validation that it's from who it says its from
if (!spf && !dkim) {
const err = new Error('No passing DKIM signature found');
err.responseCode = 550;
throw err;
}
// check against spamd if this message is spam
// <https://github.com/humantech/node-spamd#usage>
const spamScore = await mailUtilities.computeSpamScoreAsync(rawEmail);
if (spamScore >= 5) {
// TODO: blacklist IP address
const err = new Error('Message detected as spam');
err.responseCode = 554;
throw err;
}
// TODO: implement spamassassin automatic learning
// through bayes based off response from proxy (e.g. gmail response)
// (if spam errors occur, we need 550 error code)
// and we also might want to add clamav
// for attachment scanning to prevent those from going through as well
await Promise.all(
session.envelope.to.map(to => {
return new Promise(async (resolve, reject) => {
try {
// TODO: pick lowest priority address found
const addresses = await this.validateMX(to);
const transporter = nodemailer.createTransport({
debug: log,
logger: log,
direct: true,
// secure: true,
// requireTLS: true,
opportunisticTLS: true,
port: 25,
host: addresses[0].exchange,
...this.ssl,
name: 'mx1.forwardemail.net',
tls: {
rejectUnauthorized: process.env.NODE_ENV !== 'test'
}
// <https://github.com/nodemailer/nodemailer/issues/625>
});
const dkim = {};
if (process.env.NODE_ENV === 'production') {
dkim.domainName = 'forwardemail.net';
dkim.keySelector = 'default';
dkim.privateKey = fs.readFileSync(
'/home/deploy/dkim-private.key',
'utf8'
);
} else if (process.env.NODE_ENV === 'test') {
dkim.domainName = 'forwardemail.net';
dkim.keySelector = 'default';
dkim.privateKey = fs.readFileSync(
path.join(__dirname, 'dkim-private.key'),
'utf8'
);
}
const email = {
...obj,
envelope: session.envelope,
dkim
};
delete email.messageId;
delete email.headers['mime-version'];
delete email.headers['content-type'];
delete email.headers['dkim-signature'];
delete email.headers['x-google-dkim-signature'];
delete email.headers['x-gm-message-state'];
delete email.headers['x-google-smtp-source'];
delete email.headers['x-received'];
const info = await transporter.sendMail(email);
resolve(info);
} catch (err) {
reject(err);
}
});
})
);
fn();
} catch (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];
// TODO: we need to use bayes auto learning here
// to tell spam assassin that this email in particular failed
// (IFF as it was sent to a gmail, yahoo, or other major provider)
}
fn(err);
}
});
stream.on('data', chunk => {
rawEmail += chunk;
});
stream.on('end', () => {
if (stream.sizeExceeded) {
const err = new Error(
`Message size exceeds maximum of ${bytes(this.config.smtp.size)}`
);
err.responseCode = 450;
parser.emit('error', err);
}
});
parser.on('headers', headers => {
mail.headers = headers;
});
parser.on('data', data => {
if (data.type === 'text') {
Object.keys(data).forEach(key => {
if (['text', 'html', 'textAsHtml'].includes(key)) {
mail[key] = data[key];
}
});
}
if (data.type === 'attachment') {
const chunks = [];
let chunklen = 0;
mail.attachments.push(data);
data.content.on('readable', () => {
let chunk;
while ((chunk = data.content.read()) !== null) {
chunks.push(chunk);
chunklen += chunk.length;
}
});
data.content.on('end', () => {
data.content = Buffer.concat(chunks, chunklen);
data.release();
});
}
});
stream.pipe(parser);
}
// TODO: eBay/PayPal/Google cannot be forwarded so we need alt. solution
// or maybe there's a way we can get them to whitelist our server
//
// TODO: we need to add Google Structured Data and then submit whitelist req
//
// 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
//
// here's some code for reference, not sure if it's useful
// <https://github.com/mixmaxhq/spf-validator/blob/master/index.js>
// <https://github.com/Flolagale/mailin/blob/fac7dcf59404691e551568f987caaaa464303b6b/lib/mailUtilities.js#L49>
// const { spfSetup, hasSPFSender } = require('email-setup');
// const isSpfSetup = await spfSetup(domain);
// const isSpfSender = await hasSPFSender('foo.com', '_spf.google.com');
// if (!isSetup)
//
validateSPF(remoteAddress, from, clientHostname) {
// <https://github.com/Flolagale/mailin/blob/master/lib/mailin.js#L265>
return new Promise(async (resolve, reject) => {
try {
const pass = await mailUtilities.validateSpfAsync(
remoteAddress,
from,
clientHostname
);
resolve(pass);
} catch (err) {
err.responseCode = 421;
reject(err);
}
});
}
validateMX(address) {
return new Promise(async (resolve, reject) => {
try {
const domain = this.parseDomain(address);
const addresses = await promisify(dns.resolveMx)(domain);
if (!addresses || addresses.length === 0) throw invalidMXError;
resolve(addresses);
} catch (err) {
if (/queryMx ENODATA/.test(err) || /queryTxt ENOTFOUND/.test(err)) {
err.message = invalidMXError.message;
err.responseCode = invalidMXError.responseCode;
} else if (!err.responseCode) {
err.responseCode = 421;
}
reject(err);
}
});
}
validateDKIM(rawEmail) {
return new Promise(async (resolve, reject) => {
try {
// <https://github.com/jhermsmeier/node-dkim/blob/master/test/verify.js#L35>
// const records = await promisify(dkim.verify)(Buffer.from(rawEmail));
// const pass =
// records.length > 0 && records.every(record => record.verified);
// resolve(pass);
const pass = await mailUtilities.validateDkimAsync(rawEmail);
resolve(pass);
} catch (err) {
err.responseCode = 421;
reject(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) => {
const id = email;
const limit = new Limiter({ id, ...this.limiter });
limit.get((err, limit) => {
if (err) {
err.responseCode = 421;
return reject(err);
}
if (limit.remaining) return resolve();
const delta = (limit.reset * 1000 - Date.now()) | 0;
err = new Error(
`Rate limit exceeded, retry in ${ms(delta, { long: true })}`
);
err.responseCode = 451;
reject(err);
});
});
}
isDisposable(domain) {
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 this.validateRateLimit(address.address);
await this.validateMX(address.address);
fn();
} catch (err) {
fn(err);
}
}
// this returns the forwarding address for a given email address
getForwardingAddress(address) {
return new Promise(async (resolve, reject) => {
try {
const domain = this.parseDomain(address);
const records = await promisify(dns.resolveTxt)(domain);
// dns TXT record must contain `forward-email=` prefix
let record;
for (let i = 0; i < records.length; i++) {
records[i] = records[i].join(''); // join chunks together
if (records[i].startsWith('forward-email=')) {
record = records[i];
break;
}
}
if (!record) throw invalidTXTError;
// 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"
record = record.replace('forward-email=', '');
// remove trailing whitespaces from each address listed
const addresses = record.split(',').map(a => a.trim());
if (addresses.length === 0) throw invalidTXTError;
// store if we have a forwarding address or not
let forwardingAddress;
// check if we have a global redirect
if (
addresses[0].indexOf(':') === -1 &&
validator.isFQDN(this.parseDomain(addresses[0])) &&
validator.isEmail(addresses[0])
)
forwardingAddress = addresses[0];
// check if we have a specific redirect
if (!forwardingAddress) {
// get username from recipient email address
// (e.g. hello@niftylettuce.com => hello)
const username = this.parseUsername(address);
for (let i = 0; i < addresses.length; i++) {
const address = addresses[i].split(':');
if (address.length !== 2) throw invalidTXTError;
// address[0] = hello (username)
// address[1] = niftylettuce@gmail.com (forwarding email)
// check if we have a match
if (username === address[0]) {
forwardingAddress = address[1];
break;
}
}
}
// if we don't have a forwarding address then throw an error
if (!forwardingAddress) throw invalidTXTError;
// otherwise transform the + symbol filter if we had it
// and then resolve with the newly formatted forwarding address
if (address.indexOf('+') === -1) return resolve(forwardingAddress);
resolve(
`${this.parseUsername(forwardingAddress)}+${this.parseFilter(
address
)}@${this.parseDomain(forwardingAddress)}`
);
} catch (err) {
reject(err);
}
});
}
async onRcptTo(address, session, fn) {
try {
// validate forwarding address by looking up TXT record `forward-email=`
await this.getForwardingAddress(address.address);
// validate MX records exist and contain ours
const addresses = await this.validateMX(address.address);
const exchanges = addresses.map(mxAddress => mxAddress.exchange);
const hasAllExchanges = this.config.exchanges.every(exchange =>
exchanges.includes(exchange)
);
if (hasAllExchanges) return fn();
const err = new Error(
`Missing required DNS MX records: ${this.config.exchanges.join(', ')}`
);
err.responseCode = 550;
throw err;
} catch (err) {
fn(err);
}
}
}
if (!module.parent) {
const forwardEmail = new ForwardEmail();
forwardEmail.server.listen(process.env.PORT || 25);
}
module.exports = ForwardEmail;