From 496ccbfcbfdf5b6935a8c2917aaae0236df04eb3 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Sun, 19 Jul 2026 15:05:51 -0400 Subject: [PATCH] cleaup --- backend/config/__init__.py | 5 ----- backend/utils/ssrf.py | 24 +++++++++--------------- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/backend/config/__init__.py b/backend/config/__init__.py index 847b10415..0d0d38275 100644 --- a/backend/config/__init__.py +++ b/backend/config/__init__.py @@ -113,11 +113,6 @@ PLAYMATCH_API_URL: Final[str] = _get_env( "PLAYMATCH_API_URL", "https://playmatch.retrorealm.dev/api/v2" ).rstrip("/") -# Origins allowed to resolve to private/internal addresses despite SSRF -# protection, so admin-configured self-hosted providers on a LAN or Docker -# network stay reachable. Only user-configured provider URLs belong here. -SSRF_ALLOWED_INTERNAL_ORIGINS: Final[tuple[str, ...]] = (PLAYMATCH_API_URL,) - # HASHEOUS HASHEOUS_API_ENABLED: Final[bool] = safe_str_to_bool(_get_env("HASHEOUS_API_ENABLED")) diff --git a/backend/utils/ssrf.py b/backend/utils/ssrf.py index fe362d31b..507a83b44 100644 --- a/backend/utils/ssrf.py +++ b/backend/utils/ssrf.py @@ -31,6 +31,7 @@ import typing from urllib.parse import urlparse import httpcore +import pydash from httpcore._backends.auto import AutoBackend from httpcore._backends.base import ( SOCKET_OPTION, @@ -41,7 +42,7 @@ from httpcore._backends.base import ( ) from httpcore._backends.sync import SyncBackend -from config import SSRF_ALLOWED_INTERNAL_ORIGINS +from config import PLAYMATCH_API_URL from logger.logger import log from utils.validation import ValidationError @@ -102,29 +103,22 @@ def _parse_origin(origin: str) -> tuple[str, int] | None: parts = urlparse(origin) if parts.scheme not in ("http", "https") or not parts.hostname: return None + try: port = parts.port except ValueError: return None + if port is None: port = 443 if parts.scheme == "https" else 80 + return parts.hostname.lower(), port -def build_internal_origin_allowlist( - origins: typing.Iterable[str], -) -> frozenset[tuple[str, int]]: - """Normalize admin-configured trusted origins into (host, port) pairs.""" - return frozenset( - parsed for parsed in (_parse_origin(o) for o in origins if o) if parsed - ) - - -# Origins the admin explicitly configured (e.g. a self-hosted Playmatch on a -# LAN or Docker network) are trusted to reach private addresses. Matched by -# exact host and port so the exception stays as narrow as possible. -INTERNAL_ORIGIN_ALLOWLIST: frozenset[tuple[str, int]] = build_internal_origin_allowlist( - SSRF_ALLOWED_INTERNAL_ORIGINS +# Origins the admin explicitly configured are trusted to reach private addresses. +# Matched by exact host and port so the exception stays as narrow as possible. +INTERNAL_ORIGIN_ALLOWLIST: frozenset[tuple[str, int]] = frozenset( + pydash.compact([_parse_origin(PLAYMATCH_API_URL)]) )