Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed timeout issue #2

Merged
merged 8 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion config/mail_from.is_resolvable.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
timeout=29
allow_mx_ip=false
re_bogus_ip=^(?:0\.0\.0\.0|255\.255\.255\.255|127\.)

Expand Down
189 changes: 73 additions & 116 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,144 +1,101 @@
'use strict'
// Check MAIL FROM domain is resolvable to an MX

// Check MAIL FROM domain is resolvable to an MX
const net = require('node:net')

const net_utils = require('haraka-net-utils')

exports.register = function () {
this.load_ini()
this.load_ini()
}

exports.load_ini = function () {
this.cfg = this.config.get(
'mail_from.is_resolvable.ini',
{
booleans: ['-main.allow_mx_ip', '+reject.no_mx'],
},
() => {
this.load_ini()
},
)

// compat. Sunset 4.0
if (this.cfg.main.reject_no_mx) {
this.cfg.reject.no_mx = this.cfg.main.reject_no_mx
}

if (isNaN(this.cfg.main.timeout)) {
this.cfg.main.timeout = 29
}
this.cfg = this.config.get('mail_from.is_resolvable.ini', {
booleans: [
'-main.allow_mx_ip',
'+reject.no_mx',
],
}, () => {
this.load_ini()
})

if (this.timeout) {
if (this.timeout <= this.cfg.main.timeout) {
this.cfg.main.timeout = this.timeout - 1
this.logwarn(`reducing plugin timeout to ${this.cfg.main.timeout}s`)
// compat. Sunset 4.0
if (this.cfg.main.reject_no_mx) {
this.cfg.reject.no_mx = this.cfg.main.reject_no_mx
}
}

this.re_bogus_ip = new RegExp(
this.cfg.main.re_bogus_ip ||
'^(?:0\\.0\\.0\\.0|255\\.255\\.255\\.255|127\\.)',
)
this.re_bogus_ip = new RegExp(this.cfg.main.re_bogus_ip ||
'^(?:0\\.0\\.0\\.0|255\\.255\\.255\\.255|127\\.)' )
}

exports.hook_mail = function (next, connection, params) {
const plugin = this
const mail_from = params[0]
const txn = connection?.transaction
if (!txn) return next()
const { results } = txn

// ignore MAIL FROM without an @
if (!mail_from.host) {
results.add(plugin, { skip: 'null host' })
return next()
}

let called_next = 0
const domain = mail_from.host
const timeout_id = setTimeout(() => {
connection.logdebug(plugin, `DNS timeout resolving MX for ${domain}`)
called_next++
if (txn) results.add(plugin, { err: `timeout(${domain})` })
next(DENYSOFT, 'Temporary resolver error (timeout)')
}, this.cfg.main.timeout * 1000)
exports.hook_mail = async function (next, connection, params) {
const mail_from = params[0]
const {results} = connection.transaction

function mxDone(code, reply) {
if (called_next) return
clearTimeout(timeout_id)
called_next++
next(...arguments)
}
// ignore MAIL FROM without an @
if (!mail_from.host) {
results.add(this, {skip: 'null host'})
return next()
}

function mxErr(err) {
if (!connection.transaction) return
results.add(plugin, { err: `${domain}:${err.message}` })
mxDone(DENYSOFT, `Temp. resolver error (${err.code})`)
}
const domain = mail_from.host

connection.logdebug(plugin, `resolving MX for domain ${domain}`)
connection.logdebug(this, `resolving MX for domain ${domain}`)

net_utils
.get_mx(domain)
.then((exchanges) => {
if (!txn) return
let exchanges
try {
exchanges = await net_utils.get_mx(domain)
}
catch (err) {
results.add(this, {err: err.message})
return next(DENYSOFT, `Temp. resolver error (${err.code})`)
}

connection.logdebug(
plugin,
`${domain}: MX => ${JSON.stringify(exchanges)}`,
)
connection.logdebug(this, `${domain}: MX => ${JSON.stringify(exchanges)}`)

if (!exchanges || !exchanges.length) {
results.add(this, { fail: 'has_fwd_dns' })
return mxDone(
this.cfg.reject.no_mx ? DENY : DENYSOFT,
'No MX for your FROM address',
if (!exchanges || !exchanges.length) {
results.add(this, {fail: 'has_fwd_dns', emit: true})
return next(
((this.cfg.reject.no_mx) ? DENY : DENYSOFT),
'No MX for your FROM address'
)
}
}

if (this.cfg.main.allow_mx_ip) {
if (this.cfg.main.allow_mx_ip) {
for (const mx of exchanges) {
if (net.isIPv4(mx.exchange) && !this.re_bogus_ip.test(mx.exchange)) {
txn.results.add(this, { pass: 'implicit_mx', emit: true })
return mxDone()
}
if (net.isIPv6(mx.exchange) && !net_utils.ipv6_bogus(mx.exchange)) {
txn.results.add(this, { pass: 'implicit_mx', emit: true })
return mxDone()
}
if ((net.isIPv4(mx.exchange) && !this.re_bogus_ip.test(mx.exchange)) ||
(net.isIPv6(mx.exchange) && !net_utils.ipv6_bogus(mx.exchange))) {
results.add(this, {pass: 'implicit_mx', emit: true})
return next()
}
}
}

// filter out the implicit MX and resolve the MX hostnames
net_utils
.resolve_mx_hosts(exchanges.filter((a) => !net.isIP(a.exchange)))
.then((resolved) => {
connection.logdebug(
plugin,
`resolved MX => ${JSON.stringify(resolved)}`,
)
}

for (const mx of resolved) {
if (
net.isIPv4(mx.exchange) &&
!this.re_bogus_ip.test(mx.exchange)
) {
txn.results.add(this, { pass: 'has_fwd_dns', emit: true })
return mxDone()
// filter out the implicit MX and resolve the remaining MX hostnames
const mx_hostnames = exchanges.filter(a => (a.exchange && !net.isIP(a.exchange)))
if (mx_hostnames.length) {
try {
const resolved = await net_utils.resolve_mx_hosts(mx_hostnames)
connection.logdebug(this, `resolved MX => ${JSON.stringify(resolved)}`)
if (resolved.length) {
for (const mx of resolved) {
if ((net.isIPv4(mx.exchange) && !this.re_bogus_ip.test(mx.exchange)) ||
(net.isIPv6(mx.exchange) && !net_utils.ipv6_bogus(mx.exchange))) {
results.add(this, {pass: 'has_fwd_dns', emit: true})
return next()
}
}
}
if (net.isIPv6(mx.exchange) && !net_utils.ipv6_bogus(mx.exchange)) {
txn.results.add(this, { pass: 'has_fwd_dns', emit: true })
return mxDone()
}
}
}
catch (err) {
// resolve_mx_hosts ignores errors so this is unlikely to happen
results.add(this, {err: err.message})
return next(DENYSOFT, `Temp. resolver error (${err.code})`)
}
}

mxDone(
this.cfg.reject.no_mx ? DENY : DENYSOFT,
'No valid MX for your FROM address',
)
})
.catch(mxErr)
})
.catch(mxErr)
results.add(this, {fail: 'has_fwd_dns', emit: true})
return next(
((this.cfg.reject.no_mx) ? DENY : DENYSOFT),
'No valid MX for your FROM address'
)
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"lint:fix": "npx eslint *.js test --fix",
"prettier": "npx prettier . --check",
"prettier:fix": "npx prettier . --write --log-level=warn",
"test": "node --test",
"test": "npx mocha@^11 test",
"versions": "npx dependency-version-checker check",
"versions:fix": "npx dependency-version-checker update"
},
Expand All @@ -34,7 +34,8 @@
"devDependencies": {
"@haraka/eslint-config": "^2.0.2",
"address-rfc2821": "^2.1.2",
"haraka-test-fixtures": "^1.3.8"
"haraka-test-fixtures": "^1.3.8",
"sinon": "^19.0.2"
},
"prettier": {
"singleQuote": true,
Expand Down
Loading
Loading