Skip to content

Self-hosted monitoring for homelabs: host and container metrics and uptime checks from all your machines in one dashboard, with an optional AI agent that diagnoses incidents and suggests fixes.

fix(notify): connect to the checked address so DNS rebinding can't reach inside

karloks2005c6bfe0756f46
diff --git a/src/backend/notify.py b/src/backend/notify.py
index 32229ad..6463af6 100644
--- a/src/backend/notify.py
+++ b/src/backend/notify.py
@@ -3,11 +3,13 @@
The address of a channel is something a signed-in user typed, and the backend then connects to it, so it is treated
as hostile: only http and https, no credentials in the address, no redirects, a short timeout, a small answer that
is never shown to the user (only its status), and every address the name resolves to must be one a channel may
-reach. Loopback, link-local (which includes cloud metadata) and other special ranges are always refused; private
+reach. The connection then goes to the address that was checked, not to the name, so a DNS answer that changes
+between the check and the connection (DNS rebinding) can't lead it anywhere else. Loopback, link-local (which includes cloud metadata) and other special ranges are always refused; private
LAN ranges are allowed only for admins, because a homelab's ntfy usually lives there.
"""
from __future__ import annotations
+import http.client
import ipaddress
import json
import os
@@ -51,6 +53,12 @@ def resolve(host: str, port: int) -> list[str]:
def check_url(url: str, allow_private: bool) -> tuple[str, str]:
"""The cleaned address and a short form safe to display, or ChannelError."""
+ url, display, _address = _checked(url, allow_private)
+ return url, display
+
+
+def _checked(url: str, allow_private: bool) -> tuple[str, str, str]:
+ """check_url, plus the checked IP address that a send must connect to."""
url = (url or "").strip()
if not url or len(url) > MAX_URL or not url.isprintable():
raise ChannelError("Enter the channel's address.")
@@ -67,7 +75,7 @@ def check_url(url: str, allow_private: bool) -> tuple[str, str]:
if not addresses or not all(_allowed(address, allow_private) for address in addresses):
raise ChannelError("That address points at this machine or a private network." + ("" if allow_private else " Only an admin can add a channel on the local network."))
tail = parts.path.rstrip("/").rsplit("/", 1)[-1]
- return url, f"{parts.hostname}{'/…' + tail[-6:] if tail else ''}"[:120]
+ return url, f"{parts.hostname}{'/…' + tail[-6:] if tail else ''}"[:120], addresses[0]
class _NoRedirect(urllib.request.HTTPRedirectHandler):
@@ -75,13 +83,40 @@ class _NoRedirect(urllib.request.HTTPRedirectHandler):
return None
-_opener = urllib.request.build_opener(_NoRedirect)
+def _pinned(connection_class, address: str):
+ """A connection class that connects to ``address`` whatever the URL's host name resolves to now. The name is
+ still what goes in the Host header and, for https, in SNI and the certificate check."""
+ class Pinned(connection_class):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self._create_connection = lambda target, *rest, **options: socket.create_connection((address, target[1]), *rest, **options)
+ return Pinned
+
+
+class _PinnedHTTP(urllib.request.HTTPHandler):
+ def __init__(self, address: str):
+ super().__init__()
+ self.connection = _pinned(http.client.HTTPConnection, address)
+
+ def http_open(self, req):
+ return self.do_open(self.connection, req)
+
+
+class _PinnedHTTPS(urllib.request.HTTPSHandler):
+ def __init__(self, address: str):
+ super().__init__()
+ self.connection = _pinned(http.client.HTTPSConnection, address)
+
+ def https_open(self, req):
+ return self.do_open(self.connection, req, context=self._context)
-def _post(url: str, data: bytes, headers: dict[str, str]) -> tuple[bool, str]:
+def _post(url: str, data: bytes, headers: dict[str, str], address: str) -> tuple[bool, str]:
+ """POST to ``url``, connecting to the already checked ``address``. No proxy: one would resolve the name itself."""
request = urllib.request.Request(url, data=data, headers={"User-Agent": "Infradeck", **headers}, method="POST")
+ opener = urllib.request.build_opener(urllib.request.ProxyHandler({}), _NoRedirect, _PinnedHTTP(address), _PinnedHTTPS(address))
try:
- with _opener.open(request, timeout=TIMEOUT_SECONDS) as response:
+ with opener.open(request, timeout=TIMEOUT_SECONDS) as response:
response.read(1024)
return True, f"{response.status}"
except urllib.error.HTTPError as error:
@@ -118,16 +153,16 @@ def send(kind: str, target: str, title: str, body: str, severity: str = "INFO",
if kind == "email":
return _send_email(target, title, body)
try:
- url, _display = check_url(target, allow_private)
+ url, _display, address = _checked(target, allow_private)
except ChannelError:
return False, "address refused"
if kind == "ntfy":
# Header values must be Latin-1; anything else in a title goes into the body instead.
safe = title.encode("latin-1", "ignore").decode("latin-1")[:200]
- return _post(url, body.encode(), {"Title": safe, "Priority": "5" if severity == "CRITICAL" else "3", "Tags": "rotating_light" if severity == "CRITICAL" else "warning" if severity == "WARNING" else "white_check_mark"})
+ return _post(url, body.encode(), {"Title": safe, "Priority": "5" if severity == "CRITICAL" else "3", "Tags": "rotating_light" if severity == "CRITICAL" else "warning" if severity == "WARNING" else "white_check_mark"}, address)
if kind == "discord":
- return _post(url, json.dumps({"content": f"**{title}**\n{body}"[:1900], "allowed_mentions": {"parse": []}}).encode(), {"Content-Type": "application/json"})
- return _post(url, json.dumps({"title": title, "body": body, "severity": severity}).encode(), {"Content-Type": "application/json"})
+ return _post(url, json.dumps({"content": f"**{title}**\n{body}"[:1900], "allowed_mentions": {"parse": []}}).encode(), {"Content-Type": "application/json"}, address)
+ return _post(url, json.dumps({"title": title, "body": body, "severity": severity}).encode(), {"Content-Type": "application/json"}, address)
def in_quiet_hours(quiet: dict | None, timezone_name: str | None, now: datetime) -> bool:
diff --git a/tests/accounts/test_new_pages.py b/tests/accounts/test_new_pages.py
index 0dba1f9..e902f4e 100644
--- a/tests/accounts/test_new_pages.py
+++ b/tests/accounts/test_new_pages.py
@@ -200,7 +200,7 @@ def test_profile_and_preferences_keep_only_what_is_valid(app):
def outbox(monkeypatch):
sent = []
monkeypatch.setattr(notify, "resolve", lambda host, port: ["8.8.8.8"] if host == "ntfy.example" else ["10.0.0.5"] if host == "ntfy.lan" else ["127.0.0.1"])
- monkeypatch.setattr(notify, "_post", lambda url, data, headers: (sent.append((url, data, headers)) or (True, "200")))
+ monkeypatch.setattr(notify, "_post", lambda url, data, headers, address: (sent.append((url, data, headers)) or (True, "200")))
return sent
diff --git a/tests/accounts/test_notify_pinning.py b/tests/accounts/test_notify_pinning.py
new file mode 100644
index 0000000..3a7fafa
--- /dev/null
+++ b/tests/accounts/test_notify_pinning.py
@@ -0,0 +1,69 @@
+"""A notification connects to the address that was checked, not to whatever the channel's host name resolves to a
+moment later, so DNS rebinding can't turn a public-looking channel into a request to this machine or the LAN."""
+import http.server
+import socket
+import threading
+
+import pytest
+
+from src.backend import notify
+
+
+class _Hook(http.server.BaseHTTPRequestHandler):
+ seen = []
+
+ def do_POST(self): # noqa: N802
+ self.seen.append(self.headers["Host"])
+ self.rfile.read(int(self.headers.get("Content-Length") or 0))
+ self.send_response(204)
+ self.end_headers()
+
+ def log_message(self, *args):
+ pass
+
+
+def hook():
+ server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), _Hook)
+ threading.Thread(target=server.serve_forever, daemon=True).start()
+ _Hook.seen = []
+ yield server.server_address[1]
+ server.shutdown()
+
+
+def test_the_post_goes_to_the_checked_address_without_resolving_the_name_again(monkeypatch, hook):
+ real = socket.getaddrinfo
+
+ def no_second_lookup(host, *args, **kwargs):
+ assert host != "rebind.example", "the host name was resolved again at connect time"
+ return real(host, *args, **kwargs)
+
+ monkeypatch.setattr(socket, "getaddrinfo", no_second_lookup)
+
+ worked, status = notify._post(f"http://rebind.example:{hook}/hook", b"{}", {"Content-Type": "application/json"}, address="127.0.0.1")
+
+ assert (worked, status) == (True, "204")
+ assert _Hook.seen == [f"rebind.example:{hook}"] # the name still goes in the Host header
+
+
+def test_a_name_that_changes_its_answer_is_resolved_once_and_the_checked_answer_is_used(monkeypatch):
+ answers = iter([["93.184.216.34"], ["127.0.0.1"]])
+ lookups, used = [], []
+ monkeypatch.setattr(notify, "resolve", lambda host, port: lookups.append(host) or next(answers))
+ monkeypatch.setattr(notify, "_post", lambda url, data, headers, address: used.append(address) or (True, "200"))
+
+ assert notify.send("webhook", "http://rebind.example/hook", "Disk full", "body") == (True, "200")
+
+ assert lookups == ["rebind.example"] and used == ["93.184.216.34"]
+
+
+def test_https_keeps_the_host_name_for_sni_and_the_certificate_check():
+ connection = notify._PinnedHTTPS("93.184.216.34").connection("ntfy.example", 443)
+
+ assert connection.host == "ntfy.example"
+
+
+def test_a_proxy_from_the_environment_is_not_used(monkeypatch, hook):
+ monkeypatch.setenv("http_proxy", "http://127.0.0.1:9") # nothing listens there; using it would fail
+
+ assert notify._post(f"http://rebind.example:{hook}/hook", b"{}", {}, address="127.0.0.1") == (True, "204")