🐛 backup: fix file validation

Just going by name breaks backups
with arbitrary file names created
from CLI or the helper.

Instead of matching on the name, we
now check whether it's a zip and
contains a metadata.json.

Closes #4659
This commit is contained in:
Gina Häußge committed 2022-10-05 17:03:14 +02:00
1 parent 8e9c09a818
commit b4d0ed30ab
1 file changed
+13 -13
+13 -13
View File
@@ -390,9 +390,7 @@ class BackupPlugin(
"as_attachment": True,
"path_validation": path_validation_factory(
lambda path: not is_hidden_path(path)
and self._match_backup_filename(
os.path.basename(path), self._settings
),
and self._valid_backup(path),
status_code=404,
),
"access_validation": access_validation_factory(
@@ -739,9 +737,7 @@ class BackupPlugin(
for entry in os.scandir(self.get_plugin_data_folder()):
if is_hidden_path(entry.path):
continue
if not entry.is_file():
continue
if not entry.name.endswith(".zip"):
if not self._valid_backup(entry.path):
continue
backups.append(
@@ -1373,13 +1369,6 @@ class BackupPlugin(
backup_prefix, time.strftime(BACKUP_DATE_TIME_FMT)
)
@classmethod
def _match_backup_filename(cls, path, settings):
import re
backup_prefix = cls._get_backup_prefix(settings)
return re.match(re.escape(backup_prefix) + r"-backup-\d{8}-\d{6}.zip", path)
@classmethod
def _get_backup_prefix(cls, settings):
if settings.global_get(["appearance", "name"]) == "":
@@ -1397,6 +1386,17 @@ class BackupPlugin(
not in valid_boolean_trues
)
@classmethod
def _valid_backup(cls, path):
if not path.endswith(".zip") or not zipfile.is_zipfile(path):
return False
try:
with zipfile.ZipFile(path) as z:
return "metadata.json" in z.namelist()
except Exception:
return False
def _send_client_message(self, message, payload=None):
if payload is None:
payload = {}