The personal finance app for everyone (by everyone)
Ruby
fix: block unsafe OAuth redirect URIs in dynamic registration (#2638)
RealDiligent17b8fe240911
diff --git a/app/controllers/oauth_registration_controller.rb b/app/controllers/oauth_registration_controller.rbindex fb0d8e2c3c..1b3170be44 100644--- a/app/controllers/oauth_registration_controller.rb+++ b/app/controllers/oauth_registration_controller.rb@@ -1,4 +1,6 @@class OauthRegistrationController < ApplicationController+ LOOPBACK_HOSTS = [ "localhost", "127.0.0.1", "::1" ].freeze+skip_authenticationskip_before_action :verify_authenticity_tokenskip_before_action :require_onboarding_and_upgrade, raise: false@@ -33,6 +35,14 @@ class OauthRegistrationController < ApplicationControllerreturnend+ unless redirect_uris.all? { |uri| valid_redirect_uri?(uri) }+ render json: {+ error: "invalid_client_metadata",+ error_description: "redirect_uris must use https or loopback http"+ }, status: :bad_request+ return+ end+client_name = body["client_name"].presence || "MCP Client"app = Doorkeeper::Application.new(@@ -61,4 +71,20 @@ class OauthRegistrationController < ApplicationControllererror_description: "Invalid JSON"}, status: :bad_requestend++ private++ def valid_redirect_uri?(raw_uri)+ uri = URI.parse(raw_uri)+ return false if uri.host.blank?++ scheme = uri.scheme.to_s.downcase+ return true if scheme == "https"+ return false unless scheme == "http"++ host = uri.host.downcase.delete_prefix("[").delete_suffix("]")+ LOOPBACK_HOSTS.include?(host)+ rescue URI::InvalidURIError+ false+ endenddiff --git a/test/controllers/oauth_registration_controller_test.rb b/test/controllers/oauth_registration_controller_test.rbindex a80c2b1476..929e83e272 100644--- a/test/controllers/oauth_registration_controller_test.rb+++ b/test/controllers/oauth_registration_controller_test.rb@@ -68,4 +68,56 @@ class OauthRegistrationControllerTest < ActionDispatch::IntegrationTestjson = JSON.parse(response.body)assert_equal "MCP Client", json["client_name"]end++ test "rejects non-loopback http redirect uri" do+ post "/register",+ params: {+ client_name: "Claude",+ redirect_uris: [ "http://evil.example/callback" ]+ }.to_json,+ headers: { "Content-Type" => "application/json" }++ assert_response :bad_request+ json = JSON.parse(response.body)+ assert_equal "invalid_client_metadata", json["error"]+ end++ test "allows loopback http redirect uri" do+ post "/register",+ params: {+ client_name: "Claude",+ redirect_uris: [ "http://localhost:3456/callback" ]+ }.to_json,+ headers: { "Content-Type" => "application/json" }++ assert_response :created+ json = JSON.parse(response.body)+ assert_equal [ "http://localhost:3456/callback" ], json["redirect_uris"]+ end++ test "allows ipv6 loopback http redirect uri" do+ post "/register",+ params: {+ client_name: "Claude",+ redirect_uris: [ "http://[::1]:3456/callback" ]+ }.to_json,+ headers: { "Content-Type" => "application/json" }++ assert_response :created+ json = JSON.parse(response.body)+ assert_equal [ "http://[::1]:3456/callback" ], json["redirect_uris"]+ end++ test "rejects custom scheme redirect uri" do+ post "/register",+ params: {+ client_name: "Claude",+ redirect_uris: [ "myapp://callback" ]+ }.to_json,+ headers: { "Content-Type" => "application/json" }++ assert_response :bad_request+ json = JSON.parse(response.body)+ assert_equal "invalid_client_metadata", json["error"]+ endend