From f6e2fe19821ce1382500c041485dad0ef6e61dbb Mon Sep 17 00:00:00 2001 From: FayezBast Date: Thu, 26 Nov 2026 02:19:42 +0200 Subject: [PATCH] Metadata API: Accept zero-offset expiration datetimes Signed-off-by: FayezBast --- tests/test_api.py | 30 +++++++++++++++++++++++++++++- tuf/api/_payload.py | 16 ++++++---------- 2 files changed, 35 insertions(+), 11 deletions(-) diff --git a/tests/test_api.py b/tests/test_api.py index 7dcc8a7d8a..4d0c4adee0 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -15,6 +15,7 @@ from datetime import UTC, datetime, timedelta, timezone from pathlib import Path from typing import ClassVar +from zoneinfo import ZoneInfo, ZoneInfoNotFoundError from securesystemslib import exceptions as sslib_exceptions from securesystemslib.signer import ( @@ -334,11 +335,38 @@ def test_metadata_signed_is_expired(self) -> None: self.assertFalse(is_expired) md.signed.expires = expires + def test_signed_expires_normalizes_utc(self) -> None: + expires = datetime(2030, 1, 1, 12, 0, microsecond=123456, tzinfo=UTC) + expected = expires.replace(microsecond=0) + for tz in (None, UTC, timezone(timedelta(0), "GMT")): + with self.subTest(tz=tz): + timestamp = Timestamp(expires=expires.replace(tzinfo=tz)) + self.assertEqual(timestamp.expires, expected) + self.assertIs(timestamp.expires.tzinfo, UTC) + self.assertEqual( + timestamp.to_dict()["expires"], "2030-01-01T12:00:00Z" + ) + + def test_signed_expires_accepts_zoneinfo_utc(self) -> None: + try: + utc_zone = ZoneInfo("UTC") + except ZoneInfoNotFoundError: + self.skipTest("IANA timezone data for UTC is unavailable") + + expires = datetime(2030, 1, 1, 12, 0, tzinfo=utc_zone) + timestamp = Timestamp(expires=expires) + self.assertEqual(timestamp.expires, expires) + self.assertIs(timestamp.expires.tzinfo, UTC) + + timestamp.expires = expires + timedelta(days=1) + self.assertIs(timestamp.expires.tzinfo, UTC) + self.assertEqual(timestamp.to_dict()["expires"], "2030-01-02T12:00:00Z") + 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... + # A nonzero UTC offset is rejected... plus_two = timezone(timedelta(hours=2)) with self.assertRaises(ValueError): timestamp.expires = datetime(2031, 1, 1, 12, 0, tzinfo=plus_two) diff --git a/tuf/api/_payload.py b/tuf/api/_payload.py index 101ba400ad..bef0ac5136 100644 --- a/tuf/api/_payload.py +++ b/tuf/api/_payload.py @@ -12,7 +12,7 @@ import io import logging from dataclasses import dataclass -from datetime import UTC, datetime +from datetime import UTC, datetime, timedelta from typing import ( IO, TYPE_CHECKING, @@ -114,15 +114,11 @@ 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) """ - expires = value.replace(microsecond=0) - if expires.tzinfo is None: - # Naive datetime: just make it UTC - 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 + if value.tzinfo is not None and value.utcoffset() != timedelta(0): + raise ValueError(f"Expected tz UTC, not {value.tzinfo}") + + # Treat naive datetimes as UTC and store all expirations in UTC. + self._expires = value.replace(microsecond=0, tzinfo=UTC) # 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",