Skip to content

A platform for community discussion. Free, open, simple.

FIX: Prompt anonymous users to log in before MCP authorization (#43975)

Penar Musarajce1ea382ebb3
diff --git a/app/controllers/mcp_oauth_authorizations_controller.rb b/app/controllers/mcp_oauth_authorizations_controller.rb
index 345bf6be18d..fde82cf956b 100644
--- a/app/controllers/mcp_oauth_authorizations_controller.rb
+++ b/app/controllers/mcp_oauth_authorizations_controller.rb
@@ -2,9 +2,10 @@
class McpOauthAuthorizationsController < ApplicationController
layout "no_ember"
- requires_login
+ requires_login except: [:show]
skip_before_action :check_xhr, :preload_json
skip_after_action :conditionally_allow_site_embedding
+ before_action :redirect_to_login, only: [:show], unless: :current_user
before_action :validate_request
before_action :ensure_mcp_access
after_action :prevent_framing
diff --git a/spec/requests/mcp_oauth_authorizations_controller_spec.rb b/spec/requests/mcp_oauth_authorizations_controller_spec.rb
index 5d45fd71ccf..e1170522c37 100644
--- a/spec/requests/mcp_oauth_authorizations_controller_spec.rb
+++ b/spec/requests/mcp_oauth_authorizations_controller_spec.rb
@@ -4,12 +4,56 @@ describe McpOauthAuthorizationsController do
fab!(:admin)
fab!(:user)
- before do
- sign_in(admin)
- SiteSetting.mcp_server_enabled = true
+ before { SiteSetting.mcp_server_enabled = true }
+
+ describe "#show" do
+ it "redirects anonymous users to login and preserves the authorization request" do
+ client =
+ McpOauthClient.create!(
+ client_id: "login-client",
+ name: "Login client",
+ registration_type: "pre_registered",
+ trust_state: "approved",
+ redirect_uris: ["http://127.0.0.1/callback"],
+ )
+ authorization_url =
+ "/oauth2/mcp/authorize?" +
+ {
+ client_id: client.client_id,
+ redirect_uri: client.redirect_uris.first,
+ response_type: "code",
+ code_challenge: "a" * 43,
+ code_challenge_method: "S256",
+ resource: DiscourseMcp.resource_url,
+ scope: "mcp:profile:read mcp:content:read",
+ state: "client-state",
+ }.to_query
+
+ get authorization_url
+
+ expect(response).to redirect_to("/login")
+ destination_url = cookies[:destination_url]
+ expect(destination_url).to eq("http://test.localhost#{authorization_url}")
+
+ sign_in(admin)
+ get URI(destination_url).request_uri
+
+ expect(response.status).to eq(200)
+ expect(response.body).to include(client.name)
+ end
+ end
+
+ describe "#create" do
+ it "rejects anonymous consent submissions" do
+ post "/oauth2/mcp/authorize", params: { decision: "approve" }
+
+ expect(response.status).to eq(403)
+ expect(McpOauthAuthorization.count).to eq(0)
+ end
end
it "labels the OAuth resource as the MCP server" do
+ sign_in(admin)
client =
McpOauthClient.create!(
client_id: "consent-page-client",
@@ -36,6 +80,7 @@ describe McpOauthAuthorizationsController do
end
it "prevents the authorization page from being framed when site embedding is enabled" do
+ sign_in(admin)
SiteSetting.allow_embedding_site_in_an_iframe = true
SiteSetting.content_security_policy = false
client =
@@ -64,6 +109,7 @@ describe McpOauthAuthorizationsController do
end
it "rejects an authorization request without the initial scope" do
+ sign_in(admin)
client =
McpOauthClient.create!(
client_id: "missing-initial-scope-client",
@@ -153,6 +199,7 @@ describe McpOauthAuthorizationsController do
end
it "authorizes a loopback redirect using the client's active port" do
+ sign_in(admin)
client =
McpOauthClient.create!(
client_id: "loopback-client",
@@ -179,6 +226,7 @@ describe McpOauthAuthorizationsController do
end
it "shows consent when a metadata client supports public authentication as a choice" do
+ sign_in(admin)
client_id = "https://client.example.com/oauth/client.json"
redirect_uri = "https://client.example.com/callback"
SiteSetting.mcp_oauth_client_id_metadata_policy = "any_domain"
@@ -236,6 +284,7 @@ describe McpOauthAuthorizationsController do
end
it "rate limits metadata lookups across unique client IDs" do
+ sign_in(admin)
SiteSetting.mcp_oauth_client_id_metadata_policy = "any_domain"
RateLimiter.enable
allow(DiscourseMcp::OAuth::ClientResolver).to receive(:fetch_metadata) do |uri|