Skip to content
This repository was archived by the owner on Apr 13, 2024. It is now read-only.

Commit 99a48f5

Browse files
committed
Handle a case in the authorization header where there's garbage (non-keypairs) after the method name.
1 parent de3c186 commit 99a48f5

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

httpsig/utils.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,16 @@ def parse_authorization_header(header):
7171
fields = parse_http_list(auth_value)
7272

7373
for item in fields:
74-
# Split on the first '=' only.
75-
key, value = item.split('=', 1)
74+
# Only include keypairs.
75+
if '=' in item:
76+
# Split on the first '=' only.
77+
key, value = item.split('=', 1)
7678

77-
# Unquote values, if quoted.
78-
if value[0] == '"':
79-
value = value[1:-1]
79+
# Unquote values, if quoted.
80+
if value[0] == '"':
81+
value = value[1:-1]
8082

81-
values[key] = value
83+
values[key] = value
8284

8385
# ("Signature", {"headers": "date", "algorithm": "hmac-sha256", ... })
8486
return (auth[0], CaseInsensitiveDict(values))

httpsig/verify.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ class HeaderVerifier(Verifier):
4747
Verifies an HTTP signature from given headers.
4848
"""
4949
def __init__(self, headers, secret, required_headers=None, method=None, path=None, host=None):
50+
"""
51+
Instantiate a HeaderVerifier object.
52+
53+
:param headers: A dictionary of headers from the HTTP request.
54+
:param secret: The HMAC secret or RSA *public* key.
55+
:param required_headers: Optional. A list of headers required to be present to validate, even if the signature is otherwise valid. Defaults to ['date'].
56+
:param method: Optional. The HTTP method used in the request (eg. "GET"). Required for the '(request-line)' header.
57+
:param path: Optional. The HTTP path requested, exactly as sent (including query arguments and fragments). Required for the '(request-line)' header.
58+
:param host: Optional. The value to use for the Host header, if not supplied in :param:headers.
59+
"""
5060
required_headers = required_headers or ['date']
5161

5262
auth = parse_authorization_header(headers['authorization'])
@@ -64,6 +74,12 @@ def __init__(self, headers, secret, required_headers=None, method=None, path=Non
6474
super(HeaderVerifier, self).__init__(secret, algorithm=self.auth_dict['algorithm'])
6575

6676
def verify(self):
77+
"""
78+
Verify the headers based on the arguments passed at creation and current properties.
79+
80+
Raises an Exception if a required header (:param:required_headers) is not found in the signature.
81+
Returns True or False.
82+
"""
6783
auth_headers = self.auth_dict.get('headers', 'date').split(' ')
6884

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

0 commit comments

Comments
 (0)