Skip to content

Commit

Permalink
fix(deps-normalization-punycode): ZMS-189-2 (#44)
Browse files Browse the repository at this point in the history
* fix logical error with tempfail

* remove unnecessary deps, fix normalization, add support for punycode domains
  • Loading branch information
NickOvt authored Nov 20, 2024
1 parent c89a19e commit 90f58ed
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 3,775 deletions.
37 changes: 26 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const undici = require('undici');
const addressTools = require('zone-mta/lib/address-tools');
const libmime = require('libmime');
const { toUnicode } = require('punycode');
const { randomBytes } = require('node:crypto');
const os = require('os');

Expand Down Expand Up @@ -68,14 +68,31 @@ const loggelfForEveryUser = (app, short_message, data) => {
}
};

const normalizeAddress = (authenticatedUser, returnAsObject) => {
const user = authenticatedUser.substr(0, authenticatedUser.lastIndexOf('@')).normalize('NFC').toLowerCase().trim();
const domain = authenticatedUser.substr(authenticatedUser.lastIndexOf('@') + 1);
const addr = user.trim() + '@' + addressTools.normalizeDomain(domain);
const unameview = user.trim().replace(/\./g, '');
const normalizeDomain = domain => {
domain = (domain || '').toLowerCase().trim();
try {
if (/^xn--/.test(domain)) {
domain = toUnicode(domain).normalize('NFC').toLowerCase().trim();
}
} catch (E) {
// ignore
}

return domain;
};

const normalizeAddress = (address, asObject) => {
if (!address) {
return address || '';
}

const user = address.substr(0, address.lastIndexOf('@')).normalize('NFC').toLowerCase().trim();
const domain = normalizeDomain(address.substr(address.lastIndexOf('@') + 1));
const addr = user + '@' + domain;
const unameview = user.replace(/\./g, '');
const addrview = unameview + '@' + domain;

if (returnAsObject) {
if (asObject) {
return {
user,
unameview,
Expand Down Expand Up @@ -178,7 +195,7 @@ module.exports.init = async app => {
sender = addressData.user.toString();
} else {
// current user authenticated via the username, resolve to email
authenticatedUser = authenticatedUser.replace(/\./g, '').toLowerCase(); // Normalize username to unameview
authenticatedUser = authenticatedUser.replace(/\./g, '').normalize('NFC').toLowerCase().trim(); // Normalize username to unameview
const userData = await app.db.users.collection('users').findOne({ unameview: authenticatedUser });
authenticatedUserAddress = userData.address; // main address of the user
sender = userData._id.toString(); // ID of the user
Expand All @@ -189,10 +206,8 @@ module.exports.init = async app => {
_plugin_status: 'error',
_error: 'DB error. Check DB connection, or collection names, or filter params.',
_authenticated_user: authenticatedUser,
_err_json: JSON.stringify(err)
_err_json: err.toString()
});
// throw app.reject(envelope, 'tempfail', messageinfo, 'Temporary error, please try again later.');
// return from plugin
return;
}

Expand Down
Loading

0 comments on commit 90f58ed

Please sign in to comment.