From 5aeb4d90abefec3d499f05b870d12b709d6a5d8c Mon Sep 17 00:00:00 2001 From: Bahaa Mattar Date: Thu, 19 Nov 2026 09:08:25 +0200 Subject: [PATCH] dsse: Raise DeserializationError for invalid payload content SimpleEnvelope.get_signed() documents that it raises DeserializationError if the payload cannot be deserialized, but only the JSON decoding and the role type lookup were inside the try block: the call to from_dict() was outside of it. A payload with a known role type but invalid content (for example missing fields) leaked KeyError, TypeError or ValueError instead. With EnvelopeType.SIMPLE this also reached ngclient: a malformed root passed to Updater() or TrustedMetadataSet() raised e.g. KeyError instead of a RepositoryError. Move the from_dict() call into the try block and add a test. Fixes #3048 Signed-off-by: Bahaa Mattar --- tests/test_api.py | 27 +++++++++++++++++++++++++++ tuf/api/dsse.py | 4 +++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/tests/test_api.py b/tests/test_api.py index 6cec6005ab..fc69da088c 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1242,6 +1242,33 @@ def test_fail_payload_deserialization(self) -> None: with self.assertRaises(DeserializationError): envelope.get_signed() + def test_fail_invalid_payload_content_deserialization(self) -> None: + """Payload has a known role type but its content is not valid.""" + valid = { + "_type": "timestamp", + "version": 1, + "spec_version": "1.0.31", + "expires": "2030-01-01T00:00:00Z", + "meta": {"snapshot.json": {"version": 1}}, + } + invalid_payloads = [ + {"_type": "root"}, # required fields are missing + {**valid, "version": "1"}, # wrong type + {**valid, "version": 0}, # invalid value + {**valid, "expires": "tomorrow"}, # invalid date + ] + for payload in invalid_payloads: + envelope = SimpleEnvelope(json.dumps(payload).encode(), "bar", {}) + with ( + self.subTest(payload=payload), + self.assertRaises(DeserializationError), + ): + envelope.get_signed() + + # The same payload, with valid content, deserializes + envelope = SimpleEnvelope(json.dumps(valid).encode(), "bar", {}) + self.assertIsInstance(envelope.get_signed(), Timestamp) + def test_verify_delegate(self) -> None: """Basic verification test. diff --git a/tuf/api/dsse.py b/tuf/api/dsse.py index 8299100a8c..dc16cf79fc 100644 --- a/tuf/api/dsse.py +++ b/tuf/api/dsse.py @@ -150,7 +150,9 @@ def get_signed(self) -> T: else: raise ValueError(f'unrecognized role type "{_type}"') + signed = inner_cls.from_dict(payload_dict) + except Exception as e: raise DeserializationError from e - return cast("T", inner_cls.from_dict(payload_dict)) + return cast("T", signed)