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

+27
View File
@@ -1,3 +1,5 @@
import dataclasses
import json
import subprocess
import unittest
from unittest.mock import patch
@@ -6,6 +8,7 @@ from config import RuntimeSettings
from doctor import (
CheckResult,
check_cscli_json,
check_range_table,
check_systemctl_restart_capability,
run_doctor,
)
@@ -40,6 +43,30 @@ class DoctorTests(unittest.TestCase):
self.assertEqual("FAIL", result.status)
self.assertIn("Invalid JSON", result.detail)
@patch("doctor._run_cmd")
def test_check_range_table_warns_when_table_not_created_yet(self, run_cmd_mock):
run_cmd_mock.side_effect = subprocess.CalledProcessError(1, ["nft"])
settings = dataclasses.replace(_settings(), ranges_enabled=True)
result = check_range_table(settings)
self.assertEqual("WARN", result.status)
self.assertIn("creates it on its first audit", result.detail)
@patch("doctor._run_cmd")
def test_check_range_table_fails_when_a_set_is_missing(self, run_cmd_mock):
run_cmd_mock.return_value = subprocess.CompletedProcess(
args=[],
returncode=0,
stdout=json.dumps({"nftables": [{"set": {"name": "ranges-v4"}}]}),
)
settings = dataclasses.replace(_settings(), ranges_enabled=True)
result = check_range_table(settings)
self.assertEqual("FAIL", result.status)
self.assertIn("ranges-v6", result.detail)
@patch("doctor.os.geteuid", return_value=1000)
def test_check_systemctl_restart_capability_warns_when_not_root(self, _geteuid):
result = check_systemctl_restart_capability()