fix(auth): revoking, rotating or deleting a key ends its open gRPC streams (SEC-004)

A streaming call was authenticated only at open, and revoke/rotate/delete only
evicted the verification cache, so a leaked key's open StreamEvents/StreamAlarms
kept delivering after the revoke. The interceptor now registers every streaming
call by key id in ApiKeyStreamRegistry and gives the handler a call context
whose token also fires on revoke; the dashboard key actions call Revoke, and the
call ends with Unauthenticated.
This commit is contained in:
Joseph Doherty
2026-12-03 09:55:43 -05:00
parent 824107a56b
commit 835ed0bd04
11 changed files with 481 additions and 10 deletions
+6 -1
View File
@@ -104,7 +104,12 @@ library:
it after writing the cache entry (set-then-recheck); a revoke that lands while a
verification is still in flight in the inner library therefore discards that
verification's repopulation instead of re-caching the just-revoked identity for a
full TTL (SEC-34). The short TTL remains the backstop for two bounded-staleness
full TTL (SEC-34). The same revoke/rotate/delete also calls
`ApiKeyStreamRegistry.Revoke(keyId)`, which ends every streaming call the key
already has open (SEC-004): a streaming call is authenticated once, at open, so
evicting the cache alone left an open `StreamEvents`, `StreamAlarms` or
`WatchDeployEvents` delivering data until the client disconnected. See
[Authorization](./Authorization.md#streaming-calls-and-revocation). The short TTL remains the backstop for two bounded-staleness
windows it cannot close directly: (1) out-of-band mutations (a direct DB edit, or a
revoke run by the separate `apikey` CLI process, whose in-memory cache is not the
running gateway's cache); and (2) a key whose `ExpiresUtc` passes while cached keeps
+20
View File
@@ -105,6 +105,26 @@ Two rules keep the holder of the correct secret from being denied by a spray aga
The dashboard login surface is throttled independently: `POST /auth/login` carries a fixed-window ASP.NET Core rate-limiter policy keyed per remote IP (`MxGateway:Security:LoginRateLimit*`), rejecting a burst with HTTP 429 before the LDAP bind is relayed to the directory. See [GatewayConfiguration](./GatewayConfiguration.md#security-options).
### Streaming calls and revocation
A streaming call is authenticated once, when it opens. To keep a revoked key from
holding a stream open indefinitely (SEC-004), the interceptor registers every
streaming call (server, client and duplex) in `ApiKeyStreamRegistry` under the
caller's key id, and hands the handler a `RevocableServerCallContext`: the real call
context with a cancellation token that also fires when the key is revoked. The
dashboard API Keys page calls `ApiKeyStreamRegistry.Revoke(keyId)` on Revoke, Rotate
and Delete, right after it evicts the verification cache. The handler stops on its
token as it would for a client cancel, and the interceptor reports the end of the
call as `Unauthenticated` — the status a new call with that key now gets — whatever
status the handler mapped its cancellation to. Rotate ends the key's streams too,
since they were opened with the secret the rotation retires; the client reconnects
with the new secret. Unary calls are not tracked: they complete within the command
timeout.
Only revocations made inside the gateway process reach the registry. A key revoked
by the separate `apikey` CLI process keeps its open streams until they reconnect,
the same out-of-band window its verification-cache entry has.
## Scope Resolution
`GatewayGrpcScopeResolver` is a stateless singleton that switches on the runtime request type. Top-level RPC requests map directly:
+3
View File
@@ -729,6 +729,9 @@ anonymous localhost session shows the same read-only view.
old one. Active keys only — rotating a revoked key would un-revoke it, so
the button is not shown on revoked rows.
- **Revoke** marks a key revoked; a revoked key cannot be un-revoked.
Revoke, Rotate and Delete also end every streaming gRPC call the key
already has open (`ApiKeyStreamRegistry`, SEC-004); the client sees
`Unauthenticated`.
- **Delete** permanently removes a key row from the auth database, but only
when the key is already revoked. `IApiKeyAdminStore.DeleteAsync` rejects
active keys (returns false) so the revoke event lands in the audit log