Skip to content

Commit

Permalink
Compute host header correctly
Browse files Browse the repository at this point in the history
Signatures need to include the host header, but the requests library
does not include it in prepared requests by default. Rather, it trusts
that Python's HTTP client will compute and inject it when sending the
request. This forces requests-aws4auth to compute how this header will
look like.

A slight discrepancy between the implementations is that the code in
this library unconditionally skips the port, whereas the request ending
up being sent will include a port if it does not match the URL scheme's
default.

This change adjusts the implementations to match in that regard.

Fixes tedder#34
  • Loading branch information
phillipberndt committed Jan 19, 2023
1 parent 3b4d2da commit db40d90
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion requests_aws4auth/aws4auth.py
Expand Up @@ -615,7 +615,15 @@ def get_canonical_headers(cls, req, include=None):
# in the signed headers, but Requests doesn't include it in a
# PreparedRequest
if 'host' not in headers:
headers['host'] = urlparse(str(req.url)).netloc.split(':')[0]
purl = urlparse(str(req.url))
netloc = purl.netloc
# Python's http client only includes the port if it is non-default,
# see http.client.HTTPConnection.putrequest
if (':' in netloc
and (purl.scheme == 'http' and purl.port == 80 or
purl.scheme == 'https' and purl.port == 443)):
netloc = netloc.rsplit(":", 1)[0]
headers['host'] = netloc
# Aggregate for upper/lowercase header name collisions in header names,
# AMZ requires values of colliding headers be concatenated into a
# single header with lowercase name. Although this is not possible with
Expand Down

0 comments on commit db40d90

Please sign in to comment.