From 3c6ee0ce62ebde6b641cf82981d30b1aacd3ab2d Mon Sep 17 00:00:00 2001 From: Rezart Qelibari Date: Fri, 10 May 2019 02:04:45 +0200 Subject: [PATCH 1/2] Allow forward-partial wildcard addresses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Enables recognition of forward-partial wildcard addresses à la '*postfix@domain.com'. --- lib/tools.js | 20 ++++++++++++++++++++ lib/user-handler.js | 13 +++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/tools.js b/lib/tools.js index aca38934..c25025ae 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -108,6 +108,25 @@ function normalizeAddress(address, withNames, options) { return addr; } +/** + * Generate a list of possible wildcard addresses by generating all posible + * substrings of the username email address part. + * + * @param {String} username - The username part of the email address. + * @param {String} domain - The domain part of the email address. + * @return {Array} The list of all possible username wildcard addresses, + * that would match this email address (as given by the params). + */ +function getWildcardAddresses(username, domain) { + if (typeof username !== 'string' || typeof domain !== 'string') return []; + let result = []; + // <= generates the 'simple' wildcard (a la '*@') address. + for (var i = 1; i <= username.length; i++) { + result.push('*' + username.substr(i) + '@' + domain); + } + return result; +} + // returns a redis config object with a retry strategy function redisConfig(defaultConfig) { return defaultConfig; @@ -430,6 +449,7 @@ function uview(address) { module.exports = { normalizeAddress, normalizeDomain, + getWildcardAddresses, redisConfig, checkRangeQuery, decodeAddresses, diff --git a/lib/user-handler.js b/lib/user-handler.js index 0afe87d9..37ce6a6e 100644 --- a/lib/user-handler.js +++ b/lib/user-handler.js @@ -121,13 +121,22 @@ class UserHandler { return false; } + let _partialWildcards = tools.getWildcardAddresses( + username, + domain + ); + let query = { - addrview: '*@' + domain + addrview: { $in: _partialWildcards } }; if (aliasDomain) { // search for alias domain as well - query.addrview = { $in: [query.addrview, '*@' + aliasDomain] }; + let _aliasWildcards = tools.getWildcardAddresses( + username, + aliasDomain + ); + query.addrview['$in'] = query.addrview['$in'].concat(_aliasWildcards); } // try to find a catch-all address From a6390611ef8fb19156d37f2d16d024401ad46201 Mon Sep 17 00:00:00 2001 From: Rezart Qelibari Date: Fri, 10 May 2019 05:20:30 +0200 Subject: [PATCH 2/2] Fix two linting errors --- lib/tools.js | 2 +- lib/user-handler.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tools.js b/lib/tools.js index c25025ae..13b848af 100644 --- a/lib/tools.js +++ b/lib/tools.js @@ -121,7 +121,7 @@ function getWildcardAddresses(username, domain) { if (typeof username !== 'string' || typeof domain !== 'string') return []; let result = []; // <= generates the 'simple' wildcard (a la '*@') address. - for (var i = 1; i <= username.length; i++) { + for (let i = 1; i <= username.length; i++) { result.push('*' + username.substr(i) + '@' + domain); } return result; diff --git a/lib/user-handler.js b/lib/user-handler.js index 37ce6a6e..1120ec49 100644 --- a/lib/user-handler.js +++ b/lib/user-handler.js @@ -136,7 +136,7 @@ class UserHandler { username, aliasDomain ); - query.addrview['$in'] = query.addrview['$in'].concat(_aliasWildcards); + query.addrview.$in = query.addrview.$in.concat(_aliasWildcards); } // try to find a catch-all address