]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Restrict cross-test jinja2 includes to _common/ 12117/head
authorNicki Křížek <nicki@isc.org>
Wed, 20 May 2026 14:34:02 +0000 (14:34 +0000)
committerNicki Křížek <nicki@isc.org>
Thu, 28 May 2026 14:52:24 +0000 (16:52 +0200)
The previous loader was a FileSystemLoader rooted at $srcdir, which
allowed any system test to include any other test's templates -- a
wider scope than intended. Every existing cross-test include already
targets _common/, so make that the only path.

ChoiceLoader + PrefixLoader keeps the existing '_common/foo.j2' path
convention working without changes to call sites. The '_common/'
prefix is deliberately kept rather than dropping it by rooting the
FileSystemLoader at _common/ directly:

  - It signals at the include site that the file is a shared
    template, not a sibling of the current test; readers don't need
    to know the loader configuration to understand where the file
    lives.
  - It prevents shadowing: a test-local 'controls.conf.j2' would
    not collide with the shared one, and the unqualified name keeps
    its test-local meaning.
  - It makes the dependency greppable: 'grep -rl _common/'
    identifies every test that consumes shared snippets.

Assisted-by: Claude:claude-opus-4-7
(cherry picked from commit c2c2be9be0481eb8966884bb33c0153e58e1800f)

bin/tests/system/isctest/template.py

index 4562e6da3bc6f9903d502c75dec4bd3b77a9fe62..d5d3b2b181372e76da5fb685f523272add24b5ea 100644 (file)
@@ -56,10 +56,16 @@ class TemplateEngine:
                 pytest.skip("jinja2 not found")
 
             self._j2env = jinja2.Environment(
-                loader=jinja2.FileSystemLoader(
+                loader=jinja2.ChoiceLoader(
                     [
-                        str(self.directory),
-                        str(ALL["srcdir"]),  # to allow _common/ includes
+                        jinja2.FileSystemLoader(self.directory),
+                        jinja2.PrefixLoader(
+                            {
+                                "_common": jinja2.FileSystemLoader(
+                                    Path(self.env_vars["srcdir"]) / "_common"
+                                ),
+                            }
+                        ),
                     ]
                 ),
                 undefined=jinja2.StrictUndefined,