From 4397f4f57fb0b1f41c308a540c774ed2c5db8fa3 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Thu, 15 Oct 2026 16:35:09 +0200 Subject: [PATCH] fix(lookup): serve assets from the CDN sibling of the vhost's network The asset host took the canonical CDN domain and only guessed its scheme from the suffix, so pages on the onion sibling loaded their libraries from the clearnet CDN, which their CSP and Tor reach do not allow. It now takes web-svc-cdn's url.base, which follows the network of the vhost being rendered; without the internal CDN it stays jsDelivr. Co-Authored-By: Claude Opus 5.5 (1M context) --- plugins/lookup/asset.py | 13 ++++++------- tests/unit/python/plugins/lookup/test_asset.py | 16 ++++++++++------ 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/plugins/lookup/asset.py b/plugins/lookup/asset.py index fbf74ec125..df0c258b07 100644 --- a/plugins/lookup/asset.py +++ b/plugins/lookup/asset.py @@ -10,14 +10,14 @@ from ansible.plugins.lookup import LookupBase from plugins.filter.role_path_by_app_id import abs_role_path_by_application_id from utils.cache.files import read_text -from utils.tls_common import is_onion_domain _JSDELIVR = "https://cdn.jsdelivr.net" def resolve_host(variables, loader, templar) -> str: - """The asset host for the current deployment: the internal CDN origin - when web-svc-cdn is deployed with flavor internal, else jsdelivr.""" + """The asset host for the current deployment: the internal CDN origin in + the network of the vhost being rendered when web-svc-cdn is deployed with + flavor internal, else jsdelivr.""" if "web-svc-cdn" not in (variables.get("group_names") or []): return _JSDELIVR flavor = lookup_loader.get("config", loader=loader, templar=templar).run( @@ -25,11 +25,10 @@ def resolve_host(variables, loader, templar) -> str: )[0] if str(flavor).strip().lower() != "internal": return _JSDELIVR - domain = lookup_loader.get("domain", loader=loader, templar=templar).run( - ["web-svc-cdn"], variables=variables + base = lookup_loader.get("tls", loader=loader, templar=templar).run( + ["web-svc-cdn", "url.base"], variables=variables )[0] - scheme = "http" if is_onion_domain(domain) else "https" - return f"{scheme}://{domain}" + return str(base).rstrip("/") class LookupModule(LookupBase): diff --git a/tests/unit/python/plugins/lookup/test_asset.py b/tests/unit/python/plugins/lookup/test_asset.py index 775d687edc..0ee22862a4 100644 --- a/tests/unit/python/plugins/lookup/test_asset.py +++ b/tests/unit/python/plugins/lookup/test_asset.py @@ -23,19 +23,23 @@ import plugins.lookup.asset as asset_mod # noqa: E402 import plugins.lookup.asset_host as asset_host_mod # noqa: E402 -def _patched_loader(flavor="internal", domain="cdn.example.org"): +def _patched_loader(flavor="internal", base="https://cdn.example.org/"): """Patch context for the asset module's lookup_loader. Args: flavor: value the mocked `config` lookup returns for services.cdn.flavor. - domain: value the mocked `domain` lookup returns for web-svc-cdn. + base: value the mocked `tls` lookup returns for web-svc-cdn url.base. """ def _get(name, *a, **k): if name == "config": return mock.MagicMock(run=lambda *_a, **_k: [flavor]) - if name == "domain": - return mock.MagicMock(run=lambda *_a, **_k: [domain]) + if name == "tls": + return mock.MagicMock( + run=lambda terms, **_k: ( + [base] if list(terms) == ["web-svc-cdn", "url.base"] else [None] + ) + ) return mock.MagicMock(run=lambda *_a, **_k: [{}]) loader_mock = mock.MagicMock() @@ -79,9 +83,9 @@ class TestResolveHost(unittest.TestCase): host = asset_mod.resolve_host({}, None, None) self.assertEqual(host, "https://cdn.jsdelivr.net") - def test_internal_onion_domain_uses_http(self): + def test_internal_flavor_follows_the_vhost_network_base(self): onion = "cdn." + "a" * 56 + ".onion" - with _patched_loader(flavor="internal", domain=onion): + with _patched_loader(flavor="internal", base=f"http://{onion}/"): host = asset_mod.resolve_host({"group_names": ["web-svc-cdn"]}, None, None) self.assertEqual(host, f"http://{onion}")