Skip to content

Commit d1c06cb

Browse files
authored
internal/docs: document UserID for session hijacking prevention (#697)
Update our Session Hijacking mitigation documentation to describe the new TokenInfo.UserID field and how the streamable transport uses it to bind sessions to users. Fixes #571
1 parent 87c8126 commit d1c06cb

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

docs/protocol.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,9 +343,18 @@ If you create your own with
343343
If you are using Go 1.24 or above,
344344
we recommend using [`crypto/rand.Text`](https://pkg.go.dev/crypto/rand#Text)
345345

346-
- _Binding session IDs to user information_. This is an application requirement, out of scope
347-
for the SDK. You can create your own session IDs by setting
348-
[`ServerOptions.GetSessionID`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.GetSessionID).
346+
- _Binding session IDs to user information_. The SDK supports this mitigation through
347+
[`TokenInfo.UserID`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#TokenInfo.UserID).
348+
When a [`TokenVerifier`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#TokenVerifier)
349+
sets `UserID` on the returned `TokenInfo`, the streamable transport will:
350+
1. Store the user ID when a new session is created.
351+
2. Verify that subsequent requests to that session include a token with the same `UserID`.
352+
3. Reject requests with a 403 Forbidden if the user ID doesn't match.
353+
354+
**Recommendation**: If your `TokenVerifier` can extract a user identifier from the token
355+
(such as a `sub` claim in a JWT, or a user ID associated with an API key), set
356+
`TokenInfo.UserID` to enable this protection. This prevents an attacker with a valid
357+
token from hijacking another user's session by guessing or obtaining their session ID.
349358

350359
## Utilities
351360

internal/docs/protocol.src.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,18 @@ If you create your own with
269269
If you are using Go 1.24 or above,
270270
we recommend using [`crypto/rand.Text`](https://pkg.go.dev/crypto/rand#Text)
271271

272-
- _Binding session IDs to user information_. This is an application requirement, out of scope
273-
for the SDK. You can create your own session IDs by setting
274-
[`ServerOptions.GetSessionID`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/mcp#ServerOptions.GetSessionID).
272+
- _Binding session IDs to user information_. The SDK supports this mitigation through
273+
[`TokenInfo.UserID`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#TokenInfo.UserID).
274+
When a [`TokenVerifier`](https://pkg.go.dev/github.com/modelcontextprotocol/go-sdk/auth#TokenVerifier)
275+
sets `UserID` on the returned `TokenInfo`, the streamable transport will:
276+
1. Store the user ID when a new session is created.
277+
2. Verify that subsequent requests to that session include a token with the same `UserID`.
278+
3. Reject requests with a 403 Forbidden if the user ID doesn't match.
279+
280+
**Recommendation**: If your `TokenVerifier` can extract a user identifier from the token
281+
(such as a `sub` claim in a JWT, or a user ID associated with an API key), set
282+
`TokenInfo.UserID` to enable this protection. This prevents an attacker with a valid
283+
token from hijacking another user's session by guessing or obtaining their session ID.
275284

276285
## Utilities
277286

0 commit comments

Comments
 (0)