Skip to content

Commit

Permalink
Rollback unsigned encrypted eml logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cdabupulsarsd committed Jan 24, 2025
1 parent a91c18b commit bf2ad3e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 150 deletions.
154 changes: 7 additions & 147 deletions lib/mail-drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class MailDrop {
const regularId = this.seqIndex.get();
const signedId = this.seqIndex.get();
const encryptedId = this.seqIndex.get();
const unsignedEncryptedId = this.seqIndex.get();

let messageInfo = {
'message-id': '<>',
Expand Down Expand Up @@ -255,27 +254,16 @@ class MailDrop {
});
//#endregion - Unsigned and Unencrypted
} else {
const {
regularStream,
recipientsRegularValid,
regularEnvelope,
signedStream,
recipientsSignedValid,
signedEnvelope,
encryptedStream,
recipientsEncryptedValid,
encryptedEnvelope,
encryptedUnsignedStream,
recipientsUnsignedEncryptedValid,
unsignedEncryptedEnvelope
} = await envelope.signEML(raw);
const { regularStream, recipientsRegularValid, regularEnvelope,
signedStream, recipientsSignedValid, signedEnvelope,
encryptedStream, recipientsEncryptedValid, encryptedEnvelope } = await envelope.signEML(raw);

//#region - Regular Recipients
if (recipientsRegularValid && recipientsRegularValid.length > 0) {
// Create Regular Envelope
regularEnvelope.id = regularId;
regularEnvelope.to = recipientsRegularValid;

// Create Regular MessageInfo
let regularMessageInfo = _.cloneDeep(messageInfo);

Expand Down Expand Up @@ -397,13 +385,13 @@ class MailDrop {
});
}
//#endregion - Regular Recipients

//#region - Encrypted Recipients
if (recipientsEncryptedValid && recipientsEncryptedValid.length > 0) {
// Create Encrypted Envelope
encryptedEnvelope.id = encryptedId;
encryptedEnvelope.to = recipientsEncryptedValid;

// Create Encrypted MessageInfo
let encryptedMessageInfo = _.cloneDeep(messageInfo);

Expand Down Expand Up @@ -566,7 +554,7 @@ class MailDrop {
signedBodyHash.once('error', err => {
signedMessageHashStream.emit('error', err);
});

signedMessageHashStream.on('hash', data => {
signedEnvelope.sourceMd5 = data.hash;
signedMessageInfo.md5 = (data.hash || '?').substr(0, 12);
Expand Down Expand Up @@ -648,134 +636,6 @@ class MailDrop {
});
}
//#endregion - Signed Recipients

//#region - Encrypted Unsigned Recipients
if (recipientsUnsignedEncryptedValid && recipientsUnsignedEncryptedValid.length > 0) {
// Create Encrypted Envelope
unsignedEncryptedEnvelope.id = unsignedEncryptedId;
unsignedEncryptedEnvelope.to = recipientsUnsignedEncryptedValid;

// Create Encrypted MessageInfo
let unsignedEncryptedMessageInfo = _.cloneDeep(messageInfo);

// Create Encrypted StreamHash
let encryptedMessageHashStream = new StreamHash({
algo: 'md5'
});

// Create a new BodyHashStream and pipe the signed/encrypted Stream
let encryptedBodyHash = new BodyHashStream('relaxed/relaxed', unsignedEncryptedEnvelope.dkim.hashAlgo);
encryptedBodyHash.on('hash', bodyHash => {
// store relaxed body hash for signing
unsignedEncryptedEnvelope.dkim.bodyHash = bodyHash;
unsignedEncryptedEnvelope.bodySize = encryptedBodyHash.byteLength;
// re-write values for unsignedEncryptedMessageInfo after signing/encrypting
// BODY SIZE
unsignedEncryptedMessageInfo.body = unsignedEncryptedEnvelope.bodySize || 0;
// SUBJECT
let subjectUpdated = unsignedEncryptedEnvelope.headers.getFirst('subject');
try {
subjectUpdated = libmime.decodeWords(subjectUpdated);
} catch (E) {
// ignore
}
subjectUpdated = subjectUpdated.replace(/[\x00-\x1F]+/g, '_').trim(); //eslint-disable-line no-control-regex
if (subjectUpdated.length > 128) {
subjectUpdated = subjectUpdated.substr(0, 128) + '...[+' + (subjectUpdated.length - 128) + 'B]';
}
unsignedEncryptedMessageInfo.subject = subjectUpdated;
// RECIPIENTS/TO
unsignedEncryptedMessageInfo.to = [].concat(recipientsUnsignedEncryptedValid || []).join(',') || '<>';
});

encryptedUnsignedStream.pipe(encryptedBodyHash);

encryptedBodyHash.pipe(encryptedMessageHashStream);
encryptedBodyHash.once('error', err => {
encryptedMessageHashStream.emit('error', err);
});

encryptedMessageHashStream.on('hash', data => {
unsignedEncryptedEnvelope.sourceMd5 = data.hash;
unsignedEncryptedMessageInfo.md5 = (data.hash || '?').substr(0, 12);
});

// store stream to db
this.queue.store(unsignedEncryptedId, encryptedMessageHashStream, err => {
if (err) {
if (source.readable) {
source.resume(); // let the original stream to end normally before displaying the error message
}
if (/Error$/.test(err.name)) {
log.error('Queue/' + process.pid, 'id=%s %s NOQUEUE store "%s" (%s)', unsignedEncryptedEnvelope.sessionId, unsignedEncryptedId, err.message, unsignedEncryptedMessageInfo.format());
let keys = unsignedEncryptedMessageInfo.keys();
['interface', 'originhost', 'transhost', 'transtype', 'user'].forEach(key => {
if (unsignedEncryptedEnvelope[key] && !(key in keys)) {
keys[key] = unsignedEncryptedEnvelope[key];
}
});
keys.error = err.message;
plugins.handler.remotelog(unsignedEncryptedId, false, 'NOQUEUE', keys);
return this.queue.removeMessage(unsignedEncryptedId, () => callback(err));
}
return callback(err);
}

plugins.handler.runHooks('message:queue', [unsignedEncryptedEnvelope, unsignedEncryptedMessageInfo], err => {
if (err) {
return setImmediate(() => this.queue.removeMessage(unsignedEncryptedId, () => callback(err)));
}

let headerFrom = unsignedEncryptedEnvelope.headers
.getDecoded('from')
.reverse()
.map(entry => entry.value)
.join(' ');

// convert headers object to a serialized array
unsignedEncryptedEnvelope.headers = unsignedEncryptedEnvelope.headers ? unsignedEncryptedEnvelope.headers.getList() : [];

// inject message headers to the stored stream
this.queue.setMeta(unsignedEncryptedId, unsignedEncryptedEnvelope, err => {
if (err) {
log.error('Queue/' + process.pid, 'id=%s %s NOQUEUE meta "%s" (%s)', unsignedEncryptedEnvelope.sessionId, unsignedEncryptedId, err.message, unsignedEncryptedMessageInfo.format());
let keys = unsignedEncryptedMessageInfo.keys();
['interface', 'originhost', 'transhost', 'transtype', 'user'].forEach(key => {
if (unsignedEncryptedEnvelope[key] && !(key in keys)) {
keys[key] = unsignedEncryptedEnvelope[key];
}
});
keys.headerFrom = headerFrom;
keys.error = err.message;
plugins.handler.remotelog(unsignedEncryptedId, false, 'NOQUEUE', keys);
return this.queue.removeMessage(unsignedEncryptedId, () => callback(err));
}

// push delivery data
this.queue.push(unsignedEncryptedId, unsignedEncryptedEnvelope, err => {
let keys = unsignedEncryptedMessageInfo.keys();
['interface', 'originhost', 'transhost', 'transtype', 'user'].forEach(key => {
if (unsignedEncryptedEnvelope[key] && !(key in keys)) {
keys[key] = unsignedEncryptedEnvelope[key];
}
});
keys.headerFrom = headerFrom;
if (err) {
log.error('Queue/' + process.pid, 'id=%s %s NOQUEUE push "%s" (%s)', unsignedEncryptedEnvelope.sessionId, unsignedEncryptedId, err.message, unsignedEncryptedMessageInfo.format());
keys.error = err.message;
plugins.handler.remotelog(unsignedEncryptedId, false, 'NOQUEUE', keys);
return this.queue.removeMessage(unsignedEncryptedId, () => callback(err));
}

log.info('Queue/' + process.pid, 'id=%s %s QUEUED (%s)', unsignedEncryptedEnvelope.sessionId, unsignedEncryptedId, unsignedEncryptedMessageInfo.format());
plugins.handler.remotelog(unsignedEncryptedId, false, 'QUEUED', keys);
return setImmediate(() => callback(null, 'Message queued as ' + unsignedEncryptedId));
});
});
});
});
}
//#endregion - Encrypted Unsigned Recipients
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@pulsar-sd/zone-mta",
"private": false,
"version": "0.1.17",
"version": "0.1.18",
"description": "Tiny outbound MTA",
"main": "app.js",
"scripts": {
Expand Down

0 comments on commit bf2ad3e

Please sign in to comment.