]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Don't include username/password components in `Host` header (#417)
authorCan Sarıgöl <cansarigol@derinbilgi.com.tr>
Fri, 4 Oct 2019 09:33:18 +0000 (12:33 +0300)
committerTom Christie <tom@tomchristie.com>
Fri, 4 Oct 2019 09:33:18 +0000 (10:33 +0100)
* removed auth and port from host of header

* used URL attribute rather _uri_reference

* reverted removing port into host

* reverted username and password from header

* applied new copy_with with username and password

httpx/models.py
tests/client/test_headers.py

index 97820e0aff86bea8a5ec029b810dfc290d85642b..f70fdf440a5e9d6267be85dd22467b253cd43fb7 100644 (file)
@@ -136,6 +136,10 @@ class URL:
     def authority(self) -> str:
         return self._uri_reference.authority or ""
 
+    @property
+    def userinfo(self) -> str:
+        return self._uri_reference.userinfo or ""
+
     @property
     def username(self) -> str:
         userinfo = self._uri_reference.userinfo or ""
@@ -635,7 +639,10 @@ class BaseRequest:
         has_connection = "connection" in self.headers
 
         if not has_host:
-            auto_headers.append((b"host", self.url.authority.encode("ascii")))
+            url = self.url
+            if url.userinfo:
+                url = url.copy_with(username=None, password=None)
+            auto_headers.append((b"host", url.authority.encode("ascii")))
         if not has_user_agent:
             auto_headers.append((b"user-agent", USER_AGENT.encode("ascii")))
         if not has_accept:
index c5429a8713d8f20d214dbcb34fd2f444d222e020..a8c5445bcde9a3d2c24b0a28dcc78aaba32dd899 100755 (executable)
@@ -131,3 +131,22 @@ def test_header_does_not_exist():
     headers = models.Headers({"foo": "bar"})
     with pytest.raises(KeyError):
         del headers["baz"]
+
+
+def test_host_without_auth_in_header():
+    url = "http://username:password@example.org:80/echo_headers"
+
+    with Client(dispatch=MockDispatch()) as client:
+        response = client.get(url)
+
+    assert response.status_code == 200
+    assert response.json() == {
+        "headers": {
+            "accept": "*/*",
+            "accept-encoding": "gzip, deflate, br",
+            "connection": "keep-alive",
+            "host": "example.org:80",
+            "user-agent": f"python-httpx/{__version__}",
+            "authorization": "Basic dXNlcm5hbWU6cGFzc3dvcmQ=",
+        }
+    }