]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Switch hosts on HSTS preload to HTTPS (#151)
authorSeth Michael Larson <sethmichaellarson@gmail.com>
Sun, 28 Jul 2019 17:02:21 +0000 (12:02 -0500)
committerGitHub <noreply@github.com>
Sun, 28 Jul 2019 17:02:21 +0000 (12:02 -0500)
httpx/models.py
requirements.txt
setup.py
tests/models/test_url.py

index d227948e3f526dfdd0dd85fcaf84f7bf39bd1539..2f29be5f9f8e5380d877f7a661ad0835484c6107 100644 (file)
@@ -8,6 +8,7 @@ from http.cookiejar import Cookie, CookieJar
 from urllib.parse import parse_qsl, urlencode
 
 import chardet
+import hstspreload
 import rfc3986
 
 from .config import USER_AGENT
@@ -113,6 +114,14 @@ class URL:
             if not self.host:
                 raise InvalidURL("No host included in URL.")
 
+        # If the URL is HTTP but the host is on the HSTS preload list switch to HTTPS.
+        if (
+            self.scheme == "http"
+            and self.host
+            and hstspreload.in_hsts_preload(self.host)
+        ):
+            self.components = self.components.copy_with(scheme="https")
+
     @property
     def scheme(self) -> str:
         return self.components.scheme or ""
index 98e2d237286a1cf4f379bc56eded8da604196ecf..e7400b09ffec2691b5719aa46d06ace8130c36c2 100644 (file)
@@ -2,6 +2,7 @@ certifi
 chardet==3.*
 h11==0.8.*
 h2==3.*
+hstspreload
 idna==2.*
 rfc3986==1.*
 
index 2e8da065e5fee95e192a616ebf6ac0223d2f7f33..2045210ac3d314f474ffcd40b2a2ed86d320c9a4 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -51,6 +51,7 @@ setup(
         "chardet==3.*",
         "h11==0.8.*",
         "h2==3.*",
+        "hstspreload",
         "idna==2.*",
         "rfc3986==1.*",
     ],
index 34e2e1a57d901cffef97fede84dcf090dac179c6..7c865f5a66405d8bc554cbde15ed7010f923892f 100644 (file)
@@ -124,3 +124,11 @@ def test_url_set():
     url_set = set(urls)
 
     assert all(url in urls for url in url_set)
+
+
+def test_hsts_preload_converted_to_https():
+    url = URL("http://www.paypal.com")
+
+    assert url.is_ssl
+    assert url.scheme == "https"
+    assert url == "https://www.paypal.com"