From 7d40ab95dc2ba78fc18de6a08d810b70fc186d32 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Tue, 20 Jun 2023 11:10:01 -0700 Subject: [PATCH] release 1.2.1 - fix: call skip_hosts via 'this' instead of exports #11 - es6: replace `plugin` with `this` #12 - deps: bump versions to latest --- .github/workflows/ci.yml | 2 +- Changes.md | 10 +++++- index.js | 68 ++++++++++++++++++---------------------- package.json | 10 +++--- test/index.js | 21 ++++++++----- 5 files changed, 59 insertions(+), 52 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d768329..5360933 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,6 +1,6 @@ name: CI -on: [ push ] +on: [ push, pull_request ] env: CI: true diff --git a/Changes.md b/Changes.md index 15743ed..f0f8548 100644 --- a/Changes.md +++ b/Changes.md @@ -1,7 +1,14 @@ ### Unreleased -- Fix bug where skip configuration was ignored + +### [1.2.1] - 2023-06-19 + +- fix: call skip_hosts via 'this' instead of exports (#11) +- skip configuration was being ignored +- es6: replace `plugin` with `this` +- deps: bump versions to latest + ### [1.2.0] - 2023-01-19 @@ -44,3 +51,4 @@ [1.1.2]: https://github.com/haraka/haraka-plugin-spf/releases/tag/1.1.2 [1.1.3]: https://github.com/haraka/haraka-plugin-spf/releases/tag/1.1.3 [1.2.0]: https://github.com/haraka/haraka-plugin-spf/releases/tag/1.2.0 +[1.2.1]: https://github.com/haraka/haraka-plugin-spf/releases/tag/1.2.1 diff --git a/index.js b/index.js index ed0e193..b72d136 100644 --- a/index.js +++ b/index.js @@ -7,23 +7,21 @@ const DSN = require('haraka-dsn'); exports.SPF = SPF; exports.register = function () { - const plugin = this; // Override logging in SPF module - SPF.prototype.log_debug = str => plugin.logdebug(str); + SPF.prototype.log_debug = str => this.logdebug(str); - plugin.load_spf_ini(); + this.load_spf_ini(); - plugin.register_hook('helo', 'helo_spf'); - plugin.register_hook('ehlo', 'helo_spf'); + this.register_hook('helo', 'helo_spf'); + this.register_hook('ehlo', 'helo_spf'); } exports.load_spf_ini = function () { - const plugin = this; - plugin.nu = net_utils; // so tests can set public_ip - plugin.SPF = SPF; + this.nu = net_utils; // so tests can set public_ip + this.SPF = SPF; - plugin.cfg = plugin.config.get('spf.ini', { + this.cfg = this.config.get('spf.ini', { booleans: [ '-defer.helo_temperror', '-defer.mfrom_temperror', @@ -57,30 +55,30 @@ exports.load_spf_ini = function () { '-skip.auth', ] }, - () => { plugin.load_spf_ini(); } + () => { this.load_spf_ini(); } ); // when set, preserve legacy config settings - ['helo','mail'].forEach(phase => { - if (plugin.cfg.main[`${phase}_softfail_reject`]) { - plugin.cfg.deny[`${phase}_softfail`] = true; + for (const phase of ['helo','mail']) { + if (this.cfg.main[`${phase}_softfail_reject`]) { + this.cfg.deny[`${phase}_softfail`] = true; } - if (plugin.cfg.main[`${phase}_fail_reject`]) { - plugin.cfg.deny[`${phase}_fail`] = true; + if (this.cfg.main[`${phase}_fail_reject`]) { + this.cfg.deny[`${phase}_fail`] = true; } - if (plugin.cfg.main[`${phase}_temperror_defer`]) { - plugin.cfg.defer[`${phase}_temperror`] = true; + if (this.cfg.main[`${phase}_temperror_defer`]) { + this.cfg.defer[`${phase}_temperror`] = true; } - if (plugin.cfg.main[`${phase}_permerror_reject`]) { - plugin.cfg.deny[`${phase}_permerror`] = true; + if (this.cfg.main[`${phase}_permerror_reject`]) { + this.cfg.deny[`${phase}_permerror`] = true; } - }); + } - if (!plugin.cfg.relay) { - plugin.cfg.relay = { context: 'sender' }; // default/legacy + if (!this.cfg.relay) { + this.cfg.relay = { context: 'sender' }; // default/legacy } - plugin.cfg.lookup_timeout = plugin.cfg.main.lookup_timeout || plugin.timeout - 1; + this.cfg.lookup_timeout = this.cfg.main.lookup_timeout || this.timeout - 1; } exports.helo_spf = async function (next, connection, helo) { @@ -222,7 +220,6 @@ exports.hook_mail = async function (next, connection, params) { // if we check the public IP first. Only check the public IP if the // client IP returns a result other than 'Pass'. const result = await spf.check_host(connection.remote.ip, host, mfrom) - // typical inbound (!relay) if (!connection.relaying) return ch_cb(null, result) @@ -231,7 +228,6 @@ exports.hook_mail = async function (next, connection, params) { // outbound (relaying), context=myself const my_public_ip = await net_utils.get_public_ip() - let spf_result; if (result) spf_result = spf.result(result).toLowerCase(); @@ -256,7 +252,6 @@ exports.log_result = function (connection, scope, host, mfrom, result, ip) { } exports.return_results = function (next, connection, spf, scope, result, sender) { - const plugin = this; const msgpre = (scope === 'helo') ? `sender ${sender}` : `sender <${sender}>`; const deny = connection.relaying ? 'deny_relay' : 'deny'; const defer = connection.relaying ? 'defer_relay' : 'defer'; @@ -265,8 +260,8 @@ exports.return_results = function (next, connection, spf, scope, result, sender) switch (result) { case spf.SPF_NONE: - if (plugin.cfg[deny][`${scope}_none`]) { - text = plugin.cfg[deny].openspf_text ? text : `${msgpre} SPF record not found`; + if (this.cfg[deny][`${scope}_none`]) { + text = this.cfg[deny].openspf_text ? text : `${msgpre} SPF record not found`; return next(DENY, text); } return next(); @@ -274,30 +269,30 @@ exports.return_results = function (next, connection, spf, scope, result, sender) case spf.SPF_PASS: return next(); case spf.SPF_SOFTFAIL: - if (plugin.cfg[deny][`${scope}_softfail`]) { - text = plugin.cfg[deny].openspf_text ? text : `${msgpre} SPF SoftFail`; + if (this.cfg[deny][`${scope}_softfail`]) { + text = this.cfg[deny].openspf_text ? text : `${msgpre} SPF SoftFail`; return next(DENY, text); } return next(); case spf.SPF_FAIL: - if (plugin.cfg[deny][`${scope}_fail`]) { - text = plugin.cfg[deny].openspf_text ? text : `${msgpre} SPF Fail`; + if (this.cfg[deny][`${scope}_fail`]) { + text = this.cfg[deny].openspf_text ? text : `${msgpre} SPF Fail`; return next(DENY, text); } return next(); case spf.SPF_TEMPERROR: - if (plugin.cfg[defer][`${scope}_temperror`]) { + if (this.cfg[defer][`${scope}_temperror`]) { return next(DENYSOFT, `${msgpre} SPF Temporary Error`); } return next(); case spf.SPF_PERMERROR: - if (plugin.cfg[deny][`${scope}_permerror`]) { + if (this.cfg[deny][`${scope}_permerror`]) { return next(DENY, `${msgpre} SPF Permanent Error`); } return next(); default: // Unknown result - connection.logerror(plugin, `unknown result code=${result}`); + connection.logerror(this, `unknown result code=${result}`); return next(); } } @@ -314,9 +309,8 @@ exports.save_to_header = (connection, spf, result, mfrom, host, id, ip) => { } exports.skip_hosts = function (connection) { - const plugin = this; - const skip = plugin?.cfg?.skip; + const skip = this?.cfg?.skip; if (skip) { if (skip.relaying && connection.relaying) return 'relay'; if (skip.auth && connection.notes.auth_user) return 'auth'; diff --git a/package.json b/package.json index 2daf7bd..f756373 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "haraka-plugin-spf", - "version": "1.2.0", + "version": "1.2.1", "description": "Sender Policy Framework (SPF) plugin for Haraka", "main": "index.js", "scripts": { @@ -25,10 +25,10 @@ }, "homepage": "https://github.com/haraka/haraka-plugin-spf#readme", "devDependencies": { - "eslint": "8", + "eslint": "^8.42.0", "eslint-plugin-haraka": "*", "haraka-test-fixtures": "*", - "mocha": "9" + "mocha": "^9.2.0" }, "bin": { "spf": "./bin/spf" @@ -36,7 +36,7 @@ "dependencies": { "haraka-dsn": "^1.0.4", "haraka-net-utils": "^1.5.0", - "ipaddr.js": "^2.0.1", - "nopt": "^7.0.0" + "ipaddr.js": "^2.1.0", + "nopt": "^7.2.0" } } diff --git a/test/index.js b/test/index.js index 2857ddd..fa831fa 100644 --- a/test/index.js +++ b/test/index.js @@ -16,7 +16,7 @@ beforeEach(function () { this.plugin.timeout = 8000; this.plugin.load_spf_ini(); - // uncomment this line to see detailed SPF evaluation + // comment this line to see detailed SPF evaluation this.plugin.SPF.prototype.log_debug = () => {}; this.connection = fixtures.connection.createConnection(); @@ -165,10 +165,10 @@ describe('hook_helo', function () { const test_addr = new Address(''); describe('hook_mail', function () { - it('rfc1918', function (done) { - this.connection.remote.is_private=true; - this.connection.remote.ip='192.168.1.1'; + it('rfc1918', function (done) { + this.connection.set('remote.is_private', true); + this.connection.set('remote.ip', '192.168.1.1'); this.plugin.hook_mail(function next () { assert.equal(undefined, arguments[0]); done() @@ -190,22 +190,26 @@ describe('hook_mail', function () { delete this.connection.transaction; this.plugin.hook_mail(function next () { assert.equal(undefined, arguments[0]); + assert.equal(undefined, arguments[1]); done() }, this.connection); }) it('txn, no helo', function (done) { + this.timeout(3000) this.plugin.cfg.deny.mfrom_fail = false; - this.connection.remote.ip='207.85.1.1'; + this.connection.set('remote.ip', '207.85.1.1'); this.plugin.hook_mail(function next () { assert.equal(undefined, arguments[0]); + assert.equal(undefined, arguments[1]); done() }, this.connection, [test_addr]); }) it('txn', function (done) { - this.connection.set('remote', 'ip', '207.85.1.1'); - this.connection.set('hello', 'host', 'mail.example.com'); + this.timeout(3000) + this.connection.set('remote.ip', '207.85.1.1'); + this.connection.set('hello.host', 'mail.example.com'); this.plugin.hook_mail(function next (rc) { assert.equal(undefined, rc); done() @@ -213,8 +217,9 @@ describe('hook_mail', function () { }) it('txn, relaying', function (done) { + this.timeout(3000) this.connection.set('remote.ip', '207.85.1.1'); - this.connection.relaying=true; + this.connection.set('relaying', true); this.connection.set('hello.host', 'mail.example.com'); this.plugin.hook_mail(function next (rc) { assert.equal(undefined, rc);