Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(mcp): withhold the MCP client's origin from FusionAuth on authorize (#8987)
Wolf Mermelsteine5cc143504e6
diff --git a/services/mcp_auth_proxy/AGENTS.md b/services/mcp_auth_proxy/AGENTS.mdindex 5141efb0e0..f862e86a22 100644--- a/services/mcp_auth_proxy/AGENTS.md+++ b/services/mcp_auth_proxy/AGENTS.md@@ -49,6 +49,10 @@ and other library consumers. Both retain OAuth metadata, CORS, and bearer auth.## Gotchas+- Don't add an MCP client's host to FusionAuth's authorized origins+ (`infra/stacks/fusionauth-instance/origins.ts`). `/authorize` withholds the+ client's Referer from FusionAuth; a new client needs only a trusted redirect+ host. See README "Origin versus redirect URI".- `client_id` is not a secret; every endpoint must still check it against alive registration — see README "Which client a grant belongs to" beforechanging token/refresh validation.diff --git a/services/mcp_auth_proxy/README.md b/services/mcp_auth_proxy/README.mdindex 51babe9699..f24fab2c28 100644--- a/services/mcp_auth_proxy/README.md+++ b/services/mcp_auth_proxy/README.md@@ -5,6 +5,28 @@ I believe this is needed because FusionAuth doesn't support DCR(Official Docs)[https://modelcontextprotocol.io/docs/tutorials/security/authorization](Useful fusionauth article)[https://fusionauth.io/articles/ai/mcp-connecting-software-ai]+## Origin versus redirect URI++Two different values get checked during sign-in, by two different parties:++- The **origin** is the page a browser request comes from, sent as the `Origin`+ header or, on a navigation, the `Referer`. FusionAuth checks it against the+ application's authorized origins (`infra/stacks/fusionauth-instance/origins.ts`)+ and fails with `invalid_request: Invalid origin uri` when it is not listed.+ That list is for Macro's own apps that talk to FusionAuth directly.+- The **redirect URI** is where the authorization code is delivered. The broker+ checks it, as described in the next two sections. FusionAuth only ever+ redirects to the broker's own `/oauth/callback`.++MCP clients never talk to FusionAuth directly: the client opens the broker's+`/authorize`, which redirects the browser to FusionAuth. The browser would carry+the client page's `Referer` across that redirect, so a client page on an origin+FusionAuth does not list (`http://127.0.0.1:6274` for MCP Inspector, say) was+refused even though its redirect URI was fine. The `/authorize` redirect is sent+with `Referrer-Policy: no-referrer`, so FusionAuth sees no client origin and+third-party clients need no entry in the FusionAuth origin list. Whether a+client may sign in is decided only by its redirect URI.+## Which redirect URIs are usableDynamic client registration is open, because MCP clients require it. That means@@ -25,6 +47,18 @@ A redirect URI is usable when it is either:which holds the browser-based MCP clients Macro supports. Subdomains of alisted host are not themselves trusted.+Loopback hosts are interchangeable for this policy, but not for a registration:+a client that registered `http://localhost:6274/...` cannot have a code sent to+`http://127.0.0.1:6274/...`.++A new third-party HTTPS client therefore needs exactly one change: its callback+host added to `DEFAULT_MCP_ALLOWED_REDIRECT_HOSTS` (or the deployment's+`MCP_ALLOWED_REDIRECT_HOSTS`). It needs no FusionAuth origin. Registering through+DCR alone is not enough, on purpose: FusionAuth signs in a user with an existing+session without showing a consent screen, so if any registered HTTPS host were+usable, anyone could register their own callback and send a Macro user a link+that hands them that user's code.+A redirect URI carrying a fragment or credentials in its authority is refusedregardless of host.diff --git a/services/mcp_auth_proxy/src/inbound/axum_router.rs b/services/mcp_auth_proxy/src/inbound/axum_router.rsindex f97cb8500d..9743b8e31a 100644--- a/services/mcp_auth_proxy/src/inbound/axum_router.rs+++ b/services/mcp_auth_proxy/src/inbound/axum_router.rs@@ -89,7 +89,15 @@ async fn authorize<I: InflightAuthStore + 'static>(Query(params): Query<AuthorizeRequest>,) -> Response {match auth_proxy.start_authorization(params).await {- Ok(url) => Redirect::temporary(&url).into_response(),+ // The browser carries the MCP client page's Referer across this+ // redirect, and FusionAuth rejects any Referer outside its first-party+ // origin list with "Invalid origin uri". The redirect URI policy has+ // already vetted the client, so the client's origin is withheld.+ Ok(url) => (+ [(header::REFERRER_POLICY, "no-referrer")],+ Redirect::temporary(&url),+ )+ .into_response(),Err(StartAuthorizationError::UnsupportedResponseType) => (axum::http::StatusCode::BAD_REQUEST,"unsupported response_type",diff --git a/services/mcp_auth_proxy/src/inbound/axum_router/test.rs b/services/mcp_auth_proxy/src/inbound/axum_router/test.rsindex 107f0d7b43..a679ef5cdb 100644--- a/services/mcp_auth_proxy/src/inbound/axum_router/test.rs+++ b/services/mcp_auth_proxy/src/inbound/axum_router/test.rs@@ -21,6 +21,7 @@ const BODY_LIMIT: usize = 64 * 1024;struct Response {status: StatusCode,location: Option<String>,+ referrer_policy: Option<String>,body: String,}@@ -41,6 +42,10 @@ async fn send(harness: &Harness, request: Request<Body>) -> Response {.headers().get(header::LOCATION).map(|value| value.to_str().expect("Location is text").to_owned());+ let referrer_policy = response+ .headers()+ .get(header::REFERRER_POLICY)+ .map(|value| value.to_str().expect("Referrer-Policy is text").to_owned());let bytes = axum::body::to_bytes(response.into_body(), BODY_LIMIT).await.expect("body should be readable");@@ -48,6 +53,7 @@ async fn send(harness: &Harness, request: Request<Body>) -> Response {Response {status,location,+ referrer_policy,body: String::from_utf8(bytes.to_vec()).expect("body should be UTF-8"),}}@@ -155,6 +161,83 @@ async fn authorize_explains_how_to_reconnect_an_unregistered_client() {assert!(!harness.inflight.has_pending());}+/// MCP Inspector on `http://127.0.0.1:6274` failed with FusionAuth's "Invalid+/// origin uri" because the browser forwarded the inspector's Referer through+/// this redirect. Every usable client, loopback on any port or a trusted HTTPS+/// host, reaches FusionAuth without its origin.+#[tokio::test]+async fn authorize_withholds_the_client_origin_from_the_identity_provider() {+ let harness = Harness::new();++ for redirect_uri in [+ "http://localhost:6274/oauth/callback",+ "http://127.0.0.1:6274/oauth/callback",+ "http://[::1]:6274/oauth/callback",+ TRUSTED_REDIRECT_URI,+ ] {+ let registration = send(&harness, register_request(redirect_uri)).await;+ assert_eq!(registration.status, StatusCode::OK, "{redirect_uri}");+ let client_id = registration.json()["client_id"]+ .as_str()+ .expect("registration returns a client_id")+ .to_owned();++ let authorize = send(+ &harness,+ authorize_request(&client_id, redirect_uri, VICTIM_CODE_VERIFIER),+ )+ .await;++ assert_eq!(+ authorize.status,+ StatusCode::TEMPORARY_REDIRECT,+ "{redirect_uri}"+ );+ assert_eq!(+ authorize.referrer_policy.as_deref(),+ Some("no-referrer"),+ "{redirect_uri}"+ );+ }+}++/// Loopback hosts are interchangeable for the policy but not for the+/// registration: a client registered on `localhost` cannot have its code sent+/// to `127.0.0.1`.+#[tokio::test]+async fn authorize_refuses_a_loopback_redirect_the_client_did_not_register() {+ let harness = Harness::new();++ let registration = send(+ &harness,+ register_request("http://localhost:6274/oauth/callback"),+ )+ .await;+ assert_eq!(registration.status, StatusCode::OK);+ let client_id = registration.json()["client_id"]+ .as_str()+ .expect("registration returns a client_id")+ .to_owned();++ let authorize = send(+ &harness,+ authorize_request(+ &client_id,+ "http://127.0.0.1:6274/oauth/callback",+ VICTIM_CODE_VERIFIER,+ ),+ )+ .await;++ assert_eq!(authorize.status, StatusCode::BAD_REQUEST);+ assert_eq!(+ authorize.body,+ "redirect_uri was not registered for this client"+ );+ assert_eq!(authorize.location, None);+ assert!(!harness.inflight.has_pending());+}+/// The flow a real MCP client drives, from registration through to a bearer/// token, so the added checks are shown not to break it.#[tokio::test]