diff --git a/docs/services/oauth-server.md b/docs/services/oauth-server.md index 90d1ff01..1a9f2aff 100644 --- a/docs/services/oauth-server.md +++ b/docs/services/oauth-server.md @@ -106,7 +106,7 @@ The metadata document advertises these paths under the issuer, so mount them at ## Consent -The authorization endpoint sends the browser to `/connect?request=`, or to `wristband.Options.ConsentPath` on the issuer when the application sets one (for a consent page under the admin prefix, for example); an empty path keeps the PHP bytes exactly. A request that names no data scope gets `read`, or `wristband.Options.DefaultScopes` filtered to the accepted scopes when that list leaves anything; a server that accepts neither refuses it with `invalid_scope`. With `wristband.Options.DefaultScopes` set, a request that names only `offline_access` gets those scopes too and keeps offline access. That page belongs to the application: it signs the user in, shows what the client asks for and posts the decision to the application's own consent handler, which calls: +The authorization endpoint sends the browser to `/connect?request=`, or to `wristband.Options.ConsentPath` on the issuer when the application sets one (for a consent page under the admin prefix, for example); an empty path keeps the PHP bytes exactly. The path must be a plain same-origin absolute path (`wristband.CheckConsentPath`, which the application can call at boot); while it is not, authorize answers 500 instead of redirecting. A request that names no data scope gets `read`, or `wristband.Options.DefaultScopes` filtered to the accepted scopes when that list leaves anything; a server that accepts neither refuses it with `invalid_scope`. With `wristband.Options.DefaultScopes` set, a request that names only `offline_access` gets those scopes too and keeps offline access. That page belongs to the application: it signs the user in, shows what the client asks for and posts the decision to the application's own consent handler, which calls: - `wristband.Server.PendingRequest` to read what to show, as a `wristband.PendingRequestView`, including `wristband.PendingRequestView.OfflineAccess` (the pending row's flag; `offline_access` is not a scope on the view and `wristband.Server.IssueCode` is unchanged); - `wristband.Server.IssueCode` to grant, which returns the redirect URL carrying the code, `iss` and `state`; diff --git a/modules/wristband/README.md b/modules/wristband/README.md index bc5a47e8..5781382a 100644 --- a/modules/wristband/README.md +++ b/modules/wristband/README.md @@ -147,6 +147,7 @@ func providerRoutes(r pact.Router, srv *wristband.Server) { | `wristband.RefreshTokenStore` | Persists `wristband.RefreshTokenRecord` lineage rows, including rotation and lineage revocation. | | `wristband.AccessTokenIssuer` | Mints and revokes the application's access tokens, returning a `wristband.IssuedToken`. | | `wristband.IssueClientCredentials` | Generates a client ID and, for confidential clients, a one-time secret and its hash. | +| `wristband.CheckConsentPath` | Returns why a path cannot be `wristband.Options.ConsentPath` (it must start with a single `/` and carry no `?`, `#`, `\`, `@`, whitespace or control character), or nil; call it at boot. `wristband.Server.Authorize` answers 500 instead of redirecting while the configured path fails it. | | `wristband.RejectRedirectURI` | Returns why a redirect URI is not acceptable, or an empty string. | | `wristband.ErrPendingNotFound` | The pending request does not exist for this user or is no longer usable. | | `wristband.ErrNoGrantableScopes` | `wristband.Server.IssueCode` was called with no scopes. | @@ -204,7 +205,7 @@ wristband reads no config keys or environment variables; the application passes | `wristband.Options.CodeTTL` | 10 minutes | | `wristband.Options.AccessTokenTTL` | 1 hour | | `wristband.Options.RefreshTokenTTL` | 30 days | -| `wristband.Options.ConsentPath` | none (empty), which hands authorize off to `/connect` on the issuer, byte for byte as the PHP server; a set path (for example `/admin/connect`) replaces `/connect` on the same issuer | +| `wristband.Options.ConsentPath` | none (empty), which hands authorize off to `/connect` on the issuer, byte for byte as the PHP server; a set path (for example `/admin/connect`) replaces `/connect` on the same issuer; it must pass `wristband.CheckConsentPath` | | `wristband.Options.DefaultScopes` | none (empty), which keeps the PHP fallback `read` for a request that names no data scope (`invalid_scope` when `read` is not accepted); a set list is filtered to the accepted scopes and replaces `read` when anything is left, and then also applies to a request that names only `offline_access` | Always set `wristband.Options.Issuer` and `wristband.Options.Resource` for your deployment. While `wristband.Options.Resource` is empty, an MCP authorization request that sends `resource` is refused with `invalid_target`. diff --git a/modules/wristband/authorize.go b/modules/wristband/authorize.go index 79cbd4f5..a077f9f7 100644 --- a/modules/wristband/authorize.go +++ b/modules/wristband/authorize.go @@ -161,6 +161,13 @@ func (s *Server) Authorize(w http.ResponseWriter, r *http.Request) { dataScopes = append(dataScopes, sc) } + // A ConsentPath that is not a same-origin absolute path could send the + // browser, with the pending request id, off the issuer's origin; refuse + // before storing anything (fail closed, CheckConsentPath). + if err := CheckConsentPath(s.opts.ConsentPath); err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } requestID, err := s.randomBytes(32) if err != nil { w.WriteHeader(http.StatusInternalServerError) @@ -196,6 +203,27 @@ func (s *Server) Authorize(w http.ResponseWriter, r *http.Request) { writeRedirectHTML(w, http.StatusFound, spa) } +// CheckConsentPath reports why p cannot be Options.ConsentPath, or nil. The +// path is appended to Issuer as given, so it must be an absolute path on +// the issuer's origin: it starts with one "/" (not "//"), and carries no +// "?", "#", "\", "@", whitespace or control character, any of which could +// move the handoff to another host or break its query. An empty path (the +// PHP "/connect") is valid. A host calls it at boot to refuse a bad +// configuration early; Authorize answers 500 instead of redirecting while +// the configured path fails it. +func CheckConsentPath(p string) error { + if p == "" { + return nil + } + if !strings.HasPrefix(p, "/") || strings.HasPrefix(p, "//") { + return fmt.Errorf("wristband: ConsentPath %q must be an absolute path starting with a single /", p) + } + if strings.ContainsAny(p, "?#\\@") || strings.ContainsFunc(p, func(r rune) bool { return r <= ' ' || r == 0x7f }) { + return fmt.Errorf("wristband: ConsentPath %q must be a plain same-origin path", p) + } + return nil +} + // consentPath is the path the authorize handoff appends to Issuer: // Options.ConsentPath, or the PHP "/connect" while it is empty. func (s *Server) consentPath() string { diff --git a/modules/wristband/consent_path_test.go b/modules/wristband/consent_path_test.go index 14c9d7ca..e200d45a 100644 --- a/modules/wristband/consent_path_test.go +++ b/modules/wristband/consent_path_test.go @@ -241,3 +241,60 @@ func TestAuthorizeDefaultScopes(t *testing.T) { } }) } + +// TestCheckConsentPath locks the WR-02 guard: Options.ConsentPath is +// appended to Issuer as given, so only a plain same-origin absolute path is +// accepted, and Authorize answers 500 without storing a pending request or +// sending a Location while the configured path fails it. +func TestCheckConsentPath(t *testing.T) { + for _, ok := range []string{"", "/connect", "/backend/connect", "/gfadmin/mcp/connect", "/a%20b"} { + if err := CheckConsentPath(ok); err != nil { + t.Fatalf("CheckConsentPath(%q) = %v, want nil", ok, err) + } + } + for _, bad := range []string{ + "@evil.example/connect", + "backend/connect", + "//evil.example/connect", + "/\\evil.example", + "/connect?x=1", + "/connect#frag", + "/user@evil.example", + "/con nect", + "/connect\n", + "/connect\x7f", + } { + if err := CheckConsentPath(bad); err == nil { + t.Fatalf("CheckConsentPath(%q) = nil, want an error", bad) + } + } + + t.Run("authorize refuses an invalid path without a handoff", func(t *testing.T) { + backend := newMemoryBackend() + opts := DefaultOptions() + opts.Issuer = "https://app.example" + opts.Resource = opts.Issuer + "/mcp" + opts.ConsentPath = "@evil.example/connect" + srv := NewServer(opts) + srv.SetBackend(backend) + insertAuthorizeTestClient(backend, "cli", []string{authorizeTestRedirect}, nil) + _, challenge := s256Pair(t) + q := url.Values{} + q.Set("client_id", "cli") + q.Set("redirect_uri", authorizeTestRedirect) + q.Set("response_type", "code") + q.Set("code_challenge", challenge) + q.Set("code_challenge_method", "S256") + q.Set("scope", "read") + rec := httptest.NewRecorder() + srv.Authorize(rec, httptest.NewRequest(http.MethodGet, "/oauth/mcp/authorize?"+q.Encode(), nil)) + if rec.Code != http.StatusInternalServerError || rec.Header().Get("Location") != "" { + t.Fatalf("authorize = %d Location %q, want 500 and no Location", rec.Code, rec.Header().Get("Location")) + } + backend.mu.Lock() + defer backend.mu.Unlock() + if len(backend.codes) != 0 { + t.Fatalf("authorize stored %d pending requests, want none", len(backend.codes)) + } + }) +} diff --git a/modules/wristband/server.go b/modules/wristband/server.go index 03a89b54..29f1a77f 100644 --- a/modules/wristband/server.go +++ b/modules/wristband/server.go @@ -96,7 +96,9 @@ type Options struct { // authorize request. Empty or unset means "/connect", the PHP handoff, // so the redirect bytes stay identical to the PHP server. A host whose // consent page lives elsewhere on the same origin (for example under - // the admin prefix) sets the path here. The OIDC flavour ignores it. + // the admin prefix) sets the path here; it must pass CheckConsentPath, + // and Authorize answers 500 instead of redirecting while it does not. + // The OIDC flavour ignores it. ConsentPath string // DefaultScopes is what an MCP authorize request gets when it names no