diff --git a/README.md b/README.md index 6924e16..e14f107 100644 --- a/README.md +++ b/README.md @@ -62,9 +62,9 @@ You can use a domain name or an email address as the target, for additional conf - **AAAA** is an array of IPv6 addresses. Optional, resolved from exchange hostname if not set - **connectHook** _function (options, socketOptions, callback)_ is a function handler to run before establishing a tcp connection to current target (defined in `socketOptions`). If the `socketOptions` object has a `socket` property after the callback then connection is not established. Useful if you want to divert the connection is ome cases, for example if the target domain is in the Onion network then you could create a socket against a SOCK proxy yourself. - **mtaSts** is an object for MTA-STS configuration - - **enabled** - if not `true` then does not run MTA-STS checks - - **logger(logObj)** - method to log data - - **cache** - an object to manage MTA-STS policy caches + - **enabled** - if not `true` then does not run MTA-STS checks, disabled by default + - **logger(logObj)** - method to log MTA-STS information, logging is disabled by default + - **cache** - an object to manage MTA-STS policy cache - **get(domain)** -> returns cached policy object - **set(domain, policyObj)** -> caches a policy object - **connectError** _function (err, options, socketOptions)_ is a function handler to run when a connection to a MX fails. diff --git a/lib/get-connection.js b/lib/get-connection.js index 165b08d..723ef18 100644 --- a/lib/get-connection.js +++ b/lib/get-connection.js @@ -152,6 +152,7 @@ function getConnection(delivery) { hostname: mx.hostname, host: mx.host, domain: delivery.domain, + mode: mx.policyMatch.mode, testing: true }); } else { @@ -163,6 +164,7 @@ function getConnection(delivery) { hostname: mx.hostname, host: mx.host, domain: delivery.domain, + mode: mx.policyMatch.mode, testing: false }); let code = 554; @@ -183,6 +185,7 @@ function getConnection(delivery) { hostname: mx.hostname, host: mx.host, domain: delivery.domain, + mode: mx.policyMatch.mode, testing: true }); } diff --git a/lib/mx-connect.js b/lib/mx-connect.js index ea64f04..4e27fb5 100644 --- a/lib/mx-connect.js +++ b/lib/mx-connect.js @@ -7,6 +7,7 @@ const getConnection = require('./get-connection'); const net = require('net'); const dns = require('dns'); const { getPolicy, validateMx } = require('mailauth/lib/mta-sts'); +const util = require('util'); const EMTPY_CACHE_HANDLER = { async get(/*domain*/) { @@ -23,7 +24,10 @@ const resolvePolicy = async delivery => { } const knownPolicy = await delivery.mtaSts.cache.get(delivery.decodedDomain); - const { policy, status } = await getPolicy(delivery.decodedDomain, knownPolicy); + const { policy, status } = await getPolicy(delivery.decodedDomain, knownPolicy, { + resolver: delivery.dnsOptions && delivery.dnsOptions.resolve && util.promisify(delivery.dnsOptions.resolve) + }); + if (status !== 'cached') { await delivery.mtaSts.cache.set(delivery.decodedDomain, policy); } @@ -34,6 +38,10 @@ const resolvePolicy = async delivery => { }; const validateMxPolicy = async delivery => { + if (!delivery.mtaSts.enabled) { + return delivery; + } + for (let mx of delivery.mx) { mx.policyMatch = validateMx(mx.exchange, delivery.mtaSts.policy); } @@ -56,7 +64,7 @@ module.exports = (options, callback) => { } let mtaSts = Object.assign({ enabled: false }, options.mtaSts); - mtaSts.logger = mtaSts.logger || (obj => console.error(JSON.stringify(obj))); + mtaSts.logger = mtaSts.logger || (() => false); mtaSts.cache = mtaSts.cache || EMTPY_CACHE_HANDLER; let delivery = { diff --git a/package.json b/package.json index 543880f..42376ef 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mx-connect", - "version": "1.4.1", + "version": "1.4.2", "description": "Establish TCP connection to a MX server", "main": "lib/mx-connect.js", "scripts": { diff --git a/test/mx-connect-test.js b/test/mx-connect-test.js index 9f1acba..afb640e 100644 --- a/test/mx-connect-test.js +++ b/test/mx-connect-test.js @@ -2,6 +2,7 @@ 'use strict'; +const dns = require('dns'); const mxConnect = require('../lib/mx-connect'); module.exports.basic = test => { @@ -71,3 +72,31 @@ module.exports.policyFail = test => { } ); }; + +module.exports.policySkip = test => { + mxConnect( + { + target: 'andris@zone.ee', + mtaSts: { + enabled: false + }, + mx: [ + { + exchange: 'aspmx.l.google.com', + priority: 10, + A: ['64.233.165.26'], + AAAA: [] + } + ] + }, + (err, connection) => { + test.ifError(err); + test.ok(connection.socket); + + test.ok(!connection.policyMatch); + + connection.socket.once('end', () => test.done()); + connection.socket.once('data', () => connection.socket.end()); + } + ); +};