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

+29
View File
@@ -63,6 +63,35 @@ class RuntimeSettingsEnforcementTests(unittest.TestCase):
self.assertEqual(0.90, settings.check_jitter_pct)
@patch("config.load_config")
def test_ranges_settings_default_to_disabled(self, load_config_mock):
load_config_mock.return_value = {}
with patch.dict(os.environ, {}, clear=True):
settings = load_runtime_settings()
self.assertFalse(settings.ranges_enabled)
self.assertEqual("crowdsec-ranges", settings.ranges_table)
self.assertEqual(16, settings.ranges_min_prefix_v4)
self.assertEqual(32, settings.ranges_min_prefix_v6)
@patch("config.load_config")
def test_ranges_settings_loaded_and_validated(self, load_config_mock):
load_config_mock.return_value = {
"ranges": {"enabled": True, "table": "r", "min_prefix_v4": 0, "min_prefix_v6": 48}
}
with patch.dict(os.environ, {}, clear=True):
settings = load_runtime_settings()
issues = validate_runtime_settings(settings)
self.assertTrue(settings.ranges_enabled)
self.assertEqual("r", settings.ranges_table)
self.assertEqual(48, settings.ranges_min_prefix_v6)
self.assertTrue(
any(i.level == "ERROR" and "min_prefix_v4" in i.message for i in issues)
)
def test_validate_runtime_settings_reports_errors(self):
settings = RuntimeSettings(
node_name="n1",