Issue Description
Stalwart v0.16.24. The code involved is unchanged on main and v0.16.27.
With a finite ban period (x:Security scanBanPeriod, authBanPeriod, abuseBanPeriod or loiterBanPeriod), the first auto-ban of an address works and lifts on time. When the same address triggers a ban again after that, it is NOT blocked, and no new x:BlockedIp is stored: the registry keeps only the first, expired entry. Bans work again only after the “Reload blocked IPs” action or a restart, which purges expired entries.
Related to, but different from, the forum topic #410 “Auto-banned IPs not taking effect until node restarted”: there, a new ban only took effect after a restart and reloading did not help. Here the first ban works, a later ban of the same address is ignored once the first one has expired, and reloading DOES fix it. Same issue as GitHub #3326 (With a ban period set, every ban after the first expired one is logged but never enforced · Issue #3326 · stalwartlabs/stalwart · GitHub), which was closed automatically and pointed to this forum.
Probable cause (crates/common/src/network/security.rs, v0.16.24):
blocked_ip_addressesis anAHashSet<IpWithTtl<IpAddr>>(L65) whoseHash/PartialEqcompare only the IP, notexpires_at(L493-503).block_ipcallsHashSet::insert(L321-335), which leaves the existing expired entry in place;is_ip_blocked(L368-373) then treats the address as not blocked.- The registry insert of the new
BlockedIpfails (likely a key conflict with the stored expired entry), andblock_ipreturnsOk(())early without writing or broadcasting anything (L338-353). - Expired entries are only removed by
BlockedIps::parse(startup /ReloadBlockedIps). The cluster insert path incrates/common/src/cache/reload.rs(L37-52) has the same issue.
Suggested fix: use HashSet::replace (or remove then insert), keeping the later expires_at so a live ban is never shortened; on a registry conflict, update the existing BlockedIp and broadcast the change; or drop expired entries lazily / with a periodic purge.
Expected Behavior
Every ban an address triggers is enforced for the configured period, whether or not an earlier ban of the same address has expired.
Actual Behavior
After the first ban of an address expires, every later ban of that address is ignored: the address keeps being served, and x:BlockedIp still holds only the first, expired entry. This lasts until “Reload blocked IPs” or a restart. All four auto-ban kinds are affected (auth failures, RCPT abuse, loitering, scanning), since they all go through Server::block_ip.
Reproduction Steps
- Fresh v0.16.24 instance, bootstrapped and restarted out of recovery mode (auto-ban is disabled in recovery mode). A client container on the same Docker network reaches the HTTP listener directly at
stalwart:8080(plain HTTP). - Set a short ban period and reload settings:
[["x:Security/set",{"update":{"singleton":{"scanBanPeriod":20000}}},"s"],["x:Action/set",{"create":{"a":{"@type":"ReloadSettings"}}},"r"]] - From the client:
curl stalwart:8080/x.php/returns 404 and the IP is banned.curl stalwart:8080/.well-known/jmapnow fails with “Connection reset by peer” (OK).x:BlockedIp/getreturns one entry for the client, reasonportScanning,expiresAt20 s aftercreatedAt. - Wait about 30 s.
/.well-known/jmapreturns 307 again: the ban lifted (OK).x:BlockedIp/getstill returns the same expired entry. - Trigger again:
/x.php/returns 404. Then/.well-known/jmapreturns 307, 307, 307: the client is NOT blocked.x:BlockedIp/getstill returns only the single expired entry; no new entry is created. - Run the action “Reload blocked IPs” (
x:Action/set {"@type":"ReloadBlockedIps"}).x:BlockedIp/getnow returns[]. - Trigger again:
/x.php/returns 404, then/.well-known/jmapfails with a connection reset: blocked again, and a newx:BlockedIp(expiring 20 s later) is stored.
Relevant Log Output
No error is logged. On the second trigger (step 5), Stalwart logs nothing about the ban and the request is served normally (307); no new BlockedIp is written. The behaviour is visible through x:BlockedIp/get and the HTTP responses described in the reproduction steps.
Stalwart Version
v0.16.x
Installation Method
Docker
Database Backend
RocksDB
Blob Storage
RocksDB
Search Engine
Internal
Directory Backend
Internal
Additional Context
In production, Stalwart runs behind Caddy, which forwards HTTP traffic to a dedicated internal listener using the PROXY protocol (overrideProxyTrustedNetworks), so Stalwart sees the real client IP. The proxy is not involved in this bug: the reproduction above talks directly to the plain HTTP listener on port 8080, with no proxy.
Current workaround: a cron job runs the “Reload blocked IPs” action (x:Action ReloadBlockedIps) every 5 minutes, which purges expired entries so that repeat offenders are banned again.
I have reviewed the documentation and FAQ and confirm that my issue is NOT addressed there.
on
I have searched this support forum (open and closed topics) and confirm this is not a duplicate.
on