Back off every dashboard login, bound requests and connections, fix the notes renderer #146

Merged
aluminum-ice merged 1 commit from dashboard-hardening into main 2026-11-01 22:53:13 +00:00
Owner

What this PR does

Hardens the dashboard's web server and sign-in, and fixes the Markdown renderer used for the guide and release notes.

  • Every credential check backs off.
    • Everything goes through one per-address throttle: the sign-in form, HTTP Basic on any route, and a presented token. LoginThrottle.begin (new) records an attempt before the slow password check, so parallel attempts each count. Past the free attempts the answer is 429 with Retry-After, without checking.
    • A recent success skips it: a login verified within the 5-minute cache isn't throttled.
    • Failures are logged with the source address, at most once a minute per address.
  • "Couldn't check" is never "wrong password".
    • At most four checks at once: password checks against the root helper are limited to four (_AUTH_GATE).
    • What counts as "couldn't check": a check that can't get a turn, a helper that answers busy (from #145's backoff), or a helper that can't be reached. The page then answers 503, "Couldn't check your password right now. Try again in a moment.", and LoginThrottle.forgive un-records the attempt.
    • The verification can now say so: _verify_system_login returns True, False or None.
  • Request bodies are capped.
    • 64 KB everywhere: MAX_CONTENT_LENGTH is 64 KB app-wide, so a larger body gets 413 before anything parses it.
    • The potfile import: it checks the login before reading its body, then raises its own limit to max_import_mb plus 64 KB. Past that it returns to the networks page with "That file is too large to import."
  • Connections are bounded.
    • Caps: each listener serves at most 64 connections, and 16 per client address (loopback is exempt from the per-address cap).
    • Header deadline: a request must finish its request line and headers within 20 s in all; the 15 s per-read timeout stays.
    • Out of descriptors: the accept loop pauses half a second and logs once a minute, instead of spinning.
    • Daemon unit: kestrel-daemon.service gets LimitNOFILE=4096 and PrivateTmp=yes.
  • The Markdown renderer always finishes.
    • Lines starting with #: the paragraph loop now uses the heading branch's exact test, so a # line that isn't a heading (#142 fixed, #### Notes, a bare #) is paragraph text.
    • Every pass consumes a line, so a future pattern mismatch renders rather than stalls.
    • Input: NUL is stripped (it marks the inline-code placeholders), a stray placeholder renders as nothing, and input is capped at 200 KB.
    • The guide is unchanged: GUIDE_MD renders byte-for-byte as before (checked).

