Skip to content

feat: close hub connections when their session or API token is revoked - #407

Open
hhvrc wants to merge 1 commit into
developfrom
feature/hub-connection-revocation
Open

hhvrc wants to merge 1 commit into
developfrom
feature/hub-connection-revocation

Conversation

@hhvrc

@hhvrc hhvrc commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

What does this change?

SignalR authenticates a connection once, when it opens. A logged out session, or a revoked, expired or deactivated API token, could therefore keep using a UserHub connection that was already open, including sending control commands.

  • Revocations close connections right away. They are announced on Redis, and the API instance holding an affected connection aborts it:
    • a deleted session (logout, session delete, password reset, deactivation) closes that session's connections;
    • a session that expires by its Redis TTL does the same, through the key expiry events the API already listens to;
    • a deactivated or deleted account closes every connection of that user, API token ones included;
    • a deleted API token closes its connections.
  • An edited API token is re-read. Its connections read it again on their next call, so the hub uses the current permissions, pause state and control limits rather than the ones from connect time. This reuses the existing api-token-update:{id} channel.
  • Fallback check. A hub filter re-checks a connection against the database when it opens, and at most once per minute while it is making calls (HubRevalidationOptions.Interval). This covers a lost Redis message and tokens that reach ValidUntil, which nothing announces. An idle connection is never queried.

New pieces: HubConnectionTracker (per-instance registry of open authenticated hub connections), RevalidateAuthenticationHubFilter, and the session-revoked / user-auth-revoked Redis channels. SessionService publishes on every delete.

This touches authentication and device control (the SignalR control path).

Not covered here:

  • The LiveControlGateway's websockets are not closed on logout or user deletion. For API tokens the gateway already denies control when a token is revoked.
  • Changes made directly in the database (cron cleanup jobs, manual SQL) are not announced and only take effect at the fallback check.

How was it tested?

New integration tests in API.IntegrationTests/Tests/SignalRUserHubTests.cs, using real WebSocket hub connections against the test server:

  • Closed without the client calling anything: Logout_ClosesTheSessionsOpenConnection, DeletingAnApiToken_ClosesItsOpenConnection, DeactivatingAnAccount_ClosesItsSessionAndApiTokenConnections. The first two also check that another connection stays open.
  • Fallback check: Invoke_AfterSessionDeleted_FailsAndClosesConnection, Invoke_AfterAccountDeactivated_WithTheSessionLeftBehind_FailsAndClosesConnection, Invoke_AfterApiTokenDeleted_FailsAndClosesConnection, Invoke_AfterApiTokenExpired_FailsAndClosesConnection, Invoke_AfterApiTokenOwnerDeactivated_FailsAndClosesConnection.
  • The check is not run on every call: Invoke_ManyCallsInARow_ChecksTheApiTokenOncePerInterval.
  • Still works when valid: Invoke_WithValidSession_KeepsWorking, Invoke_WithValidApiToken_KeepsWorking.

API integration tests pass locally on this branch (240/240). Not tested against a multi-instance deployment or in a browser.

Checklist

  • The change is focused on one thing.
  • Tests, formatter and linter pass locally.
  • Documentation is updated where behaviour changed.
  • No secrets, credentials or personal data are included.
  • If this touches device control, limits, sharing, authentication or firmware updates, the description says so and explains how it was tested.

SignalR authenticates a connection once, when it opens, so a logged out session or a revoked, expired or deactivated API token could keep using a connection that was already open.

Revocations are now announced on Redis and the instance holding an affected UserHub connection closes it right away: deleted and expired sessions, deactivated or deleted accounts, and deleted API tokens. An edited token makes its connections read it again on their next call, so the hub uses its current permissions, pause state and control limits instead of the ones from connect time.

As a fallback for a lost message or a token that reaches its expiry, a hub filter re-checks a connection against the database when it opens and at most once per minute while it is making calls.
@hhvrc
hhvrc requested a review from LucHeart as a code owner November 26, 2026 21:12
@coderabbitai

coderabbitai Bot commented Nov 26, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 53 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: d8b02fb7-0c1a-4df4-a92d-e9ddb2acad90
Commits

Reviewing files that changed from the base of the PR and between 08b1f5e and bfa72d5.

Files selected for processing: 12

  • API.IntegrationTests/Tests/SignalRUserHubTests.cs
  • API.IntegrationTests/WebApplicationFactory.cs
  • API/Realtime/RedisSubscriberService.cs
  • API/Services/Account/AccountService.cs
  • Common/Hubs/HubConnectionTracker.cs
  • Common/Hubs/RevalidateAuthenticationHubFilter.cs
  • Common/Hubs/UserHub.cs
  • Common/OpenShockServiceHelper.cs
  • Common/Services/RedisPubSub/IRedisPubService.cs
  • Common/Services/RedisPubSub/RedisChannels.cs
  • Common/Services/RedisPubSub/RedisPubService.cs
  • Common/Services/Session/SessionService.cs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

This branch has not been deployed

No deployments
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