linux
This commit is contained in:
1 parent
f6f1ae20c2
commit
efd4e1dcf4
43 files changed
+25641
No files matched your search
@@ -0,0 +1,52 @@
|
||||
"""Validate the original browser request before proxying or executing local commands."""
|
||||
from __future__ import annotations
|
||||
|
||||
import ipaddress
|
||||
import os
|
||||
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(".")
|
||||
|
||||
|
||||
def _authority(value: str):
|
||||
try:
|
||||
url = urlsplit("http://" + value)
|
||||
hostname = (url.hostname or "").lower().rstrip(".")
|
||||
allowed = hostname in LOCAL_HOSTS or (PUBLIC_HOST and hostname == PUBLIC_HOST)
|
||||
if (not allowed or url.username is not None or url.password is not None
|
||||
or url.path or url.query or url.fragment or any(c.isspace() for c in value)):
|
||||
return None
|
||||
return url.hostname, url.port or 80
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def trusted_request(handler, *, command: bool = False) -> bool:
|
||||
try:
|
||||
if not ipaddress.ip_address(handler.client_address[0]).is_loopback:
|
||||
return False
|
||||
except ValueError:
|
||||
return False
|
||||
hosts = handler.headers.get_all("Host", [])
|
||||
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"
|
||||
or host[1] not in (80, 443)):
|
||||
return False
|
||||
origins = handler.headers.get_all("Origin", [])
|
||||
if len(origins) > 1:
|
||||
return False
|
||||
if origins:
|
||||
try:
|
||||
origin = urlsplit(origins[0])
|
||||
expected_scheme = "https" if is_public else "http"
|
||||
if (origin.scheme != expected_scheme or origin.path or origin.query or origin.fragment
|
||||
or _authority(origin.netloc) != host):
|
||||
return False
|
||||
except ValueError:
|
||||
return False
|
||||
if handler.headers.get("Sec-Fetch-Site") == "cross-site":
|
||||
return False
|
||||
return not command or handler.headers.get("X-Sim") == "1"
|
||||
Reference in new issue
Block a user