Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions platform-api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
39 changes: 39 additions & 0 deletions platform-api/tests/test_internal_boundary.py
Original file line number Diff line number Diff line change
@@ -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