Merge pull request #1621 from pallets/template-safe-path
Before adoption: this commit came in with the history the project adopted, so no landing records it.
use `posixpath.join` when loading template names
authored by David Lord · committed by GitHub · parents a292075, 040088a · Browse files · 3 files·+15 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+15 added−6 removed
- tests:+4 added−3 removed
- other:+11 added−3 removed
Provenance
- Commit
ede0f98- Tree
e30c409- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
a292075- Parent 2
040088a
3 files changed+15 added−6 removed
modified:src/jinja2/loaders.py+8 added-3 removed
1 unmodified line2 sources.3 """4 import importlib.util5 import os6+import posixpath7 import sys8 import typing as t9 import weakref10 import zipimport182 unmodified lines193 self, environment: "Environment", template: str194 ) -> t.Tuple[str, str, t.Callable[[], bool]]:195 pieces = split_template_path(template)196 for searchpath in self.searchpath:197+ # Use posixpath even on Windows to avoid "drive:" or UNC198+ # segments breaking out of the search directory.196- filename = os.path.join(searchpath, *pieces)199+ filename = posixpath.join(searchpath, *pieces)200 f = open_if_exists(filename)201 if f is None:202 continue203 try:94 unmodified lines298 299 if isinstance(loader, zipimport.zipimporter):300 self._archive = loader.archive301 pkgdir = next(iter(spec.submodule_search_locations)) # type: ignore299- template_root = os.path.join(pkgdir, package_path)302+ template_root = os.path.join(pkgdir, package_path).rstrip(os.path.sep)303 else:304 roots: t.List[str] = []305 306 # One element for regular packages, multiple for namespace21 unmodified lines328 329 def get_source(330 self, environment: "Environment", template: str331 ) -> t.Tuple[str, str, t.Optional[t.Callable[[], bool]]]:332+ # Use posixpath even on Windows to avoid "drive:" or UNC333+ # segments breaking out of the search directory.329- p = os.path.join(self._template_root, *split_template_path(template))334+ p = posixpath.join(self._template_root, *split_template_path(template))335 up_to_date: t.Optional[t.Callable[[], bool]]336 337 if self._archive is None:338 # Package is a directory.319 unmodified linesmodified:tests/test_loader.py+4 added-3 removed
1 unmodified line2 import importlib.machinery3 import importlib.util4 import os5 import platform6+import posixpath7 import shutil8 import sys9 import tempfile10 import time292 unmodified lines303 )304 def test_package_dir_source(package_dir_loader, template, expect):305 source, name, up_to_date = package_dir_loader.get_source(None, template)306 assert source.rstrip() == expect306- assert name.endswith(os.path.join(*split_template_path(template)))307+ assert name.endswith(posixpath.join(*split_template_path(template)))308 assert up_to_date()309 310 311 def test_package_dir_list(package_dir_loader):13 unmodified lines325 )326 def test_package_file_source(package_file_loader, template, expect):327 source, name, up_to_date = package_file_loader.get_source(None, template)328 assert source.rstrip() == expect328- assert name.endswith(os.path.join(*split_template_path(template)))329+ assert name.endswith(posixpath.join(*split_template_path(template)))330 assert up_to_date()331 332 333 def test_package_file_list(package_file_loader):14 unmodified lines348 )349 def test_package_zip_source(package_zip_loader, template, expect):350 source, name, up_to_date = package_zip_loader.get_source(None, template)351 assert source.rstrip() == expect351- assert name.endswith(os.path.join(*split_template_path(template)))352+ assert name.endswith(posixpath.join(*split_template_path(template)))353 assert up_to_date is None354 355 356 @pytest.mark.xfail(48 unmodified linesmodified:CHANGES.rst+3 added
34 unmodified lines35 calls when the environment is in async mode. :issue:`1573`36 - The ``groupby`` filter is case-insensitive by default, matching37 other comparison filters. Added the ``case_sensitive`` parameter to38 control this. :issue:`1463`39+- Windows drive-relative path segments in template names will not40+ result in ``FileSystemLoader`` and ``PackageLoader`` loading from41+ drive-relative paths. :pr:`1621`42 43 44 Version 3.0.345 -------------911 unmodified lines