2026-08-19 20:04:13 +02:00
|
|
|
package httpmw
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
2026-08-19 21:25:18 +02:00
|
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
2026-08-19 20:04:13 +02:00
|
|
|
"github.com/coder/coder/v2/coderd/httpapi"
|
2026-08-19 21:25:18 +02:00
|
|
|
"github.com/coder/coder/v2/coderd/rbac"
|
|
|
|
|
"github.com/coder/coder/v2/coderd/rbac/policy"
|
2026-08-19 20:04:13 +02:00
|
|
|
"github.com/coder/coder/v2/codersdk"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type mcpServerConfigParamContextKey struct{}
|
|
|
|
|
|
|
|
|
|
// MCPServerConfigParam returns the MCP server config from the
|
|
|
|
|
// ExtractMCPServerConfigParam handler.
|
|
|
|
|
func MCPServerConfigParam(r *http.Request) database.MCPServerConfig {
|
|
|
|
|
config, ok := r.Context().Value(mcpServerConfigParamContextKey{}).(database.MCPServerConfig)
|
|
|
|
|
if !ok {
|
|
|
|
|
panic("developer error: mcp server config param middleware not provided")
|
|
|
|
|
}
|
|
|
|
|
return config
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ExtractMCPServerConfigParam reads the "mcpserverconfig" URL parameter.
|
2026-08-19 21:25:18 +02:00
|
|
|
// Callers with none of the admitted actions are concealed as not found, so
|
|
|
|
|
// denied and missing rows both return 404.
|
|
|
|
|
func ExtractMCPServerConfigParam(
|
|
|
|
|
db database.Store,
|
|
|
|
|
auth func(r *http.Request, action policy.Action, object rbac.Objecter) bool,
|
|
|
|
|
actions ...policy.Action,
|
|
|
|
|
) func(http.Handler) http.Handler {
|
2026-08-19 20:04:13 +02:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
configID, parsed := ParseUUIDParam(rw, r, "mcpserverconfig")
|
|
|
|
|
if !parsed {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-19 21:25:18 +02:00
|
|
|
// Authorization follows the raw lookup because mutation-only callers
|
|
|
|
|
// may lack the read access enforced by the database wrapper.
|
|
|
|
|
//nolint:gocritic // The explicit action checks below own authorization.
|
|
|
|
|
config, err := db.GetMCPServerConfigByID(dbauthz.AsSystemRestricted(ctx), configID)
|
2026-08-19 20:04:13 +02:00
|
|
|
if httpapi.Is404Error(err) {
|
|
|
|
|
httpapi.ResourceNotFound(rw)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
httpapi.Write(ctx, rw, http.StatusInternalServerError, codersdk.Response{
|
|
|
|
|
Message: "Internal error fetching MCP server config.",
|
|
|
|
|
Detail: err.Error(),
|
|
|
|
|
})
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-08-19 21:25:18 +02:00
|
|
|
admitted := false
|
|
|
|
|
for _, action := range actions {
|
|
|
|
|
if auth(r, action, config) {
|
|
|
|
|
admitted = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if config.OrganizationID != OrganizationParam(r).ID || !admitted {
|
2026-08-19 20:04:13 +02:00
|
|
|
httpapi.ResourceNotFound(rw)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx = context.WithValue(ctx, mcpServerConfigParamContextKey{}, config)
|
|
|
|
|
next.ServeHTTP(rw, r.WithContext(ctx))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|