66
77"""Feature 53 - Frond {% include %} / {% extends %} path confinement (TAG-DEC-01).
88
9- Real templates on disk, a real secret file OUTSIDE the templates dir, and a real
9+ Real templates on disk, a real outside file OUTSIDE the templates dir, and a real
1010symlink -- NO mocks. Every case drives the REAL Frond engine against files it
1111wrote to a temp directory. A legit include/extends UNDER the templates dir
1212renders; a `..` traversal, an absolute path, and a symlink whose realpath escapes
3737
3838
3939def _make_tree ():
40- """Build a REAL templates dir with legit partial+base, and a REAL secret file
40+ """Build a REAL templates dir with legit partial+base, and a REAL outside file
4141 OUTSIDE it. Returns (base_dir, templates_dir, secret_path)."""
4242 base = Path (tempfile .mkdtemp (prefix = "frondtags_py_" ))
4343 templates = base / "templates"
@@ -46,9 +46,9 @@ def _make_tree():
4646 "Hello from a real partial" , encoding = "utf-8" )
4747 (templates / "base.twig" ).write_text (
4848 "[BASE {% block body %}default{% endblock %} END]" , encoding = "utf-8" )
49- secret = base / "secret .txt" # lives OUTSIDE templates/
50- secret .write_text (OUTSIDE_MARKER , encoding = "utf-8" )
51- return base , templates , secret
49+ outside_file = base / "outside .txt" # lives OUTSIDE templates/
50+ outside_file .write_text (OUTSIDE_MARKER , encoding = "utf-8" )
51+ return base , templates , outside_file
5252
5353
5454class TestFrondTagsConfinement :
@@ -79,35 +79,35 @@ def test_a_legit_extends_renders_under_the_templates_dir(self):
7979 def test_a_dot_dot_traversal_include_is_refused (self ):
8080 base , templates , _ = _make_tree ()
8181 try :
82- # ../secret .txt climbs OUT of the templates dir.
82+ # ../outside .txt climbs OUT of the templates dir.
8383 (templates / "evil.twig" ).write_text (
84- '{% include "../secret .txt" %}' , encoding = "utf-8" )
84+ '{% include "../outside .txt" %}' , encoding = "utf-8" )
8585 with pytest .raises (ValueError , match = "escape" ) as excinfo :
8686 Frond (str (templates )).render ("evil.twig" )
8787 assert OUTSIDE_MARKER not in str (excinfo .value )
8888 finally :
8989 shutil .rmtree (base , ignore_errors = True )
9090
9191 def test_an_absolute_path_include_is_refused (self ):
92- base , templates , secret = _make_tree ()
92+ base , templates , outside_file = _make_tree ()
9393 try :
94- # An absolute path to the real secret file.
94+ # An absolute path to the real outside file.
9595 (templates / "evil_abs.twig" ).write_text (
96- '{% include "' + str (secret ) + '" %}' , encoding = "utf-8" )
96+ '{% include "' + str (outside_file ) + '" %}' , encoding = "utf-8" )
9797 with pytest .raises (ValueError , match = "escape" ) as excinfo :
9898 Frond (str (templates )).render ("evil_abs.twig" )
9999 assert OUTSIDE_MARKER not in str (excinfo .value )
100100 finally :
101101 shutil .rmtree (base , ignore_errors = True )
102102
103103 def test_a_symlink_escaping_the_templates_dir_is_refused (self ):
104- base , templates , secret = _make_tree ()
104+ base , templates , outside_file = _make_tree ()
105105 try :
106- # A REAL symlink INSIDE the templates dir whose target is the secret
106+ # A REAL symlink INSIDE the templates dir whose target is the outside file
107107 # OUTSIDE it. Its name has no `..` and is not absolute, so only the
108108 # realpath containment can catch it.
109109 link = templates / "sneaky.twig"
110- os .symlink (secret , link )
110+ os .symlink (outside_file , link )
111111 (templates / "evil_link.twig" ).write_text (
112112 '{% include "sneaky.twig" %}' , encoding = "utf-8" )
113113 with pytest .raises (ValueError , match = "escape" ) as excinfo :
0 commit comments