1
This commit is contained in:
1 parent
730adb3ff2
commit
45b0b3044b
5 files changed
+27
-20
No files matched your search
@@ -8,6 +8,7 @@ from urllib.parse import urlsplit
|
||||
LOCAL_HOSTS = {"127.0.0.1", "localhost", "::1"}
|
||||
PUBLIC_HOST = os.environ.get("CRYPTO_SCAM_PUBLIC_HOST", "").strip().lower().rstrip(".")
|
||||
PUBLIC_PORT = int(os.environ.get("CRYPTO_SCAM_PUBLIC_PORT", "443"))
|
||||
PUBLIC_SCHEME = os.environ.get("CRYPTO_SCAM_PUBLIC_SCHEME", "https").strip().lower()
|
||||
|
||||
|
||||
def _authority(value: str):
|
||||
@@ -33,7 +34,7 @@ def trusted_request(handler, *, command: bool = False) -> bool:
|
||||
if len(hosts) != 1 or (host := _authority(hosts[0])) is None:
|
||||
return False
|
||||
is_public = bool(PUBLIC_HOST and host[0] == PUBLIC_HOST)
|
||||
if is_public and (handler.headers.get("X-Forwarded-Proto", "").lower() != "https"
|
||||
if is_public and (handler.headers.get("X-Forwarded-Proto", "").lower() != PUBLIC_SCHEME
|
||||
or host[1] not in (80, 443, PUBLIC_PORT)):
|
||||
return False
|
||||
origins = handler.headers.get_all("Origin", [])
|
||||
@@ -42,11 +43,10 @@ def trusted_request(handler, *, command: bool = False) -> bool:
|
||||
if origins:
|
||||
try:
|
||||
origin = urlsplit(origins[0])
|
||||
expected_scheme = "https" if is_public else "http"
|
||||
expected_scheme = PUBLIC_SCHEME if is_public else "http"
|
||||
origin_authority = _authority(origin.netloc)
|
||||
if (origin.scheme != expected_scheme or origin.path or origin.query or origin.fragment
|
||||
or origin_authority is None or origin_authority[0] != host[0]
|
||||
or (is_public and origin_authority[1] not in (80, 443, PUBLIC_PORT))
|
||||
or (not is_public and origin_authority != host)):
|
||||
return False
|
||||
except ValueError:
|
||||
|
||||
Reference in new issue
Block a user