From df55eb9acfd9c0b88f6bdea8f3ec7c8927b9c788 Mon Sep 17 00:00:00 2001 From: Steve Freegard Date: Thu, 13 Jul 2023 19:34:19 +0100 Subject: [PATCH] fix: Handle DNS TXT array result (#15) * Add test for facebookmail.com * Fix command-line tool, was using callback and not await * Silence skipping unknown modifier: v error * Switch to promise.then.catch for CLI util --------- Co-authored-by: Steve Freegard --- bin/spf | 11 +++++------ lib/spf.js | 10 +++++++++- test/spf.js | 7 +++++++ 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/bin/spf b/bin/spf index 9e718f0..109429f 100755 --- a/bin/spf +++ b/bin/spf @@ -34,15 +34,14 @@ if (parsed.domain) { } } -spf.check_host(parsed.ip, (domain ? domain : parsed.helo), null, function (err, result) { - if (err) { - console.log(`Error: ${err.message}`); - process.exit(1); - } +spf.check_host(parsed.ip, (domain ? domain : parsed.helo)).then((result) => { console.log([ `ip=${parsed.ip}`, `helo="${(parsed.helo ? parsed.helo : '')}"`, `domain="${(domain ? domain : '')}"`, `result=${spf.result(result)}` ].join(' ')); -}) +}).catch((err) => { + console.error(`Error: ${err.message}`); + process.exit(1); +}); diff --git a/lib/spf.js b/lib/spf.js index 55738e8..5bbe0d8 100644 --- a/lib/spf.js +++ b/lib/spf.js @@ -193,7 +193,11 @@ class SPF { let spf_record; let match; - for (const txt_rr of txt_rrs) { + for (let txt_rr of txt_rrs) { + // txt_rr might be an array, so handle that case + if (Array.isArray(txt_rr)) { + txt_rr = txt_rr.join(''); + } match = /^(v=spf1(?:$|\s.+$))/i.exec(txt_rr); if (!match) { @@ -653,6 +657,10 @@ class SPF { // NOT IMPLEMENTED return this.SPF_NONE } + + async mod_v (str) { + return this.SPF_NONE + } } exports.SPF = SPF; diff --git a/test/spf.js b/test/spf.js index 4f235d4..e64a2c8 100644 --- a/test/spf.js +++ b/test/spf.js @@ -78,6 +78,13 @@ describe('SPF', function () { } }) + it('check_host, facebook.com, pass', async function () { + this.timeout = 3000; + this.SPF.count = 0; + const rc = await this.SPF.check_host('69.171.232.145', 'facebookmail.com'); + assert.equal(rc, this.SPF.SPF_PASS, "pass"); + }) + it('valid_ip, true', function (done) { assert.equal(this.SPF.valid_ip(':212.70.129.94'), true); done()