File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66import os
77import shutil
88import stat
9- from pathlib import Path
9+ from pathlib import Path , PurePosixPath , PureWindowsPath
1010from typing import TYPE_CHECKING
1111
1212from django .conf import settings
@@ -98,7 +98,16 @@ def should_skip(location):
9898
9999def is_excluded (path : str ) -> bool :
100100 """Whether path should be excluded from zip extraction."""
101- return any (exclude in f"/{ path } /" for exclude in PATH_EXCLUDES ) or ".." in path
101+ normalized = path .replace ("\\ " , "/" )
102+ posix_path = PurePosixPath (normalized )
103+ windows_path = PureWindowsPath (path )
104+ return (
105+ any (exclude in f"/{ normalized } /" for exclude in PATH_EXCLUDES )
106+ or ".." in posix_path .parts
107+ or posix_path .is_absolute ()
108+ or windows_path .is_absolute ()
109+ or bool (windows_path .drive )
110+ )
102111
103112
104113def is_path_within_directory (path : str , directory : str ) -> bool :
Original file line number Diff line number Diff line change 1717from weblate .utils .files import (
1818 REPO_TEMP_DIRNAME ,
1919 get_repo_temp_dir ,
20+ is_excluded ,
2021 is_path_within_directory ,
2122 read_file_bytes ,
2223 remove_tree ,
@@ -71,6 +72,14 @@ def test_read_file_bytes_resets_position_to_start(self) -> None:
7172 self .assertEqual (read_file_bytes (filelike , max_size = 10 ), b"test" )
7273 self .assertEqual (filelike .tell (), 0 )
7374
75+ def test_is_excluded_rejects_path_traversal_and_absolute_paths (self ) -> None :
76+ self .assertTrue (is_excluded ("../outside.po" ))
77+ self .assertTrue (is_excluded ("/etc/passwd" ))
78+ self .assertTrue (is_excluded (r"C:\temp\escape.po" ))
79+
80+ def test_is_excluded_allows_regular_relative_paths (self ) -> None :
81+ self .assertFalse (is_excluded ("locale/cs/messages.po" ))
82+
7483 def test_is_path_within_directory_accepts_descendants (self ) -> None :
7584 with tempfile .TemporaryDirectory () as tempdir :
7685 repo_path = os .path .join (tempdir , "repo" )
You can’t perform that action at this time.
0 commit comments