Harden backup manifest metadata and database identity validation
ABSDEV Framework CI / framework-ci (push) Successful in 22s
ABSDEV Framework CI / framework-ci (push) Successful in 22s
This commit is contained in:
+64
-3
@@ -28,6 +28,45 @@ def require(condition, message):
|
||||
raise BackupError(message)
|
||||
|
||||
|
||||
def unique_json_object(pairs):
|
||||
|
||||
result = {}
|
||||
|
||||
for key, value in pairs:
|
||||
|
||||
require(
|
||||
key not in result,
|
||||
"Duplicate backup manifest field",
|
||||
)
|
||||
|
||||
result[key] = value
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def validate_timestamp(value):
|
||||
|
||||
require(
|
||||
isinstance(value, str)
|
||||
and bool(value)
|
||||
and "T" in value,
|
||||
"Invalid backup timestamp",
|
||||
)
|
||||
|
||||
try:
|
||||
parsed = datetime.fromisoformat(value)
|
||||
except ValueError as error:
|
||||
raise BackupError(
|
||||
"Invalid backup timestamp"
|
||||
) from error
|
||||
|
||||
require(
|
||||
parsed.tzinfo is not None
|
||||
and parsed.utcoffset() is not None,
|
||||
"Backup timestamp requires timezone",
|
||||
)
|
||||
|
||||
|
||||
def sync_directory(path):
|
||||
|
||||
fd = os.open(
|
||||
@@ -149,8 +188,18 @@ class BackupEngine:
|
||||
|
||||
raw = output.getvalue().decode("utf-8").strip()
|
||||
|
||||
return self.validate_identity(raw)
|
||||
|
||||
def validate_identity(self, raw):
|
||||
|
||||
require(
|
||||
raw and "\n" not in raw,
|
||||
isinstance(raw, str)
|
||||
and bool(raw.strip())
|
||||
and raw == raw.strip()
|
||||
and all(
|
||||
char == chr(9) or char.isprintable()
|
||||
for char in raw
|
||||
),
|
||||
"Invalid database identity output",
|
||||
)
|
||||
|
||||
@@ -187,7 +236,7 @@ class BackupEngine:
|
||||
if driver == "mysql":
|
||||
|
||||
require(
|
||||
bool(parts[2]),
|
||||
bool(parts[2].strip()),
|
||||
"Missing database server identity",
|
||||
)
|
||||
|
||||
@@ -369,7 +418,10 @@ class BackupEngine:
|
||||
|
||||
with os.fdopen(fd, "r", encoding="utf-8") as handle:
|
||||
|
||||
manifest = json.load(handle)
|
||||
manifest = json.load(
|
||||
handle,
|
||||
object_pairs_hook=unique_json_object,
|
||||
)
|
||||
|
||||
fields = {
|
||||
"format_version",
|
||||
@@ -401,6 +453,15 @@ class BackupEngine:
|
||||
"filename": self.filename,
|
||||
}
|
||||
|
||||
require(
|
||||
type(manifest["format_version"]) is int,
|
||||
"Invalid backup format version",
|
||||
)
|
||||
|
||||
validate_timestamp(manifest["created_at"])
|
||||
|
||||
self.validate_identity(manifest["identity"])
|
||||
|
||||
for key, value in expected.items():
|
||||
|
||||
require(
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
"""Strict backup manifest validation regression tests."""
|
||||
|
||||
import json
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
from pathlib import Path
|
||||
from types import SimpleNamespace
|
||||
|
||||
from core.backup import BackupEngine, BackupError
|
||||
|
||||
|
||||
class BackupManifestValidationTests(unittest.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
temp = tempfile.TemporaryDirectory()
|
||||
self.addCleanup(temp.cleanup)
|
||||
|
||||
self.root = Path(temp.name)
|
||||
self.root.chmod(0o700)
|
||||
|
||||
self.configure("mysql")
|
||||
|
||||
def configure(self, driver):
|
||||
self.adapter = SimpleNamespace(
|
||||
database={
|
||||
"driver": driver,
|
||||
"name": "example_db",
|
||||
"username": "example_app",
|
||||
"container": "test-database",
|
||||
},
|
||||
identity_command=lambda: ["identity"],
|
||||
backup_command=lambda: ["backup"],
|
||||
)
|
||||
|
||||
identity = (
|
||||
b"example_db\texample_app@localhost\tserver-uuid"
|
||||
if driver == "mysql"
|
||||
else b"example_db|example_app"
|
||||
)
|
||||
|
||||
def runner(command, output):
|
||||
if command == ["identity"]:
|
||||
output.write(identity)
|
||||
elif command == ["backup"]:
|
||||
output.write(b"synthetic backup data")
|
||||
else:
|
||||
raise AssertionError("Unexpected command")
|
||||
|
||||
self.engine = BackupEngine(
|
||||
self.adapter, self.root, runner
|
||||
)
|
||||
|
||||
self.backup = self.engine.create()
|
||||
self.manifest = self.backup / "manifest.json"
|
||||
|
||||
def reject(self, key, value):
|
||||
data = json.loads(self.manifest.read_text())
|
||||
data[key] = value
|
||||
self.manifest.write_text(json.dumps(data))
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
self.engine.verify(self.backup)
|
||||
|
||||
def test_valid_mysql_manifest(self):
|
||||
self.engine.verify(self.backup)
|
||||
|
||||
def test_valid_postgresql_manifest(self):
|
||||
self.configure("pgsql")
|
||||
self.engine.verify(self.backup)
|
||||
|
||||
def test_null_timestamp(self):
|
||||
self.reject("created_at", None)
|
||||
|
||||
def test_empty_timestamp(self):
|
||||
self.reject("created_at", "")
|
||||
|
||||
def test_numeric_timestamp(self):
|
||||
self.reject("created_at", 123)
|
||||
|
||||
def test_invalid_timestamp(self):
|
||||
self.reject("created_at", "not-a-date")
|
||||
|
||||
def test_date_without_time(self):
|
||||
self.reject("created_at", "2026-11-27")
|
||||
|
||||
def test_timestamp_without_timezone(self):
|
||||
self.reject(
|
||||
"created_at", "2026-11-27T08:49:03"
|
||||
)
|
||||
|
||||
def test_impossible_timestamp(self):
|
||||
self.reject(
|
||||
"created_at", "2026-02-30T08:49:03+00:00"
|
||||
)
|
||||
|
||||
def test_null_identity(self):
|
||||
self.reject("identity", None)
|
||||
|
||||
def test_empty_identity(self):
|
||||
self.reject("identity", "")
|
||||
|
||||
def test_whitespace_identity(self):
|
||||
self.reject("identity", " ")
|
||||
|
||||
def test_numeric_identity(self):
|
||||
self.reject("identity", 123)
|
||||
|
||||
def test_wrong_database_identity(self):
|
||||
self.reject(
|
||||
"identity",
|
||||
"other_db\texample_app@localhost\tserver-uuid"
|
||||
)
|
||||
|
||||
def test_wrong_account_identity(self):
|
||||
self.reject(
|
||||
"identity",
|
||||
"example_db\tother_app@localhost\tserver-uuid"
|
||||
)
|
||||
|
||||
def test_missing_mysql_server_identity(self):
|
||||
self.reject(
|
||||
"identity",
|
||||
"example_db\texample_app@localhost\t"
|
||||
)
|
||||
|
||||
def test_identity_control_character(self):
|
||||
self.reject(
|
||||
"identity",
|
||||
"example_db\texample_app@localhost\tserver\nuuid"
|
||||
)
|
||||
|
||||
def test_invalid_postgresql_identity(self):
|
||||
self.configure("pgsql")
|
||||
self.reject("identity", "example_db|wrong_account")
|
||||
|
||||
def test_boolean_format_version(self):
|
||||
self.reject("format_version", True)
|
||||
|
||||
def test_duplicate_json_key(self):
|
||||
data = self.manifest.read_text().strip()
|
||||
|
||||
# Both values are valid. This tests duplicate-key
|
||||
# rejection rather than invalid field values.
|
||||
data = data[:-1] + ', "format_version": 1}'
|
||||
|
||||
self.manifest.write_text(data)
|
||||
|
||||
with self.assertRaises(BackupError):
|
||||
self.engine.verify(self.backup)
|
||||
Reference in New Issue
Block a user