tedder / requests-aws4auth

Amazon Web Services version 4 authentication for the Python Requests module
MIT License
178 stars 62 forks source link

Add tests for signature/Host header mismatch #68

Closed phillipberndt closed 1 year ago

phillipberndt commented 1 year ago

This adds the unit tests we discussed in #65.

Pytest output:

================================================================================================================= FAILURES =================================================================================================================
_____________________________________________________________________________ AWS4Auth_GetCanonicalHeaders_Test.test_netloc_port_is_kept_for_non_standard_port _____________________________________________________________________________

self = <requests_aws4auth.test.test_requests_aws4auth.AWS4Auth_GetCanonicalHeaders_Test testMethod=test_netloc_port_is_kept_for_non_standard_port>

    def test_netloc_port_is_kept_for_non_standard_port(self):
        """
        When urllib3 is used, the Host header is not part of the prepared request,
        but generated later, and the port is kept in the header if it is not the
        standard HTTPS port. d190dcb has a bug that also strips non-standard ports
        from the signature, causing signature and host header to mismatch. This is
        a regression test for that bug.

        """
        req = requests.Request('GET', 'https://amazonaws.com:8443')
        preq = req.prepare()
        self.assertNotIn('host', preq.headers)
        result = AWS4Auth.get_canonical_headers(preq, include=['host'])
        cano_hdrs, signed_hdrs = result
        expected = 'host:amazonaws.com:8443\n'
>       self.assertEqual(cano_hdrs, expected)
E       AssertionError: 'host:amazonaws.com\n' != 'host:amazonaws.com:8443\n'
E       - host:amazonaws.com
E       + host:amazonaws.com:8443
E       ?                   +++++

requests_aws4auth/test/test_requests_aws4auth.py:977: AssertionError

..which is exactly the issue others complained about in #34, I fixed in 8e1417 and that it then turned out another user relied on in #65.

tedder commented 1 year ago

Phillip, this is great, thanks. It fails as we expect and communicates it very well.

tedder commented 1 year ago

Merging but I won't cut a release for it.