Skip to content

Navigation Menu

Sign up

Metadata API: Accept zero-offset expiration datetimes #3064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
FayezBast wants to merge 1 commit into theupdateframework:develop
base: develop
Choose a base branch
Loading
from FayezBast:fix/utc-zoneinfo-expiration
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion tests/test_api.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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月01日T12: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月02日T12: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)
Expand Down
16 changes: 6 additions & 10 deletions tuf/api/_payload.py
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down

AltStyle によって変換されたページ (->オリジナル) /