Conversation
…OST /message Co-authored-by: cfc4n <709947+cfc4n@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix security vulnerability: /message accepts text/plain bypassing CORS
security: enforce application/json Content-Type on /message to close CORS preflight bypass
Mar 15, 2026
Member
|
fix #41 |
There was a problem hiding this comment.
Pull request overview
This PR adds a requireJSONContentType middleware to reject POST requests with non-application/json Content-Types, closing a CORS preflight bypass vulnerability where browsers would send text/plain requests directly without preflight checks.
Changes:
- Added
requireJSONContentTypemiddleware that returns 415 for POST requests withoutapplication/jsonContent-Type - Modified
Serve()to wrap the SSE server with this middleware viaWithHTTPServer - Added unit tests covering GET passthrough, valid JSON content types, and rejected simple content types
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| pkg/server/server.go | Added requireJSONContentType middleware and wired it into the SSE server startup |
| pkg/server/server_test.go | Added table-driven tests for the middleware |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Comment on lines
+135
to
+138
| httpSrv := &http.Server{Addr: m.listenAddr} | ||
| sseServer := server.NewSSEServer(m.server, server.WithBaseURL(ltnAddr), server.WithHTTPServer(httpSrv)) | ||
| httpSrv.Handler = requireJSONContentType(sseServer) | ||
| return sseServer.Start(m.listenAddr) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
/messageaccepted anyContent-Typeand parsed the body as JSON regardless. Because browsers exempttext/plain,application/x-www-form-urlencoded, andmultipart/form-datafrom CORS preflight, a cross-origin attacker could execute arbitrary MCP commands by posting withContent-Type: text/plain; mode: no-cors.Changes
pkg/server/server.go—requireJSONContentTypemiddlewarePOSTrequest whose media type (parameters stripped) is notapplication/jsonwith415 Unsupported Media Type.pkg/server/server.go—Serve()*http.ServerwithrequireJSONContentTypewrapping the mcp-goSSEServer, injected viaserver.WithHTTPServer. This intercepts requests before the upstream library handler sees them.pkg/server/server_test.go—TestRequireJSONContentTypeapplication/json± charset (pass),text/plain/application/x-www-form-urlencoded/multipart/form-data/ empty (all 415).Original prompt
This section details on the original issue you should resolve
<issue_title>Security Vulnerability:/message accepts text/plain (bypassing CORS preflight)</issue_title>
<issue_description>This vulnerability is found by Songwu security researcher,Zeyu Luo security researcher, Dr. CAO Yinfeng, Kevin(The Hong Kong Polytechnic University / HKCT Institute of Higher Education)
vulnerability description
Browsers implement a CORS preflight mechanism: when JavaScript sends a non-simple request, such as a POST with Content-Type: application/json, the browser first sends an OPTIONS preflight request. If the server does not return valid CORS headers, the browser blocks the request outright, and the actual request is never sent.
However, three Content-Type values are classified as “simple requests” and therefore do not trigger a preflight check. The browser sends them directly: application/x-www-form-urlencoded, multipart/form-data, and text/plain.
Moling’s /message endpoint does not validate the Content-Type. When it receives a text/plain body, it directly parses and executes it as JSON. This allows an attacker to use text/plain to bypass the CORS preflight mechanism and send arbitrary MCP commands from the browser.
POC