Skip to content

fix(audit): emit device.paired + device.revoked audit entries#134

Merged
ntatschner merged 1 commit into
nextfrom
fix/audit-emit-device-events
May 29, 2026
Merged

fix(audit): emit device.paired + device.revoked audit entries#134
ntatschner merged 1 commit into
nextfrom
fix/audit-emit-device-events

Conversation

@ntatschner
Copy link
Copy Markdown
Collaborator

Summary

device_routes::redeem and ::revoke mutated the devices table (inserting rows / setting revoked_at) without writing to audit_log. Every device pair + unpair was a forensic black hole. Surfaced 2026-05-28 when a tray cloud-sync investigation needed to identify who/when revoked a device, but the audit log had zero device-lifecycle entries.

Now mirrors the pattern already established by set_sync (which correctly emits device.sync_enabled / device.sync_disabled):

  • redeem → emits device.paired with {device_id, label}. Actor identity comes from the looked-up user (the endpoint is unauthenticated since the desktop client is being paired because it doesn't yet have a token).
  • revoke → emits device.revoked with {device_id}. Actor is the calling user.
  • 404 revokes (device not owned by caller) emit nothing — pre-auth probes don't fill the audit log.
  • routes() now layers the audit Extension on redeem_router (previously only list_router got it; production redeem would have 500'd on the new extractor).

Test plan

  • cargo test -p starstats-server --bins → 650 passed (added 3 audit-emission tests: revoke_emits_device_revoked_audit_entry, revoke_404_does_not_emit_audit_entry, redeem_emits_device_paired_audit_entry).
  • cargo fmt -p starstats-server --check clean.
  • cargo clippy -p starstats-server --bins --tests -- -D warnings clean.
  • Manual after deploy: pair a device on the website → tray. Run SELECT * FROM audit_log WHERE action = 'device.paired' ORDER BY occurred_at DESC LIMIT 1; → expect the new entry with actor_handle + device_id + label.
  • Manual: revoke a device. Run the analogous query for device.revoked.

Compatibility

No schema change, no API contract change. Adds new action strings to the existing audit_log column. Consumers using action LIKE '%device%' already match.

Related

`device_routes::redeem` and `::revoke` were both mutating the
`devices` table (inserting a new row + setting `revoked_at`
respectively) without writing anything to `audit_log`. Surfaced
2026-05-28 during a tray cloud-sync investigation: a user's device
had been revoked overnight, but the audit log contained zero
device-lifecycle entries to attribute the action. The forensic
trail was a `revoked_at` timestamp on the row and nothing else.

`set_sync` (the sync-toggle handler) already emitted the right
shape — this PR mirrors that pattern for both endpoints.

## Changes

- `redeem` now emits `device.paired` with `{device_id, label}`
  payload. The endpoint is unauthenticated (pre-pair) so actor
  identity comes from the looked-up user, not a bearer claim.
- `revoke` now emits `device.revoked` with `{device_id}` payload,
  attributed to the calling user.
- 404 revokes (device not owned by caller) emit nothing —
  pre-auth probes shouldn't fill the audit log.
- `routes()` now layers the audit Extension on the redeem_router
  too (previously only list_router got it; production redeem
  would have 500'd on the new `Extension(audit)` extractor).

## Tests

- `revoke_emits_device_revoked_audit_entry` — happy-path emit
  with actor_sub + actor_handle + payload assertions.
- `revoke_404_does_not_emit_audit_entry` — 404 path silent.
- `redeem_emits_device_paired_audit_entry` — happy-path emit
  with all four fields (action, actor_sub, actor_handle, label).
- All 3 existing `revoke()` direct-invocation tests updated to
  pass the new `Extension(audit)` parameter via `noop_audit()`.
- Fixture's redeem router gets the audit layer too.

650/650 starstats-server tests pass. fmt + clippy clean.

## Compatibility

No schema change, no API contract change. The audit_log table
gains new action strings (`device.paired`, `device.revoked`) —
consumers iterating `action LIKE '%device%'` already match these.
@ntatschner ntatschner merged commit efb2b6a into next May 29, 2026
11 checks passed
@ntatschner ntatschner deleted the fix/audit-emit-device-events branch May 29, 2026 14:30
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