fix(32.2): WR-06 check the consent provider's redirect against the request's redirect URI

cabana loads the pending request on both decisions and refuses (500) a Decide answer whose scheme, user info, host, path or opaque part differ from the registered redirect URI. The admin SPA also refuses blob:, file:, about: and filesystem: and a destination that does not return to the host the consent card showed. Regenerated the admin OpenAPI document and rebuilt modules/boardwalk/dist.
This commit is contained in:
Jakub Zych
2026-11-21 09:32:54 +01:00
parent 60bc6ee68f
commit 0ade662a04
17 changed files with 212 additions and 49 deletions

View File

@@ -81,6 +81,29 @@ func mcpConsentRedirectHost(raw string) string {
return u.String()
}
// mcpConsentSameRedirect reports whether got, the redirect a provider
// answered for a decision, goes back to want, the pending request's
// registered redirect URI: the same scheme, user info, host (with its
// port), path and opaque part, whatever query and fragment it adds (the
// code, state, iss or error parameters). An unparsable or empty URI never
// matches, so the consent screen never sends the browser anywhere the
// request did not name.
func mcpConsentSameRedirect(want, got string) bool {
if want == "" || got == "" {
return false
}
w, err := url.Parse(want)
if err != nil {
return false
}
g, err := url.Parse(got)
if err != nil {
return false
}
return g.Scheme != "" && g.Scheme == w.Scheme && g.User.String() == w.User.String() &&
g.Host == w.Host && g.Opaque == w.Opaque && g.EscapedPath() == w.EscapedPath()
}
// mcpConsentShow serves GET /mcp/consent/{request}: the pending MCP
// authorization request of an administrator-only grant, as the published
// pact.MCPAdminConsent resolves it for the signed-in administrator, with
@@ -152,8 +175,11 @@ func (s *service) mcpConsentShow(w http.ResponseWriter, r *http.Request) {
// and that the administrator can grant (others are dropped, not refused),
// normalises them as personal-token creation does (* collapses to ["*"])
// and answers 422 no_grantable_scopes when nothing is left; offline access
// is granted only when it was requested and the body asks for it. The
// provider owns the transaction and the lock of the pending request.
// is granted only when it was requested and the body asks for it. Both
// decisions load the request first (404 when it is gone), and the
// provider's redirect must go back to the request's redirect URI
// (mcpConsentSameRedirect), else 500. The provider owns the transaction
// and the lock of the pending request.
func (s *service) mcpConsentDecide(w http.ResponseWriter, r *http.Request) {
principal := accountPrincipal(w, r)
if principal == nil {
@@ -171,22 +197,26 @@ func (s *service) mcpConsentDecide(w http.ResponseWriter, r *http.Request) {
WriteError(w, http.StatusBadRequest, "invalid_request", "Invalid request")
return
}
if body.Decision != "allow" && body.Decision != "deny" {
WriteError(w, http.StatusBadRequest, "invalid_request", "Invalid request")
return
}
ctx := r.Context()
requestID := r.PathValue("request")
// Both decisions load the request: an allow filters against its scopes,
// and either answer's redirect is checked against its redirect URI.
req, err := provider.MCPAdminConsentShow(ctx, principal.ID, requestID)
if errors.Is(err, pact.ErrMCPConsentNotFound) {
s.writeMCPConsentNotFound(w, r)
return
}
if err != nil {
moduleLog.ErrorContext(ctx, "cabana: mcp consent decide could not load the request", "admin_id", principal.ID, "error", err)
WriteError(w, http.StatusInternalServerError, "error", msgServerError)
return
}
var decision pact.MCPAdminConsentDecision
switch body.Decision {
case "deny":
case "allow":
req, err := provider.MCPAdminConsentShow(ctx, principal.ID, requestID)
if errors.Is(err, pact.ErrMCPConsentNotFound) {
s.writeMCPConsentNotFound(w, r)
return
}
if err != nil {
moduleLog.ErrorContext(ctx, "cabana: mcp consent decide could not load the request", "admin_id", principal.ID, "error", err)
WriteError(w, http.StatusInternalServerError, "error", msgServerError)
return
}
if body.Decision == "allow" {
grantable := map[string]bool{}
for _, scope := range s.grantableConsentScopes(principal, req.Scopes) {
grantable[scope.Name] = true
@@ -211,9 +241,6 @@ func (s *service) mcpConsentDecide(w http.ResponseWriter, r *http.Request) {
Scopes: scopes,
OfflineAccess: req.OfflineAccess && body.OfflineAccess,
}
default:
WriteError(w, http.StatusBadRequest, "invalid_request", "Invalid request")
return
}
redirect, err := provider.MCPAdminConsentDecide(ctx, principal.ID, requestID, decision)
switch {
@@ -228,6 +255,11 @@ func (s *service) mcpConsentDecide(w http.ResponseWriter, r *http.Request) {
WriteError(w, http.StatusInternalServerError, "error", msgServerError)
return
}
if !mcpConsentSameRedirect(req.RedirectURI, redirect) {
moduleLog.ErrorContext(ctx, "cabana: mcp consent provider answered a foreign redirect", "admin_id", principal.ID, "allow", decision.Allow, "client_id", req.ClientID)
WriteError(w, http.StatusInternalServerError, "error", msgServerError)
return
}
moduleLog.InfoContext(ctx, "cabana: mcp consent decided", "admin_id", principal.ID, "allow", decision.Allow, "scopes", decision.Scopes)
WriteData(w, http.StatusOK, MCPConsentDecisionView{RedirectURI: redirect}, nil)
}