From c7d162fa883031d763de27c90f10f36dc4eff709 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Fri, 11 Sep 2026 20:53:19 +0200 Subject: [PATCH] fix(coredns): derive the domain regex from INFINITO_DOMAIN when rendering INFINITO_DOMAIN_RE carried an already escaped regex through the env chain: EnvBuilder.setdefault prefers a value already in the process env, quote_for_dotenv escapes every backslash, and load.sh unescapes it again. One inherited value with a doubled backslash was enough to write "infinito\\\\.test" into .env, the rendered Corefile then matched a literal backslash, and getent hosts test.infinito.test failed with rc 2. The renderer now substitutes DOMAIN_RE = re.escape(INFINITO_DOMAIN) at render time, so no backslash travels through the env or .env at all. The template placeholder is renamed because it is a derived render value, not an env key, and INFINITO_DOMAIN_RE is removed from default.env and the passthrough keys, leaving INFINITO_DOMAIN as the single source. A renderer test renders the real template and requires a single-escaped match with no doubled backslash and no leftover placeholder. Verified live: the Corefile renders ^infinito\.test\.$, test.infinito.test resolves to 172.30.0.10 and test.infinitoXtest does not. Co-Authored-By: Claude Opus 5 (1M context) --- .../deploy/development/coredns.py | 5 ++- compose/coredns/Corefile.tmpl | 8 ++--- default.env | 3 -- .../deploy/development/test_coredns.py | 35 +++++++++++++++++++ utils/env/handlers/passthrough.py | 1 - 5 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 tests/unit/python/cli/administration/deploy/development/test_coredns.py diff --git a/cli/administration/deploy/development/coredns.py b/cli/administration/deploy/development/coredns.py index 2baa519bf5..d587daa3d6 100644 --- a/cli/administration/deploy/development/coredns.py +++ b/cli/administration/deploy/development/coredns.py @@ -2,6 +2,7 @@ from __future__ import annotations import itertools import os +import re import tempfile from dataclasses import dataclass from pathlib import Path @@ -114,7 +115,9 @@ class CoreDNSCorefileRenderer: self._log("Rendering Corefile (atomic write)") try: - rendered = Template(read_text(tmpl_file)).safe_substitute(env) + rendered = Template(read_text(tmpl_file)).safe_substitute( + env, DOMAIN_RE=re.escape(env["INFINITO_DOMAIN"]) + ) with os.fdopen(fd, "w", encoding="utf-8") as fout: fout.write(rendered) diff --git a/compose/coredns/Corefile.tmpl b/compose/coredns/Corefile.tmpl index 6bffd62f6d..96b788ca26 100644 --- a/compose/coredns/Corefile.tmpl +++ b/compose/coredns/Corefile.tmpl @@ -1,23 +1,23 @@ .:53 { template IN A { - match ^${INFINITO_DOMAIN_RE}\.$ + match ^${DOMAIN_RE}\.$ answer "{{ .Name }} 60 IN A ${INFINITO_IP4}" fallthrough } template IN A { - match ^(.*)\.${INFINITO_DOMAIN_RE}\.$ + match ^(.*)\.${DOMAIN_RE}\.$ answer "{{ .Name }} 60 IN A ${INFINITO_IP4}" fallthrough } template IN AAAA { - match ^${INFINITO_DOMAIN_RE}\.$ + match ^${DOMAIN_RE}\.$ fallthrough } template IN AAAA { - match ^(.*)\.${INFINITO_DOMAIN_RE}\.$ + match ^(.*)\.${DOMAIN_RE}\.$ fallthrough } diff --git a/default.env b/default.env index 9b47201978..93cc0f4c87 100644 --- a/default.env +++ b/default.env @@ -175,9 +175,6 @@ INFINITO_DOCKER_VOLUME=docker # Top-level project domain rendered into vhost/cert/test fixtures; CI/dev default, overridden in real deploys. INFINITO_DOMAIN=infinito.test -# INFINITO_DOMAIN escaped for inclusion in regex patterns (literal `.`); keep in sync with INFINITO_DOMAIN. -INFINITO_DOMAIN_RE=infinito\.test - # Repo-relative path of the filesystem userland install script (ext4, btrfs, zfs). INFINITO_FILESYSTEM_INSTALL_SCRIPT=scripts/install/filesystem.sh diff --git a/tests/unit/python/cli/administration/deploy/development/test_coredns.py b/tests/unit/python/cli/administration/deploy/development/test_coredns.py new file mode 100644 index 0000000000..ccf2926aab --- /dev/null +++ b/tests/unit/python/cli/administration/deploy/development/test_coredns.py @@ -0,0 +1,35 @@ +from __future__ import annotations + +import tempfile +import unittest +from pathlib import Path + +from cli.administration.deploy.development.coredns import CoreDNSCorefileRenderer +from utils.cache.files import PROJECT_ROOT, read_text + +TEMPLATE = "compose/coredns/Corefile.tmpl" + + +class TestCoreDNSCorefileRenderer(unittest.TestCase): + def test_the_domain_regex_is_derived_from_the_domain_escaped_once(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + (root / ".env").write_text( + 'INFINITO_DOMAIN=infinito.test\nINFINITO_IP4="172.30.0.10"\n', + encoding="utf-8", + ) + template = root / TEMPLATE + template.parent.mkdir(parents=True) + template.write_text( + read_text(str(PROJECT_ROOT / TEMPLATE)), encoding="utf-8" + ) + out = CoreDNSCorefileRenderer(repo_root=root).render(show_preview=False) + corefile = read_text(str(out)) + self.assertIn("match ^infinito\\.test\\.$", corefile) + self.assertIn("match ^(.*)\\.infinito\\.test\\.$", corefile) + self.assertNotIn("\\\\", corefile) + self.assertNotIn("${", corefile) + + +if __name__ == "__main__": + unittest.main() diff --git a/utils/env/handlers/passthrough.py b/utils/env/handlers/passthrough.py index 5a06d21c25..b0a30ff3d0 100644 --- a/utils/env/handlers/passthrough.py +++ b/utils/env/handlers/passthrough.py @@ -21,7 +21,6 @@ STATIC_KEYS = ( "INFINITO_DNS_HOSTS_FILE", "INFINITO_DNS_IP", "INFINITO_DOMAIN", - "INFINITO_DOMAIN_RE", "INFINITO_TOR_SOCKS_PORT", "INFINITO_CONTAINER", "INFINITO_CONTAINER_CA_ENABLED",