Skip to content
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.

Commit

Permalink
Return false in verify on algorith mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
fulder committed Aug 26, 2020
1 parent c5163f8 commit 912ff85
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
14 changes: 14 additions & 0 deletions httpsig/tests/test_verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,17 @@ def setUp(self):
self.sign_secret = private_key
self.verify_secret = public_key
self.sign_algorithm = PSS(salt_length=0)

def test_algorithm_mismatch(self):
unsigned = {
'Date': self.header_date
}

hs = HeaderSigner(
key_id="Test", secret=self.sign_secret, algorithm=self.algorithm,
sign_header=self.sign_header, sign_algorithm=self.sign_algorithm)
signed = hs.sign(unsigned)

hv = HeaderVerifier(
headers=signed, secret=self.verify_secret, sign_header=self.sign_header, algorithm="rsa-sha256", sign_algorithm=self.sign_algorithm)
self.assertFalse(hv.verify())
14 changes: 8 additions & 6 deletions httpsig/verify.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class HeaderVerifier(Verifier):
"""

def __init__(self, headers, secret, required_headers=None, method=None,
path=None, host=None, sign_header='authorization', sign_algorithm=None):
path=None, host=None, sign_header='authorization', algorithm=None, sign_algorithm=None):
"""
Instantiate a HeaderVerifier object.
Expand All @@ -70,6 +70,7 @@ def __init__(self, headers, secret, required_headers=None, method=None,
header, if not supplied in :param:headers.
:param sign_header: Optional. The header where the signature is.
Default is 'authorization'.
:param algorithm: Algorithm derived from keyId (required for draft version >= 12)
:param sign_algorithm: Required for 'hs2019' algorithm, specifies the
digital signature algorithm (derived from keyId) to use.
"""
Expand All @@ -89,11 +90,7 @@ def __init__(self, headers, secret, required_headers=None, method=None,
self.method = method
self.path = path
self.host = host

if 'algorithm' in self.auth_dict and self.auth_dict['algorithm'] != self.algorithm:
raise HttpSigException(
"Algorithm mismath, signature parameter algorithm was: {}, but algorithm dervice from key is: {}".format(
self.auth_dict['algorithm'], self.algorithm))
self.derived_algorithm = algorithm

if self.auth_dict['algorithm'] != DEFAULT_ALGORITHM:
print("Algorithm: {} is deprecated please update to {}".format(self.auth_dict['algorithm'], DEFAULT_ALGORITHM))
Expand All @@ -112,6 +109,11 @@ def verify(self):
not found in the signature.
Returns True or False.
"""
if 'algorithm' in self.auth_dict and self.derived_algorithm is not None and self.auth_dict['algorithm'] != self.derived_algorithm:
print("Algorithm mismatch, signature parameter algorithm was: {}, but algorithm derived from key is: {}".format(
self.auth_dict['algorithm'], self.derived_algorithm))
return False

auth_headers = self.auth_dict.get('headers', 'date').split(' ')

if len(set(self.required_headers) - set(auth_headers)) > 0:
Expand Down

0 comments on commit 912ff85

Please sign in to comment.