Skip to content

fix(auth): disambiguate "device revoked" from "device row missing"#131

Merged
ntatschner merged 1 commit into
nextfrom
fix/auth-disambiguate-revoked-vs-missing
May 28, 2026
Merged

fix(auth): disambiguate "device revoked" from "device row missing"#131
ntatschner merged 1 commit into
nextfrom
fix/auth-disambiguate-revoked-vs-missing

Conversation

@ntatschner
Copy link
Copy Markdown
Collaborator

Summary

  • The device-token auth middleware emitted "device revoked" for both actually-revoked rows AND rows that didn't exist at all (FK cascade from user delete, manual DELETE). Operators couldn't tell the two apart without a manual SQL query.
  • Replaced DeviceStore::is_device_active(bool) with device_auth_status(DeviceAuthStatus) — a 3-state enum (Active | Revoked | Missing). The SQL is unchanged; only the post-fetch pattern-match differs.
  • The auth middleware now emits "device revoked" (legacy, row+revoked_at) vs "device not found" (no row) and logs WARN device_id=… sub=… on the Missing path so operators can cross-reference the sub claim.

Why now

Investigating an overnight tray cloud-sync failure on a freshly-paired device (no user-initiated revoke action). The 401 response said "device revoked" but the user had not touched the website. Without disambiguation in the middleware, we couldn't tell from logs alone whether the row was actually revoked or had been deleted entirely. This change makes the next occurrence diagnosable in one log line.

Test plan

  • cargo test -p starstats-server --bins — 647 passed (new device_auth_status_distinguishes_active_revoked_missing test + 5 existing call sites updated).
  • cargo fmt -p starstats-server --check — clean.
  • cargo clippy -p starstats-server --bins --tests -- -D warnings — clean.
  • Manual: with the deployed change, watch the next time a tray device-token gets rejected. The response body will now distinguish revoked vs not_found, and a WARN auth: device JWT presented but no matching row in devices table line will appear when the row is gone.

Compatibility

No API contract change beyond the error-string distinction. Clients that treated "device revoked" as "token dead, clear and re-pair" continue to work — both responses are still 401 and both should still trigger the same token-clearing behavior in the tray (crates/starstats-client/src/sync.rs::clear_persisted_device_token).

No DB schema change.

The auth middleware's device-token check called `is_device_active` and
returned `"device revoked"` for ANY `false` result. But the underlying
predicate collapsed two distinct states into that single bool:

- The row exists with `revoked_at IS NOT NULL` (an actual revocation).
- The row does not exist at all (FK cascade from a user delete, a
  manual `DELETE FROM devices`, or a JWT carrying a `device_id` that
  never matched anything).

Operators reading server logs — and the tray reading the 401 response
body — couldn't tell the two apart. A user reporting "my device says
revoked but I never touched the website" needed a manual SQL query
against `devices` to know whether the row was actually present.

Surfaced 2026-05-28 investigating a tray cloud-sync failure: the
device row was returning `"token rejected: device revoked"` overnight
with no user-initiated revoke action. Whether the row was actually
revoked or had been deleted entirely was indeterminable from the log.

## Changes

- New `DeviceAuthStatus` enum (`Active | Revoked | Missing`) in
  `devices.rs`.
- `DeviceStore::is_device_active(bool)` replaced by
  `DeviceStore::device_auth_status(DeviceAuthStatus)`. Both impls
  (Postgres + Memory) updated; the SQL is unchanged, only the
  pattern-match around `fetch_optional` is.
- `auth.rs` middleware now matches all three variants:
  - `Active` → pass.
  - `Revoked` → `401 "device revoked"` (unchanged legacy message).
  - `Missing` → `401 "device not found"` + a server-side
    `WARN device_id=… sub=…` so operators can cross-reference the
    sub claim against the users table.
- Five test call sites updated from the bool API to the enum API.
- New `device_auth_status_distinguishes_active_revoked_missing` test
  pins the 3-state contract.

No DB schema change. No API contract change beyond the error-string
distinction (clients that treated both responses as "token dead and
needs re-pair" continue to work — both still 401, both should still
trigger token clearing).
@ntatschner ntatschner merged commit 4dae45f into next May 28, 2026
11 checks passed
@ntatschner ntatschner deleted the fix/auth-disambiguate-revoked-vs-missing branch May 28, 2026 20:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant