Skip to content

[SignalR] Bind connection tokens to their endpoint mapping - #69279

Merged
BrennanConroy merged 4 commits into
mainfrom
javiercn/signalr-endpoint-token-binding
Oct 14, 2026
Merged

BrennanConroy merged 4 commits into
mainfrom
javiercn/signalr-endpoint-token-binding

Conversation

@javiercn

@javiercn javiercn commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

Fixes #69090

Summary

  • assign each MapConnections registration an opaque internal mapping identity shared by its negotiate, authentication-refresh, and transport endpoints
  • attach that identity to all endpoints from the mapping as endpoint metadata and retrieve it from the selected endpoint during each request
  • store the identity on the connection created during negotiation and enforce it centrally in HttpConnectionManager before token-consuming paths diverge
  • reject a connection token presented to a different mapping while preserving normal use on the mapping that negotiated it

Design

MapConnections creates one private HttpConnectionEndpointMetadata marker and adds the same instance to the metadata for its negotiate, optional authentication-refresh, and transport endpoints. The instance must remain unique to that MapConnections invocation and must not be cached or reused globally. The dispatcher reads the marker from HttpContext.GetEndpoint() rather than receiving it through captured handler parameters. The negotiated connection retains that object reference, and manager lookup succeeds only when the consuming endpoint supplies the same identity.

The marker is intentionally data-free: its object identity represents the endpoint group. It is not derived from route text, and the separate routing Endpoint instances cannot be compared directly because negotiate, refresh, and transport are distinct endpoints. No public API is added.

Enforcement covers the shared execute entrypoint and the token-consuming send, poll, delete, Server-Sent Events, WebSockets/reconnect, and authentication-refresh paths. Direct WebSocket connections without negotiation are created with the current mapping identity as well.

Tests

The issue was faithfully reproduced before the fix with two mapped endpoints:

  1. negotiate on endpoint A with CloseOnAuthenticationExpiration = false
  2. present A's token to endpoint B, configured with CloseOnAuthenticationExpiration = true
  3. observe the previous cross-endpoint request succeed with HTTP 200
  4. verify the fix rejects B with HTTP 404, leaves A's original authentication-expiration behavior authoritative, and allows normal token use on A

The final coverage follows the existing dispatcher test infrastructure without TestHost:

  • normal attachment rejects a negotiated token on the wrong mapping with HTTP 404 and still allows it on the originating mapping
  • WebSocket attachment through GetOrCreateConnectionAsync rejects a negotiated token on the wrong mapping with HTTP 404 while the originating mapping retains ownership
  • enabled POST /refresh rejects a negotiated token on the wrong mapping with HTTP 404 and connection_not_found, while refresh remains successful on the originating mapping
  • mapping tests verify negotiate, refresh, and transport endpoints from one MapConnections registration share the same metadata instance, while separate registrations receive different instances

Removing only the centralized reference-identity comparison makes all three dispatcher cases fail with expected 404, actual 200; all pass with enforcement restored.

Microsoft.AspNetCore.Http.Connections.Tests: 288 passed, 0 failed, 0 skipped.

Compatibility and deployment considerations

  • Compatibility: Same-mapping connections and protocol payloads are unchanged. The intentional behavior change is that tokens can no longer be reused across separate MapConnections registrations.
  • Scale-out: Connections remain owned by the existing process-local HttpConnectionManager; deployments still require the same routing affinity/stickiness. This change adds no distributed identity or serialization contract.
  • Long polling: Every poll/send/delete request resolves the same metadata identity from the selected mapping, so normal multi-request long-polling behavior is preserved.
  • Public API: No public or protected API surface changes.

@github-actions github-actions Bot added the area-signalr Includes: SignalR clients and servers label Sep 14, 2026
@javiercn
javiercn marked this pull request as ready for review September 14, 2026 16:53
Copilot AI lite review requested due to automatic review settings September 14, 2026 16:53

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.

🟢 Approval recommended

No unresolved review comments remain, and coverage passes.

Pull request overview

Binds SignalR connection tokens to their originating MapConnections registration, preventing cross-endpoint reuse.

Changes:

  • Adds opaque per-mapping endpoint identity metadata.
  • Enforces identity matching across token-consuming paths.
  • Adds integration coverage for HTTP, WebSocket, and refresh scenarios.
File summaries
File Summary
src/SignalR/common/Http.Connections/test/Microsoft.AspNetCore.Http.Connections.Tests.csproj Adds the TestHost dependency.
src/SignalR/common/Http.Connections/test/MapConnectionHandlerTests.cs Tests endpoint isolation and same-mapping behavior.
src/SignalR/common/Http.Connections/src/Internal/HttpConnectionManager.cs Stores and validates mapping identity.
src/SignalR/common/Http.Connections/src/Internal/HttpConnectionEndpoint.cs Defines the internal mapping marker.
src/SignalR/common/Http.Connections/src/Internal/HttpConnectionDispatcher.cs Propagates and enforces endpoint identity.
src/SignalR/common/Http.Connections/src/Internal/HttpConnectionContext.cs Stores the originating mapping identity.
src/SignalR/common/Http.Connections/src/ConnectionEndpointRouteBuilderExtensions.cs Applies shared identity metadata to mapped endpoints.
Review details
  • Files reviewed: 7/7 changed files
  • Comments generated: 0
  • Review effort level: Lite

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

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime.
To make sure no conflicting changes have occurred, please rerun validation before merging. You can do this by leaving an /azp run comment here (requires commit rights), or by simply closing and reopening.

@dotnet-policy-service dotnet-policy-service Bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 6, 2026
@BrennanConroy

Copy link
Copy Markdown
Member

/azp run

@dotnet-policy-service dotnet-policy-service Bot removed the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Oct 13, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
2 pipeline(s) were filtered out due to trigger conditions.

@BrennanConroy
BrennanConroy merged commit 06b1452 into main Oct 14, 2026
36 checks passed
@BrennanConroy
BrennanConroy deleted the javiercn/signalr-endpoint-token-binding branch October 14, 2026 18:48
@BrennanConroy

Copy link
Copy Markdown
Member

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

✅ No documentation PR was created.

Confidence that documentation is needed: 15%

No docs needed: internal fix binding connection tokens to their MapConnections mapping, with no public API or configuration changes.

Documentation surfaces considered

  • Conceptual article: Not required — No public API, option, or documented behavior changed; the marker is internal and signals.json reported no_signal.
  • Migration guidance: Not required — No upgrade action is needed for normal use; same-mapping behavior and protocol payloads are unchanged.
  • Breaking change: Not required — Only unsupported cross-MapConnections token reuse is now rejected; the PR doesn't present this as a documented compatibility break. Human confirmation is optional.

This branch was previously deployed

1 inactive deployment
copilot-pat-pool — 27e4583c Deployed Oct 14, 2026 by BrennanConroy via conclusion #40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-signalr Includes: SignalR clients and servers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SignalR connection tokens are not bound to the endpoint that negotiated them

3 participants