Bound what a census read keeps and drop malformed DHT replies #147

Merged
aluminum-ice merged 1 commit from census-hardening into main 2026-11-01 23:01:51 +00:00
Owner

What this PR does

Bounds what the census plugin keeps from a read, and makes its DHT client drop a malformed reply instead of ending the query round.

  • Per slot: _census_proto.union keeps at most MAX_ENTRIES (49, what one slot holds) from a slot's copies. When the copies together hold more, it keeps the entries held by the most copies (then the later hour, then the token) and counts the rest as rejected. read_slot and the writer's merge use it unchanged.
  • Per day: a reader keeps at most proto.DAY_ENTRIES tokens a day (64 slots × 4 locations × 49 = 12,544, a full census). Past that, tokens it already has still update; new ones are tallied in over.
    • The Census page's Today card then says "The census is full today: some reports weren't counted."
    • The plugin logs it once a day.
    • seen.json loading applies the same cap, keeping the latest hours.
  • DHT replies: KrpcClient matches a datagram to its query in a new _take, which accepts only a 2-byte transaction id of the reply kinds from the queried address. Any datagram that doesn't match, or makes the handling raise, is counted in dropped, and the round goes on.

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

  • The format is unchanged (kestrel-census/1): entries, slots and keys are the same; this is reader behaviour only. The desktop report (tools/census-report/) loads the same helper files, so it gets the per-slot bound too.
  • Tests: a reply round with five malformed transaction ids, a dict-shaped error from the wrong host and a non-dict message still uses the real reply (7 dropped); a packet whose handling raises is dropped too; union keeps the 30 entries two copies agree on against 49 from one copy; the day cap stops new tokens and still updates a known one; the page line comes and goes with over; seen.json loading keeps the latest hours. All 104 census tests pass.
  • Docs: the reading rules in census-protocol.md, and the new page line in census-setup.md's troubleshooting table.
  • 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 Bounds what the census plugin keeps from a read, and makes its DHT client drop a malformed reply instead of ending the query round. - **Per slot:** `_census_proto.union` keeps at most `MAX_ENTRIES` (49, what one slot holds) from a slot's copies. When the copies together hold more, it keeps the entries held by the most copies (then the later hour, then the token) and counts the rest as rejected. `read_slot` and the writer's `merge` use it unchanged. - **Per day:** a reader keeps at most `proto.DAY_ENTRIES` tokens a day (64 slots × 4 locations × 49 = 12,544, a full census). Past that, tokens it already has still update; new ones are tallied in `over`. - The Census page's Today card then says "The census is full today: some reports weren't counted." - The plugin logs it once a day. - `seen.json` loading applies the same cap, keeping the latest hours. - **DHT replies:** `KrpcClient` matches a datagram to its query in a new `_take`, which accepts only a 2-byte transaction id of the reply kinds from the queried address. Any datagram that doesn't match, or makes the handling raise, is counted in `dropped`, and the round goes on. ## 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 - **The format is unchanged** (`kestrel-census/1`): entries, slots and keys are the same; this is reader behaviour only. The desktop report (`tools/census-report/`) loads the same helper files, so it gets the per-slot bound too. - **Tests:** a reply round with five malformed transaction ids, a dict-shaped error from the wrong host and a non-dict message still uses the real reply (7 dropped); a packet whose handling raises is dropped too; `union` keeps the 30 entries two copies agree on against 49 from one copy; the day cap stops new tokens and still updates a known one; the page line comes and goes with `over`; `seen.json` loading keeps the latest hours. All 104 census tests pass. - **Docs:** the reading rules in `census-protocol.md`, and the new page line in `census-setup.md`'s troubleshooting table. - **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)
Bound what a census read keeps and drop malformed DHT replies
All checks were successful
Test / test (pull_request) Successful in 32s
Lint / lint (pull_request) Successful in 33s
Test / test (push) Successful in 32s
Lint / lint (push) Successful in 32s
e323af6180
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
aluminum-ice deleted branch census-hardening 2026-11-01 23:01:51 +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!147
No description provided.