Switch hosts on HSTS preload to HTTPS (#151)

This commit is contained in:
Seth Michael Larson 2019-07-28 12:02:21 -05:00 committed by GitHub
parent 7e9110b978
commit 3ba2e8c328
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 0 deletions

View 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 ""

View File

@ -2,6 +2,7 @@ certifi
chardet==3.*
h11==0.8.*
h2==3.*
hstspreload
idna==2.*
rfc3986==1.*

View File

@ -51,6 +51,7 @@ setup(
"chardet==3.*",
"h11==0.8.*",
"h2==3.*",
"hstspreload",
"idna==2.*",
"rfc3986==1.*",
],

View 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"