Skip to content

Commit

Permalink
fix: do not rate limit IP addresses until onClose and onConnect avail…
Browse files Browse the repository at this point in the history
…able in WildDuck
  • Loading branch information
titanism committed Aug 30, 2024
1 parent 665eb9f commit 4256656
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions helpers/imap-notifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,7 @@ class IMAPNotifier extends EventEmitter {
// <https://github.com/nodemailer/wildduck/blob/48b9efb8ca4b300597b2e8f5ef4aa307ac97dcfe/lib/imap-notifier.js#L368>
// <https://github.com/nodemailer/wildduck/blob/48b9efb8ca4b300597b2e8f5ef4aa307ac97dcfe/imap-core/lib/imap-connection.js#L364C46-L365>
async releaseConnection(data, fn) {
// TODO: decrease # connections for this IP address
if (!data?.session) return fn(null, true);

// cleanup `WeakMap` instance
Expand Down
2 changes: 2 additions & 0 deletions helpers/on-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ async function onAuth(auth, session, fn) {
// NOTE: until onConnect is available for IMAP and POP3 servers
// we leverage the existing SMTP helper in the interim
// <https://github.com/nodemailer/wildduck/issues/540>
// <https://github.com/nodemailer/wildduck/issues/721>
// (see this same comment in `helpers/on-connect.js`)
//
if (this.server instanceof IMAPServer || this.server instanceof POP3Server)
await onConnectPromise.call(this, session);
Expand Down
12 changes: 11 additions & 1 deletion helpers/on-connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

const punycode = require('node:punycode');

const POP3Server = require('wildduck/lib/pop3/server');
const isFQDN = require('is-fqdn');
const { IMAPServer } = require('wildduck/imap-core');

const SMTPError = require('#helpers/smtp-error');
const ServerShutdownError = require('#helpers/server-shutdown-error');
Expand Down Expand Up @@ -118,8 +120,16 @@ async function onConnect(session, fn) {
if (session.isAllowlisted) return fn();

//
// do not allow more than 10 concurrent connections using constructor
// NOTE: until onConnect is available for IMAP and POP3 servers
// we leverage the existing SMTP helper in the interim
// <https://github.com/nodemailer/wildduck/issues/540>
// <https://github.com/nodemailer/wildduck/issues/721>
// (see this same comment in `helpers/on-auth.js`)
//
if (this.server instanceof IMAPServer || this.server instanceof POP3Server)
return fn();

// do not allow more than 10 concurrent connections using constructor
try {
// NOTE: do not change this prefix unless you also change it in `helpers/on-close.js`
const prefix = `concurrent_${this.constructor.name.toLowerCase()}_${
Expand Down
7 changes: 7 additions & 0 deletions helpers/refine-and-log-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,15 @@ function refineAndLogError(err, session, isIMAP = false, instance) {
)
err.imapResponse = 'UNAVAILABLE';
}

// TODO: we could arbitrarily render alerts if we updated wildduck
// else if (!err.imapResponse) err.imapResponse = 'ALERT';

//
// NOTE: do not set `err.response` here since WildDuck uses it internally
// (e.g. NO or BAD must be value of err.response for commands like AUTHENTICATE PLAIN
// (otherwise the client will think that the authentication succeeded)
//
}

return err;
Expand Down

0 comments on commit 4256656

Please sign in to comment.