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

@@ -35,8 +35,10 @@ type recordingAdminConsent struct {
required bool
showErr error
decideErr error
shows []consentCall
calls []consentCall
// redirect replaces Decide's redirect URI while set.
redirect string
shows []consentCall
calls []consentCall
}
func (f *recordingAdminConsent) set(fn func(*recordingAdminConsent)) {
@@ -71,6 +73,9 @@ func (f *recordingAdminConsent) MCPAdminConsentDecide(_ context.Context, adminID
if requestID != "req1" {
return "", pact.ErrMCPConsentNotFound
}
if f.redirect != "" {
return f.redirect, nil
}
if !decision.Allow {
return "myagent:/callback?error=access_denied", nil
}
@@ -405,8 +410,9 @@ func TestMCPAdminConsent(t *testing.T) {
if rec := decide("nope", `{"decision":"deny"}`, readerP); rec.Code != http.StatusNotFound || errorCode(rec) != "request_not_found" {
t.Fatalf("deny of an unknown request = %d %s", rec.Code, rec.Body.String())
}
if got := provider.lastCall(t); got.request != "nope" || got.decision.Allow {
t.Fatalf("deny of an unknown request reached Decide with %+v", got)
// Both decisions load the request first: neither reaches Decide.
if got := provider.lastCall(t); got.request == "nope" {
t.Fatalf("a decision of an unknown request reached Decide with %+v", got)
}
provider.set(func(f *recordingAdminConsent) { f.decideErr = pact.ErrMCPConsentNotFound })
rec := decide("req1", `{"decision":"allow","scopes":["admin:read"]}`, readerP)
@@ -421,6 +427,34 @@ func TestMCPAdminConsent(t *testing.T) {
}
})
t.Run("a provider redirect away from the request's redirect URI is refused", func(t *testing.T) {
for _, foreign := range []string{
"https://evil.example/callback?code=c1",
"evilagent:/callback?code=c1",
"myagent:/elsewhere?code=c1",
"myagent://host/callback?code=c1",
"javascript:alert(1)",
"/callback?code=c1",
} {
provider.set(func(f *recordingAdminConsent) { f.redirect = foreign })
allow := decide("req1", `{"decision":"allow","scopes":["admin:read"]}`, readerP)
deny := decide("req1", `{"decision":"deny"}`, readerP)
provider.set(func(f *recordingAdminConsent) { f.redirect = "" })
for name, rec := range map[string]*httptest.ResponseRecorder{"allow": allow, "deny": deny} {
if rec.Code != http.StatusInternalServerError || strings.Contains(rec.Body.String(), foreign) {
t.Fatalf("%s answering %q = %d %s, want 500 without the URI", name, foreign, rec.Code, rec.Body.String())
}
}
}
// The registered redirect with its own query and a fragment kept.
provider.set(func(f *recordingAdminConsent) { f.redirect = "myagent:/callback?state=1&code=c9#frag" })
rec := decide("req1", `{"decision":"allow","scopes":["admin:read"]}`, readerP)
provider.set(func(f *recordingAdminConsent) { f.redirect = "" })
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "code=c9") {
t.Fatalf("the registered redirect = %d %s", rec.Code, rec.Body.String())
}
})
t.Run("malformed bodies and the body cap", func(t *testing.T) {
before := provider.callCount()
for _, body := range []string{
@@ -456,3 +490,39 @@ func TestMCPAdminConsent(t *testing.T) {
}
})
}
// TestMCPConsentSameRedirect locks the redirect check of the consent
// decision (32.2 WR-06): the provider's redirect must keep the registered
// URI's scheme, user info, host with port, path and opaque part; only the
// query and fragment may differ. Runs under -short.
func TestMCPConsentSameRedirect(t *testing.T) {
const want = "https://agent.example.test:8443/oauth/cb?x=1"
for got, ok := range map[string]bool{
"https://agent.example.test:8443/oauth/cb?x=1&code=a&state=s": true,
"https://agent.example.test:8443/oauth/cb?error=access_denied": true,
"https://agent.example.test:8443/oauth/cb#frag": true,
"http://agent.example.test:8443/oauth/cb?code=a": false,
"https://agent.example.test/oauth/cb?code=a": false,
"https://evil.example:8443/oauth/cb?code=a": false,
"https://user@agent.example.test:8443/oauth/cb?code=a": false,
"https://agent.example.test:8443/oauth/cb/../x?code=a": false,
"https://agent.example.test:8443/oauth/cb2?code=a": false,
"//agent.example.test:8443/oauth/cb?code=a": false,
"javascript:alert(1)": false,
"": false,
"%zz": false,
} {
if mcpConsentSameRedirect(want, got) != ok {
t.Fatalf("mcpConsentSameRedirect(%q, %q) = %v, want %v", want, got, !ok, ok)
}
}
if !mcpConsentSameRedirect("urn:ietf:wg:oauth:2.0:oob", "urn:ietf:wg:oauth:2.0:oob?code=a") {
t.Fatal("an opaque redirect with a query added does not match")
}
if mcpConsentSameRedirect("urn:ietf:wg:oauth:2.0:oob", "urn:ietf:wg:oauth:2.0:other?code=a") {
t.Fatal("a different opaque redirect matches")
}
if mcpConsentSameRedirect("", "https://agent.example.test/cb") {
t.Fatal("an empty registered redirect matches")
}
}