Skip to content

fix: scope OAuth client admin routes to the caller's tenant - #450

Merged
rsharath merged 2 commits into
highflame-ai:mainfrom
RyoKusnadi:fix/433-tenant-scope-oauth-clients
Oct 21, 2026
Merged

rsharath merged 2 commits into
highflame-ai:mainfrom
RyoKusnadi:fix/433-tenant-scope-oauth-clients

Conversation

@RyoKusnadi

Copy link
Copy Markdown
Contributor

Fixes #433.

A client linked to an identity takes that identity's tenant, but GET /oauth/clients, GET /oauth/clients/{id}, POST .../rotate-secret and DELETE /oauth/clients/{id} acted on any client. A caller in tenant B could rotate the secret of a client linked into tenant A.

Changes

  • When the caller has a tenant in context, these routes only act on unlinked clients or clients whose linked identity is in that tenant.
  • A hidden client is answered like a missing one: 404 on get and rotate-secret, and the existing no-op 200 on delete (the client is left untouched). Tenants can't probe each other's clients.
  • List filters with one batched identity lookup (new IdentityService.GetIdentitiesByIDs).
  • Callers with no tenant in context keep today's behaviour.
  • UpdateClient has no HTTP route, and the RFC 7592 dynamic-client routes authenticate with the per-client registration token, so I left them unchanged.

Tests

New integration test tests/integration/oauth_client_tenant_scope_test.go: tenant B gets 404 on get and rotate for a client linked into tenant A, is left out of list, and delete leaves the client in place. An unlinked client stays global. The full integration suite and the handler and service unit tests pass.

A client linked to an identity takes that identity's tenant, but get, list,
rotate-secret and delete acted on any client. When the caller has a tenant in
context, these routes now only act on unlinked clients or clients whose linked
identity is in that tenant; others answer like a missing client. Callers with
no tenant in context keep today's behaviour.

Fixes #433
@rsharath

Copy link
Copy Markdown
Contributor

@RyoKusnadi could you fix one more case in this PR?

Deleting an identity makes its linked clients global. oauth_clients.identity_id is ON DELETE SET NULL (migration 018). When an identity is deleted, its linked clients become unlinked. Under this PR's rule, unlinked means global, so any tenant can then get, rotate the secret of, or delete a client that belonged to tenant A. That's the #433 gap again, reached through identity deletion.

Expected: deleting an identity removes its linked OAuth clients, or at least stops them being reachable from other tenants.

Points to weigh:

  • SET NULL was deliberate. The comment in migration 018 says it keeps in-flight refresh tokens from disappearing and expects them to fail with identity_expired. But once the link is null, the client falls back to the "no identity gate" behaviour, so that expectation may not hold. Please check what a refresh on a client whose identity was deleted actually does.
  • ON DELETE CASCADE is the smallest fix (new migration). But it skips OAuthClientService.DeleteClient's ChannelTrusted check. Either confirm that identity deletion is already gated the same way, or delete the linked clients in the identity delete path instead.
  • Test: link a client to an identity in tenant A, delete the identity, then check that tenant B can't get or rotate the client, and that the client is gone (or still scoped to A).

@rsharath
rsharath requested a review from saucam October 21, 2026 04:37
oauth_clients.identity_id was ON DELETE SET NULL, so a hard delete of an
identity turned its linked clients into unlinked ones, which the tenant
scope rule treats as global. Make the foreign key ON DELETE CASCADE so
the clients go with the identity.

Signed-off-by: Ryo Kusnadi <ryokusnadi@gmail.com>
@RyoKusnadi

Copy link
Copy Markdown
Contributor Author

Thanks @rsharath, pushed 237bd1c.

Migration 061 changes oauth_clients.identity_id to ON DELETE CASCADE, so a hard delete of an identity removes its linked clients.

What I checked:

  • DELETE /identities/{id} is a soft delete (DeactivateIdentity), which keeps the row and the link, so it isn't affected. The hard delete is PurgeIdentity (the rollback in RegisterAgent) or a direct row delete, and both reach the SET NULL case.
  • You were right about refresh: oauth.go only gates on the identity when client.IdentityID is set, so with the link nulled a refresh skips the check instead of failing with identity_expired. With the cascade the client is gone, so the exchange fails.
  • I went with the migration instead of deleting clients in the identity delete path because it also covers deletes made directly in the database. It skips the ChannelTrusted check in DeleteClient, but PurgeIdentity and direct deletes never went through that check either.

Test: TestOAuthClient_HardDeletingIdentityRemovesLinkedClient links a client to an identity in tenant A and deletes the identity. The client is then 404 on get for both tenants, rotate-secret is 404 for tenant B, it is gone from B's list, and an unlinked client is untouched. The full integration suite passes (1476 tests).

@rsharath
rsharath merged commit e7866be into highflame-ai:main Oct 21, 2026
11 checks passed
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.

OAuth client admin routes are not tenant-scoped, and an identity_id link makes that matter

2 participants