Skip to content

Mail: limit how many messages an authenticated user can send #8381

In review

Description

@DavidePrincipi

When a user password is stolen, the mailbox is used to send thousands of spam messages before anyone notices. The antispam filter does not stop it: such messages look legitimate.

The goal is to limit how many messages each user can send, and alert the administrator.

Proposed solution

Limit how many messages each authenticated user can send in a given time. Mail received from other servers is not affected.

  • When a user exceeds the limit, further messages are refused until the limit refills. Possible approaches:
    • temporary refusal: the mail client retries later, so a legitimate user loses nothing;
    • permanent refusal, with a clear message for the user;
    • both: a temporary refusal first, and a permanent one if the user keeps sending.
  • Some users legitimately send more (newsletters, scanners, applications): the administrator can give them higher limits or exclude them.
  • Opt-in: the feature is off by default, so existing installations do not change behavior after the update. The administrator enables it explicitly.
  • Configuration: no UI for now. The limits are set with environment variables of the Mail module. Ideally one or two variables drive the whole configuration, and the other values are derived from them with formulas in the configuration templates. For example, RSPAMD_RL_HOURLY sets the maximum number of messages per hour for each user: if it is not set, empty, 0 or -1, the whole rate limiter is off.
  • Notification: the Mail module exports metrics about users hitting the limit to the Metrics module (ns8-metrics). An alert rule notifies the administrator through the existing alert channels (mail, cluster alert list, my.nethesis.it), so the password can be changed quickly. A Grafana dashboard could show the trend.

The Rspamd ratelimit module, already part of the Mail application, provides the limits. A quick test with Mail 2.0 confirmed that the approaches above are feasible with configuration only, and that the Rspamd configuration templates can compute values from environment variables.

Alternative solutions

  • A configuration UI in the Filter page: can be added later, on top of the environment variables.
  • Limits applied by the mail server itself (Postfix), for example with a policy service.
  • Notification only, without blocking: simpler, but it does not stop the damage.

See also

Activity

  1. moved this from Backlog to ToDo in NethServeron Nov 25, 2026
  2. moved this from ToDo to In Progress in NethServeron Nov 26, 2026
  3. DavidePrincipi commented on Nov 27, 2026

    @DavidePrincipi
    MemberAuthor

    Testing release: Mail 2.0.0-beta.6 (NethServer/ns8-mail#358)

    The feature is experimental and opt-in. The automated tests already cover the basic limit, the bulk and exempt lists, unauthenticated messages and disabling. Test cases left for manual QA:

    1. Update keeps the old behavior -- update an existing Mail instance: no limit applies, and /var/lib/rspamd/ratelimit_bulk.map and ratelimit_exempt.map exist inside the rspamd container.
    2. Mail clients -- set RSPAMD_RL_HOURLY=12 in state/rspamd.env, run systemctl --user reload rspamd, then send some messages from Thunderbird, Outlook and a phone on port 587: after the first message, the client shows Sending rate limit exceeded, try again later and keeps the message in the outbox.
    3. Webmail -- repeat with Roundcube and WebTop: check whether they are limited like other mail clients, and how they show the error.
    4. Applications that retry -- send through an authenticated relay from another mail server, a scanner or an application: messages over the limit are queued and delivered later, not lost.
    5. Many recipients -- one message to several recipients counts as one message.
    6. Lists from the Rspamd UI -- while the limit is enabled, the two maps are listed in the Rspamd UI configuration tab: add a user to the exempt list and check it applies within a minute, without reload. Try a user name with mixed case.
    7. Antispam disabled -- disable antispam checks in the Filter page: the limit still applies.
    8. Wrong values -- RSPAMD_RL_HOURLY empty, 0, -1 or abc: no limit applies, and Rspamd starts normally.
    9. Logs -- refused messages are logged by Rspamd with the user name and the RATELIMIT_EXCEEDED symbol, and appear in the Rspamd UI history.
    10. Backup and restore -- restore a backup of a Mail instance with the limit enabled and a user in the exempt list: the variable and the list are restored.
  4. added
    testing
    on Nov 27, 2026
  5. moved this from In Progress to Testing in NethServeron Nov 27, 2026
  6. DavidePrincipi commented on Nov 27, 2026

    @DavidePrincipi
    MemberAuthor

    VERIFIED

    Tested Mail 2.0.0-beta.6 on a test cluster, following the test cases above. Not tested:

    • Desktop and phone mail clients (Thunderbird, Outlook, iOS/Android): only the server side was checked, with SMTP scripts and an authenticated Postfix relay.
    • WebTop: not installed. It uses the same Mail submission service as Roundcube, which shows the 451 error in the compose window and keeps the draft.
    • Rspamd UI in a browser: map editing was tested through the same Rspamd API. An empty body is refused, a single empty line empties a map (now noted in the README).

    Note: during the backup/restore test, the restored instance's Rspamd failed to start because the replaced instance still held its ports, and needed a manual restart. This is not related to the rate limit.

  7. added
    verified
    on Nov 27, 2026
  8. removed
    testing
    on Nov 27, 2026
  9. moved this from Testing to Verified in NethServeron Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

verified

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions