From ff40e8ca84449eb0ff8df30ef310a1c7d5d3a1ab Mon Sep 17 00:00:00 2001 From: victorcavero14 Date: Sat, 7 Nov 2026 00:43:30 +0100 Subject: [PATCH] fix(security): hosted sites on pleng_web no longer count as internal Every hosted site joins pleng_web, and platform-api treated any private IP without X-Forwarded-For as internal: a site with an SSRF or a compromised container could read /internal/key and use the admin API (compose deploys with the Docker socket). Requests from the pleng_web subnets now need the API key like any external caller; the agent and Telegram bot on the internal network keep the no-auth path. If the subnet can't be read it fails closed. Co-Authored-By: Claude Opus 5.5 --- platform-api/app.py | 33 ++++++++++++++++- platform-api/tests/test_internal_boundary.py | 39 ++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 platform-api/tests/test_internal_boundary.py diff --git a/platform-api/app.py b/platform-api/app.py index 18a9e45..b20b5ad 100644 --- a/platform-api/app.py +++ b/platform-api/app.py @@ -41,13 +41,42 @@ ] +_web_nets: list = [] + + +def _hosted_site_nets() -> list: + """Subnets of the pleng_web network. Every hosted site joins it, so a request + from there is a site container (internet-facing code), never a platform + service: it must not get the internal no-auth path or the API key.""" + global _web_nets + if not _web_nets: + try: + r = subprocess.run( + ["docker", "network", "inspect", deployer.NETWORK, + "--format", "{{range .IPAM.Config}}{{.Subnet}} {{end}}"], + capture_output=True, text=True, timeout=5) + _web_nets = [ipaddress.ip_network(s) for s in r.stdout.split() if s] + except Exception as e: + logger.warning(f"Could not read {deployer.NETWORK} subnets: {e}") + return _web_nets + + def _is_internal(ip: str) -> bool: - """Check if request comes from Docker internal network.""" + """Check if request comes from a platform service on the Docker internal + network (agent, telegram bot), not from a hosted site on pleng_web.""" try: addr = ipaddress.ip_address(ip) - return any(addr in net for net in INTERNAL_NETS) except ValueError: return False + if addr.is_loopback: + return True + if not any(addr in net for net in INTERNAL_NETS): + return False + web = _hosted_site_nets() + if not web: + # Unknown site subnet: fail closed, the caller must use the API key. + return False + return not any(addr in net for net in web) _dashboard_password = "" diff --git a/platform-api/tests/test_internal_boundary.py b/platform-api/tests/test_internal_boundary.py new file mode 100644 index 0000000..fbd837d --- /dev/null +++ b/platform-api/tests/test_internal_boundary.py @@ -0,0 +1,39 @@ +"""A hosted site container sits on pleng_web; it must never count as internal.""" +import ipaddress +import os +import tempfile + +os.environ.setdefault("DATABASE_PATH", tempfile.mktemp(suffix=".db")) +os.environ.setdefault("PROJECTS_DIR", tempfile.mkdtemp()) + +import app as app_module + + +def _web(monkeypatch, nets): + monkeypatch.setattr(app_module, "_hosted_site_nets", + lambda: [ipaddress.ip_network(n) for n in nets]) + + +def test_site_on_pleng_web_is_not_internal(monkeypatch): + _web(monkeypatch, ["172.18.0.0/16"]) + assert app_module._is_internal("172.18.0.7") is False + + +def test_platform_service_on_internal_network_is_internal(monkeypatch): + _web(monkeypatch, ["172.18.0.0/16"]) + assert app_module._is_internal("172.19.0.3") is True + + +def test_loopback_is_internal(monkeypatch): + _web(monkeypatch, []) + assert app_module._is_internal("127.0.0.1") is True + + +def test_unknown_site_subnet_fails_closed(monkeypatch): + _web(monkeypatch, []) + assert app_module._is_internal("172.19.0.3") is False + + +def test_public_address_is_not_internal(monkeypatch): + _web(monkeypatch, ["172.18.0.0/16"]) + assert app_module._is_internal("8.8.8.8") is False