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

+30
View File
@@ -85,6 +85,35 @@ def check_nft_tables(settings):
return CheckResult("nft_tables", "PASS", ", ".join(summaries))
def check_range_table(settings):
if not settings.ranges_enabled:
return CheckResult("range_table", "PASS", "Range enforcement disabled.")
try:
res = _run_cmd(["/usr/sbin/nft", "-j", "list", "table", "inet", settings.ranges_table])
data = json.loads(res.stdout or "{}")
set_names = {
item["set"].get("name")
for item in data.get("nftables", [])
if isinstance(item.get("set"), dict)
}
missing = {"ranges-v4", "ranges-v6"} - set_names
if missing:
return CheckResult(
"range_table",
"FAIL",
f"inet {settings.ranges_table} lacks sets: {', '.join(sorted(missing))}",
)
return CheckResult("range_table", "PASS", f"inet {settings.ranges_table} present.")
except subprocess.CalledProcessError:
return CheckResult(
"range_table",
"WARN",
f"inet {settings.ranges_table} not found; the agent creates it on its first audit.",
)
except Exception as e:
return CheckResult("range_table", "FAIL", f"Unexpected error: {e}")
def check_state_paths(settings):
state_dir = os.path.dirname(os.path.abspath(settings.state_file))
history_file = os.path.splitext(settings.state_file)[0] + ".history"
@@ -150,6 +179,7 @@ def run_doctor(settings, output=print):
check_runtime_settings(settings),
check_cscli_json(),
check_nft_tables(settings),
check_range_table(settings),
check_state_paths(settings),
check_systemctl_restart_capability(),
]