feat: client log with request ids, joined to the server's

The browser now keeps its half of the one event shape logs.py defines:
lib/log.ts is a levelled, redacted, bounded (2000 events) in-memory ring
with the server's level names and severity numbers. Nothing leaves the
browser yet; sending is the consented diagnostics level (S5).

- lib/http.ts `send` is the one fetch: every request carries an
  X-Request-Id and becomes one http.request event. The server echoes the
  matched route template in X-Route (only for a matched route, so a raw
  path with a value in it is never sent back); without one the client
  templates the path itself. req, beacon, the upload XHR, the public
  watch page and the legal pages all go through it.
- Hooks: app start snapshot, uncaught errors and rejections, History
  push/replace/popstate (a scroll-only replace is not an event; parameter
  values only at DEBUG, typed search only at TRACE), notifications, the
  error dialog, the crash boundary, failed queries and mutations (key
  head only), and the messages socket (lifecycle, frame types at DEBUG).
- Redaction is one contract: backend/tests/fixtures/log_redaction_cases.json
  runs against both redact() implementations.
- Lint: fetch, XMLHttpRequest and WebSocket each belong to one logging
  module, console to none; a contract test proves the rules fire.

Fixes a socket takeover: the close of a socket released by the last
subscriber cleared the newer socket a quick re-subscribe had opened and
scheduled a third, leaving two live sockets that delivered every message
twice (measured in Chromium: 3 created / 2 live before, 2 / 1 after).
This commit is contained in:
2026-09-17 01:44:29 +02:00
parent 0517de1f59
commit a210992bfc
24 changed files with 1207 additions and 34 deletions
+92
View File
@@ -0,0 +1,92 @@
{
"_comment": "One redaction contract for both logs: backend/app/logs.py redact() and frontend/src/lib/log.ts redact() must turn every `value` (under `key`) into `expected`. Both test suites run this file (backend tests/test_logs.py, frontend src/lib/log.contract.test.ts), so the two implementations cannot drift apart. Some cases prove a secret goes; others prove ordinary content (a search term, a Plex title, a chat line) is not mangled — what a LEVEL may carry is decided at the call site, not here.",
"cases": [
{
"name": "a password field is dropped whole",
"key": "",
"value": {
"email": "a@b.co",
"password": "hunter2",
"current_password": "x"
},
"expected": {
"email": "[email]",
"password": "[redacted]",
"current_password": "[redacted]"
}
},
{
"name": "a token under any spelling of the key, nested and in lists",
"key": "",
"value": {
"plex": { "plex_server_token": "abc", "Authorization": "Bearer z" },
"items": [{ "access_token": "t1" }, { "id": 3 }]
},
"expected": {
"plex": {
"plex_server_token": "[redacted]",
"Authorization": "[redacted]"
},
"items": [{ "access_token": "[redacted]" }, { "id": 3 }]
}
},
{
"name": "E2EE material and cookies",
"key": "",
"value": {
"ciphertext": "AAAA",
"passphrase": "p",
"private_key": "k",
"cookies": "SID=1"
},
"expected": {
"ciphertext": "[redacted]",
"passphrase": "[redacted]",
"private_key": "[redacted]",
"cookies": "[redacted]"
}
},
{
"name": "a reset link and a verify fragment",
"key": "msg",
"value": "open https://app.example/#reset=QWErty123 or /?verify=abc&lang=hu",
"expected": "open https://app.example/#reset=[redacted] or /?verify=[redacted]&lang=hu"
},
{
"name": "an OAuth callback URL",
"key": "url",
"value": "/auth/callback?code=4/0Ab-xyz&state=s3cr3t&scope=email",
"expected": "/auth/callback?code=[redacted]&state=[redacted]&scope=email"
},
{
"name": "an email address inside a sentence",
"key": "message",
"value": "Invitation sent to Some.One+tag@mail.example.org.",
"expected": "Invitation sent to [email]."
},
{
"name": "a search term is kept (TRACE decides whether it is logged at all)",
"key": "q",
"value": "lo-fi beats to relax",
"expected": "lo-fi beats to relax"
},
{
"name": "a Plex title with an at-sign is not an address",
"key": "title",
"value": "Live @ Wembley 1986",
"expected": "Live @ Wembley 1986"
},
{
"name": "a chat line is text like any other; its only secret part is scrubbed",
"key": "body",
"value": "see you at 8, my mail is me@home.example",
"expected": "see you at 8, my mail is [email]"
},
{
"name": "non-text values pass through",
"key": "",
"value": { "status": 404, "ok": false, "ratio": 0.5, "none": null },
"expected": { "status": 404, "ok": false, "ratio": 0.5, "none": null }
}
]
}