Extract import files without reusing source credentials
This commit is contained in:
1 parent
040b70fd60
commit
4bc0012fa1
2 files changed
+79
-8
No files matched your search
@@ -1,12 +1,18 @@
|
|||||||
import stat
|
import stat
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
from pathlib import Path
|
||||||
from tarfile import TarFile, TarInfo
|
from tarfile import TarFile, TarInfo
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
from zipfile import ZipFile, ZipInfo
|
from zipfile import ZipFile, ZipInfo
|
||||||
|
|
||||||
from django.test import SimpleTestCase
|
from django.test import SimpleTestCase
|
||||||
|
|
||||||
from coulobre.wordpress_import import REQUIRED_FILES, inspect_export
|
from coulobre.wordpress_import import (
|
||||||
|
REQUIRED_FILES,
|
||||||
|
inspect_export,
|
||||||
|
unpack_export,
|
||||||
|
)
|
||||||
from coulobre.wordpress_staging_updates import package_url
|
from coulobre.wordpress_staging_updates import package_url
|
||||||
from coulobre.wordpress_updates import WordPressUpdate
|
from coulobre.wordpress_updates import WordPressUpdate
|
||||||
|
|
||||||
@@ -72,6 +78,26 @@ class WordPressImportInspectionTests(SimpleTestCase):
|
|||||||
self.assertRaisesRegex(ValueError, "expanded archive"),
|
self.assertRaisesRegex(ValueError, "expanded archive"),
|
||||||
):
|
):
|
||||||
inspect_export(export_zip())
|
inspect_export(export_zip())
|
||||||
|
|
||||||
|
def test_extract_keeps_sql_separate_and_skips_source_configuration(self):
|
||||||
|
source = export_zip(
|
||||||
|
("files/wp-config.php", b"private credentials"),
|
||||||
|
("files/wp-content/db.php", b"source database override"),
|
||||||
|
("files/wp-content/uploads/photo.jpg", b"image"),
|
||||||
|
)
|
||||||
|
with TemporaryDirectory() as work:
|
||||||
|
public = Path(work)
|
||||||
|
sql = BytesIO()
|
||||||
|
unpack_export(source, public, sql)
|
||||||
|
self.assertEqual(sql.read(), b"SELECT 1;")
|
||||||
|
self.assertTrue((public / "wp-load.php").is_file())
|
||||||
|
self.assertFalse((public / "wp-config.php").exists())
|
||||||
|
self.assertFalse((public / "wp-content/db.php").exists())
|
||||||
|
self.assertEqual(
|
||||||
|
(public / "wp-content/uploads/photo.jpg").read_bytes(),
|
||||||
|
b"image",
|
||||||
|
)
|
||||||
|
self.assertFalse((public / "database.sql").exists())
|
||||||
with (
|
with (
|
||||||
patch("coulobre.wordpress_import.MAX_ARCHIVE_ENTRIES", 2),
|
patch("coulobre.wordpress_import.MAX_ARCHIVE_ENTRIES", 2),
|
||||||
self.assertRaisesRegex(ValueError, "too many entries"),
|
self.assertRaisesRegex(ValueError, "too many entries"),
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
"""Inspect WordPress exports without extracting them."""
|
"""Inspect WordPress exports without extracting them."""
|
||||||
|
|
||||||
import stat
|
import stat
|
||||||
|
from contextlib import contextmanager
|
||||||
from pathlib import PurePosixPath
|
from pathlib import PurePosixPath
|
||||||
|
from shutil import copyfileobj
|
||||||
from tarfile import TarError, TarFile
|
from tarfile import TarError, TarFile
|
||||||
from zipfile import BadZipFile, ZipFile, is_zipfile
|
from zipfile import BadZipFile, ZipFile, is_zipfile
|
||||||
|
|
||||||
@@ -13,6 +15,13 @@ REQUIRED_FILES = {
|
|||||||
"files/wp-admin/admin.php",
|
"files/wp-admin/admin.php",
|
||||||
"files/wp-includes/version.php",
|
"files/wp-includes/version.php",
|
||||||
}
|
}
|
||||||
|
SKIP_PHP = {
|
||||||
|
"files/wp-config.php",
|
||||||
|
"files/wp-content/advanced-cache.php",
|
||||||
|
"files/wp-content/db.php",
|
||||||
|
"files/wp-content/object-cache.php",
|
||||||
|
"files/wp-content/sunrise.php",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def archive_name(name):
|
def archive_name(name):
|
||||||
@@ -52,7 +61,7 @@ def zip_entries(archive):
|
|||||||
raise ValueError("Use only regular files and directories.")
|
raise ValueError("Use only regular files and directories.")
|
||||||
if member.flag_bits & 1:
|
if member.flag_bits & 1:
|
||||||
raise ValueError("Use an archive without password protection.")
|
raise ValueError("Use an archive without password protection.")
|
||||||
yield member.orig_filename, is_directory, member.file_size
|
yield member.orig_filename, is_directory, member.file_size, member
|
||||||
|
|
||||||
|
|
||||||
def tar_entries(archive):
|
def tar_entries(archive):
|
||||||
@@ -61,13 +70,13 @@ def tar_entries(archive):
|
|||||||
raise ValueError("Use only regular files and directories.")
|
raise ValueError("Use only regular files and directories.")
|
||||||
if member.isdir() and member.size != 0:
|
if member.isdir() and member.size != 0:
|
||||||
raise ValueError("The TAR has an invalid directory entry.")
|
raise ValueError("The TAR has an invalid directory entry.")
|
||||||
yield member.name, member.isdir(), member.size
|
yield member.name, member.isdir(), member.size, member
|
||||||
|
|
||||||
|
|
||||||
def inspect_entries(entries, archive_type):
|
def inspect_entries(entries, archive_type):
|
||||||
paths = {}
|
paths = {}
|
||||||
total = sql = files = 0
|
total = sql = files = 0
|
||||||
for name, directory, size in entries:
|
for name, directory, size, _ in entries:
|
||||||
name = archive_name(name)
|
name = archive_name(name)
|
||||||
if name in paths:
|
if name in paths:
|
||||||
raise ValueError(f"The archive lists {name} more than once.")
|
raise ValueError(f"The archive lists {name} more than once.")
|
||||||
@@ -102,7 +111,8 @@ def inspect_entries(entries, archive_type):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def inspect_export(source):
|
@contextmanager
|
||||||
|
def open_export(source):
|
||||||
source.seek(0, 2)
|
source.seek(0, 2)
|
||||||
if not 0 < source.tell() <= MAX_ARCHIVE_BYTES:
|
if not 0 < source.tell() <= MAX_ARCHIVE_BYTES:
|
||||||
raise ValueError("The upload must be no larger than 2 GiB.")
|
raise ValueError("The upload must be no larger than 2 GiB.")
|
||||||
@@ -112,11 +122,46 @@ def inspect_export(source):
|
|||||||
source.seek(0)
|
source.seek(0)
|
||||||
if zip_format:
|
if zip_format:
|
||||||
with ZipFile(source) as archive:
|
with ZipFile(source) as archive:
|
||||||
return inspect_entries(zip_entries(archive), "ZIP")
|
yield zip_entries(archive), archive.open, "ZIP"
|
||||||
# Iterate so entry and size limits apply during inspection.
|
else:
|
||||||
|
# Iterate so size limits apply during TAR inspection.
|
||||||
with TarFile.open(fileobj=source, mode="r|*") as archive:
|
with TarFile.open(fileobj=source, mode="r|*") as archive:
|
||||||
return inspect_entries(tar_entries(archive), "TAR")
|
yield tar_entries(archive), archive.extractfile, "TAR"
|
||||||
except (OSError, EOFError, BadZipFile, TarError, UnicodeError) as error:
|
except (OSError, EOFError, BadZipFile, TarError, UnicodeError) as error:
|
||||||
raise ValueError("Upload a readable ZIP or TAR archive.") from error
|
raise ValueError("Upload a readable ZIP or TAR archive.") from error
|
||||||
finally:
|
finally:
|
||||||
source.seek(0)
|
source.seek(0)
|
||||||
|
|
||||||
|
|
||||||
|
def inspect_export(source):
|
||||||
|
with open_export(source) as (entries, _, archive_type):
|
||||||
|
return inspect_entries(entries, archive_type)
|
||||||
|
|
||||||
|
|
||||||
|
def unpack_export(source, public, database_sql):
|
||||||
|
"""Unpack into an empty workspace owned by the caller."""
|
||||||
|
summary = inspect_export(source)
|
||||||
|
if any(public.iterdir()) or database_sql.tell():
|
||||||
|
raise ValueError("Choose an empty workspace for the import.")
|
||||||
|
with open_export(source) as (entries, open_member, _):
|
||||||
|
for original, directory, length, member in entries:
|
||||||
|
name = archive_name(original)
|
||||||
|
if name in SKIP_PHP or name == "files":
|
||||||
|
continue
|
||||||
|
if name == "database.sql":
|
||||||
|
with open_member(member) as data:
|
||||||
|
copyfileobj(data, database_sql, length=1024**2)
|
||||||
|
if database_sql.tell() != length:
|
||||||
|
raise ValueError("The SQL dump is incomplete.")
|
||||||
|
continue
|
||||||
|
target = public.joinpath(*PurePosixPath(name).parts[1:])
|
||||||
|
if directory:
|
||||||
|
target.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||||
|
else:
|
||||||
|
target.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||||
|
with open_member(member) as data, target.open("xb") as output:
|
||||||
|
copyfileobj(data, output, length=1024**2)
|
||||||
|
if output.tell() != length:
|
||||||
|
raise ValueError("A WordPress file is incomplete.")
|
||||||
|
database_sql.seek(0)
|
||||||
|
return summary
|
||||||
Reference in new issue
Block a user