From d7585094de8ff969ce40501d7ef879990ed8e2dd Mon Sep 17 00:00:00 2001 From: Bahaa Mattar Date: Thu, 19 Nov 2026 23:58:48 +0200 Subject: [PATCH] Metadata API: Do not store a rejected expires value The Signed.expires setter assigned the new value to self._expires before checking its timezone. When the value was rejected with ValueError (a timezone-aware datetime that is not UTC), the object kept the rejected value: reading expires returned the non-UTC datetime, and to_dict() then serialized its local time with a "Z" suffix, so the stored expiry was off by the UTC offset. Validate first and only assign once the value is known to be valid. Add a test that a rejected value leaves expires, and the serialized expiry, unchanged. Fixes #3054 Signed-off-by: Bahaa Mattar --- tests/test_api.py | 15 ++++++++++++++- tuf/api/_payload.py | 13 ++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index fc69da088c..7dcc8a7d8a 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -12,7 +12,7 @@ import tempfile import unittest from copy import copy, deepcopy -from datetime import UTC, datetime, timedelta +from datetime import UTC, datetime, timedelta, timezone from pathlib import Path from typing import ClassVar @@ -334,6 +334,19 @@ def test_metadata_signed_is_expired(self) -> None: self.assertFalse(is_expired) md.signed.expires = expires + def test_signed_expires_setter_invalid_timezone(self) -> None: + # Use of Timestamp is arbitrary, we're just testing the base class + timestamp = Timestamp(expires=datetime(2030, 1, 1, tzinfo=UTC)) + + # A timezone other than UTC is rejected... + plus_two = timezone(timedelta(hours=2)) + with self.assertRaises(ValueError): + timestamp.expires = datetime(2031, 1, 1, 12, 0, tzinfo=plus_two) + + # ... and the rejected value must not leak into the object + self.assertEqual(timestamp.expires, datetime(2030, 1, 1, tzinfo=UTC)) + self.assertEqual(timestamp.to_dict()["expires"], "2030-01-01T00:00:00Z") + def test_metadata_verify_delegate(self) -> None: root_path = os.path.join(self.repo_dir, "metadata", "root.json") root = Metadata[Root].from_file(root_path) diff --git a/tuf/api/_payload.py b/tuf/api/_payload.py index c7ed365d0d..101ba400ad 100644 --- a/tuf/api/_payload.py +++ b/tuf/api/_payload.py @@ -114,12 +114,15 @@ def expires(self, value: datetime) -> None: # Use 'datetime' module to e.g. expire in seven days from now obj.expires = now(timezone.utc) + timedelta(days=7) """ - self._expires = value.replace(microsecond=0) - if self._expires.tzinfo is None: + expires = value.replace(microsecond=0) + if expires.tzinfo is None: # Naive datetime: just make it UTC - self._expires = self._expires.replace(tzinfo=UTC) - elif self._expires.tzinfo != UTC: - raise ValueError(f"Expected tz UTC, not {self._expires.tzinfo}") + expires = expires.replace(tzinfo=UTC) + elif expires.tzinfo != UTC: + raise ValueError(f"Expected tz UTC, not {expires.tzinfo}") + + # Store the value only after it has been validated + self._expires = expires # NOTE: Signed is a stupid name, because this might not be signed yet, but # we keep it to match spec terminology (I often refer to this as "payload",