Skip to content

Commit 4837a41

Browse files
committed
fix(validators): reject certain paths from being used
Restrict based on the translation-finder blacklist which covers files we do not want to touch.
1 parent 6af6532 commit 4837a41

4 files changed

Lines changed: 16 additions & 3 deletions

File tree

weblate/trans/backups.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ def validate(self) -> None:
550550
self.load_memory(zipfile)
551551
self.load_components(zipfile)
552552
for name in zipfile.namelist():
553-
validate_filename(name)
553+
validate_filename(name, check_prohibited=False)
554554

555555
def restore_unit(
556556
self,

weblate/utils/files.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def should_skip(location):
9090
)
9191

9292

93-
def is_excluded(path):
93+
def is_excluded(path: str) -> bool:
9494
"""Whether path should be excluded from zip extraction."""
9595
return any(exclude in f"/{path}/" for exclude in PATH_EXCLUDES) or ".." in path
9696

weblate/utils/tests/test_validators.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@ def test_simplification(self) -> None:
139139
def test_empty(self) -> None:
140140
validate_filename("")
141141

142+
def test_prohibited(self) -> None:
143+
with self.assertRaises(ValidationError):
144+
validate_filename(".git/config")
145+
validate_filename(".git/config", check_prohibited=False)
146+
147+
def test_prohibited_subdir(self) -> None:
148+
with self.assertRaises(ValidationError):
149+
validate_filename("path/.git/config")
150+
validate_filename("path/.git/config", check_prohibited=False)
151+
142152

143153
class RegexTest(SimpleTestCase):
144154
def test_empty(self) -> None:

weblate/utils/validators.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from weblate.trans.util import cleanup_path
3434
from weblate.utils.const import WEBHOOKS_SECRET_PREFIX
3535
from weblate.utils.data import data_dir
36+
from weblate.utils.files import is_excluded
3637

3738
USERNAME_MATCHER = re.compile(r"^[\w@+-][\w.@+-]*$")
3839

@@ -238,7 +239,7 @@ def validate_plural_formula(value) -> None:
238239
) from error
239240

240241

241-
def validate_filename(value) -> None:
242+
def validate_filename(value: str, *, check_prohibited: bool = True) -> None:
242243
if "../" in value or "..\\" in value:
243244
raise ValidationError(
244245
gettext("The filename can not contain reference to a parent directory.")
@@ -254,6 +255,8 @@ def validate_filename(value) -> None:
254255
"Maybe you want to use: {}"
255256
).format(cleaned)
256257
)
258+
if check_prohibited and is_excluded(cleaned):
259+
raise ValidationError(gettext("The filename contains a prohibited folder."))
257260

258261

259262
def validate_backup_path(value: str) -> None:

0 commit comments

Comments
 (0)