@@ -3,17 +3,15 @@ const dns = require('dns');
3
3
const fs = require ( 'fs' ) ;
4
4
const util = require ( 'util' ) ;
5
5
6
- // const SpamScanner = require('spamscanner');
7
-
8
6
const DKIM = require ( 'nodemailer/lib/dkim' ) ;
9
7
const Limiter = require ( 'ratelimiter' ) ;
10
8
const MimeNode = require ( 'nodemailer/lib/mime-node' ) ;
11
9
const RE2 = require ( 're2' ) ;
12
10
const Redis = require ( '@ladjs/redis' ) ;
11
+ const SpamScanner = require ( 'spamscanner' ) ;
13
12
const _ = require ( 'lodash' ) ;
14
13
const addressParser = require ( 'nodemailer/lib/addressparser' ) ;
15
14
const arrayJoinConjunction = require ( 'array-join-conjunction' ) ;
16
- const zoneMTABounces = require ( 'zone-mta/lib/bounces' ) ;
17
15
const bytes = require ( 'bytes' ) ;
18
16
const dnsbl = require ( 'dnsbl' ) ;
19
17
const getFQDN = require ( 'get-fqdn' ) ;
@@ -31,6 +29,7 @@ const sharedConfig = require('@ladjs/shared-config');
31
29
const splitLines = require ( 'split-lines' ) ;
32
30
const superagent = require ( 'superagent' ) ;
33
31
const validator = require ( 'validator' ) ;
32
+ const zoneMTABounces = require ( 'zone-mta/lib/bounces' ) ;
34
33
const { Iconv } = require ( 'iconv' ) ;
35
34
const { SMTPServer } = require ( 'smtp-server' ) ;
36
35
const { SRS } = require ( 'sender-rewriting-scheme' ) ;
@@ -257,7 +256,7 @@ class ForwardEmail {
257
256
} ,
258
257
sendingZone : 'bounces' ,
259
258
userAgent : `${ pkg . name } /${ pkg . version } ` ,
260
- // spamScanner: {},
259
+ spamScanner : { } ,
261
260
ttlMs : ms ( '7d' ) ,
262
261
maxRetry : 5 ,
263
262
messageIdDomain : env . MESSAGE_ID_DOMAIN ,
@@ -341,7 +340,7 @@ class ForwardEmail {
341
340
} ) ;
342
341
343
342
// expose spamscanner
344
- // this.scanner = new SpamScanner(this.config.spamScanner);
343
+ this . scanner = new SpamScanner ( this . config . spamScanner ) ;
345
344
346
345
this . listen = this . listen . bind ( this ) ;
347
346
this . close = this . close . bind ( this ) ;
@@ -958,7 +957,7 @@ class ForwardEmail {
958
957
// 2) X ensure all email headers were parsed
959
958
// 3) X reverse SRS bounces
960
959
// 4) X prevent replies to [email protected] (no bottleneck)
961
- // 5) O check for spam
960
+ // 5) X check for spam
962
961
// 6) X validate SPF, DKIM, DMARC, and ARC
963
962
// 7) X lookup forwarding recipients recursively
964
963
// 8) X normalize recipients by host and without "+" symbols
@@ -1065,52 +1064,49 @@ class ForwardEmail {
1065
1064
//
1066
1065
// 5) check for spam
1067
1066
//
1068
- /*
1069
- let results;
1067
+ let scan ;
1070
1068
try {
1071
- results = await this.scanner.scan(originalRaw);
1069
+ scan = await this . scanner . scan ( originalRaw ) ;
1070
+ if ( scan . is_spam )
1071
+ this . config . logger . fatal ( `spam detected: ${ JSON . stringify ( scan . results ) } ` ) ;
1072
1072
} catch ( err ) {
1073
- logger.error (err);
1073
+ this . config . logger . fatal ( err ) ;
1074
1074
}
1075
1075
1076
- if (results) {
1077
- if (results.is_spam)
1078
- logger.error('spam detected', {
1079
- originalRaw: originalRaw.toString(),
1080
- results
1081
- });
1082
-
1076
+ if ( _ . isObject ( scan ) && _ . isObject ( scan . results ) ) {
1083
1077
//
1084
1078
// NOTE: until we are confident with the accuracy
1085
1079
// we are not utilizing classification right now
1086
1080
// however we still want to use other detections
1087
1081
//
1088
1082
const messages = [ ] ;
1089
1083
1090
- if (
1091
- _.isObject(results.phishing) &&
1092
- _.isArray(results.phishing.messages)
1093
- )
1094
- for (const message of results.phishing.messages) {
1084
+ if ( _ . isArray ( scan . results . phishing ) )
1085
+ for ( const message of scan . results . phishing ) {
1086
+ messages . push ( message ) ;
1087
+ }
1088
+
1089
+ if ( _ . isArray ( scan . results . executables ) ) {
1090
+ for ( const message of scan . results . executables ) {
1095
1091
messages . push ( message ) ;
1096
1092
}
1093
+ }
1097
1094
1098
- if (_.isArray(results.executables )) {
1099
- for (const message of results.executables ) {
1095
+ if ( _ . isArray ( scan . results . arbitrary ) ) {
1096
+ for ( const message of scan . results . arbitrary ) {
1100
1097
messages . push ( message ) ;
1101
1098
}
1102
1099
}
1103
1100
1104
- if (_.isArray(results.arbitrary )) {
1105
- for (const message of results.arbitrary ) {
1101
+ if ( _ . isArray ( scan . results . viruses ) ) {
1102
+ for ( const message of scan . results . viruses ) {
1106
1103
messages . push ( message ) ;
1107
1104
}
1108
1105
}
1109
1106
1110
1107
if ( messages . length > 0 )
1111
1108
throw new CustomError ( messages . join ( ' ' ) , 554 ) ;
1112
1109
}
1113
- */
1114
1110
1115
1111
//
1116
1112
// 6) validate SPF, DKIM, DMARC, and ARC
@@ -1820,7 +1816,7 @@ class ForwardEmail {
1820
1816
err . message = err . message . split ( 'msg:' ) [ 1 ] ;
1821
1817
}
1822
1818
1823
- err . message += ` If you need help please forward this email to ${ this . config . email } or visit ${ this . config . website } ` ;
1819
+ err . message += ` If you need help please forward this email to ${ this . config . email } or visit ${ this . config . website } . ` ;
1824
1820
fn ( err ) ;
1825
1821
} ) ;
1826
1822
@@ -1976,6 +1972,7 @@ class ForwardEmail {
1976
1972
if ( verifications . length > 0 ) {
1977
1973
if ( verifications . length > 1 )
1978
1974
throw new CustomError (
1975
+ // TODO: we may want to replace this with "Invalid Recipients"
1979
1976
`Domain ${ domain } has multiple verification TXT records of "${ this . config . recordPrefix } -site-verification" and should only have one`
1980
1977
) ;
1981
1978
// if there was a verification record then perform lookup
@@ -2007,6 +2004,7 @@ class ForwardEmail {
2007
2004
// if the record was blank then throw an error
2008
2005
if ( ! isSANB ( record ) )
2009
2006
throw new CustomError (
2007
+ // TODO: we may want to replace this with "Invalid Recipients"
2010
2008
`${ address } domain of ${ domain } has a blank "${ this . config . recordPrefix } " TXT record or has zero aliases configured` ,
2011
2009
420
2012
2010
) ;
@@ -2025,6 +2023,7 @@ class ForwardEmail {
2025
2023
2026
2024
if ( addresses . length === 0 )
2027
2025
throw new CustomError (
2026
+ // TODO: we may want to replace this with "Invalid Recipients"
2028
2027
`${ address } domain of ${ domain } has zero forwarded addresses configured in the TXT record with "${ this . config . recordPrefix } "` ,
2029
2028
420
2030
2029
) ;
@@ -2083,6 +2082,7 @@ class ForwardEmail {
2083
2082
! validator . isURL ( addr [ 1 ] , this . config . isURLOptions ) )
2084
2083
)
2085
2084
throw new CustomError (
2085
+ // TODO: we may want to replace this with "Invalid Recipients"
2086
2086
`${ lowerCaseAddress } domain of ${ domain } has an invalid "${ this . config . recordPrefix } " TXT record due to an invalid email address of "${ element } "`
2087
2087
) ;
2088
2088
0 commit comments