parent
35f1b07e48
commit
41e1f941bb
1 changed files with 41 additions and 118 deletions
|
|
@ -180,9 +180,15 @@ type StreamableHTTPOptions struct {
|
|||
// CrossOriginProtection allows to customize cross-origin protection.
|
||||
// The deny handler set in the CrossOriginProtection through SetDenyHandler
|
||||
// is ignored.
|
||||
// If nil, default (zero-value) cross-origin protection will be used.
|
||||
// Use `disablecrossoriginprotection` MCPGODEBUG compatibility parameter
|
||||
// to disable the default protection until v1.7.0.
|
||||
// If nil, no cross-origin protection is applied. Use the `enableoriginverification`
|
||||
// MCPGODEBUG compatibility parameter to enable the default protection until v1.8.0.
|
||||
//
|
||||
// Deprecated: wrap the handler with cross-origin protection middleware
|
||||
// instead. For example:
|
||||
//
|
||||
// handler := mcp.NewStreamableHTTPHandler(...)
|
||||
// protection := http.NewCrossOriginProtection()
|
||||
// protectedHandler := protection.Handler(handler)
|
||||
CrossOriginProtection *http.CrossOriginProtection
|
||||
}
|
||||
|
||||
|
|
@ -202,7 +208,7 @@ func NewStreamableHTTPHandler(getServer func(*http.Request) *Server, opts *Strea
|
|||
|
||||
h.opts.Logger = ensureLogger(h.opts.Logger)
|
||||
|
||||
if h.opts.CrossOriginProtection == nil {
|
||||
if h.opts.CrossOriginProtection == nil && enableoriginverification == "1" {
|
||||
h.opts.CrossOriginProtection = &http.CrossOriginProtection{}
|
||||
}
|
||||
|
||||
|
|
@ -235,15 +241,16 @@ func (h *StreamableHTTPHandler) closeAll() {
|
|||
// disablelocalhostprotection is a compatibility parameter that allows to disable
|
||||
// DNS rebinding protection, which was added in the 1.4.0 version of the SDK.
|
||||
// See the documentation for the mcpgodebug package for instructions how to enable it.
|
||||
// The option will be removed in the 1.7.0 version of the SDK.
|
||||
// The option will be removed in the 1.6.0 version of the SDK.
|
||||
var disablelocalhostprotection = mcpgodebug.Value("disablelocalhostprotection")
|
||||
|
||||
// disablecrossoriginprotection is a compatibility parameter that allows to disable
|
||||
// the verification of the 'Origin' and 'Content-Type' headers, which was added in
|
||||
// the 1.4.1 version of the SDK. See the documentation for the mcpgodebug package
|
||||
// for instructions how to enable it.
|
||||
// The option will be removed in the 1.7.0 version of the SDK.
|
||||
var disablecrossoriginprotection = mcpgodebug.Value("disablecrossoriginprotection")
|
||||
// enableoriginverification is a compatibility parameter that restores the
|
||||
// default cross-origin protection behavior from v1.4.1-v1.5.0. When set to
|
||||
// "1", a zero-value CrossOriginProtection will be applied if none is
|
||||
// explicitly provided in StreamableHTTPOptions.
|
||||
// See the documentation for the mcpgodebug package for instructions how to enable it.
|
||||
// The option will be removed in the 1.8.0 version of the SDK.
|
||||
var enableoriginverification = mcpgodebug.Value("enableoriginverification")
|
||||
|
||||
func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
// DNS rebinding protection: auto-enabled for localhost servers.
|
||||
|
|
@ -257,17 +264,18 @@ func (h *StreamableHTTPHandler) ServeHTTP(w http.ResponseWriter, req *http.Reque
|
|||
}
|
||||
}
|
||||
|
||||
if disablecrossoriginprotection != "1" {
|
||||
if h.opts.CrossOriginProtection != nil {
|
||||
// Verify the 'Origin' header to protect against CSRF attacks.
|
||||
if err := h.opts.CrossOriginProtection.Check(req); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusForbidden)
|
||||
return
|
||||
}
|
||||
// Validate 'Content-Type' header.
|
||||
if req.Method == http.MethodPost && baseMediaType(req.Header.Get("Content-Type")) != "application/json" {
|
||||
http.Error(w, "Content-Type must be 'application/json'", http.StatusUnsupportedMediaType)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Validate 'Content-Type' header.
|
||||
if req.Method == http.MethodPost && baseMediaType(req.Header.Get("Content-Type")) != "application/json" {
|
||||
http.Error(w, "Content-Type must be 'application/json'", http.StatusUnsupportedMediaType)
|
||||
return
|
||||
}
|
||||
|
||||
// Allow multiple 'Accept' headers.
|
||||
|
|
|
|||
Loading…
Reference in a new issue