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.
Python ai-agentsaiopsdashboarddockerfastapihomelabincident-managementllmmonitoringopenaireactroot-cause-analysisself-hosteduptime-monitoring
fix: backend cleanups from the pre-1.0.0 review (#226)
Karlo Kajba Šimanić2df41651db51
diff --git a/.claude/skills/security-audit/SKILL.md b/.claude/skills/security-audit/SKILL.mdindex e55f199..6966d80 100644--- a/.claude/skills/security-audit/SKILL.md+++ b/.claude/skills/security-audit/SKILL.md@@ -25,7 +25,7 @@ tek kad korisnik to izričito zatraži.| `collector` | `src/collector/**` (collector, docker_proxy, system_helper) || `infra` | `docker/**`, `*compose*.yml`, `example.env`, `.github/**`, `.gitignore`, `.dockerignore` || `frontend` | `src/frontend/**` |-| `assistant` | Incident agent: `src/agent/**` (providers, chat, prompts, health_checks, jobs, worker, usage), `SYSTEM_PROMPT.md`, konverzacije u backendu |+| `assistant` | Incident agent: `src/agent/**` (providers, tools, investigate, diagnosis, chat, masking, safety, jobs, worker, usage), `SYSTEM_PROMPT.md`, `DIAGNOSIS_PROMPT.md`, konverzacije u backendu || `<path>` | taj put (datoteka ili direktorij) plus ono što ga poziva || `diff` | samo neprihvaćene promjene (`git diff` + `git diff --staged`) pročitane u kontekstu cijelih datoteka |diff --git a/.claude/skills/security-audit/references/checklist-backend.md b/.claude/skills/security-audit/references/checklist-backend.mdindex d4fa8e4..46651a5 100644--- a/.claude/skills/security-audit/references/checklist-backend.md+++ b/.claude/skills/security-audit/references/checklist-backend.md@@ -106,10 +106,10 @@## 6. Izlazne veze (SSRF) i integracije-- [ ] Webhook/alarm URL-ovi (`ADMIN_ALERT_WEBHOOK_URL`, kanali u `alerts.py`/`rules.py`):- tko ih može postaviti (samo env ili i API)? Ako ih postavlja korisnik/admin preko API-ja:- shema samo http(s), blokiranje loopback/link-local/privatnih adresa i metadata IP-a,- provjera nakon DNS razrješenja, bez praćenja preusmjeravanja na unutarnje adrese, timeout.+- [ ] Kanali obavijesti (`notify.py`: ntfy, Discord, webhook, email) postavljaju se preko API-ja+ po korisniku: shema samo http(s), loopback/link-local/metadata uvijek odbijeni, privatne+ LAN adrese samo za admine, provjera nakon DNS razrješenja i spajanje na provjerenu adresu+ (DNS rebinding), bez praćenja preusmjeravanja, timeout. Provjeri i SMTP put.- [ ] Sadržaj odlazne poruke ne sadrži tajne (tokene, hashove) ni neocišćene tuđe stringove ukontekstu gdje se interpretiraju (Slack/markdown/HTML/email zaglavlja → injekcija).- [ ] Nema `requests`/`httpx` poziva prema URL-u iz zahtjeva bez gornjih zaštita.diff --git a/.claude/skills/security-audit/references/checklist-collector-agent.md b/.claude/skills/security-audit/references/checklist-collector-agent.mdindex 89089d7..be9b848 100644--- a/.claude/skills/security-audit/references/checklist-collector-agent.md+++ b/.claude/skills/security-audit/references/checklist-collector-agent.md@@ -1,7 +1,8 @@# Kontrolna lista: collector, docker proxy, system helper (`src/collector/**`), incident agent (`src/agent/**`)-Čitaj `collector.py`, `docker_proxy.py`, `system_helper.py`, `health_checks.py`, `service.py`,-`prompts.py`, `main.py`, `utils.py` i `SYSTEM_PROMPT.md` u cijelosti. Docker proxy i system+Čitaj `collector.py`, `docker_proxy.py`, `system_helper.py` te za agenta `src/agent/` (`tools/`,+`investigate.py`, `diagnosis.py`, `chat.py`, `masking.py`, `safety.py`, `providers/`, `worker.py`,+`usage.py`, oba prompta) u cijelosti. Docker proxy i systemhelper drže privilegije: najveća pažnja ide njima.## 1. Docker proxy (`docker_proxy.py`): drži root-ekvivalent@@ -67,19 +68,33 @@ helper drže privilegije: najveća pažnja ide njima.- [ ] Ograničenja količine prijavljenih podataka (kontejneri, portovi, događaji) na stranikolektora, ne samo backenda.-## 4. LLM asistent (`service.py`, `prompts.py`, `SYSTEM_PROMPT.md`, `health_checks.py`)+## 4. Incident agent (`src/agent/`)-Dokumentirano: nema putanje za shell, samo tri fiksne provjere (`server_health`,-`container_health`, `daily_checkup`), webhook samo na `ADMIN_ALERT_WEBHOOK_URL`.+Dokumentirano (`docs/assistant.md`, `docs/agent-investigation.md`, SECURITY.md): agent nema+shell ni pristup hostu. Ima zatvoren popis read-only alata (`tools/__init__.py`: `server_info`,+`host_metrics`, `list_containers`, `container_history`, `disk_usage`, `events`,+`compare_to_baseline`, `find_incidents`, `fleet_snapshot`, `check_results`, `network_state`)+koji čitaju samo bazu Infradecka. Petlja (`investigate.py`) ima budžet poziva, rok i mjesečni+limit troška. Preporučene naredbe su samo tekst koji `safety.py` provjerava; ništa se ne izvršava.-- [ ] **Alati su doista fiksni:** parametri alata validirani bijelom listom; nema alata s- proizvoljnim stringom koji završava u naredbi, putanji, URL-u ili SQL-u. Nema- dinamičkog dodavanja alata iz podataka.-- [ ] **Prompt injection:** podaci koji ulaze u kontekst modela (imena kontejnera, linije- loga, poruke grešaka, rezultati provjera, tekst korisnika) mogu sadržavati upute.- Što najgore može model napraviti ako ih posluša? (Pozvati dopuštene provjere, poslati- webhook, otkriti sistemski prompt/konverzaciju.) Webhook i alarmi ne smiju nositi- tajne, a model ne smije moći promijeniti odredište.+- [ ] **Alati su doista fiksni:** `run` odbija nepoznato ime, argumenti se provjeravaju prema+ shemi prije čitanja; nema argumenta koji završava u naredbi, putanji, URL-u ili SQL-u+ izvan ORM-a. Rezultat je ograničen (`bounds.py`, `MAX_RESULT_CHARS`) i ne sadrži ništa+ što vide samo admini (IP adrese, korisnici, audit log), jer isti odgovor dobiva svatko.+- [ ] **Maskiranje:** sve što ide pružatelju (rezultati alata, kontekst incidenta, poruke+ korisnika u chatu, greške u `usage`/`worker`) prolazi `masking.py`. Traži put koji ga+ zaobilazi (novi alat, novo polje, poruka greške pružatelja koja se sprema ili prikazuje).+- [ ] **Prompt injection:** podaci koji ulaze u kontekst modela (imena kontejnera, događaji,+ greške provjera, tekst korisnika) mogu sadržavati upute. Što najgore model može ako ih+ posluša? (Potrošiti budžet, citirati krive činjenice, preporučiti štetnu naredbu.)+ Provjeri da `check_answer` prihvaća samo ID-eve činjenica iz stvarnih rezultata, da+ `safety.py` hvata destruktivne i skrivene znakove (bidi, zero-width) i da se naredba+ koja briše podatke nikad ne preporučuje.+- [ ] **Pružatelj i ključ:** ključ šifriran (`secrets_box`), nikad u odgovoru API-ja, logu ni+ audit zapisu; adrese pružatelja fiksne u kodu (korisnik ne može postaviti `base_url`);+ promjena pružatelja, ključa i uključivanje chata traže reauth (lozinka + 2FA).+- [ ] **Posao u pozadini:** worker (`jobs.py`, `worker.py`) ima ukupni rok i ne može ostaviti+ posao zauvijek u stanju "running"; red u Redisu ne prima nepouzdane podatke kao kod.- [ ] **Izlaz modela se ne tretira kao pouzdan:** prikazuje se kao tekst (frontend ne radi`dangerouslySetInnerHTML`/neprovjeren markdown s HTML-om), ne ulazi u SQL/naredbe/URL-ove.- [ ] **Konverzacije su vezane uz korisnika** (IDOR: čitanje/brisanje tuđih konverzacija@@ -91,6 +106,5 @@ Dokumentirano: nema putanje za shell, samo tri fiksne provjere (`server_health`,ispisati).- [ ] Pozivi prema Docker API-ju idu samo kroz proxy (`DOCKER_SOCKET_PATH`); backend ne montira`docker.sock`. Provjeri compose i kod.-- [ ] Webhook: samo `ADMIN_ALERT_WEBHOOK_URL` iz okoline; bez toga nikakav zahtjev; payload- bez neocijenjenog sadržaja koji bi napravio injekciju u primatelju (Slack mrkdwn,- mention-i `@channel` iz imena kontejnera).+- [ ] Agent nema izlaznih veza osim prema pružatelju; obavijesti šalje backend+ (`notify.py`, vidi `checklist-backend.md` §6), a model ne bira odredište ni sadržaj.diff --git a/.claude/skills/security-audit/references/threat-model.md b/.claude/skills/security-audit/references/threat-model.mdindex 785ab11..26b6f22 100644--- a/.claude/skills/security-audit/references/threat-model.md+++ b/.claude/skills/security-audit/references/threat-model.md@@ -14,7 +14,7 @@ razilaze, **vjeruj kodu** i to prijavi kao nalaz o dokumentaciji.| Collector | `src/collector/collector.py` | `/proc` hosta, host FS read-only (`/hostfs`), Docker proxy, backend preko HTTP-a | pokreće se na udaljenim hostovima, može biti kompromitiran || Docker proxy | `src/collector/docker_proxy.py` | Docker socket (uid 0), Unix socket prema collectoru | drži root-ekvivalentan pristup; mora ostati minimalan || System helper (opcionalno) | `src/collector/system_helper.py` | kernel log, wtmp, `CAP_SYSLOG` | privilegiran; njegov izlaz je nepouzdan |-| LLM asistent | `src/agent/chat.py`, `prompts.py`, `SYSTEM_PROMPT.md`, `health_checks.py` | fiksni read-only provjere, opcionalni webhook | ulaz uključuje podatke s hosta |+| Incident agent (LLM) | `src/agent/` (`tools/`, `investigate.py`, `diagnosis.py`, `chat.py`, `masking.py`, `safety.py`, `providers/`, `worker.py`) | read-only alati nad bazom Infradecka; vanjski LLM pružatelj (Anthropic, OpenAI, Gemini) s ključem šifriranim u bazi | ulaz uključuje podatke s hosta; sve što ide pružatelju prolazi `masking.py` || CI | `.github/workflows/images.yml` | GHCR, cosign | opskrbni lanac za slike koje hostovi izvršavaju |Izvan repozitorija (nisu u opsegu, ali ih treba imati na umu kao pretpostavke): Traefik preddiff --git a/SECURITY.md b/SECURITY.mdindex 5b63444..3df9c3e 100644--- a/SECURITY.md+++ b/SECURITY.md@@ -296,7 +296,8 @@ check results) don't exist on the public side at all.`collector-gate` container instead, which publishes one port on your **VPN address** (set`COLLECTOR_BIND`, e.g. your OpenVPN server's tun address) and is not attached to your reverseproxy's network, so other apps can't reach it. The backend refuses collector endpoints unless they- arrived through the gate (`COLLECTOR_ACCESS=strict`), as a second layer.+ arrived through the gate (`COLLECTOR_ACCESS=strict`, the backend's default; only the development compose+ file sets `open`), as a second layer.- **Bind to the VPN address, not `0.0.0.0` plus a firewall rule.** Docker's published ports bypass`ufw` and similar firewalls, so a "deny from outside" rule does not protect them.- **Join tokens.** The Connect a server button creates a random single-use token that expires@@ -333,7 +334,8 @@ What this still depends on:add other containers to its network, `front`, and that includes your reverse proxy: attach the frontend tothe proxy's network instead, see [Other reverse proxies](docs/deployment.md#other-reverse-proxies)).- Anyone on your VPN can reach the gate. With a valid join token they can request to join, and an- admin still has to approve them; ingest additionally needs a server's token.+ admin still has to approve them; ingest additionally needs a server's token. A signed-in session, even an+ admin's, can't send metrics in a collector's place.- Rotating a collector token means re-adding that server.## Data from collectors is untrusted@@ -482,6 +484,12 @@ URL, a keyword or a value in a JSON reply, a TCP port, a TLS certificate, a DNSsessions. Note what this does and doesn't do: hashing protects against *reading* Redis. It does not stopsomeone who can *write* to it, because they can pick a secret and store its hash; the password and thenetwork separation are what stop that.+- **The login cookie is `Secure` by default.** `SESSION_COOKIE_SECURE` defaults to `true` (an empty value+ too), so a backend run outside the compose files never sends the cookie over plain HTTP by accident; only+ the development compose file sets `false`.+- **The browser keeps almost nothing.** The dashboard stores only your user ID, role and whether two-factor+ setup is pending in `localStorage`, to pick the pages it shows before `/auth/me` answers. Your name and+ email aren't stored there, so they aren't left behind when a session expires.- **Upgrade.** Sessions written in the old format no longer work (everyone signs in once) and are deleted fromRedis at start, so the old session IDs don't linger there.- **The backend** trusts `X-Forwarded-For` from any address that can reach it (`--forwarded-allow-ips *`). Thatdiff --git a/TESTS.md b/TESTS.mdindex aec2646..61bb420 100644--- a/TESTS.md+++ b/TESTS.md@@ -169,7 +169,7 @@ The chart helper tests for the frontend are separate, in `src/frontend/tests/`.| [`test_history.py`](#test_historypy) | 36 | The history API behind long-range charts: which level of detail answers each range, point limits, gaps, and agreement with the raw samples. || [`test_indexes.py`](#test_indexespy) | 9 | The database indexes on the metrics tables: only the ones queries use, none duplicated. || [`test_migrations.py`](#test_migrationspy) | 5 | Database migrations: new and pre-migration databases both end at the latest revision with the models' schema. |-| [`test_ingest_validation.py`](#test_ingest_validationpy) | 178 | What collectors report is checked and bounded: numbers, names, filesystems, spacing of check-ins. |+| [`test_ingest_validation.py`](#test_ingest_validationpy) | 179 | What collectors report is checked and bounded: numbers, names, filesystems, spacing of check-ins. || [`test_input_limits.py`](#test_input_limitspy) | 38 | Size and shape limits on request bodies, the audit-log endpoint, and safe defaults. || [`test_nginx_headers.py`](#test_nginx_headerspy) | 4 | The development and server nginx send the same security headers, HSTS only on the server one. || [`test_network_checks.py`](#test_network_checkspy) | 94 | The host's real network totals, listening-port inventory, optional internet check, firewall fingerprints, and the validation of all of it. |@@ -855,6 +855,7 @@ Nothing a collector reports is trusted: numbers, names and filesystems are check| `test_the_next_check_in_is_accepted_when_the_slot_has_passed` | Once the spacing has passed, check-ins work again. || `test_the_spacing_is_never_shorter_than_the_minimum` | Even with a 15-second interval the spacing is at least 5 seconds. || `test_someone_without_the_token_cannot_use_up_a_servers_slot` | A request with a wrong token is refused (401) without using up the real collector's slot. |+| `test_a_signed_in_admin_cannot_send_metrics_in_place_of_a_collector` | An admin session with a wrong or missing token, or for an unknown server, gets 401 and nothing is stored. || `test_a_redis_outage_does_not_stop_check_ins` | If Redis is down the limit is skipped and monitoring continues. || `test_one_server_with_a_flood_of_alerts_cannot_hide_everyone_elses` | 100 alerts from one server show at most 10, so another server's single alert stays visible; the total count stays true. || `test_one_query_never_reads_more_raw_rows_than_the_backstop` | A history query stops at the row cap. |diff --git a/compose.yml b/compose.ymlindex b5de439..218a511 100644--- a/compose.yml+++ b/compose.yml@@ -72,6 +72,8 @@ services:REDIS_URL: redis://:${REDIS_PASSWORD:?Set REDIS_PASSWORD in .env (generate one with openssl rand -hex 24)}@redis:6379/0# Set to true when the dashboard is served over HTTPS.SESSION_COOKIE_SECURE: ${SESSION_COOKIE_SECURE:-false}+ # This stack has one listener, so collector traffic can't be told apart (the backend's default is strict).+ COLLECTOR_ACCESS: openALLOW_REGISTRATION: ${ALLOW_REGISTRATION:-false}CORS_ORIGINS: ${CORS_ORIGINS:-http://localhost:5173}SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-86400}diff --git a/docs/api.md b/docs/api.mdindex fa07706..229931f 100644--- a/docs/api.md+++ b/docs/api.md@@ -42,9 +42,9 @@ All paths are relative to `/api` when going through the frontend/nginx.| `POST /monitoring/collectors/register` | join token (rate limited) | Collector asks to join; returns a poll secret and pairing code. || `POST /monitoring/collectors/register/poll` | poll secret | Collector checks approval; returns its token once approved. || `POST /monitoring/collectors/enroll` | enrollment key | Join without approval. |-| `POST /monitoring/metrics/ingest` | collector token | Collector reports host, container and system metrics. |+| `POST /monitoring/metrics/ingest` | collector token | Collector reports host, container and system metrics. Only the server's own token is accepted (a signed-in session is not); a wrong token or unknown server gets 401. || `GET /conversations`, `POST /conversations`, `DELETE /conversations/{id}`, `POST /agent/chat` | session | Chats with the incident agent (403 for users chat isn't allowed for). `POST /agent/chat` returns `message` and `steps` (as in a report); each message in `GET /conversations` carries its `steps`. A new conversation's `conversation_id` must end with `-u<your user id>` (the page uses `general-u<id>` and `incident-<incident id>-u<id>`), so nobody can take another person's ID first; anything else is refused with 400. |-| `GET /logs`, `POST /logs`, `GET /dashboard/summary` | session | Audit log and summary counts (`agent_lookups`: the agent's tool calls). `POST /logs` adds an entry to your own log (level from a fixed list, message up to 2000 characters, at most 120 an hour); its source is always `dashboard`, and system event types such as `agent_tool_call` are refused. |+| `GET /logs`, `POST /logs` | session | Audit log. `POST /logs` adds an entry to your own log (level from a fixed list, message up to 2000 characters, at most 120 an hour); its source is always `dashboard`, and system event types such as `agent_tool_call` are refused. |The four collector endpoints above exist only on the collector gate in the serverstack (the public listener answers 404); the development stack serves them on thediff --git a/docs/configuration.md b/docs/configuration.mdindex d57fb3b..45264c1 100644--- a/docs/configuration.md+++ b/docs/configuration.md@@ -11,7 +11,7 @@ Every setting below is listed, with no values, in [`example.env`](../example.env| `POSTGRES_PASSWORD` | *(required)* | PostgreSQL password. || `POSTGRES_DB` / `POSTGRES_USER` | `infradeck` / `infradeck` | Name of the database and its user. Installs from before the rename can keep `homelab` (see [SETUP_DATABASES.md](../SETUP_DATABASES.md)). || `REDIS_PASSWORD` | *(required)* | Redis password. Redis holds the login sessions, so nothing that can merely reach it may use it. Use letters and digits only (`openssl rand -hex 24`) because it is put into a URL. |-| `SESSION_COOKIE_SECURE` | `false` (dev), `true` (server file) | Send the login cookie over HTTPS only. |+| `SESSION_COOKIE_SECURE` | `true`; the dev compose file sets `false` | Send the login cookie over HTTPS only. Empty counts as `true`. Set `false` only when you open the dashboard over plain HTTP. || `SESSION_TTL_SECONDS` | `86400` | Session lifetime without *Remember me*. || `SESSION_COOKIE_NAME` | `homie_session` | Name of the sign-in cookie. Change it only if it clashes with another app on the same domain. || `SMTP_HOST` / `SMTP_PORT` / `SMTP_STARTTLS` / `SMTP_USER` / `SMTP_PASSWORD` / `SMTP_FROM` | empty / `587` / `true` / empty / empty / empty | Email for incident notifications. Email is off unless `SMTP_HOST` and `SMTP_FROM` are set. |@@ -35,7 +35,7 @@ Every setting below is listed, with no values, in [`example.env`](../example.env| `COLLECTOR_PUBLIC_URL` | empty | What **Connect a server** tells new machines to use, e.g. `http://<vpn-address>:8081/api`. || `COLLECTOR_IMAGE` / `DOCKER_PROXY_IMAGE` | empty | Digest-pinned images remote machines install. **Connect a server** needs both. || `JOIN_TOKEN_TTL_SECONDS` | `900` | How long a generated join token stays valid. |-| `COLLECTOR_ACCESS` | `open` (dev), `strict` (server file) | `strict` makes the backend answer collector endpoints only when they came through the collector gate. |+| `COLLECTOR_ACCESS` | `strict`; the dev compose file sets `open` | `strict` makes the backend answer collector endpoints only when they came through the collector gate. `open` is for a stack with a single listener, like the dev one. || `INFRADECK_ENROLL_KEY` | empty | Lets the stack's own collector skip approval. || `INFRADECK_SERVER_NAME` | host's hostname | Name shown for the stack's own machine. || `DOMAIN` | `localhost` | Domain for the optional Caddy profile. |diff --git a/docs/troubleshooting.md b/docs/troubleshooting.mdindex 0efb268..c016c65 100644--- a/docs/troubleshooting.md+++ b/docs/troubleshooting.md@@ -13,6 +13,7 @@| A server shows only `/` and no **Storage** bars for other disks | Its collector predates per-mount reporting. Install a newer collector image (run a freshly generated command, or `docker compose -f collector-compose.yml up -d --build`). A disk mounted after the collector started appears after a collector restart. || Logged out after opening the dashboard at a new address | Login cookies belong to the address they were set on; log in again. || Login fails behind HTTPS with `SESSION_COOKIE_SECURE=true` | The cookie is only sent over HTTPS; open the `https://` address, not `http://…:5173`. |+| Backend started outside the compose files: login "works" but you're signed out at once, and collectors get 404 | The backend's defaults are the safe ones (`SESSION_COOKIE_SECURE=true`, `COLLECTOR_ACCESS=strict`). Serve it over HTTPS behind the collector gate, or for a plain-HTTP, single-listener setup set `SESSION_COOKIE_SECURE=false` and `COLLECTOR_ACCESS=open`. || Collector log says "Registration refused (HTTP 403): The join token is invalid, expired or already used" | The token in the command has expired, was already used, or is wrong. Click **Generate command** again and run the new command. || **Connect a server** says no collector images are configured | Set `COLLECTOR_IMAGE` and `DOCKER_PROXY_IMAGE` in `.env` (see [Publishing the collector images](deployment.md#publishing-the-collector-images)) and rebuild. || The register page asks for a setup token | The install has no accounts yet. Use `docker compose logs backend \| grep "setup token"`, or `ADMIN_BOOTSTRAP_TOKEN`. |diff --git a/example.env b/example.envindex 62a6cfc..9e81154 100644--- a/example.env+++ b/example.env@@ -116,7 +116,7 @@ ADMIN_BOOTSTRAP_TOKEN=# (both) Send the sign-in cookie over HTTPS only. Must be true when the dashboard is served over HTTPS,# and false when you open it over plain HTTP, or you can't stay signed in.-# Default: false (dev), true (server)+# Default: true (an empty value too); the dev compose file sets falseSESSION_COOKIE_SECURE=# (both) How long a sign-in lasts without "Remember me", in seconds. Default: 86400 (24 hours)diff --git a/src/backend/config.py b/src/backend/config.pyindex 15385a2..bccd687 100644--- a/src/backend/config.py+++ b/src/backend/config.py@@ -21,7 +21,8 @@ DATABASE_URL = os.getenv(REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379/0")SESSION_TTL_SECONDS = int(os.getenv("SESSION_TTL_SECONDS", "86400"))SESSION_COOKIE_NAME = os.getenv("SESSION_COOKIE_NAME", "homie_session")-SESSION_COOKIE_SECURE = os.getenv("SESSION_COOKIE_SECURE", "false").lower() == "true"+# The safe values are the defaults (an empty value counts as unset); the development compose file turns them off.+SESSION_COOKIE_SECURE = (os.getenv("SESSION_COOKIE_SECURE") or "true").lower() == "true"# When false (the default), only the first account can be created, so a fresh install can still be set up and# nobody else can sign themselves up. Set to true only if everyone who can reach the dashboard may get an account.ALLOW_REGISTRATION = os.getenv("ALLOW_REGISTRATION", "false").lower() == "true"@@ -39,7 +40,7 @@ CORS_ORIGINS = [# "strict" makes the collector endpoints answer only requests that arrived through the VPN or# internal listener (see docker/nginx.strict.conf); "open" keeps the single-listener behaviour.-COLLECTOR_ACCESS = os.getenv("COLLECTOR_ACCESS", "open").lower()+COLLECTOR_ACCESS = (os.getenv("COLLECTOR_ACCESS") or "strict").lower()# What the Connect a server panel shows: the collector listener's address on your VPN, and the# pinned images remote hosts install. All optional.COLLECTOR_PUBLIC_URL = os.getenv("COLLECTOR_PUBLIC_URL", "").strip().rstrip("/")diff --git a/src/backend/main.py b/src/backend/main.pyindex eeede1d..6c37ba5 100644--- a/src/backend/main.py+++ b/src/backend/main.py@@ -1492,29 +1492,6 @@ def _enforce_report_spacing(server: Server) -> None:raise HTTPException(status_code=429, detail="Check results are arriving too often.", headers={"Retry-After": "2"})[email protected]("/dashboard/summary")-def dashboard_summary(- session_id: Annotated[str | None, Cookie(alias=SESSION_COOKIE_NAME)] = None,- db=Depends(get_db),-):- user = get_user_from_session(redis_client, session_id)- if not user:- raise HTTPException(status_code=401, detail="Session is invalid or expired.")-- total_users = db.query(User).count()- total_logs = db.query(AuditLog).count()- error_logs = db.query(AuditLog).filter(AuditLog.level.in_(["ERROR", "WARN"])).count()- agent_lookups = db.query(AuditLog).filter(AuditLog.event_type == "agent_tool_call").count()-- return {- "total_users": total_users,- "total_logs": total_logs,- "error_logs": error_logs,- "uptime": "99.97%",- "agent_lookups": agent_lookups,- }--LOG_WRITE_LIMIT = 120CHAT_MESSAGE_LIMIT = 30 # messages to the assistant per user per hour (each can cost the admin's OpenAI quota)CONVERSATION_CREATE_LIMIT = 30 # new conversations per user per hourdiff --git a/src/backend/routes/monitoring.py b/src/backend/routes/monitoring.pyindex 6be79a7..92a3cf7 100644--- a/src/backend/routes/monitoring.py+++ b/src/backend/routes/monitoring.py@@ -443,16 +443,12 @@ def update_check_interval(server_id: main.PathId, payload: dict, session_id: Annreturn {"id": server.id, "check_interval_seconds": server.check_interval_seconds}@router.post("/monitoring/metrics/ingest", dependencies=[Depends(main.require_collector_listener)])-def ingest_metrics(payload: main.MetricsIngestRequest, authorization: Annotated[str | None, Header()] = None, session_id: Annotated[str | None, Cookie(alias=SESSION_COOKIE_NAME)] = None, db=Depends(get_db), request: Request = None):+def ingest_metrics(payload: main.MetricsIngestRequest, authorization: Annotated[str | None, Header()] = None, db=Depends(get_db), request: Request = None):+ """A collector reports its host, container and system metrics. Only the server's own collector token is accepted."""server = db.query(main.Server).filter(main.Server.id == payload.server_id).first()- if not server:- raise main.HTTPException(status_code=404, detail="Server not found.")token = authorization.removeprefix("Bearer ").strip() if authorization and authorization.startswith("Bearer ") else None- token_hash = hashlib.sha256(token.encode()).hexdigest() if token else None- if server.collector_token_hash and token_hash and hmac.compare_digest(server.collector_token_hash, token_hash):- pass- else:- main._require_admin(session_id, db)+ if not server or not token or not server.collector_token_hash or not hmac.compare_digest(server.collector_token_hash, hashlib.sha256(token.encode()).hexdigest()):+ raise main.HTTPException(status_code=401, detail="Unknown collector.")main._enforce_ingest_spacing(server)updates_before = main._load_json(server.system_info)main._apply_server_fields(db, server, payload.server)diff --git a/src/frontend/src/App.jsx b/src/frontend/src/App.jsxindex fa4ca4d..9c6ef12 100644--- a/src/frontend/src/App.jsx+++ b/src/frontend/src/App.jsx@@ -42,7 +42,9 @@ function App() {useEffect(() => {try {- if (user) window.localStorage.setItem(AUTH_KEY, JSON.stringify(user))+ // Only what the first render needs (which pages to allow); the name and email come from /auth/me on load,+ // so they aren't left in the browser after a session expires.+ if (user) window.localStorage.setItem(AUTH_KEY, JSON.stringify({ id: user.id, role: user.role, setup_required: user.setup_required }))else window.localStorage.removeItem(AUTH_KEY)} catch {// Private windows: the sign-in just isn't remembered across reloads.diff --git a/tests/accounts/test_account_security.py b/tests/accounts/test_account_security.pyindex d5c77bd..6ce1b60 100644--- a/tests/accounts/test_account_security.py+++ b/tests/accounts/test_account_security.py@@ -304,7 +304,7 @@ def test_a_limited_session_is_refused_everywhere_except_the_two_factor_setup(appclient = _signed_in()password_body = {"current_password": PASSWORD, "new_password": NEW_PASSWORD}for method, path, body in (("get", "/monitoring/overview", None), ("get", "/admin/settings", None), ("get", "/auth/sessions", None), ("get", "/agent/config", None),- ("get", "/dashboard/summary", None), ("post", "/auth/password", password_body), ("delete", "/monitoring/servers/1", None),+ ("post", "/auth/password", password_body), ("delete", "/monitoring/servers/1", None),("post", "/monitoring/join-tokens", None), ("get", "/monitoring/enrollment-key", None), ("get", "/conversations", None),("get", "/logs", None), ("post", "/auth/sessions/revoke-others", None)):response = client.request(method.upper(), path, json=body) if body else getattr(client, method)(path)diff --git a/tests/accounts/test_new_pages.py b/tests/accounts/test_new_pages.pyindex bfd3fbd..99f81b3 100644--- a/tests/accounts/test_new_pages.py+++ b/tests/accounts/test_new_pages.py@@ -728,7 +728,6 @@ def test_a_chat_message_looks_things_up_and_the_look_ups_outlive_the_conversatioassert app.db.query(AgentRun).count() == 2looked = app.db.query(AuditLog).filter(AuditLog.event_type == "agent_tool_call").one()assert (looked.user_id, looked.conversation_id, looked.task_title) == (1, "c1-u1", "Why did it restart?")- assert owner.get("/dashboard/summary").json()["agent_lookups"] == 1assert owner.delete("/conversations/c1-u1").status_code == 200kept = app.db.query(AuditLog).filter(AuditLog.event_type == "agent_tool_call").one()diff --git a/tests/collectors/test_dashboard_host.py b/tests/collectors/test_dashboard_host.pyindex 7c82b72..1a3a2bd 100644--- a/tests/collectors/test_dashboard_host.py+++ b/tests/collectors/test_dashboard_host.py@@ -30,7 +30,7 @@ def db(monkeypatch):def _ingest(db, server_id, **server):- monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=server_id, server=server, host={"cpu_utilization_pct": 10, "ram_pct": 20, "disk_usage_pct": 30}), authorization=f"Bearer token-{server_id}", session_id=None, db=db)+ monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=server_id, server=server, host={"cpu_utilization_pct": 10, "ram_pct": 20, "disk_usage_pct": 30}), authorization=f"Bearer token-{server_id}", db=db)def test_dashboard_host_is_listed_first_and_flagged(db):@@ -79,7 +79,7 @@ def test_collector_sends_flag_only_when_configured(monkeypatch):def _ingest_containers(db, server_id, names):containers = [{"container_id": f"id-{name}", "name": name, "status": "running"} for name in names]- monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=server_id, host={"cpu_utilization_pct": 1}, containers=containers), authorization=f"Bearer token-{server_id}", session_id=None, db=db)+ monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=server_id, host={"cpu_utilization_pct": 1}, containers=containers), authorization=f"Bearer token-{server_id}", db=db)def _overview_server(db, server_id):diff --git a/tests/collectors/test_enrollment.py b/tests/collectors/test_enrollment.pyindex 8990054..2543f2e 100644--- a/tests/collectors/test_enrollment.py+++ b/tests/collectors/test_enrollment.py@@ -90,7 +90,7 @@ def test_offline_alert_raised_once_and_resolved_on_ingest(db):assert main.mark_offline_servers(db, now) == 0assert db.query(IncidentAlert).filter_by(status="active").count() == 1- monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5), authorization=f"Bearer {token}", session_id=None, db=db)+ monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5), authorization=f"Bearer {token}", db=db)assert db.query(IncidentAlert).filter_by(status="active").count() == 0diff --git a/tests/collectors/test_ingest_validation.py b/tests/collectors/test_ingest_validation.pyindex 2bc686e..dc87c34 100644--- a/tests/collectors/test_ingest_validation.py+++ b/tests/collectors/test_ingest_validation.py@@ -243,14 +243,22 @@ def test_the_spacing_is_never_shorter_than_the_minimum(app, spaced):assert ingest(app, 1, {"host": {}}).headers["Retry-After"] == "5"-def test_someone_without_the_token_cannot_use_up_a_servers_slot(app, spaced, monkeypatch):- monkeypatch.setattr(main, "_require_admin", lambda session_id, db: (_ for _ in ()).throw(main.HTTPException(status_code=401, detail="no")))-+def test_someone_without_the_token_cannot_use_up_a_servers_slot(app, spaced):assert ingest(app, 1, {"host": {}}, token="wrong").status_code == 401assert ingest(app, 1, {"host": {}}).status_code == 200 # the real collector is not held up+def test_a_signed_in_admin_cannot_send_metrics_in_place_of_a_collector(app):+ # The fixture signs everyone in as an admin, so only the token can decide here.+ for headers in ({"Authorization": "Bearer wrong"}, {"Authorization": ""}):+ response = app.post("/monitoring/metrics/ingest", json={"server_id": 1, "host": {"cpu_utilization_pct": 99}}, headers=headers)+ assert response.status_code == 401++ assert app.post("/monitoring/metrics/ingest", json={"server_id": 99, "host": {}}).status_code == 401 # no hint which servers exist+ assert latest(app) is None++def test_a_redis_outage_does_not_stop_check_ins(app, spaced, monkeypatch):class Down:def set(self, *args, **kwargs):diff --git a/tests/conftest.py b/tests/conftest.pyindex 8d4c9fe..b1896a8 100644--- a/tests/conftest.py+++ b/tests/conftest.py@@ -3,6 +3,9 @@ import osos.environ.setdefault("DATABASE_URL", "sqlite://")os.environ.setdefault("REDIS_URL", "redis://localhost:6379/0")+# The test client speaks plain HTTP and doesn't come through the collector gate; tests of the strict mode set it themselves.+os.environ.setdefault("SESSION_COOKIE_SECURE", "false")+os.environ.setdefault("COLLECTOR_ACCESS", "open")import pytestdiff --git a/tests/monitoring/test_alerts.py b/tests/monitoring/test_alerts.pyindex 450091e..c32a50e 100644--- a/tests/monitoring/test_alerts.py+++ b/tests/monitoring/test_alerts.py@@ -26,7 +26,7 @@ def db():def _ingest(db, containers):- monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5, containers=containers), authorization=f"Bearer {TOKEN}", session_id=None, db=db)+ monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5, containers=containers), authorization=f"Bearer {TOKEN}", db=db)def _container(name, status="running", health=None, restarts=0):diff --git a/tests/monitoring/test_container_checks.py b/tests/monitoring/test_container_checks.pyindex 8d932ce..0ec015d 100644--- a/tests/monitoring/test_container_checks.py+++ b/tests/monitoring/test_container_checks.py@@ -31,7 +31,7 @@ def db():def _ingest(db, containers=(), **host):- monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5, host=host, containers=list(containers)), authorization=f"Bearer {TOKEN}", session_id=None, db=db)+ monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5, host=host, containers=list(containers)), authorization=f"Bearer {TOKEN}", db=db)def _c(name, status="running", **extra):diff --git a/tests/monitoring/test_network_checks.py b/tests/monitoring/test_network_checks.pyindex ce126a6..aa85c37 100644--- a/tests/monitoring/test_network_checks.py+++ b/tests/monitoring/test_network_checks.py@@ -336,7 +336,7 @@ def db():def _ingest(db, **host):- monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5, host=host), authorization=f"Bearer {TOKEN}", session_id=None, db=db)+ monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5, host=host), authorization=f"Bearer {TOKEN}", db=db)def _l(proto, port, scope="any"):diff --git a/tests/monitoring/test_rules.py b/tests/monitoring/test_rules.pyindex dcde041..ed6101c 100644--- a/tests/monitoring/test_rules.py+++ b/tests/monitoring/test_rules.py@@ -30,7 +30,7 @@ def db(monkeypatch):def _ingest(db, **host):- monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5, host=host), authorization=f"Bearer {TOKEN}", session_id=None, db=db)+ monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5, host=host), authorization=f"Bearer {TOKEN}", db=db)def _active(db):diff --git a/tests/monitoring/test_system_checks.py b/tests/monitoring/test_system_checks.pyindex 08a94c8..7f946ea 100644--- a/tests/monitoring/test_system_checks.py+++ b/tests/monitoring/test_system_checks.py@@ -30,7 +30,7 @@ def db():def _ingest(db, **system):host = {"boot_id": BOOT_A, "oom_kill_total": 0, "kernel_tainted": 0, "ntp_synced": True, "reboot_required": False, **system}- monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5, host=host), authorization=f"Bearer {TOKEN}", session_id=None, db=db)+ monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5, host=host), authorization=f"Bearer {TOKEN}", db=db)def _active(db):diff --git a/tests/monitoring/test_system_helper.py b/tests/monitoring/test_system_helper.pyindex 26c1704..c424ccd 100644--- a/tests/monitoring/test_system_helper.py+++ b/tests/monitoring/test_system_helper.py@@ -177,7 +177,7 @@ def db():def _ingest(db, **host):host = {"boot_id": BOOT, "oom_kill_total": 0, "kernel_tainted": 0, "ntp_synced": True, "reboot_required": False, **host}- monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5, host=host), authorization=f"Bearer {TOKEN}", session_id=None, db=db)+ monitoring_routes.ingest_metrics(main.MetricsIngestRequest(server_id=5, host=host), authorization=f"Bearer {TOKEN}", db=db)def _event(kind="oom_kill", key="boot-a:oom:1.000000", minutes_ago=5, detail="The kernel OOM killer stopped java (pid 1)"):