feat(ops): read-only device audit + devices registry template (#15) #21

Merged
maximus merged 2 commits from feat/device-audit-15 into main 2026-11-04 08:48:53 +00:00
Member

Implements most of #15: a read-only device audit plus a registry template. Does not close #15 — two acceptance items are deliberately left for Bear (see "Deviations").

What

  • scripts/audit_devices.py (single-file uv script, PEP 723 deps: pyyaml): loads a registry, calls GET /_synapse/admin/v2/users/<mxid>/devices once per registered user (plus optional extra users:), and reports
    • unexpected devices (in Synapse, not in the registry; keyed by (mxid, device_id), so a reused id on another account is also unexpected),
    • missing devices (registered, absent from Synapse),
    • idle devices (registered, last_seen older than --idle-days, default 30, per-device max_idle_days override; never-seen counts as idle).
      Output follows the shape in the issue; summary line N registered + M missing + U unexpected + I idle = OK | ACTION REQUIRED.
  • Exit codes: 0 OK, 1 ACTION REQUIRED, 2 registry/usage/API error (so cron/alerts can distinguish "fleet drifted" from "audit broken").
  • Safety: GET only, no delete/modify path at all; token from env (SYNAPSE_ADMIN_TOKEN, --token-env to rename), never printed; HTTP errors report only the status code; non-https homeservers rejected (except http://localhost).
  • devices.example.yaml: template with the <PLACEHOLDER> names already used in the docs. The audit refuses to run while any placeholder is left in.
  • docs/bridge-client-setup.md: rotation procedure gains step 8 "Update the device registry"; new section "Auditing the device fleet" (usage, read-only contract, where the registry lives, scheduling).
  • .gitignore: /devices.yaml + Python caches. scripts/ruff.toml: line length 100 / py312.

Deviations from the issue (decisions for Bear)

  1. Real devices.yaml not committed. This repo is public and was explicitly scrubbed of deployment identifiers (6534ffe, 7f042e5: <MAXIMUS_MXID>, <MAXIMUS_NAS_DEVICE>, …). Committing the fleet's mxids/device ids would undo that, so only the template is here and the real file is gitignored. Suggested home: the private deployment repo next to the bridge (nas-docker-compose / wherever the NAS bridge config lives). The current ids appear in the #15 issue text (mba/nas/rpi5 for Maximus as of 2026-05-27) but I could not confirm they are still current (rotations since), and Felinus/jmz/kitty ids are not in the repo or docs — fill them from a first run (it lists every live device as unexpected until registered).
  2. No scheduled run enabled. Documented only. Unattended runs need the Synapse admin token (msc3861.admin_token, god-mode) on the scheduling host; where that token may live is Bear's call. A weekly systemd timer on the NAS that posts to the bridge room on exit 1/2 is the suggested shape.
  3. Not added to Forgejo CI: the existing check job runs on the rpi5 runner with only a Rust toolchain; adding a Python job is a separate small change if wanted.

Security: output escaping

Strings from the Synapse API (device id, display name) and from the registry (mxid, host, role) are never printed raw. A newline, \r, ANSI escape or bidi override in a device id could otherwise fake report lines, e.g. a forged --- summary --- ... OK. Free text is rendered via repr(). Device ids outside [A-Za-z0-9._=-] and malformed mxids are prefixed (suspicious id) and shown escaped. Non-string display names are dropped. last_seen_ts is only used if it is an int, and the IP is not printed. Tests cover hostile ids and names (\n, \r, \x1b[2J, a faked ─── summary ───\n OK): the report keeps the same line count, has no unprintable characters, and its only summary line is the real last one.

Tests

uv run --no-project --with pytest --with pyyaml python -m pytest scripts/tests → 38 passed (mocked Synapse: output-injection cases, clean fleet, unexpected/missing/idle, idle boundary and per-device override, never-seen, extra users, cross-account id reuse, rendering, registry validation incl. placeholders/duplicates, exit codes, missing token, HTTP 401 path never leaks the token, GET + URL-encoded mxid + bearer header, plain-http rejected).
ruff check + ruff format --check clean; mypy --strict clean. CLI smoke: uv run scripts/audit_devices.py --config devices.example.yaml → exit 2 "placeholder value left in registry". Not run against live Synapse (no admin token used overnight).

Refs #15. No merge, no deploy.

🤖 Generated with Claude Code

https://claude.ai/code/session_01XMf4EKNjKMd6bwCpS7K4jU

Implements most of #15: a **read-only** device audit plus a registry template. Does not close #15 — two acceptance items are deliberately left for Bear (see "Deviations"). ## What - `scripts/audit_devices.py` (single-file uv script, PEP 723 deps: pyyaml): loads a registry, calls `GET /_synapse/admin/v2/users/<mxid>/devices` once per registered user (plus optional extra `users:`), and reports - **unexpected** devices (in Synapse, not in the registry; keyed by `(mxid, device_id)`, so a reused id on another account is also unexpected), - **missing** devices (registered, absent from Synapse), - **idle** devices (registered, `last_seen` older than `--idle-days`, default 30, per-device `max_idle_days` override; never-seen counts as idle). Output follows the shape in the issue; summary line `N registered + M missing + U unexpected + I idle = OK | ACTION REQUIRED`. - Exit codes: `0` OK, `1` ACTION REQUIRED, `2` registry/usage/API error (so cron/alerts can distinguish "fleet drifted" from "audit broken"). - Safety: GET only, no delete/modify path at all; token from env (`SYNAPSE_ADMIN_TOKEN`, `--token-env` to rename), never printed; HTTP errors report only the status code; non-https homeservers rejected (except `http://localhost`). - `devices.example.yaml`: template with the `<PLACEHOLDER>` names already used in the docs. The audit refuses to run while any placeholder is left in. - `docs/bridge-client-setup.md`: rotation procedure gains **step 8 "Update the device registry"**; new section **"Auditing the device fleet"** (usage, read-only contract, where the registry lives, scheduling). - `.gitignore`: `/devices.yaml` + Python caches. `scripts/ruff.toml`: line length 100 / py312. ## Deviations from the issue (decisions for Bear) 1. **Real `devices.yaml` not committed.** This repo is public and was explicitly scrubbed of deployment identifiers (6534ffe, 7f042e5: `<MAXIMUS_MXID>`, `<MAXIMUS_NAS_DEVICE>`, …). Committing the fleet's mxids/device ids would undo that, so only the template is here and the real file is gitignored. Suggested home: the private deployment repo next to the bridge (nas-docker-compose / wherever the NAS bridge config lives). The current ids appear in the #15 issue text (mba/nas/rpi5 for Maximus as of 2026-05-27) but I could not confirm they are still current (rotations since), and Felinus/jmz/kitty ids are not in the repo or docs — fill them from a first run (it lists every live device as *unexpected* until registered). 2. **No scheduled run enabled.** Documented only. Unattended runs need the Synapse admin token (`msc3861.admin_token`, god-mode) on the scheduling host; where that token may live is Bear's call. A weekly systemd timer on the NAS that posts to the bridge room on exit 1/2 is the suggested shape. 3. Not added to Forgejo CI: the existing `check` job runs on the rpi5 runner with only a Rust toolchain; adding a Python job is a separate small change if wanted. ## Security: output escaping Strings from the Synapse API (device id, display name) and from the registry (mxid, host, role) are never printed raw. A newline, `\r`, ANSI escape or bidi override in a device id could otherwise fake report lines, e.g. a forged `--- summary --- ... OK`. Free text is rendered via `repr()`. Device ids outside `[A-Za-z0-9._=-]` and malformed mxids are prefixed `(suspicious id)` and shown escaped. Non-string display names are dropped. `last_seen_ts` is only used if it is an int, and the IP is not printed. Tests cover hostile ids and names (`\n`, `\r`, `\x1b[2J`, a faked `─── summary ───\n OK`): the report keeps the same line count, has no unprintable characters, and its only summary line is the real last one. ## Tests `uv run --no-project --with pytest --with pyyaml python -m pytest scripts/tests` → **38 passed** (mocked Synapse: output-injection cases, clean fleet, unexpected/missing/idle, idle boundary and per-device override, never-seen, extra users, cross-account id reuse, rendering, registry validation incl. placeholders/duplicates, exit codes, missing token, HTTP 401 path never leaks the token, GET + URL-encoded mxid + bearer header, plain-http rejected). `ruff check` + `ruff format --check` clean; `mypy --strict` clean. CLI smoke: `uv run scripts/audit_devices.py --config devices.example.yaml` → exit 2 "placeholder value left in registry". Not run against live Synapse (no admin token used overnight). Refs #15. No merge, no deploy. 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01XMf4EKNjKMd6bwCpS7K4jU
feat(ops): read-only device audit + devices registry template
Some checks failed
check / check (pull_request) Has been cancelled
c254f81b32
Add scripts/audit_devices.py: diffs a canonical device registry against
the Synapse admin API (GET /_synapse/admin/v2/users/<mxid>/devices only)
and reports unexpected, missing and idle devices. Exit 0 = OK,
1 = ACTION REQUIRED, 2 = registry/API error. The admin token is read
from the environment and never printed.

The repo is public and scrubbed of deployment identifiers, so only a
placeholder template (devices.example.yaml) is committed; the real
devices.yaml is gitignored and the audit refuses placeholder values.
The rotation runbook gains an "update the registry" step and a new
"Auditing the device fleet" section documents usage and scheduling.

Refs #15

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XMf4EKNjKMd6bwCpS7K4jU
fix(ops): escape server-supplied strings in device audit output
All checks were successful
check / check (pull_request) Successful in 4m17s
check / check (push) Successful in 4m11s
90f525c175
Device ids, display names and user ids come from the Synapse API (and
host/role from the registry) and were interpolated raw, so control
characters, newlines or ANSI escapes could forge or hide audit lines.
Free text is now rendered via repr(); device ids outside
[A-Za-z0-9._=-] and malformed mxids are flagged "(suspicious id)" and
shown escaped. Non-string display names are dropped.

Refs #15

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01XMf4EKNjKMd6bwCpS7K4jU
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
public/matrix-bridge!21
No description provided.