Skip to content

fix: revoke attributed API keys on user deletion - #1488

Open
pjb157 wants to merge 1 commit into
mainfrom
peter/revoke-deleted-user-api-keys
Open

fix: revoke attributed API keys on user deletion#1488
pjb157 wants to merge 1 commit into
mainfrom
peter/revoke-deleted-user-api-keys

Conversation

@pjb157

@pjb157 pjb157 commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Summary

  • revoke API keys owned by or attributed to a deleted user
  • preserve active keys attributed to other users in shared account contexts
  • retain the existing statement-level cache invalidation path

Testing

  • regression test failed before the query change and passes after it
  • cargo test -p dwctl test_delete_user_ -- --test-threads=1 (7 passed)
  • just lint rust -- -D warnings
  • cargo fmt --all --check

The complete local Rust suite reached 1,979 passing tests; nine unrelated organization email-domain tests could not initialize the host DNS resolver (invalid IP address syntax) and also failed when rerun individually.

Copilot AI lite review requested due to automatic review settings August 14, 2026 09:00
@cloudflare-workers-and-pages

Copy link
Copy Markdown

Deploying control-layer with  Cloudflare Pages  Cloudflare Pages

Latest commit: d778576
Status: ✅  Deploy successful!
Preview URL: https://7c9ad136.control-layer.pages.dev
Branch Preview URL: https://peter-revoke-deleted-user-ap.control-layer.pages.dev

View logs

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the user-deletion flow in dwctl to ensure API keys associated with a deleted user are revoked not only when the key is owned by that user, but also when the key is attributed to them (e.g., org/shared-account key attribution via created_by). This closes a gap where users could retain access through attributed keys in shared account contexts.

Changes:

  • Expand the API key revocation query during user deletion to delete keys where user_id = deleted_user or created_by = deleted_user.
  • Add a regression test that asserts attributed keys are revoked on user deletion while unrelated attributed keys remain active.
  • Update the SQLx offline query metadata for the modified DELETE statement.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
dwctl/src/db/handlers/users.rs Extend user deletion to revoke API keys attributed to the deleted user; add a regression test for shared-account attribution behavior.
.sqlx/query-c0ecf188c2d5b41a1192bcc08ad1dea8e744fcc8e6c47913081bc174d3b82f21.json Update SQLx prepared-query metadata to match the new DELETE predicate.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines 484 to 493
// Only when we actually transitioned the user to deleted (idempotent on
// repeat calls). Hard-delete keys owned by the user (user_id) — these
// authenticate as them. Keys they merely created for others (created_by)
// belong to those users and are left alone. The api_keys DELETE trigger
// emits NOTIFY, so the onwards proxy drops them from its cache at once.
// repeat calls). Hard-delete keys owned by or attributed to the user so
// keys issued in another account context cannot outlive their holder.
// The api_keys DELETE trigger emits NOTIFY, so the onwards proxy drops
// them from its cache at once.
if result.rows_affected() > 0 {
sqlx::query!(r#"DELETE FROM api_keys WHERE user_id = $1"#, id)
sqlx::query!(r#"DELETE FROM api_keys WHERE user_id = $1 OR created_by = $1"#, id)
.execute(&mut *tx)
.await?;
}
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.

2 participants