Skip to content
Merged
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
15 changes: 14 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
13 changes: 8 additions & 5 deletions tuf/api/_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading