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

Added SHA-512 fallback by default #114

Merged
merged 1 commit into from
Oct 26, 2018
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
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Version 1.1.0
Released 2018-10-26

- Change default signing algorithm back to SHA-1. (`#113`_)
- Added a default SHA-512 fallback.
- Add support for fallback algorithms during deserialization to
support changing the default in the future without breaking existing
signatures. (`#113`_)
Expand Down
13 changes: 11 additions & 2 deletions src/itsdangerous/serializer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import hashlib

from ._compat import text_type
from ._json import json
from .encoding import want_bytes
Expand Down Expand Up @@ -57,7 +59,9 @@ class to the constructor as well as keyword arguments as a dict that
the constructor.

.. versionchanged:: 1.1:
Added support for ``fallback_signers``.
Added support for ``fallback_signers`` and configured a default
SHA-512 fallback. This fallback is for users who used the 1.0
release which was yanked which had SHA-512 support in it.
"""

#: If a serializer module or class is not passed to the constructor
Expand All @@ -70,6 +74,9 @@ class to the constructor as well as keyword arguments as a dict that
#: .. versionadded:: 0.14
default_signer = Signer

#: The default fallback signers.
default_fallback_signers = [{"digest_method": hashlib.sha512}]

def __init__(
self,
secret_key,
Expand All @@ -90,7 +97,9 @@ def __init__(
signer = self.default_signer
self.signer = signer
self.signer_kwargs = signer_kwargs or {}
self.fallback_signers = fallback_signers or ()
if fallback_signers is None:
fallback_signers = list(self.default_fallback_signers or ())
self.fallback_signers = fallback_signers
self.serializer_kwargs = serializer_kwargs or {}

def load_payload(self, payload, serializer=None):
Expand Down
5 changes: 5 additions & 0 deletions src/itsdangerous/timed.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ def loads(self, s, max_age=None, return_timestamp=False, salt=None):
if return_timestamp:
return payload, timestamp
return payload
# If we get a signature expired it means we could read the
# signature but it's invalid. In that case we do not want to
# try the next signer.
except SignatureExpired:
raise
except BadSignature as err:
last_exception = err
raise last_exception
Expand Down
4 changes: 2 additions & 2 deletions tests/test_itsdangerous/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,12 @@ def test_serializer_kwargs(self, serializer_factory):
assert serializer.loads(serializer.dumps({(): 1})) == {}

def test_fallback_signers(self, serializer_factory, value):
serializer = serializer_factory(signer_kwargs={"digest_method": hashlib.sha512})
serializer = serializer_factory(signer_kwargs={"digest_method": hashlib.sha256})
signed = serializer.dumps(value)

fallback_serializer = serializer_factory(
signer_kwargs={"digest_method": hashlib.sha1},
fallback_signers=[{"digest_method": hashlib.sha512}],
fallback_signers=[{"digest_method": hashlib.sha256}],
)

assert fallback_serializer.loads(signed) == value
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ setenv =
deps =
pytest-cov
freezegun
commands = pytest --cov --cov-report= {posargs}
commands = pytest --tb=short --cov --cov-report= {posargs}

[testenv:stylecheck]
deps = pre-commit
Expand Down