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

Make timestamped signatures compatible with Python #2

Merged
merged 3 commits into from
Oct 7, 2024
Merged

Conversation

alext
Copy link
Member

@alext alext commented Oct 7, 2024

This fixes a few issues relating to handling of timestamps to make this compatible with the Python version:

The Python implementation initially used an epoch of 2011/01/01, but this was dropped in favour of the standard unix epoch prior to version 1.0.0. This implementation had not been updated to reflect this change

Fix handling for timestamps > 4 bytes - the Python implementation serialises the timestamp as an 8-byte integer
(using pack with '>Q'), and then trims leading zeroes. For current timestamps, this output is the same as for a 4-byte integer. This implementation was parsing the timestamp assuming a fixed 4-byte integer representation, which means it would break when the current timestamp spills over into more than 4 bytes.

alext added 3 commits October 7, 2024 10:31
The Python implementation initially used an epoch of 2011/01/01, but
this was dropped in favour of the standard unix epoch prior to version
1.0.0[1]. This implementation is still using the original epoch, which
means it's not compatible with Python. This therefore updates this
implementation to use the unix epoch to match the Python implementation.

In order to test this change, I've introduced a NowFunc function to
allow overriding the function it uses to obtain the local time, so that
the tests can mock the current time for repeatability. I considered that
this would be also be useful for consumers of this library, so I've made
it an exported value.

[1]pallets/itsdangerous#46
The Python implementation serialises the timestamp as an 8-byte integer
(using pack with '>Q'[1]), and then trims leading zeroes[2].

This implementation was assuming a fixed 4-byte representation, which
only works because all current timestamps are 4 bytes once leading
zeroes are removed. This updates it to match the Python implementation,
which means it won't break when the current timestamp overflows 4 bytes.

[1]https://github.com/pallets/itsdangerous/blob/2.2.0/src/itsdangerous/encoding.py#L44
[2]https://github.com/pallets/itsdangerous/blob/2.2.0/src/itsdangerous/encoding.py#L50
Remove the need for buffers and readers by using the binary.BigEndian
interface directly to read/write from the byte slice.
@alext alext merged commit 28100d0 into master Oct 7, 2024
1 check passed
@alext alext deleted the fix_timestamps branch October 7, 2024 09:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant