Skip to content

feat(rspamd): add opt-in user rate limit - #358

Merged
DavidePrincipi merged 3 commits into
mainfrom
feat-8381
Nov 27, 2026
Merged

DavidePrincipi merged 3 commits into
mainfrom
feat-8381

Conversation

@DavidePrincipi

Copy link
Copy Markdown
Member

A stolen password is often used to send thousands of spam messages through the mail server before anyone notices, and the antispam filter does not stop them because they look legitimate. This PR adds an optional limit on how many messages each authenticated user can send per hour. NethServer/dev#8381

The feature is off by default: existing installations do not change behavior. When enabled, messages over the limit get a temporary error (451 Sending rate limit exceeded, try again later): mail servers and applications sending through an authenticated account retry later, mail clients show the error. Mail received from other servers and clients in Postfix mynetworks are not limited.

  • Rspamd ratelimit module, configured by environment variables in state/rspamd.env: RSPAMD_RL_HOURLY (messages per hour, burst of 5 minutes of messages) and RSPAMD_RL_BULK_HOURLY (users that legitimately send more, default 10 times the base limit)
  • User lists in /var/lib/rspamd/ratelimit_bulk.map and ratelimit_exempt.map, editable from the Rspamd UI and reloaded without restart
  • Refused messages are tagged with the RATELIMIT_EXCEEDED symbol, for logs and a future ns8-metrics alert
  • The limit also works when antispam checks are disabled

How to test

  1. Install the module and send 30 messages as an authenticated user: all are accepted
  2. Add RSPAMD_RL_HOURLY=300 to state/rspamd.env and run systemctl --user reload rspamd
  3. Send 30 messages as the same user: 25 are accepted, the others get the 451 error
  4. Add the user name to /var/lib/rspamd/ratelimit_exempt.map, wait a minute: all messages are accepted again
  5. Repeat with a user in ratelimit_bulk.map and RSPAMD_RL_BULK_HOURLY set to a low value
  6. Remove RSPAMD_RL_HOURLY and reload: no limit applies

Generated by Claude Code:claude-opus-5-5 with nethserver-pr

A stolen password is often used to send large amounts of spam through
the mail server. Limit how many messages each authenticated user can
send per hour. Messages over the limit get a temporary error: mail
servers and applications retry them later, mail clients show the
error.

The limit is off unless RSPAMD_RL_HOURLY is a positive number. Users
listed in ratelimit_bulk.map get the RSPAMD_RL_BULK_HOURLY limit;
users in ratelimit_exempt.map are never limited. Refused messages are
tagged with RATELIMIT_EXCEEDED for logs and metrics.

Assisted-by: Claude Code:Opus 5.5
@DavidePrincipi DavidePrincipi self-assigned this Nov 26, 2026

@stephdl stephdl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on a real cluster with RSPAMD_RL_HOURLY=12 and SMTP AUTH on 587. The 451 comes as expected, a message counts once whatever the number of recipients, and the exempt and bulk maps work, also with mixed case. Looks good to me.

Could we add a robot test in tests/50__smtp? It must send through SMTP, not rspamc. Rspamd skips the rate limit for rspamc requests, so such a test would never hit the limit.

Comment thread README.md Outdated
message in the outbox. Messages refused by the limit do not count. A
message counts once, regardless of the number of recipients.

The following map files list one user name per line. They are available

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These maps show up in the Rspamd UI only when RSPAMD_RL_HOURLY is set. Otherwise the multimap rule and ratelimit.conf are not rendered. Should we say it here? An admin could try to fill the exempt list before enabling the limit.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, done in afbe07a: the README now says the maps show up in the Rspamd UI only once the rate limit is enabled, and explains how to fill them before, by editing the files inside the rspamd container.

The rate limit maps are loaded, and shown in the Rspamd UI, only when
the rate limit is enabled. Explain how to fill them before enabling
it.

Assisted-by: Claude Code:Opus 5.5
Send messages through SMTP with authentication, because Rspamd skips
the rate limit for rspamc requests. Check the burst limit of a default
user, the bulk and exempt user lists, unauthenticated messages, and
that removing the variable disables the limit.

Assisted-by: Claude Code:Opus 5.5
@DavidePrincipi

Copy link
Copy Markdown
Member Author

Thanks @stephdl for testing it! I added the robot test in 0c2af90: tests/50__smtp/00__ratelimit.robot. It sends authenticated messages through SMTP (port 465), so Rspamd applies the limit, and checks:

  • a default user gets the 451 error after the burst
  • a user in the bulk map gets a higher limit
  • a user in the exempt map is never limited
  • unauthenticated messages are not limited
  • removing RSPAMD_RL_HOURLY disables the limit

I ran the install, rate limit and uninstall suites on a test cluster with the branch image: all passed.

@DavidePrincipi
DavidePrincipi merged commit 6a3c1fb into main Nov 27, 2026
5 checks passed
@DavidePrincipi
DavidePrincipi deleted the feat-8381 branch November 27, 2026 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants