Skip to content

Commit

Permalink
fix(loop): reject messages with too many Received headers
Browse files Browse the repository at this point in the history
  • Loading branch information
andris9 committed Feb 2, 2024
1 parent 26e4896 commit cbffa6d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,14 @@ module.exports = {
addSignatureTimestamp: false,
// Time validity of the signature given in seconds, for default value see below
signatureExpireIn: 0
},

// Make sure messages have all required headers like Date or Message-ID
'core/delivery-loop': {
enabled: ['receiver', 'main'],

// Reject messages with higher Received count
maxHops: 35
}
},

Expand Down
28 changes: 28 additions & 0 deletions plugins/core/delivery-loop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

// Set module title
module.exports.title = 'DeliveryLoop';

// Initialize the module
module.exports.init = (app, done) => {
const MAX_HOPS = app.config.maxHops || 25;

app.addHook('message:headers', (envelope, messageInfo, next) => {
let receivedLength = envelope.headers.get('Received').length;

if (receivedLength > MAX_HOPS) {
// too many hops
app.logger.info('DeliveryLoop', 'Too many hops (%s)! Delivery loop detected for %s, rejecting message', receivedLength, envelope.id);
let err = new Error(`A delivery loop was detected which causes this email which caused the email to be undeliverable (${receivedLength})`);
err.name = 'SMTPResponse';
err.responseCode = 500;
return next(err);
}

// allow the message to pass
return next();
});

// all set up regarding this plugin
done();
};

0 comments on commit cbffa6d

Please sign in to comment.