Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show the path for forbidden dir in child folder #387

Merged
merged 3 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 24 additions & 5 deletions qgis-app/plugins/tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,10 @@ def test_zipfile_with_MACOSX(self, mock_namelist):
mock_namelist.return_value = ["__MACOSX/"]
with self.assertRaisesMessage(
Exception,
("For security reasons, zip file cannot contain " "'__MACOSX' directory"),
(
"For security reasons, zip file cannot contain <strong> '__MACOSX' </strong> directory. "
"However, there is one present at the root of the archive."
),
):
validator(self.package)

Expand All @@ -167,8 +170,20 @@ def test_zipfile_with_pycache(self, mock_namelist):
with self.assertRaisesMessage(
Exception,
(
"For security reasons, zip file cannot contain "
"'__pycache__' directory"
"For security reasons, zip file cannot contain <strong> '__pycache__' </strong> directory. "
"However, there is one present at the root of the archive."
),
):
validator(self.package)

@mock.patch("zipfile.ZipFile.namelist")
def test_zipfile_with_pycache_in_children(self, mock_namelist):
mock_namelist.return_value = ["path/to/__pycache__/"]
with self.assertRaisesMessage(
Exception,
(
"For security reasons, zip file cannot contain <strong> '__pycache__' </strong> directory. "
"However, it has been found at <strong> 'path/to/__pycache__/' </strong>."
),
):
validator(self.package)
Expand All @@ -178,7 +193,10 @@ def test_zipfile_with_git(self, mock_namelist):
mock_namelist.return_value = [".git"]
with self.assertRaisesMessage(
Exception,
("For security reasons, zip file cannot contain " "'.git' directory"),
(
"For security reasons, zip file cannot contain <strong> '.git' </strong> directory. "
"However, there is one present at the root of the archive."
),
):
validator(self.package)

Expand All @@ -191,7 +209,8 @@ def test_zipfile_with_gitignore(self, mock_namelist):
exception = cm.exception
self.assertNotEqual(
exception.message,
"For security reasons, zip file cannot contain '.git' directory",
"For security reasons, zip file cannot contain <strong> '.git' </strong> directory. ",
"However, there is one present at the root of the archive."
)


Expand Down
12 changes: 10 additions & 2 deletions qgis-app/plugins/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,19 @@ def validator(package):
_("For security reasons, zip file cannot contain .pyc file")
)
for forbidden_dir in ["__MACOSX", ".git", "__pycache__"]:
if forbidden_dir in zname.split("/"):
dir_name_list = zname.split("/")
if forbidden_dir in dir_name_list:
if forbidden_dir == dir_name_list[0]:
raise ValidationError(
_(
"For security reasons, zip file "
"cannot contain <strong> '%s' </strong> directory. However, there is one present at the root of the archive." % (forbidden_dir,)
)
)
raise ValidationError(
_(
"For security reasons, zip file "
"cannot contain '%s' directory" % (forbidden_dir,)
"cannot contain <strong> '%s' </strong> directory. However, it has been found at <strong> '%s' </strong>." % (forbidden_dir, zname)
)
)
bad_file = zip.testzip()
Expand Down