38 lines
1.5 KiB
Python
38 lines
1.5 KiB
Python
"""Check database member identity/type/header without extracting an archive.
|
|
|
|
This is format qualification, not SQLite integrity, a restore or recovery-set proof.
|
|
"""
|
|
from pathlib import PurePosixPath
|
|
import sys
|
|
import tarfile
|
|
|
|
|
|
def validate(archive):
|
|
found = False
|
|
with tarfile.open(archive, mode='r|gz') as bundle:
|
|
for member in bundle:
|
|
path = PurePosixPath(member.name)
|
|
if path.name != 'jobtracker.db':
|
|
continue
|
|
if path.is_absolute() or '..' in path.parts or '\\' in member.name:
|
|
raise ValueError('SQLite backup has an unsafe database member path.')
|
|
if not member.isfile() or member.size < 16:
|
|
raise ValueError('SQLite backup database must be a nonempty regular file.')
|
|
with bundle.extractfile(member) as data:
|
|
if data.read(16) != b'SQLite format 3\x00':
|
|
raise ValueError('SQLite backup database header is invalid.')
|
|
found = True
|
|
if not found:
|
|
raise ValueError('SQLite backup contains no database member.')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
try:
|
|
if len(sys.argv) != 2:
|
|
raise ValueError('Provide one SQLite backup archive.')
|
|
validate(sys.argv[1])
|
|
except (ValueError, OSError, EOFError, tarfile.TarError):
|
|
# Archive names, member names and database contents may contain private data.
|
|
print('SQLite backup member validation failed.', file=sys.stderr)
|
|
sys.exit(1)
|