@@ -304,13 +304,30 @@ async def test_cors_preflight_malicious_origin_blocked(self, middleware_test_ser
304304 async def test_cors_actual_request_no_cors_headers (self , middleware_test_server ):
305305 """Test actual request without Origin header (should work - not CORS)."""
306306 async with aiohttp .ClientSession () as session :
307+ # First, initiate session with the MCP server
307308 async with session .post (
308309 "http://127.0.0.1:8005/mcp/" ,
309- json = {"jsonrpc" : "2.0" , "id" : 1 , "method" : "tools/list" },
310+ json = {"jsonrpc" : "2.0" , "id" : 1 , "method" : "initialize" , "params" : { "protocolVersion" : "2024-11-05" , "capabilities" : {}} },
310311 headers = {
311312 "Accept" : "application/json, text/event-stream" ,
312313 "Content-Type" : "application/json" ,
313314 },
315+ ) as init_response :
316+ # Get session ID from response headers
317+ session_id = init_response .headers .get ("mcp-session-id" )
318+
319+ # Now make the actual test request with session ID
320+ headers = {
321+ "Accept" : "application/json, text/event-stream" ,
322+ "Content-Type" : "application/json" ,
323+ }
324+ if session_id :
325+ headers ["mcp-session-id" ] = session_id
326+
327+ async with session .post (
328+ "http://127.0.0.1:8005/mcp/" ,
329+ json = {"jsonrpc" : "2.0" , "id" : 1 , "method" : "tools/list" },
330+ headers = headers ,
314331 ) as response :
315332 # The server will fail to authenticate with dummy credentials, but that's ok
316333 # We just want to test that the middleware allows the request through
@@ -321,14 +338,30 @@ async def test_cors_actual_request_no_cors_headers(self, middleware_test_server)
321338 async def test_cors_actual_request_with_origin_blocked (self , middleware_test_server ):
322339 """Test actual CORS request with origin header (should work but no CORS headers)."""
323340 async with aiohttp .ClientSession () as session :
341+ # First, initiate session with the MCP server
324342 async with session .post (
325343 "http://127.0.0.1:8005/mcp/" ,
326- json = {"jsonrpc" : "2.0" , "id" : 1 , "method" : "tools/list" },
344+ json = {"jsonrpc" : "2.0" , "id" : 1 , "method" : "initialize" , "params" : { "protocolVersion" : "2024-11-05" , "capabilities" : {}} },
327345 headers = {
328346 "Accept" : "application/json, text/event-stream" ,
329347 "Content-Type" : "application/json" ,
330- "Origin" : "http://localhost:3000" ,
331348 },
349+ ) as init_response :
350+ session_id = init_response .headers .get ("mcp-session-id" )
351+
352+ # Now make the actual test request with session ID and Origin header
353+ headers = {
354+ "Accept" : "application/json, text/event-stream" ,
355+ "Content-Type" : "application/json" ,
356+ "Origin" : "http://localhost:3000" ,
357+ }
358+ if session_id :
359+ headers ["mcp-session-id" ] = session_id
360+
361+ async with session .post (
362+ "http://127.0.0.1:8005/mcp/" ,
363+ json = {"jsonrpc" : "2.0" , "id" : 1 , "method" : "tools/list" },
364+ headers = headers ,
332365 ) as response :
333366 # Request should still work (server processes it) but no CORS headers
334367 assert response .status in [200 , 401 , 500 ] # Any non-CORS error is fine
@@ -340,15 +373,30 @@ async def test_cors_actual_request_with_origin_blocked(self, middleware_test_ser
340373 async def test_dns_rebinding_protection_trusted_hosts (self , middleware_test_server ):
341374 """Test DNS rebinding protection with TrustedHostMiddleware - allowed hosts."""
342375 async with aiohttp .ClientSession () as session :
343- # Test with localhost - should be allowed (in default allowed_hosts)
376+ # First, initiate session with the MCP server
344377 async with session .post (
345378 "http://127.0.0.1:8005/mcp/" ,
346- json = {"jsonrpc" : "2.0" , "id" : 1 , "method" : "tools/list" },
379+ json = {"jsonrpc" : "2.0" , "id" : 1 , "method" : "initialize" , "params" : { "protocolVersion" : "2024-11-05" , "capabilities" : {}} },
347380 headers = {
348381 "Accept" : "application/json, text/event-stream" ,
349382 "Content-Type" : "application/json" ,
350- "Host" : "localhost:8005" ,
351383 },
384+ ) as init_response :
385+ session_id = init_response .headers .get ("mcp-session-id" )
386+
387+ # Test with localhost - should be allowed (in default allowed_hosts)
388+ headers = {
389+ "Accept" : "application/json, text/event-stream" ,
390+ "Content-Type" : "application/json" ,
391+ "Host" : "localhost:8005" ,
392+ }
393+ if session_id :
394+ headers ["mcp-session-id" ] = session_id
395+
396+ async with session .post (
397+ "http://127.0.0.1:8005/mcp/" ,
398+ json = {"jsonrpc" : "2.0" , "id" : 1 , "method" : "tools/list" },
399+ headers = headers ,
352400 ) as response :
353401 print (f"Trusted host (localhost) response status: { response .status } " )
354402 print (f"Trusted host (localhost) response headers: { dict (response .headers )} " )
0 commit comments