-
Notifications
You must be signed in to change notification settings - Fork 68
fix: mcp SSE hardening #587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e8b5c63
update deps
arunanshub 690fc03
add origin and host guard
arunanshub 478285f
add test
arunanshub e8f0e1b
host before origin guard
arunanshub ea6da4c
add cli argument
arunanshub 60aec9f
update sse test
arunanshub 3ba62de
add docs
arunanshub 0eeeade
use config and 403
arunanshub e9a824e
allow custom origin servers
arunanshub cef1548
Update docs/mcp.md
arunanshub 3be0339
conditionally allow allow-origin
arunanshub 49570aa
Merge remote-tracking branch 'origin/fix/mcp-sse-hardening' into fix/…
arunanshub 2365aee
use defaults from config itself
arunanshub 43a5f95
Apply suggestions from code review
arunanshub 304d32c
distinguish between nil and empty slice
arunanshub d76d48e
Merge remote-tracking branch 'origin/fix/mcp-sse-hardening' into fix/…
arunanshub File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "slices" | ||
| "strings" | ||
| ) | ||
|
|
||
| var ( | ||
| defaultAllowedOriginsPrefix = []string{ | ||
| "http://localhost:", | ||
| "http://127.0.0.1:", | ||
| "https://localhost:", | ||
| } | ||
| defaultAllowedHosts = []string{"localhost:9988", "127.0.0.1:9988", "[::1]:9988"} | ||
| ) | ||
|
|
||
| // hostGuard is a middleware that allows only the allowed hosts to access the | ||
| // MCP server. nil allowedHosts will use the default allowed hosts. Empty | ||
| // allowedHosts will block all hosts. | ||
| func hostGuard(allowedHosts []string, next http.Handler) http.Handler { | ||
arunanshub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if allowedHosts == nil { | ||
| allowedHosts = defaultAllowedHosts | ||
| } | ||
|
|
||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| // contains is faster than a map lookup for small lists | ||
| if !slices.Contains(allowedHosts, r.Host) { | ||
| // 421 (misdirected request) is ideal; 403 (forbidden) is fine too. | ||
| w.WriteHeader(http.StatusMisdirectedRequest) | ||
arunanshub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return | ||
| } | ||
| next.ServeHTTP(w, r) | ||
| }) | ||
| } | ||
|
|
||
| // originGuard is a middleware that allows only the allowed origins to access | ||
| // the MCP server. nil allowedOriginsPrefix will use the default allowed origins | ||
| // prefix. Empty allowedOriginsPrefix will block all origins. | ||
arunanshub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| func originGuard(allowedOriginsPrefix []string, next http.Handler) http.Handler { | ||
| return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| o := r.Header.Get("Origin") | ||
| if o == "" { | ||
| // Non-browser/same-origin fetches may omit Origin. Don't block | ||
| // solely on this. | ||
| next.ServeHTTP(w, r) | ||
| return | ||
| } | ||
|
|
||
| if allowedOriginsPrefix == nil { | ||
arunanshub marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| allowedOriginsPrefix = defaultAllowedOriginsPrefix | ||
| } | ||
| if !isAllowedOrigin(o, allowedOriginsPrefix) { | ||
| http.Error(w, "forbidden origin", http.StatusForbidden) | ||
arunanshub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return | ||
| } | ||
|
|
||
| next.ServeHTTP(w, r) | ||
| }) | ||
| } | ||
|
|
||
| // isAllowedOrigin checks if the origin is in the allowed origins prefix list. | ||
| func isAllowedOrigin(origin string, allowedOriginsPrefix []string) bool { | ||
abhisek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for _, allowedOriginPrefix := range allowedOriginsPrefix { | ||
| if strings.HasPrefix(origin, allowedOriginPrefix) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.