Skip to content

Commit 404aad0

Browse files
authored
Merge pull request #287 from Xpirix/backslashes_handling
Add validation for backslashes in ZIP file names
2 parents 6c35797 + a44de5f commit 404aad0

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

qgis-app/plugins/tests/test_validator.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,30 @@ def test_plugin_with_single_parent(self):
333333
)
334334
multiple_parent_folders = self._get_value_by_attribute('multiple_parent_folders', result)
335335
self.assertIsNone(multiple_parent_folders)
336+
337+
338+
class TestPathWithBackslashValidator(TestCase):
339+
def setUp(self) -> None:
340+
path_with_backslash = os.path.join(TESTFILE_DIR, "path_with_backslash.zip_")
341+
self.path_with_backslash = open(path_with_backslash, "rb")
342+
343+
def tearDown(self):
344+
self.path_with_backslash.close()
345+
346+
def test_path_with_backslash(self):
347+
"""
348+
The path_with_backslash.zip contains file with
349+
path with backslash.
350+
"""
351+
self.assertRaises(
352+
ValidationError,
353+
validator,
354+
InMemoryUploadedFile(
355+
self.path_with_backslash,
356+
field_name="tempfile",
357+
name="testfile.zip",
358+
content_type="application/zip",
359+
size=39889,
360+
charset="utf8",
361+
),
362+
)
41.6 KB
Binary file not shown.

qgis-app/plugins/validator.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,29 @@ def validator(package, is_new: bool = False):
298298
except:
299299
pass
300300

301+
# Check if the zip namelist contains backslashes or drive letters, which is not allowed.
302+
for name in namelist:
303+
if "\\" in name:
304+
raise ValidationError(
305+
_(
306+
"Your archive does not conform to the ZIP specification, "
307+
"it cannot contain backslashes in file names (found '{}'). "
308+
"Please try again with a valid ZIP file (or use a different archiving tool).".format(
309+
name
310+
)
311+
)
312+
)
313+
if re.match(r"^[A-Za-z]:", name):
314+
raise ValidationError(
315+
_(
316+
"Your archive does not conform to the ZIP specification, "
317+
"it cannot contain drive letters in file names (found '{}'). "
318+
"Please try again with a valid ZIP file (or use a different archiving tool).".format(
319+
name
320+
)
321+
)
322+
)
323+
301324
# Checks that package_name exists
302325
try:
303326
package_name = namelist[0][: namelist[0].index("/")]

0 commit comments

Comments
 (0)