Skip to content
Myr

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

3 files+15 added−6 removed

modified:src/jinja2/loaders.py+8 added-3 removed
1 unmodified line
2sources.3"""4import importlib.util5import os6import posixpath7import sys8import typing as t9import weakref10import zipimport
182 unmodified lines
193 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 lines
298299 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] = []305306 # One element for regular packages, multiple for namespace
21 unmodified lines
328329 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]]336337 if self._archive is None:338 # Package is a directory.
319 unmodified lines
modified:tests/test_loader.py+4 added-3 removed
1 unmodified line
2import importlib.machinery3import importlib.util4import os5import platform6import posixpath7import shutil8import sys9import tempfile10import time
292 unmodified lines
303)304def 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()309310311def test_package_dir_list(package_dir_loader):
13 unmodified lines
325)326def 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()331332333def test_package_file_list(package_file_loader):
14 unmodified lines
348)349def 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 None354355356@pytest.mark.xfail(
48 unmodified lines
modified:CHANGES.rst+3 added
34 unmodified lines
35 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`424344Version 3.0.345-------------
911 unmodified lines