From 0f0d804a677b61e4b20c6f4e673dde8cd6863b65 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Tue, 8 Sep 2026 15:29:37 +0200 Subject: [PATCH] fix(proxy): withdraw an ACL-gated MCP path instead of leaving it to the ACL mcp/vhost.conf.j2 wrapped its whole body in `{% if not mcp_acl_gated %}`. That skipped the serve block, which was the intent, but it also skipped the withdrawal loop underneath it. An ACL-gated vhost therefore rendered no `location` for its MCP paths at all, so requests fell through to the application's generic location and the endpoint was reachable behind nothing but the SSO proxy ACL. Folding the gate into the `mcp_served` expression keeps the serve block gated while letting the withdrawal loop run in every case. With an empty `mcp_served` both declared paths differ from it, so both are withdrawn with `return 404`. The behaviour is latent today: only moodle and wordpress declare `exposure: public`, and both carry `flavor: oidc`, so `is_proxy_gated` is false and the branch is never taken. This closes the hole before a role combines public exposure with an oauth2 proxy flavor. test_mcp_vhost_exposure.py could not reach the branch: its lookup stub returned "" for every non-config kind, so mcp_acl_gated was always falsy. It now resolves is_proxy_gated and oauth2_acl, and gained the bool filter that the internal-exposure cases never needed because `mcp.exposure == 'public'` short-circuits before it. Rendering the pre-fix template under gating yields zero withdrawals; the new one yields two. --- .../sys-svc-proxy/templates/mcp/vhost.conf.j2 | 4 +- .../templates/test_mcp_vhost_exposure.py | 41 ++++++++++++++++++- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/roles/sys-svc-proxy/templates/mcp/vhost.conf.j2 b/roles/sys-svc-proxy/templates/mcp/vhost.conf.j2 index e063ad5727..5b4385afe2 100644 --- a/roles/sys-svc-proxy/templates/mcp/vhost.conf.j2 +++ b/roles/sys-svc-proxy/templates/mcp/vhost.conf.j2 @@ -5,8 +5,7 @@ and lookup('config', application_id, 'mcp.enabled', false) | bool %} {% set mcp_acl_gated = lookup('sso', application_id, 'is_proxy_gated') and (lookup('sso', application_id, 'oauth2_acl') | length) %} -{% if not mcp_acl_gated %} -{% set mcp_served = (mcp_endpoint_path | regex_replace('/+$', '')) if mcp_public else '' %} +{% set mcp_served = (mcp_endpoint_path | regex_replace('/+$', '')) if (mcp_public and not mcp_acl_gated) else '' %} {% if mcp_served %} {% set mcp_limits = lookup('config', application_id, 'mcp.limits') %} {% set mcp_zone = 'mcp_' ~ (application_id | replace('-', '_')) %} @@ -34,4 +33,3 @@ {% endif %} {% endfor %} {% endif %} -{% endif %} diff --git a/tests/unit/python/roles/sys-svc-proxy/templates/test_mcp_vhost_exposure.py b/tests/unit/python/roles/sys-svc-proxy/templates/test_mcp_vhost_exposure.py index 349b20b8f3..cc2eaea1c3 100644 --- a/tests/unit/python/roles/sys-svc-proxy/templates/test_mcp_vhost_exposure.py +++ b/tests/unit/python/roles/sys-svc-proxy/templates/test_mcp_vhost_exposure.py @@ -43,12 +43,13 @@ def meta(role: str) -> dict: return load_yaml(PROJECT_ROOT / "roles" / role / ROLE_FILE_META_MCP) -def render(role: str, *, exposure: str) -> str: +def render(role: str, *, exposure: str, sso_gated: bool = False) -> str: """Return `vhost.conf.j2` for one role at one exposure. Args: role: the role directory name. exposure: the `mcp.exposure` value to render under. + sso_gated: whether the vhost sits behind an SSO proxy ACL. """ block = meta(role) values = { @@ -64,6 +65,18 @@ def render(role: str, *, exposure: str) -> str: def config(_app, key, default=None): return values.get(key, default) + sso_values = { + "is_proxy_gated": sso_gated, + "oauth2_acl": ["group:staff"] if sso_gated else [], + } + + def lookup(kind, *args, **_kw): + if kind == "config": + return config(*args) + if kind == "sso": + return sso_values.get(args[1], "") + return "" + env = Environment( loader=FileSystemLoader(str(TEMPLATE_DIR)), undefined=StrictUndefined, @@ -73,9 +86,14 @@ def render(role: str, *, exposure: str) -> str: pattern, repl, value ) env.filters["regex_escape"] = re.escape + env.filters["bool"] = lambda value: ( + value + if isinstance(value, bool) + else str(value).strip().lower() in {"true", "1", "yes", "on"} + ) return env.get_template("vhost.conf.j2").render( application_id=role, - lookup=lambda kind, *args, **_kw: config(*args) if kind == "config" else "", + lookup=lookup, ) @@ -164,6 +182,25 @@ class TestInternalSurfaceIsWithdrawn(unittest.TestCase): "pass vacuously", ) + def test_an_acl_gated_public_surface_is_withdrawn_not_left_to_the_acl(self): + block = meta(FRONT_CONTROLLER_ROLE) + rendered = render(FRONT_CONTROLLER_ROLE, exposure="public", sso_gated=True) + self.assertNotIn( + "proxy_pass", + rendered, + "an ACL-gated surface must not be served at the edge", + ) + for declared in ( + block["endpoint"]["path"], + block["adapter"]["upstream_path"], + ): + with self.subTest(path=declared): + self.assertTrue( + is_blocked(rendered, declared), + f"{declared} is left to the SSO ACL alone instead of being " + f"withdrawn at the edge", + ) + if __name__ == "__main__": unittest.main()