Switch hosts on HSTS preload to HTTPS (#151)
This commit is contained in:
parent
7e9110b978
commit
3ba2e8c328
@ -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 ""
|
||||
|
||||
@ -2,6 +2,7 @@ certifi
|
||||
chardet==3.*
|
||||
h11==0.8.*
|
||||
h2==3.*
|
||||
hstspreload
|
||||
idna==2.*
|
||||
rfc3986==1.*
|
||||
|
||||
|
||||
1
setup.py
1
setup.py
@ -51,6 +51,7 @@ setup(
|
||||
"chardet==3.*",
|
||||
"h11==0.8.*",
|
||||
"h2==3.*",
|
||||
"hstspreload",
|
||||
"idna==2.*",
|
||||
"rfc3986==1.*",
|
||||
],
|
||||
|
||||
@ -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"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user