From f83f0bf5e796262af6eb6f2ca139f6411f26f876 Mon Sep 17 00:00:00 2001 From: Dan Smith Date: Thu, 27 Jun 2024 09:33:55 -0700 Subject: [PATCH] Add QED format detection to format_inspector This merely recognizes this format and always marks it as unsafe because no service supports it. This prevents someone from uploading one that we will ask qemu-img to inspect. Change-Id: Ieea7b7eb0f380571bd4937cded920776e05f7ec4 (cherry picked from commit 4096c5aff1d046d5c28d0e4a69b3c9574e9e8cc8) (cherry picked from commit ba98022b98ef5b9c98e1d7d20c88e1ca4b23fa80) (cherry picked from commit a8dadcd7994c88a61f94341286b4bcd693b51b32) (cherry picked from commit 9b3faf376bfc911c7cabde458507a0c65d63d70c) --- glance/common/format_inspector.py | 18 ++++++++++++++++++ .../tests/unit/common/test_format_inspector.py | 6 ++++++ 2 files changed, 24 insertions(+) diff --git a/glance/common/format_inspector.py b/glance/common/format_inspector.py index a11ff1a5e0..4d5e4fa45c 100755 --- a/glance/common/format_inspector.py +++ b/glance/common/format_inspector.py @@ -366,6 +366,23 @@ class QcowInspector(FileInspector): not self.has_unknown_features) +class QEDInspector(FileInspector): + def __init__(self, tracing=False): + super().__init__(tracing) + self.new_region('header', CaptureRegion(0, 512)) + + @property + def format_match(self): + if not self.region('header').complete: + return False + return self.region('header').data.startswith(b'QED\x00') + + def safety_check(self): + # QED format is not supported by anyone, but we want to detect it + # and mark it as just always unsafe. + return False + + # The VHD (or VPC as QEMU calls it) format consists of a big-endian # 512-byte "footer" at the beginning of the file with various # information, most of which does not matter to us: @@ -879,6 +896,7 @@ def get_inspector(format_name): 'vhdx': VHDXInspector, 'vmdk': VMDKInspector, 'vdi': VDIInspector, + 'qed': QEDInspector, } return formats.get(format_name) diff --git a/glance/tests/unit/common/test_format_inspector.py b/glance/tests/unit/common/test_format_inspector.py index 9b458f970b..9d4a7cb9ef 100644 --- a/glance/tests/unit/common/test_format_inspector.py +++ b/glance/tests/unit/common/test_format_inspector.py @@ -179,6 +179,12 @@ class TestFormatInspectors(test_utils.BaseTestCase): # a local file. self.assertLess(fmt.actual_size, file_size) + def test_qed_always_unsafe(self): + img = self._create_img('qed', 10 * units.Mi) + fmt = format_inspector.get_inspector('qed').from_file(img) + self.assertTrue(fmt.format_match) + self.assertFalse(fmt.safety_check()) + def _test_vmdk_bad_descriptor_offset(self, subformat=None): format_name = 'vmdk' image_size = 10 * units.Mi