diff --git a/scripts/network-sandbox-guests-host.py b/scripts/network-sandbox-guests-host.py index 4358412..a262b00 100644 --- a/scripts/network-sandbox-guests-host.py +++ b/scripts/network-sandbox-guests-host.py @@ -4,7 +4,6 @@ The caller supplies CONFIGS and AUTHORIZED_KEY when streaming this program to the host. Existing VM/container IDs are never adopted or overwritten. """ -import gzip import hashlib import json import os @@ -14,6 +13,7 @@ import subprocess import sys import tempfile import urllib.request +import zlib from pathlib import Path BASE = Path('/data/jeannie/network-sandbox') @@ -91,10 +91,22 @@ def check_preconditions(): raise RuntimeError('opi5_ssd is not available for the OpenWrt VM disk') +def extract_openwrt_image(archive, image): + """Extract the verified gzip member; OpenWrt may append image metadata.""" + decoder = zlib.decompressobj(16 + zlib.MAX_WBITS) + with archive.open('rb') as source, image.open('wb') as dest: + while not decoder.eof: + chunk = source.read(1024 * 1024) + if not chunk: + raise RuntimeError('OpenWrt gzip stream is truncated') + while chunk and not decoder.eof: + dest.write(decoder.decompress(chunk, 1024 * 1024)) + chunk = decoder.unconsumed_tail + + def write_router_image(archive, work): image = work / 'openwrt.img' - with gzip.open(archive, 'rb') as source, image.open('wb') as dest: - shutil.copyfileobj(source, dest) + extract_openwrt_image(archive, image) loop = run('losetup', '--find', '--partscan', '--show', str(image)) mountpoint = work / 'root' mountpoint.mkdir() @@ -161,7 +173,7 @@ def main(): print('Created stopped OpenWrt VM 9100 and stopped Alpine containers 9110 and 9111.') print('WAN remains disconnected. Guests were not started; runtime guard was not changed.') print(f'Provisioning files retained at {work}') - except (OSError, RuntimeError, KeyError, ValueError, subprocess.SubprocessError) as exc: + except (OSError, RuntimeError, KeyError, ValueError, zlib.error, subprocess.SubprocessError) as exc: print(f'Sandbox guest provisioning failed: {exc}. Existing and partially created resources were preserved.', file=sys.stderr) return 1 return 0 diff --git a/tests/jeannie/test_network_sandbox_guests.py b/tests/jeannie/test_network_sandbox_guests.py index 6e98deb..1f460e2 100644 --- a/tests/jeannie/test_network_sandbox_guests.py +++ b/tests/jeannie/test_network_sandbox_guests.py @@ -1,5 +1,6 @@ """Offline checks for sandbox guest image verification.""" import hashlib +import gzip import importlib.util import tempfile import unittest @@ -13,6 +14,22 @@ spec.loader.exec_module(guests) class SandboxGuestTests(unittest.TestCase): + def test_openwrt_gzip_with_appended_metadata(self): + content = b'openwrt image data' * 1000 + with tempfile.TemporaryDirectory() as directory: + archive = Path(directory) / 'image.gz' + output = Path(directory) / 'image' + archive.write_bytes(gzip.compress(content) + b'# appended metadata') + guests.extract_openwrt_image(archive, output) + self.assertEqual(output.read_bytes(), content) + + def test_truncated_openwrt_gzip_is_rejected(self): + with tempfile.TemporaryDirectory() as directory: + archive = Path(directory) / 'image.gz' + archive.write_bytes(gzip.compress(b'content')[:-4]) + with self.assertRaisesRegex(RuntimeError, 'truncated'): + guests.extract_openwrt_image(archive, Path(directory) / 'image') + def test_download_requires_matching_vendor_checksum(self): content = b'example guest image' digest = hashlib.sha256(content).hexdigest()