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(availability): refuse URL credentials and hide check query strings from non-admins

karloks2005d9d38b8d18e1
diff --git a/src/backend/availability.py b/src/backend/availability.py
index 2a88f72..fe42096 100644
--- a/src/backend/availability.py
+++ b/src/backend/availability.py
@@ -23,7 +23,7 @@ import re
import statistics
from collections import defaultdict
from datetime import datetime, timedelta, timezone
-from urllib.parse import urlsplit
+from urllib.parse import urlsplit, urlunsplit
from .alerts import raise_alert, resolve_alerts
from .models import Check, CheckResult, CheckRollup, IncidentAlert, Server
@@ -138,6 +138,8 @@ def validate(raw: dict, server_ids: set[int]) -> dict:
parts = urlsplit(target)
if parts.scheme not in ("http", "https") or not parts.hostname or len(target) > 2048 or any(char.isspace() for char in target):
raise ValueError("The URL must start with http:// or https:// and name a host.")
+ if parts.username or parts.password:
+ raise ValueError("Put no user name or password in the URL; everyone who can sign in can read a check's address.")
codes = raw.get("accepted_codes") or ["200-299"]
if not isinstance(codes, list) or not 1 <= len(codes) <= 10 or not all(isinstance(code, str) and _CODES.fullmatch(code) for code in codes):
raise ValueError("Accepted status codes are like 200-299 or 301, at most 10.")
@@ -426,7 +428,7 @@ def evaluate(db, check: Check, now: datetime | None = None, alerting: bool = Tru
failing = [names.get(server, f"server {server}") for server, state in server_states(db, check, now).items() if state == "down"]
why = (latest.detail if latest and latest.detail else "No answer") + f" from {', '.join(failing) or 'its servers'}"
raise_alert(db, servers[0], rule_key(check), check.severity, f"{check.name} is down"[:255],
- f"{why}. Failed {check.retries + 1} checks in a row. {check.target}"[:1000])
+ f"{why}. Failed {check.retries + 1} checks in a row. {redacted_target(check.target)}"[:1000])
elif servers:
resolve_alerts(db, servers[0], [rule_key(check)], now)
return new
@@ -471,14 +473,28 @@ def _uptime(db, check: Check, since: datetime, now: datetime) -> float | None:
return round(100 * (total - down) / total, 2) if total else None
-def summary(db, check: Check, names: dict[int, str], now: datetime | None = None) -> dict:
+def redacted_target(target: str) -> str:
+ """A check's address with the secret-bearing parts removed, for anyone who can't edit it: credentials in the
+ URL and the query string (which can carry tokens) go; scheme, host, port and path stay. The collector still
+ gets the full target. Non-URL targets (host:port, a name) have no secret part and pass through unchanged."""
+ parts = urlsplit(target)
+ if parts.scheme not in ("http", "https") or not parts.hostname:
+ return target
+ host = f"[{parts.hostname}]" if ":" in parts.hostname else parts.hostname
+ if parts.port:
+ host = f"{host}:{parts.port}"
+ cleaned = urlunsplit((parts.scheme, host, parts.path, "", ""))
+ return f"{cleaned}?\u2026" if parts.query else cleaned
+
+
+def summary(db, check: Check, names: dict[int, str], now: datetime | None = None, redact: bool = False) -> dict:
now = now or datetime.now(timezone.utc)
recent = slots(check, _results(db, check, now - timedelta(seconds=check.interval_seconds * (STRIP + 2) + 3600)))[-STRIP:]
latest = db.query(CheckResult).filter(CheckResult.check_id == check.id).order_by(CheckResult.time.desc()).first()
cert = db.query(CheckResult.cert_days).filter(CheckResult.check_id == check.id, CheckResult.cert_days.isnot(None)).order_by(CheckResult.time.desc()).limit(1).scalar()
options = options_of(check)
return {
- "id": check.id, "name": check.name, "type": check.type, "target": check.target, "tags": [tag for tag in check.tags.split(",") if tag],
+ "id": check.id, "name": check.name, "type": check.type, "target": redacted_target(check.target) if redact else check.target, "tags": [tag for tag in check.tags.split(",") if tag],
"keyword": options.get("keyword"), "options": options, "rule": check.rule, "severity": check.severity, "paused": check.paused, "state": check.state,
"state_since": _utc(check.state_since).isoformat() if check.state_since else None, "interval_seconds": check.interval_seconds, "retries": check.retries,
"retry_interval_seconds": check.retry_interval_seconds, "timeout_seconds": check.timeout_seconds,
@@ -492,10 +508,10 @@ def summary(db, check: Check, names: dict[int, str], now: datetime | None = None
}
-def overview(db, now: datetime | None = None) -> dict:
+def overview(db, now: datetime | None = None, redact: bool = False) -> dict:
now = now or datetime.now(timezone.utc)
names = dict(db.query(Server.id, Server.hostname).all())
- checks = [summary(db, check, names, now) for check in db.query(Check).order_by(Check.name)]
+ checks = [summary(db, check, names, now, redact=redact) for check in db.query(Check).order_by(Check.name)]
month = [check["uptime_30d"] for check in checks if check["uptime_30d"] is not None and not check["paused"]]
return {
"checks": checks,
diff --git a/src/backend/main.py b/src/backend/main.py
index bd33ed1..317b288 100644
--- a/src/backend/main.py
+++ b/src/backend/main.py
@@ -1946,7 +1946,9 @@ def _check_or_404(db, check_id: int) -> Check:
@app.get("/availability")
def get_availability(session_id: Annotated[str | None, Cookie(alias=SESSION_COOKIE_NAME)] = None, db=Depends(get_db)):
user = _current_user(session_id, db)
- return {**availability.overview(db), "can_edit": _is_admin(db, user["id"])}
+ is_admin = _is_admin(db, user["id"])
+ # Non-admins can't edit checks, so they get targets with credentials and query strings (possible secrets) removed.
+ return {**availability.overview(db, redact=not is_admin), "can_edit": is_admin}
def _save_check(db, check: Check, payload: dict) -> dict:
diff --git a/src/collector/collector.py b/src/collector/collector.py
index ecf05b9..ec15205 100644
--- a/src/collector/collector.py
+++ b/src/collector/collector.py
@@ -1292,6 +1292,8 @@ def validate_check(raw: Any) -> dict[str, Any] | None:
parts = urllib.parse.urlsplit(target)
if parts.scheme not in ("http", "https") or not parts.hostname or len(target) > 2048 or any(char.isspace() or not char.isprintable() for char in target):
return None
+ if parts.username or parts.password:
+ return None
codes = raw.get("accepted_codes")
if not isinstance(codes, list) or not 1 <= len(codes) <= 10 or not all(isinstance(code, str) and _CHECK_CODES.fullmatch(code) for code in codes):
return None
diff --git a/tests/collectors/test_collector_checks.py b/tests/collectors/test_collector_checks.py
index 1f0a821..bdbbbd9 100644
--- a/tests/collectors/test_collector_checks.py
+++ b/tests/collectors/test_collector_checks.py
@@ -266,3 +266,9 @@ def test_run_check_dispatches_by_type(monkeypatch):
monkeypatch.setattr(collector, "run_tls", lambda c: {"ok": True, "response_ms": 1, "code": None, "detail": None, "cert_days": 40})
out = collector.run_check({"id": 7, "type": "tls", "host": "h", "port": 443, "timeout": 5, "warn_days": 14})
assert out["check_id"] == 7 and out["ok"] is True and out["cert_days"] == 40
+
+
+def test_validate_check_rejects_credentials_in_a_url():
+ base = {"id": 1, "interval": 60, "retry_interval": 20, "timeout": 10, "accepted_codes": ["200-299"]}
+ assert collector.validate_check({**base, "type": "http", "target": "https://user:pass@host/"}) is None
+ assert collector.validate_check({**base, "type": "http", "target": "https://host/?token=x"}) is not None
diff --git a/tests/monitoring/test_availability.py b/tests/monitoring/test_availability.py
index 5921692..2c011a9 100644
--- a/tests/monitoring/test_availability.py
+++ b/tests/monitoring/test_availability.py
@@ -494,3 +494,31 @@ def test_a_tls_check_sends_its_warn_days_to_the_collector(app):
spec = next(c for c in report(app, 1, []).json()["checks"] if c["type"] == "tls")
assert spec["target"] == "mail.example.com:993" and spec["warn_days"] == 21 and "upside_down" not in spec
+
+
+# --- a check's address may hold a secret; non-admins and incidents don't see it --------------------
+
+def test_credentials_in_a_check_url_are_refused(app):
+ assert app.post("/availability/checks", json={**NEW_CHECK, "target": "https://user:[email protected]/"}).status_code == 400
+
+
+def test_a_non_admin_sees_the_target_without_its_query_or_credentials(app):
+ create(app, name="Stock", target="https://stock.home.lan/api/?token=secret")
+
+ full = app.get("/availability").json()["checks"]
+ app.user.update(id=2, role="member")
+ redacted = app.get("/availability").json()["checks"]
+
+ assert next(c for c in full if c["name"] == "Stock")["target"] == "https://stock.home.lan/api/?token=secret"
+ shown = next(c for c in redacted if c["name"] == "Stock")["target"]
+ assert shown == "https://stock.home.lan/api/?…" and "secret" not in shown
+
+
+def test_the_incident_description_does_not_leak_the_query_string(app):
+ check = create(app, server_ids=[1], target="https://stock.home.lan/api/?token=secret")
+ age(app, check["id"])
+
+ report(app, 1, runs(check["id"], [False, False, False]))
+
+ alert = incident(app, check["id"])
+ assert alert is not None and "secret" not in alert.description and "stock.home.lan/api/" in alert.description