Skip to content

Commit d865984

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 6118615 commit d865984

3 files changed

Lines changed: 13 additions & 2 deletions

File tree

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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ 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+
146+
def test_prohibited_subdir(self) -> None:
147+
with self.assertRaises(ValidationError):
148+
validate_filename("path/.git/config")
149+
142150

143151
class RegexTest(SimpleTestCase):
144152
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)