Extra validation of the uploaded ZIP #73

Merged
Anna Sirota merged 13 commits from validation-single-theme-xml into main 2024-04-11 12:32:50 +02:00
3 changed files with 9 additions and 2 deletions
Showing only changes of commit faf55f1925 - Show all commits

View File

@ -78,6 +78,7 @@ EXPECTED_VALIDATION_ERRORS = {
'source': ['The manifest file is missing.'], 'source': ['The manifest file is missing.'],
}, },
'invalid-manifest-toml.zip': {'source': ['Could not parse the manifest file.']}, 'invalid-manifest-toml.zip': {'source': ['Could not parse the manifest file.']},
'invalid-theme-multiple-xmls.zip': {'source': ['A theme should have exactly one XML file.']},
} }

View File

@ -34,7 +34,7 @@ class FileForm(forms.ModelForm):
# TODO: surface TOML parsing errors? # TODO: surface TOML parsing errors?
'invalid_manifest_toml': _('Could not parse the manifest file.'), 'invalid_manifest_toml': _('Could not parse the manifest file.'),
'invalid_missing_init': _('An add-on should have an __init__.py file.'), 'invalid_missing_init': _('An add-on should have an __init__.py file.'),
'invalid_theme_multiple_xmls': _('A theme should only have one XML file.'), 'missing_or_multiple_theme_xml': _('A theme should have exactly one XML file.'),
'invalid_zip_archive': msg_only_zip_files, 'invalid_zip_archive': msg_only_zip_files,
'missing_manifest_toml': _('The manifest file is missing.'), 'missing_manifest_toml': _('The manifest file is missing.'),
} }

View File

@ -101,6 +101,12 @@ def read_manifest_from_zip(archive_path):
error_codes = [] error_codes = []
try: try:
with zipfile.ZipFile(archive_path) as myzip: with zipfile.ZipFile(archive_path) as myzip:
bad_file = myzip.testzip()
if bad_file is not None:
logger.error('Bad file in ZIP')
error_codes.append('invalid_zip_archive')
return None, error_codes
file_list = myzip.namelist() file_list = myzip.namelist()
manifest_filepath = find_path_by_name(file_list, manifest_name) manifest_filepath = find_path_by_name(file_list, manifest_name)
@ -123,7 +129,7 @@ def read_manifest_from_zip(archive_path):
if type_slug == 'theme': if type_slug == 'theme':
theme_xmls = filter_paths_by_ext(file_list, '.xml') theme_xmls = filter_paths_by_ext(file_list, '.xml')
if len(list(theme_xmls)) != 1: if len(list(theme_xmls)) != 1:
error_codes.append('invalid_theme_multiple_xmls') error_codes.append('missing_or_multiple_theme_xml')
elif type_slug == 'add-on': elif type_slug == 'add-on':
# __init__.py is expected to be next to the manifest # __init__.py is expected to be next to the manifest
expected_init_path = os.path.join(os.path.dirname(manifest_filepath), '__init__.py') expected_init_path = os.path.join(os.path.dirname(manifest_filepath), '__init__.py')