Skip to content
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

Catch unhandled ValueError from future timestamp #296

Merged
merged 3 commits into from Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -3,6 +3,8 @@ Version 2.1.1

Unreleased

- Handle date overflow in timed unsign. :pr:`296`


Version 2.1.0
-------------
Expand Down
10 changes: 8 additions & 2 deletions src/itsdangerous/timed.py
Expand Up @@ -38,7 +38,8 @@ def get_timestamp(self) -> int:

def timestamp_to_datetime(self, ts: int) -> datetime:
"""Convert the timestamp from :meth:`get_timestamp` into an
aware :class`datetime.datetime` in UTC.
aware :class`datetime.datetime` in UTC. Raises :exc:`.ValueError`
if the timestamp is too far in the past or future for Python.

.. versionchanged:: 2.0
The timestamp is returned as a timezone-aware ``datetime``
Expand Down Expand Up @@ -124,7 +125,12 @@ def unsign(
# split the value and the timestamp.
if sig_error is not None:
if ts_int is not None:
ts_dt = self.timestamp_to_datetime(ts_int)
try:
ts_dt = self.timestamp_to_datetime(ts_int)
except ValueError as exc:
raise BadTimeSignature(
"Malformed timestamp", payload=value
) from exc

raise BadTimeSignature(str(sig_error), payload=value, date_signed=ts_dt)

Expand Down
13 changes: 13 additions & 0 deletions tests/test_itsdangerous/test_timed.py
@@ -1,3 +1,4 @@
import sys
from datetime import datetime
from datetime import timedelta
from datetime import timezone
Expand Down Expand Up @@ -66,6 +67,18 @@ def test_malformed_timestamp(self, signer):
assert "Malformed" in str(exc_info.value)
assert exc_info.value.date_signed is None

@pytest.mark.skipif(
sys.platform == "win32", reason="Freezegun Invalid argument occurs on Windows"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be ignored. It's not freezegun that fails, it's datetime. Apparently datetime on Windows raises an OSError for an invalid timestamp value. This is expected since the handling is based on the operating system: https://bugs.python.org/issue45031

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, I see you fixed it and merged. Thanks!

)
def test_malformed_future_timestamp(self, signer):
signed = b"value.TgPVoaGhoQ.AGBfQ6G6cr07byTRt0zAdPljHOY"

with pytest.raises(BadTimeSignature) as exc_info:
signer.unsign(signed)

assert "Malformed" in str(exc_info.value)
assert exc_info.value.date_signed is None

def test_future_age(self, signer):
signed = signer.sign("value")

Expand Down