fix: replace shorter CrowdSec bans instead of adding beside them #1

Merged
patrick merged 2 commits from fix/multi-set-ttl-drift into main 2026-10-14 20:29:00 +00:00
Owner

Problem

When the engine added a ban for an IP that already had a shorter one in
CrowdSec, the longer ban never reached the firewall. The bouncer keeps the
entry it already holds for an address, so the kernel entry kept the shorter
timeout and lapsed with it while LAPI still reported the IP as banned.

Seen on Argus: an IP held a 4-hour CrowdSec ban, the engine added a 24-hour
ban, and the nft element still showed the 4-hour timeout until the bouncer
was restarted. Reconciliation could not catch it, because it only checks that
LAPI holds a ban for the target.

Fix

When CrowdSec holds a ban on the same IP or prefix that ends more than a
minute before the pending one, the runner deletes it and then adds its own.

  • Applies to the engine's own upgrades and to bans added by CrowdSec scenarios.
  • Uses the existing delete / delete-range wrapper actions, so no wrapper
    reinstall is needed.
  • Targets with no existing ban, or a longer one, get a plain add as before.
    Covering ranges and addresses inside a prefix are not touched.
  • The delete and the add count as one attempt, and both command timeouts are
    reserved before the delete so a budget cannot stop between them.

Behaviour changes

  • The minimum --enforcement-budget-seconds rises from 80 to 120, so a
    replacement always fits after a full-timeout state query. The example unit
    passes 240.
  • If the add fails after the delete succeeds, the target is unbanned until a
    later run retries it. The stored error says the shorter ban was already
    removed.

Not covered

  • Bans already accepted in LAPI but missing from the firewall are not pending,
    so this does not repair existing drift.
  • Range replacement follows the same logic but has not been checked against
    the real bouncer.

Testing

  • New tests/test_ban_replacement.py covers replacement, the no-delete cases,
    ranges, and both failure orders.
  • A budget test covers reserving both timeouts before the delete.
  • Delete and re-add was confirmed by hand on Argus for a single IP: the nft
    element took the 24-hour timeout.
  • Not yet run against the live LAPI and bouncer.

🤖 Generated with Claude Code

## Problem When the engine added a ban for an IP that already had a shorter one in CrowdSec, the longer ban never reached the firewall. The bouncer keeps the entry it already holds for an address, so the kernel entry kept the shorter timeout and lapsed with it while LAPI still reported the IP as banned. Seen on Argus: an IP held a 4-hour CrowdSec ban, the engine added a 24-hour ban, and the nft element still showed the 4-hour timeout until the bouncer was restarted. Reconciliation could not catch it, because it only checks that LAPI holds a ban for the target. ## Fix When CrowdSec holds a ban on the same IP or prefix that ends more than a minute before the pending one, the runner deletes it and then adds its own. - Applies to the engine's own upgrades and to bans added by CrowdSec scenarios. - Uses the existing `delete` / `delete-range` wrapper actions, so no wrapper reinstall is needed. - Targets with no existing ban, or a longer one, get a plain add as before. Covering ranges and addresses inside a prefix are not touched. - The delete and the add count as one attempt, and both command timeouts are reserved before the delete so a budget cannot stop between them. ## Behaviour changes - The minimum `--enforcement-budget-seconds` rises from 80 to 120, so a replacement always fits after a full-timeout state query. The example unit passes 240. - If the add fails after the delete succeeds, the target is unbanned until a later run retries it. The stored error says the shorter ban was already removed. ## Not covered - Bans already accepted in LAPI but missing from the firewall are not pending, so this does not repair existing drift. - Range replacement follows the same logic but has not been checked against the real bouncer. ## Testing - New `tests/test_ban_replacement.py` covers replacement, the no-delete cases, ranges, and both failure orders. - A budget test covers reserving both timeouts before the delete. - Delete and re-add was confirmed by hand on Argus for a single IP: the nft element took the 24-hour timeout. - Not yet run against the live LAPI and bouncer. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
patrick added 2 commits 2026-10-14 03:02:12 +00:00
The bouncer keeps one nft set per decision origin, so an IP with both a
crowdsec ban and a later, longer cscli ban sits in two sets. The kernel
scan kept whichever set it read last, so the short TTL could be compared
against the longest LAPI decision and reported as metadata drift.

Keep the longest expiry per IP instead, and log per-IP evidence (expected
vs kernel seconds, source sets, LAPI decisions) whenever drift is flagged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A decision created moments before an audit shows up as missing=1 until
the bouncer's next poll, which triggered a bouncer restart for nothing.

When the audit finds a membership mismatch, wait membership_recheck_delay
seconds (default 30, 0 disables) and audit again; only drift that is still
present is reported and healed. Metadata drift is not delayed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
patrick merged commit 053991669b into main 2026-10-14 20:29:00 +00:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: patrick/threat-engine-agent#1