Checklist

  • Lint passes — make lint (ansible --syntax-check, yamllint, shellcheck --severity=error, and the real-data check). The Lint workflow runs this on the PR.
  • Tests pass — make test (the kestrel_pi pytest suite and the real-data check's tests). The Test workflow runs this on the PR.
  • Nothing real: no secrets, no personal information, no real data. This is a public repository. The diff contains no real account passwords or hashes, Wi-Fi passphrases, private keys, API tokens, .env files, real names, personal emails, device serials, and no real captured or device data: SSIDs, MAC addresses, GPS coordinates, capture files, diagnostics bundles, logs, or screenshots of a real device — placeholders only. The real-data check catches MACs, emails, keys and capture files; real SSIDs, coordinates and screenshots are on you.
  • Consent gates intact. Capture and network plugins still ship disabled, enabling them still takes the authorized-use acknowledgment (never pre-ticked or given by code), and the capture whitelist is still enforced.
  • Docs updated in the same PR, if this changes something an operator sees or configures: the README package table, the relevant page under docs/, and the on-device operator guide (GUIDE_MD in kestrel_pi/userguide.py) where it applies.
  • Commit messages are short and imperative.

Release impact

  • App-layer-safe — changes only files the in-place update engine carries: the kestrel_pi package, plugins/*.py, systemd units, the journald drop-in, a lib script already in the engine's LIB_SCRIPTS, or the CLI.
  • Reflash-class — touches a build input the engine does not carry onto a running device: apt or pip packages, boot options / config.txt / cmdline, kernel modules, the base image or partition size, builder/config.json, builder/config.yml, the Makefile, the Release workflow (.forgejo/workflows/main.yml), a shipped config default (config.toml / default.conf), or a brand-new lib script.

Notes for review

  • Files: web.py, webtls.py, and the daemon's unit. The update engine installs units and runs daemon-reload, so PrivateTmp/LimitNOFILE apply after an in-place update.
  • Server class: the threaded server is now built directly as werkzeug's ThreadedWSGIServer subclassed with the caps (_capped_server), instead of through make_server(threaded=True), which built the same class. The deferred TLS handshake is unchanged.
  • Tests:
    • Backoff: Basic on any route reaches 429 after five failures and isn't checked meanwhile, even with the right password; a recently verified login still gets in; a busy or unreachable helper gives 503 and never 429, on Basic and on the sign-in form; a presented token backs off; at most four checks run at once.
    • Parallel attempts: begin lets only the free number of parallel attempts through, and forgive un-records.
    • Bodies: an oversized /login body gets 413; the import refuses without the login before reading a large body; past its limit it gets the banner.
    • Renderer: the # lines, the placeholder case, and a fuzz test of 3,000 random inputs, each finishing in under 0.5 s.
    • Connections: the caps' bookkeeping (per address, loopback, release); a real server closing connections past the cap and serving again once they go; headers trickled past the deadline cut off.
  • Docs: the on-device guide (sign-in), web-ui-conventions.md (two new security conventions) and CLAUDE.md.
  • Locally: the suite passes except the two tests that depend on this host (the daemon SIGHUP test and the GPS serial-device test); CI runs both.

🤖 Generated with Claude Code

## What this PR does Hardens the dashboard's web server and sign-in, and fixes the Markdown renderer used for the guide and release notes. - **Every credential check backs off.** - **Everything goes through one per-address throttle:** the sign-in form, HTTP Basic on any route, and a presented token. `LoginThrottle.begin` (new) records an attempt *before* the slow password check, so parallel attempts each count. Past the free attempts the answer is 429 with `Retry-After`, without checking. - **A recent success skips it:** a login verified within the 5-minute cache isn't throttled. - **Failures are logged** with the source address, at most once a minute per address. - **"Couldn't check" is never "wrong password".** - **At most four checks at once:** password checks against the root helper are limited to four (`_AUTH_GATE`). - **What counts as "couldn't check":** a check that can't get a turn, a helper that answers `busy` (from #145's backoff), or a helper that can't be reached. The page then answers 503, "Couldn't check your password right now. Try again in a moment.", and `LoginThrottle.forgive` un-records the attempt. - **The verification can now say so:** `_verify_system_login` returns True, False or None. - **Request bodies are capped.** - **64 KB everywhere:** `MAX_CONTENT_LENGTH` is 64 KB app-wide, so a larger body gets 413 before anything parses it. - **The potfile import:** it checks the login before reading its body, then raises its own limit to `max_import_mb` plus 64 KB. Past that it returns to the networks page with "That file is too large to import." - **Connections are bounded.** - **Caps:** each listener serves at most 64 connections, and 16 per client address (loopback is exempt from the per-address cap). - **Header deadline:** a request must finish its request line and headers within 20 s in all; the 15 s per-read timeout stays. - **Out of descriptors:** the accept loop pauses half a second and logs once a minute, instead of spinning. - **Daemon unit:** `kestrel-daemon.service` gets `LimitNOFILE=4096` and `PrivateTmp=yes`. - **The Markdown renderer always finishes.** - **Lines starting with `#`:** the paragraph loop now uses the heading branch's exact test, so a `#` line that isn't a heading (`#142 fixed`, `#### Notes`, a bare `#`) is paragraph text. - **Every pass consumes a line,** so a future pattern mismatch renders rather than stalls. - **Input:** NUL is stripped (it marks the inline-code placeholders), a stray placeholder renders as nothing, and input is capped at 200 KB. - **The guide is unchanged:** `GUIDE_MD` renders byte-for-byte as before (checked). ## Checklist - [x] **Lint passes** — `make lint` (ansible `--syntax-check`, `yamllint`, `shellcheck --severity=error`, and the real-data check). The **Lint** workflow runs this on the PR. - [x] **Tests pass** — `make test` (the `kestrel_pi` pytest suite and the real-data check's tests). The **Test** workflow runs this on the PR. - [x] **Nothing real: no secrets, no personal information, no real data.** This is a public repository. The diff contains no real account passwords or hashes, Wi-Fi passphrases, private keys, API tokens, `.env` files, real names, personal emails, device serials, and no real captured or device data: SSIDs, MAC addresses, GPS coordinates, capture files, diagnostics bundles, logs, or screenshots of a real device — placeholders only. The real-data check catches MACs, emails, keys and capture files; real SSIDs, coordinates and screenshots are on you. - [x] **Consent gates intact.** Capture and network plugins still ship disabled, enabling them still takes the authorized-use acknowledgment (never pre-ticked or given by code), and the capture whitelist is still enforced. - [x] **Docs updated in the same PR**, if this changes something an operator sees or configures: the README package table, the relevant page under `docs/`, and the on-device operator guide (`GUIDE_MD` in `kestrel_pi/userguide.py`) where it applies. - [x] Commit messages are short and imperative. ## Release impact - [x] **App-layer-safe** — changes only files the in-place update engine carries: the `kestrel_pi` package, `plugins/*.py`, systemd units, the journald drop-in, a lib script already in the engine's `LIB_SCRIPTS`, or the CLI. - [ ] **Reflash-class** — touches a build input the engine does not carry onto a running device: apt or pip packages, boot options / `config.txt` / `cmdline`, kernel modules, the base image or partition size, `builder/config.json`, `builder/config.yml`, the `Makefile`, the Release workflow (`.forgejo/workflows/main.yml`), a shipped config default (`config.toml` / `default.conf`), or a brand-new lib script. ## Notes for review - **Files:** `web.py`, `webtls.py`, and the daemon's unit. The update engine installs units and runs `daemon-reload`, so `PrivateTmp`/`LimitNOFILE` apply after an in-place update. - **Server class:** the threaded server is now built directly as werkzeug's `ThreadedWSGIServer` subclassed with the caps (`_capped_server`), instead of through `make_server(threaded=True)`, which built the same class. The deferred TLS handshake is unchanged. - **Tests:** - **Backoff:** Basic on any route reaches 429 after five failures and isn't checked meanwhile, even with the right password; a recently verified login still gets in; a busy or unreachable helper gives 503 and never 429, on Basic and on the sign-in form; a presented token backs off; at most four checks run at once. - **Parallel attempts:** `begin` lets only the free number of parallel attempts through, and `forgive` un-records. - **Bodies:** an oversized `/login` body gets 413; the import refuses without the login before reading a large body; past its limit it gets the banner. - **Renderer:** the `#` lines, the placeholder case, and a fuzz test of 3,000 random inputs, each finishing in under 0.5 s. - **Connections:** the caps' bookkeeping (per address, loopback, release); a real server closing connections past the cap and serving again once they go; headers trickled past the deadline cut off. - **Docs:** the on-device guide (sign-in), `web-ui-conventions.md` (two new security conventions) and CLAUDE.md. - **Locally:** the suite passes except the two tests that depend on this host (the daemon SIGHUP test and the GPS serial-device test); CI runs both. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Back off every dashboard login, bound requests and connections, fix the notes renderer
All checks were successful
Test / test (pull_request) Successful in 32s
Lint / lint (pull_request) Successful in 32s
Lint / lint (push) Successful in 33s
Test / test (push) Successful in 32s
952b6bd263
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
aluminum-ice deleted branch dashboard-hardening 2026-11-01 22:53:13 +00:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
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
aluminum-ice/kestrel!146
No description provided.