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
View File
@@ -12,6 +12,9 @@ DEFAULT_MEMBERSHIP_RECHECK_DELAY = 30
DEFAULT_CONFIG_PATH = "config.yaml"
DEFAULT_NODE_NAME = "Unknown Node"
DEFAULT_ENFORCEMENT_TARGETS = (("ip", "crowdsec"), ("ip6", "crowdsec6"))
DEFAULT_RANGES_TABLE = "crowdsec-ranges"
DEFAULT_RANGES_MIN_PREFIX_V4 = 16
DEFAULT_RANGES_MIN_PREFIX_V6 = 32
@dataclass(frozen=True)
@@ -27,6 +30,10 @@ class RuntimeSettings:
enforcement_sets: tuple[str, ...]
config_path: str
membership_recheck_delay: int = DEFAULT_MEMBERSHIP_RECHECK_DELAY
ranges_enabled: bool = False
ranges_table: str = DEFAULT_RANGES_TABLE
ranges_min_prefix_v4: int = DEFAULT_RANGES_MIN_PREFIX_V4
ranges_min_prefix_v6: int = DEFAULT_RANGES_MIN_PREFIX_V6
@dataclass(frozen=True)
@@ -256,6 +263,16 @@ def load_runtime_settings():
)
enforcement_targets = _resolve_enforcement_targets(config)
enforcement_sets = _resolve_enforcement_sets(config)
ranges_enabled = cfg_get(config, "ranges", "enabled", default=False) is True
ranges_table = (
str(cfg_get(config, "ranges", "table", default=DEFAULT_RANGES_TABLE)).strip()
)
ranges_min_prefix_v4 = as_int(
cfg_get(config, "ranges", "min_prefix_v4"), DEFAULT_RANGES_MIN_PREFIX_V4
)
ranges_min_prefix_v6 = as_int(
cfg_get(config, "ranges", "min_prefix_v6"), DEFAULT_RANGES_MIN_PREFIX_V6
)
return RuntimeSettings(
node_name=node_name,
@@ -269,6 +286,10 @@ def load_runtime_settings():
enforcement_sets=enforcement_sets,
config_path=config_path,
membership_recheck_delay=membership_recheck_delay,
ranges_enabled=ranges_enabled,
ranges_table=ranges_table,
ranges_min_prefix_v4=ranges_min_prefix_v4,
ranges_min_prefix_v6=ranges_min_prefix_v6,
)
@@ -322,6 +343,26 @@ def validate_runtime_settings(settings):
)
)
if settings.ranges_enabled:
if not settings.ranges_table:
issues.append(ValidationIssue("ERROR", "ranges.table must not be empty."))
if not 1 <= settings.ranges_min_prefix_v4 <= 32:
issues.append(
ValidationIssue(
"ERROR",
"ranges.min_prefix_v4 must be within [1, 32] "
f"(got {settings.ranges_min_prefix_v4})",
)
)
if not 1 <= settings.ranges_min_prefix_v6 <= 128:
issues.append(
ValidationIssue(
"ERROR",
"ranges.min_prefix_v6 must be within [1, 128] "
f"(got {settings.ranges_min_prefix_v6})",
)
)
if settings.check_jitter_pct < 0.0 or settings.check_jitter_pct > 0.90:
issues.append(
ValidationIssue(