Fix: createSession method always creates a session, even if authentication fails (Issue #186) #188
+468
−7
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Authentication Fix Documentation
Date: 2025-10-06
Status: Ready to submit
Tested: ✅ Fixes proven to work in fork
Issue 1: FastMCP - Session Created Despite Authentication Failure
Repository: https://github.com/modelcontextprotocol/fastmcp
Severity: High - Security vulnerability
Affects: HTTP Stream transport with OAuth/JWT authentication
Problem Statement
FastMCP's
#createSession
method always creates a session even when the authentication callback returns{ authenticated: false }
. This allows unauthenticated clients to establish sessions and potentially access protected resources.Current Behavior (Bug)
File:
FastMCP.js
(line ~1227)Problem: The method creates a
FastMCPSession
regardless ofauth.authenticated
status.Expected Behavior
When the authenticate callback returns
{ authenticated: false, error: "..." }
, FastMCP should reject the request instead of creating a session.Steps to Reproduce
The Fix
File:
FastMCP.js
(line ~1227)Key Changes
authenticated
property: Validatesauth.authenticated === false
auth.error
if availableSecurity Impact
Before Fix:
After Fix:
Test Results
Tested with:
[email protected]
,[email protected]
Before fix:
After fix:
Related Issues
This fix works in conjunction with mcp-proxy authentication handling (see companion issue).
TypeScript Source Location
If this code originates from TypeScript source files, the fix should be applied to the source
.ts
file in the same location where#createSession
is defined.Likely source file:
src/FastMCP.ts
or similarMethod signature to locate:
Suggested Unit Test
Problem: FastMCP returns
{ authenticated: false, error: "..." }
which is a truthy object, so the checkif (!authResult)
evaluates to false and authentication proceeds.Current Behavior (Bug #2)
File:
src/startHTTPServer.ts
(lines ~200-210)Problem: When FastMCP throws authentication errors, they're caught here and returned as HTTP 500 instead of HTTP 401.
The Fix - Part 1: Stateless Authentication Check
File:
src/startHTTPServer.ts
(lines ~137-163)The Fix - Part 2: createServer Catch Block
File:
src/startHTTPServer.ts
(lines ~200-210)Key Changes
Fix #1 - Stateless Auth Check:
authResult.authenticated === false
(not just falsy)authResult.error
Fix #2 - createServer Catch:
Steps to Reproduce
Security Impact
Before Fix:
After Fix:
Test Results
Tested with:
[email protected]
,[email protected]
Before fixes:
After fixes:
Type Safety Improvement (Optional)
The authenticate callback type should be updated to make the contract explicit:
Current:
Suggested:
Related Issues
This fix works in conjunction with FastMCP's
#createSession
authentication check (see companion issue).Testing Both Fixes Together
Test Setup
Test Results
Implementation Notes
For FastMCP Maintainers
#createSession
method (likely insrc/FastMCP.ts
)authenticated: false
resultsFor mcp-proxy Maintainers
handleStreamRequest
stateless auth check (likely insrc/startHTTPServer.ts
)createServer
catch block (same file)File to modify:
src/startHTTPServer.ts
Function to locate:
handleStreamRequest
(async function, ~line 137-210)Search for:
Suggested Unit Tests
After Fix: Can use proper API
Breaking Changes
None - These fixes only affect failed authentication paths. Successful authentication behavior is unchanged.
Rollout Recommendation
authenticated: false
Summary
Both fixes are required for proper OAuth/JWT authentication:
authenticated: false
and reject session creationWithout both fixes, unauthenticated clients can bypass authentication and establish sessions, creating a security vulnerability in production systems using OAuth 2.1 / JWT authentication with FastMCP.
Submitted by: MCP-OAuth Framework Team
Contact: [Your contact information]
Related Project: https://github.com/[your-repo]/mcp-oauth