Fix: createSession method always creates a session, even if authentication fails (Issue #186) #38
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/punkpeye/mcp-proxy
Severity: High - Security vulnerability
Affects: HTTP Stream transport with OAuth/JWT authentication
Problem Statement
FastMCP's
#createSessionmethod 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:
dist/FastMCP.js(line ~1227)Problem: The method creates a
FastMCPSessionregardless ofauth.authenticatedstatus.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:
dist/FastMCP.js(line ~1227)Key Changes
authenticatedproperty: Validatesauth.authenticated === falseauth.errorif 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
.tsfile in the same location where#createSessionis defined.Likely source file:
src/FastMCP.tsor 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.errorFix #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
#createSessionauthentication check (see companion issue).Testing Both Fixes Together
Test Setup
Test Results
Implementation Notes
For FastMCP Maintainers
#createSessionmethod (likely insrc/FastMCP.ts)authenticated: falseresultsFor mcp-proxy Maintainers
handleStreamRequeststateless auth check (likely insrc/startHTTPServer.ts)createServercatch block (same file)File to modify:
src/startHTTPServer.tsFunction 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: falseSummary
Both fixes are required for proper OAuth/JWT authentication:
authenticated: falseand 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.