Skip to content

Commit

Permalink
add files
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Sep 15, 2017
1 parent 65bcb29 commit b6ba62d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 61 deletions.
70 changes: 35 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

var tlds = require('haraka-tld');
const tlds = require('haraka-tld');

exports.register = function () {
var plugin = this;
const plugin = this;
plugin.inherits('haraka-plugin-redis');

plugin.load_sender_ini();
Expand All @@ -18,7 +18,7 @@ exports.register = function () {
}

exports.load_sender_ini = function () {
var plugin = this;
const plugin = this;

plugin.cfg = plugin.config.get('known-senders.ini', function () {
plugin.load_sender_ini();
Expand All @@ -37,7 +37,7 @@ exports.load_sender_ini = function () {
*/

exports.update_sender = function (next, connection, params) {
var plugin = this;
const plugin = this;
// queue_ok arguments: next, connection, msg
// ok 1390590369 qp 634 (F82E2DD5-9238-41DC-BC95-9C3A02716AD2.1)

Expand All @@ -50,19 +50,19 @@ exports.update_sender = function (next, connection, params) {
if (!connection) return errNext('no connection');
if (!connection.transaction) return errNext('no transaction');
if (!connection.relaying) return next();
var txn = connection.transaction;
const txn = connection.transaction;

var sender_od = plugin.get_sender_domain_by_txn(txn);
const sender_od = plugin.get_sender_domain_by_txn(txn);
if (!sender_od) return errNext('no sender domain');

var rcpt_domains = plugin.get_recipient_domains_by_txn(txn);
const rcpt_domains = plugin.get_recipient_domains_by_txn(txn);
if (rcpt_domains.length === 0) {
return errNext('no rcpt ODs for ' + sender_od);
}

// within this function, the sender is a local domain
// and the recipient is an external domain
var multi = plugin.db.multi();
const multi = plugin.db.multi();
for (let i = 0; i < rcpt_domains.length; i++) {
multi.hincrby(sender_od, rcpt_domains[i], 1);
}
Expand All @@ -80,26 +80,26 @@ exports.update_sender = function (next, connection, params) {
}

exports.get_sender_domain_by_txn = function (txn) {
var plugin = this;
const plugin = this;

if (!txn.mail_from) return;
if (!txn.mail_from.host) return;
var sender_od = tlds.get_organizational_domain(txn.mail_from.host);
const sender_od = tlds.get_organizational_domain(txn.mail_from.host);
if (txn.mail_from.host !== sender_od) {
plugin.logdebug('sender: ' + txn.mail_from.host + ' -> ' + sender_od);
}
return sender_od;
}

exports.get_recipient_domains_by_txn = function (txn) {
var plugin = this;
const plugin = this;

var rcpt_domains = [];
const rcpt_domains = [];
if (!txn.rcpt_to) return rcpt_domains;

for (let i=0; i < txn.rcpt_to.length; i++) {
if (!txn.rcpt_to[i].host) continue;
var rcpt_od = tlds.get_organizational_domain(txn.rcpt_to[i].host);
const rcpt_od = tlds.get_organizational_domain(txn.rcpt_to[i].host);
if (txn.rcpt_to[i].host !== rcpt_od) {
plugin.loginfo('rcpt: ' + txn.rcpt_to[i].host + ' -> ' + rcpt_od);
}
Expand All @@ -123,12 +123,12 @@ exports.get_recipient_domains_by_txn = function (txn) {

// early checks, on the mail hook
exports.is_authenticated = function (next, connection, params) {
var plugin = this;
const plugin = this;

// only validate inbound messages
if (connection.relaying) return next();

var sender_od = plugin.get_sender_domain_by_txn(connection.transaction);
const sender_od = plugin.get_sender_domain_by_txn(connection.transaction);

if (plugin.has_fcrdns_match(sender_od, connection)) {
connection.logdebug(plugin, '+fcrdns: ' + sender_od);
Expand All @@ -151,33 +151,33 @@ exports.is_authenticated = function (next, connection, params) {
}

exports.get_validated_sender_od = function (connection) {
var plugin = this;
const plugin = this;
if (!connection) return;
if (!connection.transaction) return;
var txn_res = connection.transaction.results.get(plugin.name);
const txn_res = connection.transaction.results.get(plugin.name);
if (!txn_res) return;
return txn_res.sender;
}

exports.get_rcpt_ods = function (connection) {
var plugin = this;
const plugin = this;
if (!connection) return [];
if (!connection.transaction) return [];

var txn_r = connection.transaction.results.get(plugin.name);
const txn_r = connection.transaction.results.get(plugin.name);
if (!txn_r) return [];

return txn_r.rcpt_ods;
}

function already_matched (connection) {
var res = connection.transaction.results.get(this);
const res = connection.transaction.results.get(this);
if (!res) return false;
return (res.pass && res.pass.length) ? true : false;
}

exports.check_recipient = function (next, connection, rcpt) {
var plugin = this;
const plugin = this;
// rcpt is a valid local email address. Some rcpt_to.* plugin has
// accepted it.

Expand All @@ -192,13 +192,13 @@ exports.check_recipient = function (next, connection, rcpt) {
if (!rcpt.host) return errNext('rcpt.host unset?');

// reduce the host portion of the email address to an OD
var rcpt_od = tlds.get_organizational_domain(rcpt.host);
const rcpt_od = tlds.get_organizational_domain(rcpt.host);
if (!rcpt_od) return errNext('no rcpt od for ' + rcpt.host);

connection.transaction.results.push(plugin, { rcpt_ods: rcpt_od });

// if no validated sender domain, there's nothing to do...yet
var sender_od = plugin.get_validated_sender_od(connection);
const sender_od = plugin.get_validated_sender_od(connection);
if (!sender_od) return next();

// The sender OD is validated, check Redis for a match
Expand All @@ -216,10 +216,10 @@ exports.check_recipient = function (next, connection, rcpt) {
}

exports.is_dkim_authenticated = function (next, connection) {
var plugin = this;
const plugin = this;
if (connection.relaying) return next();

var rcpt_ods = [];
let rcpt_ods = [];

function errNext (err) {
connection.logerror(plugin, 'is_dkim_authenticated: ' + err);
Expand All @@ -228,20 +228,20 @@ exports.is_dkim_authenticated = function (next, connection) {

if (already_matched(connection)) return errNext('already matched');

var sender_od = plugin.get_validated_sender_od(connection);
const sender_od = plugin.get_validated_sender_od(connection);
if (!sender_od) return errNext('no sender_od');

rcpt_ods = plugin.get_rcpt_ods(connection);
if (!rcpt_ods || ! rcpt_ods.length) return errNext('no rcpt_ods');

var dkim = connection.transaction.results.get('dkim_verify');
const dkim = connection.transaction.results.get('dkim_verify');
if (!dkim) return next();
if (!dkim.pass || !dkim.pass.length) return errNext('no dkim pass');

var multi = plugin.db.multi();
const multi = plugin.db.multi();

for (let i = 0; i < dkim.pass.length; i++) {
var dkim_od = tlds.get_organizational_domain(dkim.pass[i]);
const dkim_od = tlds.get_organizational_domain(dkim.pass[i]);
if (dkim_od === sender_od) {
connection.transaction.results.add(plugin, { sender: sender_od, auth: 'dkim' });
for (let j = 0; j < rcpt_ods.length; j++) {
Expand Down Expand Up @@ -270,17 +270,17 @@ exports.is_dkim_authenticated = function (next, connection) {
}

exports.has_fcrdns_match = function (sender_od, connection) {
var plugin = this;
var fcrdns = connection.results.get('fcrdns');
const plugin = this;
const fcrdns = connection.results.get('fcrdns');
if (!fcrdns) return false;
if (!fcrdns.fcrdns) return false;

connection.logdebug(plugin, fcrdns.fcrdns);

var mail_host = fcrdns.fcrdns;
let mail_host = fcrdns.fcrdns;
if (Array.isArray(mail_host)) mail_host = fcrdns.fcrdns[0];

var fcrdns_od = tlds.get_organizational_domain(mail_host);
const fcrdns_od = tlds.get_organizational_domain(mail_host);
if (fcrdns_od !== sender_od) return false;

connection.transaction.results.add(plugin, {
Expand All @@ -290,9 +290,9 @@ exports.has_fcrdns_match = function (sender_od, connection) {
}

exports.has_spf_match = function (sender_od, connection) {
var plugin = this;
const plugin = this;

var spf = connection.results.get('spf');
let spf = connection.results.get('spf');
if (spf && spf.domain && spf.result === 'Pass') {
// scope=helo (HELO/EHLO)
if (tlds.get_organizational_domain(spf.domain) === sender_od) {
Expand Down
52 changes: 26 additions & 26 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@

var assert = require('assert');
const assert = require('assert');

var Address = require('address-rfc2821').Address;
var fixtures = require('haraka-test-fixtures');
const Address = require('address-rfc2821').Address;
const fixtures = require('haraka-test-fixtures');

describe('is_authenticated', function () {

var plugin = fixtures.plugin('index');
var connection;
const plugin = fixtures.plugin('index');
let connection;

beforeEach(function (done) {
connection = fixtures.connection.createConnection();
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('is_authenticated', function () {
});

describe('check_recipient', function () {
var connection;
let connection;

beforeEach(function (done) {
connection = fixtures.connection.createConnection();
Expand All @@ -99,11 +99,11 @@ describe('check_recipient', function () {
});

it('reduces domain to OD', function (done) {
var plugin = fixtures.plugin('index');
const plugin = fixtures.plugin('index');
plugin.validated_sender_od = 'example.com';

plugin.check_recipient(function () {
var res = connection.transaction.results.get(plugin.name);
const res = connection.transaction.results.get(plugin.name);
assert.equal(res.rcpt_ods[0], 'example.com');
done();
},
Expand All @@ -114,14 +114,14 @@ describe('check_recipient', function () {

describe('update_sender', function () {

var plugin = fixtures.plugin('index');
const plugin = fixtures.plugin('index');
plugin.db = {
multi :function () { return plugin.db; },
hget : function () {},
hincrby : function () {},
exec: function (cb) { if (cb) cb(null, []); },
}
var connection;
let connection;

beforeEach(function (done) {
connection = fixtures.connection.createConnection();
Expand Down Expand Up @@ -184,21 +184,21 @@ describe('get_sender_domain_by_txn', function () {
});

it('returns a sender domain: example.com', function (done) {
var plugin = this.plugin;
const plugin = this.plugin;
this.connection.transaction.mail_from = new Address('<user@mail.example.com>');
assert.equal(plugin.get_sender_domain_by_txn(this.connection.transaction), 'example.com');
done();
});

it('returns a sender domain: mail.example.com', function (done) {
var plugin = this.plugin;
const plugin = this.plugin;
this.connection.transaction.mail_from = new Address('<user@mail.example.com>');
assert.equal(plugin.get_sender_domain_by_txn(this.connection.transaction), 'example.com');
done();
});

it('returns a sender domain: bbc.co.uk', function (done) {
var plugin = this.plugin;
const plugin = this.plugin;
this.connection.transaction.mail_from = new Address('<user@anything.bbc.co.uk>');
assert.equal(plugin.get_sender_domain_by_txn(this.connection.transaction), 'bbc.co.uk');
done();
Expand All @@ -217,30 +217,30 @@ describe('get_recipient_domains_by_txn', function () {
});

it('retrieves domains from txn recipients: example.com', function (done) {
var plugin = this.plugin;
var txn = this.connection.transaction;
const plugin = this.plugin;
const txn = this.connection.transaction;
txn.rcpt_to.push(new Address('<user@example.com>'));
var rcpt_doms = plugin.get_recipient_domains_by_txn(txn);
const rcpt_doms = plugin.get_recipient_domains_by_txn(txn);
assert.deepEqual(rcpt_doms, ['example.com'], rcpt_doms);
done();
});

it('retrieves domains from txn recipients: example[1-2].com', function (done) {
var plugin = this.plugin;
var txn = this.connection.transaction;
const plugin = this.plugin;
const txn = this.connection.transaction;
txn.rcpt_to.push(new Address('<user@example1.com>'));
txn.rcpt_to.push(new Address('<user@example2.com>'));
var rcpt_doms = plugin.get_recipient_domains_by_txn(txn);
const rcpt_doms = plugin.get_recipient_domains_by_txn(txn);
assert.deepEqual(rcpt_doms, ['example1.com', 'example2.com'], rcpt_doms);
done();
});

it('retrieves unique domains from txn recipients: example.com', function (done) {
var plugin = this.plugin;
var txn = this.connection.transaction;
const plugin = this.plugin;
const txn = this.connection.transaction;
txn.rcpt_to.push(new Address('<user1@example.com>'));
txn.rcpt_to.push(new Address('<user2@example.com>'));
var rcpt_doms = plugin.get_recipient_domains_by_txn(txn);
const rcpt_doms = plugin.get_recipient_domains_by_txn(txn);
assert.deepEqual(rcpt_doms, ['example.com'], rcpt_doms);
done();
});
Expand All @@ -261,15 +261,15 @@ describe('is_dkim_authenticated', function () {
});

it('finds dkim results', function (done) {
var plugin = this.plugin;
var connection = this.connection;
var txn = this.connection.transaction;
const plugin = this.plugin;
const connection = this.connection;
const txn = this.connection.transaction;
txn.results.add(plugin, { sender: 'sender.com' });
txn.results.push(plugin, { rcpt_ods: 'rcpt.com' });
txn.results.add({ name: 'dkim_verify'}, { pass: 'sender.com'});

plugin.is_dkim_authenticated(function () {
var res = txn.results.get(plugin.name);
const res = txn.results.get(plugin.name);
assert.equal('dkim', res.auth);
done();
},
Expand Down

0 comments on commit b6ba62d

Please sign in to comment.