From 2393e8ddba22e67ee51be153ac07eb46b4a19ea2 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Fri, 30 Oct 2026 16:52:06 +0100 Subject: [PATCH] fix(web-app-keycloak): reject scoped IPv6 admin networks, drop module docstring Addresses two CodeRabbit findings on PR #1261. 1. Scoped IPv6 (thread 4421596356): ipaddress.ip_network accepts a scope ID, so `fe80::1%eth0` resolved to `fe80::1%eth0/128` and the vhost would carry `allow fe80::1%eth0/128;`, which nginx refuses to load. kc_admin_networks now raises AnsibleFilterError for any entry whose network address carries a scope ID. test_admin_allowlist.py covers a scoped address and a scoped CIDR; both fail against the previous filter. 2. Module docstring (thread 4421596351): removed; it described the module instead of documenting parameters or a trip-wire. Co-Authored-By: Claude Opus 5.5 --- .../web-app-keycloak/filter_plugins/admin_networks.py | 10 +++++++--- .../web-app-keycloak/templates/test_admin_allowlist.py | 2 ++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/roles/web-app-keycloak/filter_plugins/admin_networks.py b/roles/web-app-keycloak/filter_plugins/admin_networks.py index 84c5ed2f8b..8dc8d3800a 100644 --- a/roles/web-app-keycloak/filter_plugins/admin_networks.py +++ b/roles/web-app-keycloak/filter_plugins/admin_networks.py @@ -1,5 +1,3 @@ -"""Source networks the Keycloak vhost serves its admin paths to.""" - from __future__ import annotations import ipaddress @@ -39,12 +37,18 @@ def kc_admin_networks(networks: Any) -> list[str]: "address or a CIDR block" ) try: - network = str(ipaddress.ip_network(entry.strip(), strict=True)) + parsed = ipaddress.ip_network(entry.strip(), strict=True) except ValueError as exc: raise AnsibleFilterError( f"kc_admin_networks: {CONFIG_PATH} entry {entry!r} is not an " f"address or a CIDR block without host bits: {exc}" ) from exc + if getattr(parsed.network_address, "scope_id", None): + raise AnsibleFilterError( + f"kc_admin_networks: {CONFIG_PATH} entry {entry!r} carries an " + "IPv6 scope ID, which nginx allow/deny does not accept" + ) + network = str(parsed) if network not in resolved: resolved.append(network) return resolved diff --git a/tests/unit/python/roles/web-app-keycloak/templates/test_admin_allowlist.py b/tests/unit/python/roles/web-app-keycloak/templates/test_admin_allowlist.py index d155a27c6d..2b218f263b 100644 --- a/tests/unit/python/roles/web-app-keycloak/templates/test_admin_allowlist.py +++ b/tests/unit/python/roles/web-app-keycloak/templates/test_admin_allowlist.py @@ -318,6 +318,8 @@ class TestDeclaredValues(unittest.TestCase): ["auth.example.com"], [""], [42], + ["fe80::1%eth0"], + ["fe80::%eth0/64"], ): with self.subTest(value=value), self.assertRaises(AnsibleFilterError): admin_networks(value)