diff --git a/api/app/config.py b/api/app/config.py index e718b6e..1d7d9a1 100644 --- a/api/app/config.py +++ b/api/app/config.py @@ -42,6 +42,44 @@ MONERO_WALLET_RPC_USER = os.getenv("MONERO_WALLET_RPC_USER", "") MONERO_WALLET_RPC_PASSWORD = os.getenv("MONERO_WALLET_RPC_PASSWORD", "") MONERO_WALLET_RPC_WALLET_PASSWORD = os.getenv("MONERO_WALLET_RPC_WALLET_PASSWORD", "") MONERO_DAEMON_URL = os.getenv("MONERO_DAEMON_URL") + + +def check_daemon_url(url: str | None) -> None: + """Refuse a plain-http daemon on a public host. + + Payment detection trusts the daemon. Over plain http to a host on the + internet, the operator of that host or anyone on the path can fake + transfers or confirmations. http is accepted for loopback, private and + link-local addresses and for single-label or local-only names (Docker + service names such as `wownerod`); anything else must use https. + """ + if not url: + return + from ipaddress import ip_address + from urllib.parse import urlsplit + + parts = urlsplit(url if "://" in url else f"http://{url}") + if parts.scheme == "https": + return + host = (parts.hostname or "").lower().rstrip(".") + try: + address = ip_address(host) + except ValueError: + local = ( + "." not in host + or host == "localhost" + or host.endswith((".local", ".lan", ".internal", ".home.arpa")) + ) + else: + local = address.is_private or address.is_loopback or address.is_link_local + if not local: + raise RuntimeError( + "MONERO_DAEMON_URL uses plain http to a public host; use your own daemon on a " + "private network or an https endpoint" + ) + + +check_daemon_url(MONERO_DAEMON_URL) MONERO_WALLET_RPC_WALLET_DIR = os.getenv("MONERO_WALLET_RPC_WALLET_DIR", "") FOUNDER_PAYMENT_ADDRESS = os.getenv("FOUNDER_PAYMENT_ADDRESS", "") FOUNDER_VIEW_KEY = os.getenv("FOUNDER_VIEW_KEY", "") diff --git a/api/tests/test_daemon_url.py b/api/tests/test_daemon_url.py new file mode 100644 index 0000000..e0e85f7 --- /dev/null +++ b/api/tests/test_daemon_url.py @@ -0,0 +1,48 @@ +import unittest + +from app.config import check_daemon_url + + +class DaemonUrlTests(unittest.TestCase): + def test_a_plain_http_public_daemon_is_refused(self): + for url in [ + "http://node.wownero.example:34568", + "http://node.example.org:34568", + "http://93.184.216.34:34568", + "node.example.org:34568", + ]: + with self.subTest(url=url), self.assertRaises(RuntimeError): + check_daemon_url(url) + + def test_own_daemons_are_accepted(self): + for url in [ + "http://wownerod:34568", + "http://127.0.0.1:34568", + "http://192.168.1.20:34568", + "http://10.0.0.5:34568", + "http://host.docker.internal:34568", + "https://node.example.org:34569", + None, + ]: + with self.subTest(url=url): + check_daemon_url(url) + + +class ShippedDefaultTests(unittest.TestCase): + def test_no_shipped_file_defaults_to_a_plain_http_public_daemon(self): + import pathlib + import re + + root = pathlib.Path(__file__).resolve().parents[2] + pattern = re.compile(r"MONERO_DAEMON_URL(?::-|=)(http://[^}\s\"]+)") + for name in ["docker-compose.yml", ".env.example", "api/.env.example"]: + path = root / name + if not path.exists(): + continue + for url in pattern.findall(path.read_text()): + with self.subTest(file=name, url=url): + check_daemon_url(url) + + +if __name__ == "__main__": + unittest.main()