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 time out issue #3436

Closed
wants to merge 6 commits into from
Closed
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@
"haraka-test-fixtures": "^1.3.8",
"mocha": "^11.1.0",
"mock-require": "^3.0.3",
"nodemailer": "^6.9.16"
"nodemailer": "^6.9.16",
"sinon": "^19.0.2"
},
"bugs": {
"mail": "haraka.mail@gmail.com",
Expand Down
155 changes: 63 additions & 92 deletions plugins/mail_from.is_resolvable.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';
'use strict'

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

const net_utils = require('haraka-net-utils');
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 () {
Expand All @@ -16,117 +15,89 @@ exports.load_ini = function () {
'+reject.no_mx',
],
}, () => {
this.load_ini();
});
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;
}

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`);
}
}

this.re_bogus_ip = new RegExp(this.cfg.main.re_bogus_ip ||
'^(?:0\\.0\\.0\\.0|255\\.255\\.255\\.255|127\\.)' );
'^(?: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;
exports.hook_mail = async function (next, connection, params) {
const mail_from = params[0]
const {results} = connection.transaction

// ignore MAIL FROM without an @
if (!mail_from.host) {
results.add(plugin, {skip: 'null host'});
return next();
results.add(this, {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);
const domain = mail_from.host

function mxDone (code, reply) {
if (called_next) return;
clearTimeout(timeout_id);
called_next++;
next(...arguments);
}
connection.logdebug(this, `resolving MX for domain ${domain}`)

function mxErr (err) {
if (!connection.transaction) return;
results.add(plugin, {err: `${domain}:${err.message}`});
mxDone(DENYSOFT, `Temp. resolver error (${err.code})`);
let exchanges
try {
exchanges = await net_utils.get_mx(domain)
}
catch (err) {
connection.logwarn(this, `Temporary resolver error: ${err.code}`)
results.add(this, {err: `${domain}:${err.message}`})
return next(DENYSOFT, `Temp. resolver error (${err.code})`)
}

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

net_utils
.get_mx(domain)
.then((exchanges) => {
if (!txn) return;
connection.logdebug(this, `${domain}: MX => ${JSON.stringify(exchanges)}`)

connection.logdebug(plugin, `${domain}: MX => ${JSON.stringify(exchanges)}`)
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 (!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 (this.cfg.main.allow_mx_ip) {
for (const mx of exchanges) {
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()
}
}
}

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()
// 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()
}
}
}
}
catch (err) {
// resolve_mx_hosts ignores errors so this is unlikely to happen
connection.logwarn(this, `Temp. resolver error: ${err.code}`)
results.add(this, {err: `${domain}:${err.message}`})
return next(DENYSOFT, `Temp. resolver error (${err.code})`)
}
}

// 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()
}
if (net.isIPv6(mx.exchange) && !net_utils.ipv6_bogus(mx.exchange)) {
txn.results.add(this, {pass: 'has_fwd_dns', emit: true});
return mxDone()
}
}

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'
)
}
Loading
Loading