fix: hardening
This commit is contained in:
168 files changed
+9166
-1644
No files matched your search
+179
-14
@@ -1,7 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import base64
|
||||
import ipaddress
|
||||
import re
|
||||
import socket
|
||||
import sys
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
@@ -56,6 +58,8 @@ def _xray_tls(tls: dict | None) -> tuple[str, dict]:
|
||||
"XRay no longer supports tlsSettings.allowInsecure; "
|
||||
"remove insecure=1 or use a supported certificate verification option"
|
||||
)
|
||||
if tls.get("certificate_sha256"):
|
||||
settings["pinnedPeerCertSha256"] = tls["certificate_sha256"]
|
||||
if tls.get("ech_config_list"):
|
||||
settings["echConfigList"] = tls["ech_config_list"]
|
||||
utls = tls.get("utls", {})
|
||||
@@ -213,28 +217,109 @@ def build_outbound(
|
||||
if backend not in {"sing-box", "xray"}:
|
||||
raise ValueError(f"unsupported backend '{backend}'")
|
||||
|
||||
outbound = _parse_url(url, tag, backend)
|
||||
return _finish_outbound(_parse_url(url, tag, backend), routing_mark, backend)
|
||||
|
||||
|
||||
def _finish_outbound(outbound: dict, routing_mark: int | None, backend: str) -> dict:
|
||||
if backend == "xray":
|
||||
return render_xray_outbound(outbound, routing_mark)
|
||||
if routing_mark is not None:
|
||||
if backend == "xray":
|
||||
return render_xray_outbound(outbound, routing_mark)
|
||||
outbound["routing_mark"] = routing_mark
|
||||
elif backend == "xray":
|
||||
return render_xray_outbound(outbound)
|
||||
return outbound
|
||||
|
||||
|
||||
class RefusedServer(ValueError):
|
||||
"""A subscription entry whose server is this host or its link."""
|
||||
|
||||
|
||||
# Opt-in per subscription (allowPrivateServers): a provider's servers are not on the LAN.
|
||||
_PRIVATE_NETWORKS = tuple(
|
||||
ipaddress.ip_network(net)
|
||||
for net in ("10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "100.64.0.0/10", "fc00::/7")
|
||||
)
|
||||
|
||||
|
||||
def _server_address(host: str) -> "ipaddress.IPv4Address | ipaddress.IPv6Address | None":
|
||||
try:
|
||||
address = ipaddress.ip_address(host)
|
||||
except ValueError:
|
||||
# inet_aton's forms (2130706433, 0x7f.1, 127.1), which a resolver may still take.
|
||||
if not re.fullmatch(r"[0-9A-Fa-fXx.]+", host):
|
||||
return None
|
||||
try:
|
||||
address = ipaddress.IPv4Address(socket.inet_aton(host))
|
||||
except OSError:
|
||||
return None
|
||||
if isinstance(address, ipaddress.IPv6Address) and address.ipv4_mapped is not None:
|
||||
return address.ipv4_mapped
|
||||
return address
|
||||
|
||||
|
||||
def _check_address(host, address, allow_private: bool) -> None:
|
||||
if address.is_loopback or address.is_unspecified or address in ipaddress.ip_network("0.0.0.0/8"):
|
||||
raise RefusedServer(f"server {host} is this host")
|
||||
if address.is_link_local or address.is_multicast:
|
||||
raise RefusedServer(f"server {host} is a link-local or multicast address")
|
||||
if not allow_private and any(address in net for net in _PRIVATE_NETWORKS):
|
||||
raise RefusedServer(f"server {host} is a private address (see allowPrivateServers)")
|
||||
|
||||
|
||||
def check_server(host, allow_private: bool = False) -> None:
|
||||
"""Raises RefusedServer for a literal address (or localhost) on this host, its link, or,
|
||||
unless allow_private, a private network: urltest would pick a loopback entry as the exit."""
|
||||
if not isinstance(host, str):
|
||||
return
|
||||
name = host.strip().rstrip(".").lower()
|
||||
# The backends dial "[127.0.0.1]" as 127.0.0.1: judged unbracketed, or it is no
|
||||
# address and no name either, and passes unchecked.
|
||||
if name.startswith("[") and name.endswith("]"):
|
||||
name = name[1:-1].rstrip(".")
|
||||
if name == "localhost" or name.endswith(".localhost"):
|
||||
raise RefusedServer(f"server {host} is this host")
|
||||
address = _server_address(name)
|
||||
if address is not None:
|
||||
_check_address(host, address, allow_private)
|
||||
|
||||
|
||||
# A subscription is a list of share links; the biggest real ones are tens of KiB.
|
||||
# Read runs as root at service start, so a server that never stops sending must not
|
||||
# take the whole proxy down with it.
|
||||
MAX_SUBSCRIPTION_BYTES = 8 * 1024 * 1024
|
||||
|
||||
|
||||
class FetchError(ValueError):
|
||||
"""A fetch refused or cut short here: its message never quotes the URL, which is a secret."""
|
||||
|
||||
|
||||
def _nearness(host) -> int:
|
||||
"""How close to this host a URL's host is: 2 for this host or its link, 1 for a private
|
||||
network, 0 for anywhere else or a name other than localhost."""
|
||||
if not isinstance(host, str):
|
||||
return 0
|
||||
name = host.strip().rstrip(".").lower()
|
||||
if name == "localhost" or name.endswith(".localhost"):
|
||||
return 2
|
||||
address = _server_address(name)
|
||||
if address is None:
|
||||
return 0
|
||||
if address.is_loopback or address.is_unspecified or address.is_link_local or address in ipaddress.ip_network("0.0.0.0/8"):
|
||||
return 2
|
||||
return 1 if any(address in net for net in _PRIVATE_NETWORKS) else 0
|
||||
|
||||
|
||||
class _HttpOnlyRedirects(urllib.request.HTTPRedirectHandler):
|
||||
"""Redirects stay on http and https: urllib's own also follows one to ftp:."""
|
||||
"""Redirects stay on http and https (urllib's own also follows ftp:), never downgrade
|
||||
https, and never point nearer to this host (loopback, LAN, metadata) than their source."""
|
||||
|
||||
def redirect_request(self, req, fp, code, msg, headers, newurl):
|
||||
if urllib.parse.urlsplit(newurl).scheme not in ("http", "https"):
|
||||
raise ValueError("subscription redirected off http:// and https://")
|
||||
target = urllib.parse.urlsplit(newurl)
|
||||
scheme = target.scheme
|
||||
if scheme not in ("http", "https"):
|
||||
raise FetchError("subscription redirected off http:// and https://")
|
||||
if scheme == "http" and urllib.parse.urlsplit(req.full_url).scheme == "https":
|
||||
raise FetchError("subscription redirected from https:// to http://")
|
||||
if _nearness(target.hostname) > _nearness(urllib.parse.urlsplit(req.full_url).hostname):
|
||||
raise FetchError("subscription redirected to this host or a private network")
|
||||
return super().redirect_request(req, fp, code, msg, headers, newurl)
|
||||
|
||||
|
||||
@@ -243,7 +328,7 @@ def fetch_raw(url: str) -> bytes:
|
||||
# reaches it from a group-writable spool, so only the two a subscription is
|
||||
# ever served over, redirects included.
|
||||
if urllib.parse.urlsplit(url).scheme not in ("http", "https"):
|
||||
raise ValueError("subscription URL must be http:// or https://")
|
||||
raise FetchError("subscription URL must be http:// or https://")
|
||||
request = urllib.request.Request(
|
||||
url,
|
||||
headers={"User-Agent": "v2rayN/6.0"},
|
||||
@@ -252,7 +337,7 @@ def fetch_raw(url: str) -> bytes:
|
||||
with opener.open(request, timeout=30) as response:
|
||||
data = response.read(MAX_SUBSCRIPTION_BYTES + 1)
|
||||
if len(data) > MAX_SUBSCRIPTION_BYTES:
|
||||
raise ValueError(f"subscription is larger than {MAX_SUBSCRIPTION_BYTES} bytes")
|
||||
raise FetchError(f"subscription is larger than {MAX_SUBSCRIPTION_BYTES} bytes")
|
||||
return data
|
||||
|
||||
|
||||
@@ -308,6 +393,48 @@ def _subscription_tag(line: str, tag_prefix: str, index: int, seen_tags: set[str
|
||||
return _unique_tag(make_tag(tag_prefix, _remark_from_line(line), index), seen_tags)
|
||||
|
||||
|
||||
# Real subscriptions hold hundreds; 8 MiB of short lines would be minutes of start script
|
||||
# and gigabytes of backend config.
|
||||
MAX_SUBSCRIPTION_ENTRIES = 5000
|
||||
|
||||
|
||||
def _ech_dns_server(value) -> str | None:
|
||||
"""The resolver XRay asks for the ECH config, when echConfigList names one instead of
|
||||
holding the config: "[name+]udp://1.1.1.1:53", "https://dns.example/dns-query", "h2c://…"."""
|
||||
if not isinstance(value, str) or "://" not in value:
|
||||
return None
|
||||
url = value.split("+", 1)[1] if "+" in value.split("://", 1)[0] else value
|
||||
try:
|
||||
host = urllib.parse.urlsplit(url).hostname
|
||||
except ValueError:
|
||||
host = None
|
||||
# Unparsable: the whole value, which check_server passes unless it is an address itself.
|
||||
return host or value
|
||||
|
||||
|
||||
def _dialed_servers(outbound: dict) -> list:
|
||||
"""Every address the outbound dials: its server, any "address" (any case) in an xhttp
|
||||
extra's downloadSettings, and the resolver an ECH config is fetched from."""
|
||||
servers = [outbound.get("server")]
|
||||
tls = outbound.get("tls")
|
||||
if isinstance(tls, dict) and (ech := _ech_dns_server(tls.get("ech_config_list"))):
|
||||
servers.append(ech)
|
||||
transport = outbound.get("transport")
|
||||
stack = [transport.get("extra")] if isinstance(transport, dict) else []
|
||||
while stack:
|
||||
item = stack.pop()
|
||||
if isinstance(item, dict):
|
||||
for key, value in item.items():
|
||||
if str(key).lower() == "address":
|
||||
servers.append(value if isinstance(value, str) else str(value))
|
||||
elif str(key).lower() == "echconfiglist" and (ech := _ech_dns_server(value)):
|
||||
servers.append(ech)
|
||||
stack.append(value)
|
||||
elif isinstance(item, list):
|
||||
stack.extend(item)
|
||||
return servers
|
||||
|
||||
|
||||
def _walk_subscription(
|
||||
lines: list[str],
|
||||
tag_prefix: str,
|
||||
@@ -315,10 +442,13 @@ def _walk_subscription(
|
||||
backends: list[str],
|
||||
links: dict[str, str] | None,
|
||||
keep,
|
||||
allow_private: bool = False,
|
||||
allow_insecure: bool = False,
|
||||
) -> None:
|
||||
"""Parses every supported line, trying each backend in turn, and hands the result to keep.
|
||||
|
||||
A line no backend accepts is reported with every backend's complaint and skipped.
|
||||
A line no backend accepts is reported with every backend's complaint and skipped, as is
|
||||
one whose server check_server refuses.
|
||||
"""
|
||||
seen_tags: set[str] = set()
|
||||
|
||||
@@ -326,12 +456,29 @@ def _walk_subscription(
|
||||
scheme = detect_scheme(line)
|
||||
if scheme not in PARSERS:
|
||||
continue
|
||||
if len(seen_tags) >= MAX_SUBSCRIPTION_ENTRIES:
|
||||
print(
|
||||
f"warning: subscription has more than {MAX_SUBSCRIPTION_ENTRIES} entries: the rest are left out",
|
||||
file=sys.stderr,
|
||||
)
|
||||
break
|
||||
|
||||
tag = _subscription_tag(line, tag_prefix, index, seen_tags)
|
||||
errors = []
|
||||
for backend in backends:
|
||||
try:
|
||||
outbound = build_outbound(line, tag, routing_mark, backend)
|
||||
outbound = _parse_url(line, tag, backend)
|
||||
for server in _dialed_servers(outbound):
|
||||
check_server(server, allow_private)
|
||||
# Whoever serves the list could otherwise have the tunnel trust any certificate:
|
||||
# anyone on the way stands in for the server then. Per backend: XRay may pin it.
|
||||
if not allow_insecure and isinstance(outbound.get("tls"), dict) and outbound["tls"].get("insecure"):
|
||||
raise ValueError("it turns certificate checks off (insecure=1; see allowInsecure)")
|
||||
outbound = _finish_outbound(outbound, routing_mark, backend)
|
||||
except RefusedServer as exc:
|
||||
# The same server whichever backend reads the line.
|
||||
print(f"warning: skipping entry {index} ({scheme}): {exc}", file=sys.stderr)
|
||||
break
|
||||
except Exception as exc:
|
||||
errors.append(f"{backend}: {exc}" if len(backends) > 1 else str(exc))
|
||||
continue
|
||||
@@ -354,17 +501,33 @@ def parse_subscription(
|
||||
routing_mark: int | None,
|
||||
backend: str = "sing-box",
|
||||
links: dict[str, str] | None = None,
|
||||
allow_private: bool = False,
|
||||
allow_insecure: bool = False,
|
||||
) -> list[dict]:
|
||||
"""links, when given, collects tag -> the line it came from, to share it back."""
|
||||
if backend not in {"sing-box", "xray"}:
|
||||
raise ValueError(f"unsupported backend '{backend}'")
|
||||
outbounds: list[dict] = []
|
||||
_walk_subscription(
|
||||
lines, tag_prefix, routing_mark, [backend], links, lambda _, ob: outbounds.append(ob)
|
||||
lines,
|
||||
tag_prefix,
|
||||
routing_mark,
|
||||
[backend],
|
||||
links,
|
||||
lambda _, ob: outbounds.append(ob),
|
||||
allow_private,
|
||||
allow_insecure,
|
||||
)
|
||||
return outbounds
|
||||
|
||||
|
||||
def parse_hybrid_subscription(
|
||||
lines: list[str], tag_prefix: str, routing_mark: int | None = None, links: dict[str, str] | None = None
|
||||
lines: list[str],
|
||||
tag_prefix: str,
|
||||
routing_mark: int | None = None,
|
||||
links: dict[str, str] | None = None,
|
||||
allow_private: bool = False,
|
||||
allow_insecure: bool = False,
|
||||
) -> dict[str, list[dict]]:
|
||||
"""Each line goes to whichever backend can dial it, sing-box first."""
|
||||
outbounds: dict[str, list[dict]] = {"singBox": [], "xray": []}
|
||||
@@ -375,5 +538,7 @@ def parse_hybrid_subscription(
|
||||
["sing-box", "xray"],
|
||||
links,
|
||||
lambda backend, ob: outbounds["singBox" if backend == "sing-box" else "xray"].append(ob),
|
||||
allow_private,
|
||||
allow_insecure,
|
||||
)
|
||||
return outbounds
|
||||
Reference in new issue
Block a user