|
| 1 | +// Papercut |
| 2 | +// |
| 3 | +// Copyright © 2008 - 2012 Ken Robertson |
| 4 | +// Copyright © 2013 - 2025 Jaben Cargman |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | + |
| 19 | +using SmtpServer; |
| 20 | +using SmtpServer.Mail; |
| 21 | +using SmtpServer.Protocol; |
| 22 | +using SmtpServer.Storage; |
| 23 | + |
| 24 | +namespace Papercut.Infrastructure.Smtp.RateLimiting; |
| 25 | + |
| 26 | +using ILogger = Serilog.ILogger; |
| 27 | + |
| 28 | +/// <summary> |
| 29 | +/// Mailbox filter that enforces a message reception rate limit, letting developers |
| 30 | +/// test how their application behaves against a mail server that is throttling them. |
| 31 | +/// |
| 32 | +/// Returning false from CanAcceptFromAsync would reject with a hardcoded 550, so this |
| 33 | +/// throws SmtpResponseException instead -- the SmtpServer session loop writes the |
| 34 | +/// carried response back to the client verbatim, which is what allows a configurable |
| 35 | +/// reply code (421/451/452/550). |
| 36 | +/// </summary> |
| 37 | +internal sealed class RateLimitMailboxFilter(SmtpRateLimiter rateLimiter, ILogger logger) : IMailboxFilter |
| 38 | +{ |
| 39 | + public Task<bool> CanAcceptFromAsync( |
| 40 | + ISessionContext context, |
| 41 | + IMailbox from, |
| 42 | + int size, |
| 43 | + CancellationToken cancellationToken) |
| 44 | + { |
| 45 | + var decision = rateLimiter.TryAcquire(); |
| 46 | + |
| 47 | + if (decision.IsAllowed) |
| 48 | + { |
| 49 | + logger.Verbose( |
| 50 | + "SMTP message accepted against rate limit {RateLimit} ({Count} so far this window)", |
| 51 | + rateLimiter.Limit, |
| 52 | + decision.Count); |
| 53 | + |
| 54 | + return Task.FromResult(true); |
| 55 | + } |
| 56 | + |
| 57 | + logger.Warning( |
| 58 | + "Rejected SMTP MAIL FROM command from {RemoteIp} with {ReplyCode} - rate limit {RateLimit} reached, resets in {RetryAfter}", |
| 59 | + context.GetRemoteIpAddress(), |
| 60 | + (int)rateLimiter.Limit.ReplyCode, |
| 61 | + rateLimiter.Limit, |
| 62 | + decision.RetryAfter); |
| 63 | + |
| 64 | + // quit: true closes the session after the reply. Without it the session loop |
| 65 | + // appends ", N retry(ies) remaining." to the message, which is noise in a |
| 66 | + // response the client is meant to parse. |
| 67 | + throw new SmtpResponseException( |
| 68 | + new SmtpResponse(rateLimiter.Limit.ReplyCode, rateLimiter.Limit.ReplyMessage), |
| 69 | + quit: true); |
| 70 | + } |
| 71 | + |
| 72 | + public Task<bool> CanDeliverToAsync( |
| 73 | + ISessionContext context, |
| 74 | + IMailbox to, |
| 75 | + IMailbox from, |
| 76 | + CancellationToken cancellationToken) |
| 77 | + { |
| 78 | + // The limit counts messages, not recipients -- it is applied once at MAIL FROM. |
| 79 | + return Task.FromResult(true); |
| 80 | + } |
| 81 | +} |
0 commit comments