feat(ranges): enforce LAPI range decisions in an agent-owned nft table
CI / Unit tests (push) Successful in 9s
CI / Unit tests (pull_request) Successful in 6s

crowdsec-firewall-bouncer's nftables mode strips the prefix from Range
decisions and bans only the network address, so a /24 decision blocked
a single IP. With ranges.enabled, every audit now syncs range decisions
into a dedicated `inet crowdsec-ranges` table (interval+timeout sets with
input/forward drop chains) in one atomic `nft -f` transaction, then reads
it back to confirm each range is enforced.

- element timeouts track each decision's remaining lifetime; stale
  elements are removed and extended decisions refreshed
- ranges nested in another active range are covered, not inserted
- ranges broader than min_prefix_v4/v6 (default /16, /32) are refused
- ranges still missing after the sync stay in unenforceable_ranges, so
  the node reports DEGRADED rather than HEALTHY
- the bouncer-set scan normalizes nft prefix elements to addr/len
  instead of failing the table scan on them
- doctor checks the range table; the audit log line says when a synced
  node is degraded

Disabled by default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
patrickandClaude Opus 5.5 committed 2026-10-23 09:54:26 -04:00
1 parent e76e957282
commit 7156cfd4fe
10 files changed
+671 -8

No files matched your search

+41 -1
View File
@@ -92,6 +92,7 @@ It needs sufficient access to:
- inspect `nftables` tables via `nft -j list table ...`
- flush managed CrowdSec sets when performing metadata-drift healing
- restart `crowdsec-firewall-bouncer` via `systemctl`
- create and update its own range table via `nft -f -` (only with `ranges.enabled`)
### What it changes
@@ -101,6 +102,7 @@ Depending on drift type, the agent may:
- read `nftables` kernel state
- flush **managed CrowdSec sets only**
- restart `crowdsec-firewall-bouncer`
- maintain the dedicated range table (see [Range decisions](#range-decisions)), when enabled
- write local health state / history files
### What it does not change
@@ -108,7 +110,7 @@ Depending on drift type, the agent may:
By design, the agent does **not**:
- manage unrelated firewall policy
- rewrite arbitrary `nftables` rulesets
- rewrite `nftables` rulesets other than its own range table
- invent its own ban decisions
- replace CrowdSec or the firewall bouncer
- act as a general-purpose firewall orchestration framework
@@ -210,6 +212,31 @@ This is especially important for longer-duration bans, where stale metadata can
To reduce noise, short-duration TTL variance is intentionally tolerated. The goal is to catch meaningful desynchronization, not harmless decay.
### Range decisions
`crowdsec-firewall-bouncer` (nftables mode) cannot enforce `Range` decisions: it strips the prefix and bans only the network address, so `195.178.110.0/24` blocks just `195.178.110.0`. The agent pairs such a range with that address so it is not counted as membership drift, and handles it as below.
With `ranges.enabled: false` (the default), the range is reported in `unenforceable_ranges` and the node status is `DEGRADED`.
With `ranges.enabled: true`, every audit syncs the range decisions into a table the agent owns, in one atomic `nft -f` transaction:
```text
table inet crowdsec-ranges {
set ranges-v4 { type ipv4_addr; flags interval, timeout; }
set ranges-v6 { type ipv6_addr; flags interval, timeout; }
chain input { type filter hook input priority -10; policy accept; ip saddr @ranges-v4 drop; ip6 saddr @ranges-v6 drop; }
chain forward { type filter hook forward priority -10; policy accept; ip saddr @ranges-v4 drop; ip6 saddr @ranges-v6 drop; }
}
```
- each range is added with its decision's remaining lifetime as the element timeout, so bans expire on their own even if the agent stops
- elements whose decision is gone are removed, and elements whose decision was extended are refreshed
- a range inside another active range is covered by the wider one rather than inserted (interval sets reject overlaps)
- ranges broader than `min_prefix_v4` / `min_prefix_v6` are refused, so a bad decision cannot cut off a node
- after applying, the table is read back; any range not present is reported in `unenforceable_ranges` (status `DEGRADED`)
To remove the table after disabling the feature: `nft delete table inet crowdsec-ranges`.
---
## Project structure
@@ -221,6 +248,7 @@ To reduce noise, short-duration TTL variance is intentionally tolerated. The goa
├── config.py # Runtime settings + logging setup
├── auditor.py # LAPI vs nft reconciliation and drift fingerprinting
├── healing.py # Heal gating + post-heal verification
├── ranges.py # Range decision enforcement in a dedicated nft table
├── doctor.py # Preflight diagnostics
├── reporter.py # Local state + heal history writer
├── config.yaml # Runtime defaults (overridable)
@@ -308,6 +336,18 @@ Supported environment overrides:
If no explicit set allowlist is given, the agent defaults to managing CrowdSec-style `*-cscli` sets where applicable. This keeps healing scope intentionally narrow.
### Range enforcement
```yaml
ranges:
enabled: true # default false
table: "crowdsec-ranges" # inet table the agent owns
min_prefix_v4: 16 # refuse IPv4 ranges broader than /16
min_prefix_v6: 32 # refuse IPv6 ranges broader than /32
```
See [Range decisions](#range-decisions).
---
## Operational notes