From 5fa95fa2390197a85eb838281edb7be1fba27018 Mon Sep 17 00:00:00 2001 From: Matt Simerson Date: Sat, 24 Dec 2022 10:44:52 -0800 Subject: [PATCH] lint changes, cherry picked from #3135 (#3138) --- dkim.js | 6 +---- logger.js | 39 +++++++++++++---------------- plugins/auth/auth_proxy.js | 13 +++++----- plugins/avg.js | 3 ++- plugins/clamd.js | 23 +++++++++-------- plugins/delay_deny.js | 6 ++--- plugins/esets.js | 15 +++++------ plugins/greylist.js | 2 +- plugins/messagesniffer.js | 24 ++++++++---------- plugins/prevent_credential_leaks.js | 4 +-- plugins/process_title.js | 17 ++++++------- plugins/queue/quarantine.js | 4 +-- plugins/queue/smtp_forward.js | 7 +++--- plugins/rcpt_to.host_list_base.js | 12 +++------ plugins/spamassassin.js | 6 ++--- plugins/xclient.js | 5 +--- smtp_client.js | 2 +- tests/plugins/dns_list_base.js | 14 +++++------ tests/plugins/spamassassin.js | 2 +- tls_socket.js | 4 +-- 20 files changed, 92 insertions(+), 116 deletions(-) diff --git a/dkim.js b/dkim.js index 81018c053..61b99b366 100644 --- a/dkim.js +++ b/dkim.js @@ -302,11 +302,7 @@ class DKIMObject { } } if (!res) return this.result('no key for signature', 'invalid'); - for (let record of res) { - // Node 0.11.x compatibility - if (Array.isArray(record)) { - record = record.join(''); - } + for (const record of res) { if (!record.includes('p=')) { this.debug(`${this.identity}: ignoring TXT record: ${record}`); continue; diff --git a/logger.js b/logger.js index 349bcd6be..37b9091c7 100644 --- a/logger.js +++ b/logger.js @@ -134,8 +134,7 @@ logger.log = (level, data, logobj) => { if (level === 'PROTOCOL') { data = data.replace(/\n/g, '\\n'); } - data = data.replace(/\r/g, '\\r') - .replace(/\n$/, ''); + data = data.replace(/\r/g, '\\r').replace(/\n$/, ''); const item = { level, data, obj: logobj}; @@ -158,18 +157,19 @@ logger.log = (level, data, logobj) => { logger.log_respond = (retval, msg, data) => { // any other return code is irrelevant - if (retval !== constants.cont) { return false; } + if (retval !== constants.cont) return false; + let timestamp_string = ''; - if (logger.timestamps) { - timestamp_string = `${new Date().toISOString()} `; - } + if (logger.timestamps) timestamp_string = `${new Date().toISOString()} `; + const color = logger.colors[data.level]; if (color && stdout_is_tty) { process.stdout.write(`${timestamp_string}${logger.colorize(color,data.data)}\n`); - return true; + } + else { + process.stdout.write(`${timestamp_string}${data.data}\n`); } - process.stdout.write(`${timestamp_string}${data.data}\n`); return true; } @@ -215,7 +215,7 @@ logger._init_loglevel = function () { } logger.would_log = level => { - if (logger.loglevel < level) { return false; } + if (logger.loglevel < level) return false; return true; } @@ -229,8 +229,7 @@ logger._init_timestamps = function () { this._init_timestamps(); }); - // If we've already been toggled to true by the cfg, we should respect - // this. + // If we've already been toggled to true by the cfg, we should respect this. this.set_timestamps(logger.timestamps || _timestamps); } @@ -254,9 +253,7 @@ logger.log_if_level = (level, key, plugin) => function () { // if the object is a connection, add the connection id if (data instanceof connection.Connection) { logobj.uuid = data.uuid; - if (data.tran_count > 0) { - logobj.uuid += `.${data.tran_count}`; - } + if (data.tran_count > 0) logobj.uuid += `.${data.tran_count}`; } else if (data instanceof plugins.Plugin) { logobj.origin = data.name; @@ -267,10 +264,8 @@ logger.log_if_level = (level, key, plugin) => function () { else if (data instanceof outbound.HMailItem) { logobj.origin = 'outbound'; if (data.todo) { - if (data.todo.uuid) - logobj.uuid = data.todo.uuid; - if (data.todo.client_uuid) { - // dirty hack + if (data.todo.uuid) logobj.uuid = data.todo.uuid; + if (data.todo.client_uuid) { // dirty hack logobj.origin = `outbound] [${data.todo.client_uuid}`; } } @@ -294,32 +289,34 @@ logger.log_if_level = (level, key, plugin) => function () { logobj.message += (util.inspect(data)); } } + switch (logger.format) { case logger.formats.LOGFMT: logger.log( level, stringify(logobj) ); - return true; + break case logger.formats.JSON: logger.log( level, JSON.stringify(logobj) ); - return true; + break case logger.formats.DEFAULT: default: logger.log( level, `[${logobj.level}] [${logobj.uuid}] [${logobj.origin}] ${logobj.message}` ); - return true; } + return true; } logger.add_log_methods = (object, plugin) => { if (!object) return; if (typeof(object) !== 'object') return; + for (const level in logger.levels) { const fname = `log${level.toLowerCase()}`; if (object[fname]) continue; // already added diff --git a/plugins/auth/auth_proxy.js b/plugins/auth/auth_proxy.js index 83ffb23ca..f3e4bcbec 100644 --- a/plugins/auth/auth_proxy.js +++ b/plugins/auth/auth_proxy.js @@ -1,7 +1,9 @@ // Proxy AUTH requests selectively by domain + const sock = require('./line_socket'); const utils = require('haraka-utils'); -const smtp_regexp = /^([0-9]{3})([ -])(.*)/; + +const smtp_regexp = /^(\d{3})([ -])(.*)/; exports.register = function () { this.inherits('auth/auth_base'); @@ -25,8 +27,8 @@ exports.hook_capabilities = (next, connection) => { } exports.check_plain_passwd = function (connection, user, passwd, cb) { - let domain; - if ((domain = /@([^@]+)$/.exec(user))) { + let domain = /@([^@]+)$/.exec(user); + if (domain) { domain = domain[1].toLowerCase(); } else { @@ -82,7 +84,6 @@ exports.try_auth_proxy = function (connection, hosts, user, passwd, cb) { socket.on('error', err => { connection.logerror(self, `connection failed to host ${host}: ${err}`); socket.end(); - return; }); socket.send_command = function (cmd, data) { let line = cmd + (data ? (` ${data}`) : ''); @@ -175,8 +176,8 @@ exports.try_auth_proxy = function (connection, hosts, user, passwd, cb) { } if (code.startsWith('5')) { // Initial attempt failed; strip domain and retry. - let u; - if ((u = /^([^@]+)@.+$/.exec(user))) { + const u = /^([^@]+)@.+$/.exec(user) + if (u) { user = u[1]; if (methods.includes('PLAIN')) { socket.send_command('AUTH', `PLAIN ${utils.base64(`\0${user}\0${passwd}`)}`); diff --git a/plugins/avg.js b/plugins/avg.js index f9bc4c52d..981067423 100644 --- a/plugins/avg.js +++ b/plugins/avg.js @@ -7,7 +7,8 @@ const fs = require('fs'); const path = require('path'); const sock = require('./line_socket'); -const smtp_regexp = /^([0-9]{3})([ -])(.*)/; + +const smtp_regexp = /^(\d{3})([ -])(.*)/; exports.register = function () { diff --git a/plugins/clamd.js b/plugins/clamd.js index 83de84a3f..6a735ce11 100644 --- a/plugins/clamd.js +++ b/plugins/clamd.js @@ -363,18 +363,19 @@ exports.send_clamd_predata = (socket, cb) => { } function clamd_connect (socket, host) { - let match; + if (host.match(/^\//)) { - // assume unix socket - socket.connect(host); - } - else if ((match = /^\[([^\] ]+)\](?::(\d+))?/.exec(host))) { - // IPv6 literal - socket.connect((match[2] || 3310), match[1]); + socket.connect(host); // starts with /, unix socket + return } - else { - // IP:port, hostname:port or hostname - const hostport = host.split(/:/); - socket.connect((hostport[1] || 3310), hostport[0]); + + const match = /^\[([^\] ]+)\](?::(\d+))?/.exec(host); + if (match) { + socket.connect((match[2] || 3310), match[1]); // IPv6 literal + return } + + // IP:port, hostname:port or hostname + const hostport = host.split(/:/); + socket.connect((hostport[1] || 3310), hostport[0]); } diff --git a/plugins/delay_deny.js b/plugins/delay_deny.js index 23c016c37..570ddd547 100644 --- a/plugins/delay_deny.js +++ b/plugins/delay_deny.js @@ -93,7 +93,7 @@ exports.hook_deny = function (next, connection, params) { // fall through default: // No delays - return next(); + next(); } } @@ -128,7 +128,7 @@ exports.hook_rcpt_ok = function (next, connection, rcpt) { return next(params[0], params[1]); } } - return next(); + next(); } exports.hook_data = (next, connection) => { @@ -145,5 +145,5 @@ exports.hook_data = (next, connection) => { } if (fails.length) transaction.add_header('X-Haraka-Fail-Pre', fails.join(' ')); - return next(); + next(); } diff --git a/plugins/esets.js b/plugins/esets.js index c422a9fda..615ca1e4c 100644 --- a/plugins/esets.js +++ b/plugins/esets.js @@ -5,17 +5,16 @@ const virus_re = new RegExp('virus="([^"]+)"'); exports.hook_data_post = function (next, connection) { const plugin = this; - const txn = connection.transaction; const cfg = this.config.get('esets.ini'); // Write message to temporary file const tmpdir = cfg.main.tmpdir || '/tmp'; - const tmpfile = `${tmpdir}/${txn.uuid}.esets`; + const tmpfile = `${tmpdir}/${connection?.transaction?.uuid}.esets`; const ws = fs.createWriteStream(tmpfile); ws.once('error', err => { connection.logerror(plugin, `Error writing temporary file: ${err.message}`); - return next(); + next(); }); let start_time; @@ -39,10 +38,8 @@ exports.hook_data_post = function (next, connection) { }); // Get virus name - let virus; - if ((virus = virus_re.exec(stdout))) { - virus = virus[1]; - } + let virus = virus_re.exec(stdout) + if (virus) virus = virus[1]; // Log a summary const exit_code = parseInt((error) ? error.code : 0) @@ -60,7 +57,7 @@ exports.hook_data_post = function (next, connection) { return next(DENYSOFT, 'Virus scanner error'); } } - return next(); + next(); } ws.once('close', () => { @@ -70,5 +67,5 @@ exports.hook_data_post = function (next, connection) { wsOnClose); }); - txn.message_stream.pipe(ws, { line_endings: '\r\n' }); + connection.transaction.message_stream.pipe(ws, { line_endings: '\r\n' }); } diff --git a/plugins/greylist.js b/plugins/greylist.js index 1cb3af24d..b6633edf4 100644 --- a/plugins/greylist.js +++ b/plugins/greylist.js @@ -342,7 +342,7 @@ exports.process_skip_rules = function (connection) { } } - return false; + return ''; } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/plugins/messagesniffer.js b/plugins/messagesniffer.js index 005bf3329..20f6af293 100644 --- a/plugins/messagesniffer.js +++ b/plugins/messagesniffer.js @@ -89,11 +89,11 @@ exports.hook_connect = function (next, connection) { default: // Unknown connection.logerror(this, `Unknown GBUdb range: ${gbudb.range}`); - return next(); + next(); } } else { - return next(); + next(); } }); } @@ -136,7 +136,7 @@ exports.hook_data_post = function (next, connection) { ws.once('error', err => { connection.logerror(this, `Error writing temporary file: ${err.message}`); - return next(); + next(); }); ws.once('close', () => { @@ -338,11 +338,11 @@ exports.hook_disconnect = function (next, connection) { else { connection.logdebug(this, `GBUdb bad encounter added for ${connection.remote.ip}`); } - return next(); + next(); }); } else { - return next(); + next(); } } @@ -352,7 +352,7 @@ function SNFClient (req, cb) { sock.setTimeout(30 * 1000); // Connection timeout sock.once('timeout', function () { this.destroy(); - return cb(new Error('connection timed out')); + cb(new Error('connection timed out')); }); sock.once('error', err => cb(err)); sock.once('connect', function () { @@ -367,16 +367,14 @@ function SNFClient (req, cb) { }); sock.once('end', () => { // Check for result + if (/ { let user = connection.notes.auth_user; let domain; - let idx; - if ((idx = user.indexOf('@'))) { + const idx = user.indexOf('@') + if (idx) { // If the username is qualified (e.g. user@domain.com) // then we make the @domain.com part optional in the regexp. domain = user.substr(idx); diff --git a/plugins/process_title.js b/plugins/process_title.js index 0b7993b7b..34bc5a740 100644 --- a/plugins/process_title.js +++ b/plugins/process_title.js @@ -120,7 +120,7 @@ exports.hook_init_master = function (next, server) { }); } this._interval = setupInterval(title, server); - return next(); + next(); } exports.hook_init_child = function (next, server) { @@ -134,10 +134,9 @@ exports.hook_init_child = function (next, server) { server.notes.pt_messages = 0; server.notes.pt_mps_diff = 0; server.notes.pt_mps_max = 0; - const title = 'Haraka (worker)'; - process.title = title; - this._interval = setupInterval(title, server); - return next(); + process.title = 'Haraka (worker)'; + this._interval = setupInterval(process.title, server); + next(); } exports.shutdown = function () { @@ -154,7 +153,7 @@ exports.hook_connect_init = (next, connection) => { } server.notes.pt_connections++; server.notes.pt_concurrent++; - return next(); + next(); } exports.hook_disconnect = (next, connection) => { @@ -177,7 +176,7 @@ exports.hook_disconnect = (next, connection) => { worker.send({event: 'process_title.disconnect', wid: worker.id}); } server.notes.pt_concurrent--; - return next(); + next(); } exports.hook_rcpt = (next, connection) => { @@ -187,7 +186,7 @@ exports.hook_rcpt = (next, connection) => { worker.send({event: 'process_title.recipient'}); } server.notes.pt_recipients++; - return next(); + next(); } exports.hook_data = (next, connection) => { @@ -197,5 +196,5 @@ exports.hook_data = (next, connection) => { worker.send({event: 'process_title.message'}); } server.notes.pt_messages++; - return next(); + next(); } diff --git a/plugins/queue/quarantine.js b/plugins/queue/quarantine.js index a72294f6b..472118787 100644 --- a/plugins/queue/quarantine.js +++ b/plugins/queue/quarantine.js @@ -51,9 +51,7 @@ function wants_quarantine (connection) { if (transaction.notes.quarantine) return transaction.notes.quarantine; - if (transaction.notes.get('queue.wants') === 'quarantine') return true; - - return false; + return transaction.notes.get('queue.wants') === 'quarantine'; } exports.get_base_dir = function () { diff --git a/plugins/queue/smtp_forward.js b/plugins/queue/smtp_forward.js index 91adaee1e..f75b9ff43 100644 --- a/plugins/queue/smtp_forward.js +++ b/plugins/queue/smtp_forward.js @@ -189,16 +189,17 @@ exports.auth = function (cfg, connection, smtp_client) { if (cfg.auth_type === 'plain') { connection.loginfo(this, `Authenticating with AUTH PLAIN ${cfg.auth_user}`); smtp_client.send_command('AUTH', `PLAIN ${base64(`\0${cfg.auth_user}\0${cfg.auth_pass}`)}`); + return } - else if (cfg.auth_type === 'login') { + + if (cfg.auth_type === 'login') { smtp_client.authenticating = true; smtp_client.authenticated = false; connection.loginfo(this, `Authenticating with AUTH LOGIN ${cfg.auth_user}`); smtp_client.send_command('AUTH', 'LOGIN'); smtp_client.on('auth', () => { - //TODO: nothing? - + // do nothing }); smtp_client.on('auth_username', () => { smtp_client.send_command(base64(cfg.auth_user)); diff --git a/plugins/rcpt_to.host_list_base.js b/plugins/rcpt_to.host_list_base.js index 81e803c53..8aeb2fcc4 100644 --- a/plugins/rcpt_to.host_list_base.js +++ b/plugins/rcpt_to.host_list_base.js @@ -23,9 +23,7 @@ exports.load_host_list_regex = function () { () => { this.load_host_list_regex(); } ); - this.hl_re = new RegExp (`^(?:${ - - this.host_list_regex.join('|')})$`, 'i'); + this.hl_re = new RegExp (`^(?:${this.host_list_regex.join('|')})$`, 'i'); } exports.hook_mail = function (next, connection, params) { @@ -58,10 +56,7 @@ exports.hook_mail = function (next, connection, params) { exports.in_host_list = function (domain) { this.logdebug(`checking ${domain} in config/host_list`); - if (this.host_list[domain]) { - return true; - } - return false; + return !!(this.host_list[domain]); } exports.in_host_regex = function (domain) { @@ -70,6 +65,5 @@ exports.in_host_regex = function (domain) { this.logdebug(`checking ${domain} against config/host_list_regex `); - if (this.hl_re.test(domain)) { return true; } - return false; + return !!(this.hl_re.test(domain)); } diff --git a/plugins/spamassassin.js b/plugins/spamassassin.js index fe387ad8c..f1d17cab0 100644 --- a/plugins/spamassassin.js +++ b/plugins/spamassassin.js @@ -220,11 +220,9 @@ exports.score_too_high = function (conn, spamd_response) { } const max = this.cfg.main.reject_threshold; - if (max && (score >= max)) { - return "spam score exceeded threshold"; - } + if (max && (score >= max)) return "spam score exceeded threshold"; - return false; + return ''; } exports.get_spamd_username = function (conn) { diff --git a/plugins/xclient.js b/plugins/xclient.js index b524fcc47..f2bf8416a 100644 --- a/plugins/xclient.js +++ b/plugins/xclient.js @@ -22,10 +22,7 @@ exports.load_xclient_hosts = function () { } function xclient_allowed (ip) { - if (ip === '127.0.0.1' || ip === '::1' || allowed_hosts[ip]) { - return true; - } - return false; + return !!(ip === '127.0.0.1' || ip === '::1' || allowed_hosts[ip]); } exports.hook_capabilities = (next, connection) => { diff --git a/smtp_client.js b/smtp_client.js index 608a182ea..ab00af4d3 100644 --- a/smtp_client.js +++ b/smtp_client.js @@ -20,7 +20,7 @@ const line_socket = require('./line_socket'); const logger = require('./logger'); const HostPool = require('./host_pool'); -const smtp_regexp = /^([0-9]{3})([ -])(.*)/; +const smtp_regexp = /^(\d{3})([ -])(.*)/; const STATE = { IDLE: 1, ACTIVE: 2, diff --git a/tests/plugins/dns_list_base.js b/tests/plugins/dns_list_base.js index c243214b9..887b9599d 100644 --- a/tests/plugins/dns_list_base.js +++ b/tests/plugins/dns_list_base.js @@ -93,7 +93,7 @@ exports.multi = { test.done(); } } - this.plugin.multi('127.0.0.2', 'cbl.abuseat.org', cb); + this.plugin.multi('127.0.0.2', 'xbl.spamhaus.org', cb); }, 'Spamcop + CBL' (test) { test.expect(12); @@ -111,7 +111,7 @@ exports.multi = { test.done(); } } - const dnsbls = ['bl.spamcop.net','cbl.abuseat.org']; + const dnsbls = ['bl.spamcop.net','xbl.spamhaus.org']; this.plugin.multi('127.0.0.2', dnsbls, cb); }, 'Spamcop + CBL + negative result' (test) { @@ -129,7 +129,7 @@ exports.multi = { test.done(); } } - const dnsbls = ['bl.spamcop.net','cbl.abuseat.org']; + const dnsbls = ['bl.spamcop.net','xbl.spamhaus.org']; this.plugin.multi('127.0.0.1', dnsbls, cb); }, 'IPv6 addresses supported' (test) { @@ -148,7 +148,7 @@ exports.multi = { test.done(); } } - const dnsbls = ['bl.spamcop.net','cbl.abuseat.org']; + const dnsbls = ['bl.spamcop.net','xbl.spamhaus.org']; this.plugin.multi('::1', dnsbls, cb); } } @@ -163,7 +163,7 @@ exports.first = { test.ok((Array.isArray(a) && a.length > 0)); test.done(); } - const dnsbls = [ 'cbl.abuseat.org', 'bl.spamcop.net' ]; + const dnsbls = [ 'xbl.spamhaus.org', 'bl.spamcop.net' ]; this.plugin.first('127.0.0.2', dnsbls , cb); }, 'negative result' (test) { @@ -174,12 +174,12 @@ exports.first = { test.equal(null, a); test.done(); } - const dnsbls = [ 'cbl.abuseat.org', 'bl.spamcop.net' ]; + const dnsbls = [ 'xbl.spamhaus.org', 'bl.spamcop.net' ]; this.plugin.first('127.0.0.1', dnsbls, cb); }, 'each_cb' (test) { test.expect(7); - const dnsbls = [ 'cbl.abuseat.org', 'bl.spamcop.net' ]; + const dnsbls = [ 'xbl.spamhaus.org', 'bl.spamcop.net' ]; let pending = dnsbls.length; function cb () { test.ok(pending); diff --git a/tests/plugins/spamassassin.js b/tests/plugins/spamassassin.js index b073b1181..b4ef87bcb 100644 --- a/tests/plugins/spamassassin.js +++ b/tests/plugins/spamassassin.js @@ -158,7 +158,7 @@ exports.score_too_high = { test.expect(1); this.connection.relaying = true; this.plugin.cfg.main.relay_reject_threshold = 7; - test.equal(false, this.plugin.score_too_high(this.connection, {score: 6})); + test.equal('', this.plugin.score_too_high(this.connection, {score: 6})); test.done(); }, 'too high score with relaying is too high' (test) { diff --git a/tls_socket.js b/tls_socket.js index 1f3a2ff38..f82e1ffa6 100644 --- a/tls_socket.js +++ b/tls_socket.js @@ -629,9 +629,7 @@ exports.get_rejectUnauthorized = (rejectUnauthorized, port, port_list) => { if (rejectUnauthorized) return true; - if (port_list.includes(port)) return true; - - return false; + return !!(port_list.includes(port)); } function createServer (cb) {