Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix: bearer header parsing could be made to backtrack

Damian Legawiec06fc2ea0ed7e
diff --git a/spree/mcp/app/controllers/spree/api/v3/admin/mcp_controller.rb b/spree/mcp/app/controllers/spree/api/v3/admin/mcp_controller.rb
index 15b8e4d642..7bb21a89cc 100644
--- a/spree/mcp/app/controllers/spree/api/v3/admin/mcp_controller.rb
+++ b/spree/mcp/app/controllers/spree/api/v3/admin/mcp_controller.rb
@@ -53,10 +53,15 @@ module Spree
private
+ # Split rather than matched: a regex with `\s+(.+)` backtracks on a
+ # header of many spaces, and this one is attacker-supplied on an
+ # unauthenticated request.
def bearer_secret_key
- token = request.headers['Authorization'].to_s[/\ABearer\s+(.+)\z/i, 1]
+ scheme, token = request.headers['Authorization'].to_s.split(' ', 2)
+ return unless scheme&.casecmp?('Bearer')
- token if token.to_s.start_with?(Spree::ApiKey::PREFIXES['secret'])
+ token = token.to_s.strip
+ token if token.start_with?(Spree::ApiKey::PREFIXES['secret'])
end
def server
diff --git a/spree/mcp/spec/requests/spree/api/v3/admin/mcp_spec.rb b/spree/mcp/spec/requests/spree/api/v3/admin/mcp_spec.rb
index a640ec064c..e49b91c6db 100644
--- a/spree/mcp/spec/requests/spree/api/v3/admin/mcp_spec.rb
+++ b/spree/mcp/spec/requests/spree/api/v3/admin/mcp_spec.rb
@@ -1,4 +1,5 @@
require 'spec_helper'
+require 'benchmark'
# The endpoint, from the header down. A request spec rather than a controller
# spec because the unit under test is the whole request: the credential, the
@@ -35,6 +36,23 @@ RSpec.describe 'Admin MCP endpoint', type: :request do
expect(body['result']['tools']).to be_present
end
+ # Parsing the bearer header must not backtrack: this header arrives
+ # unauthenticated, so a pathological value is free to send.
+ it 'parses a bearer header of many spaces without stalling' do
+ elapsed = Benchmark.realtime do
+ post_rpc('tools/list', headers: { 'Authorization' => "Bearer #{' ' * 50_000}x" })
+ end
+
+ expect(response).to have_http_status(:unauthorized)
+ expect(elapsed).to be < 1.0
+ end
+
+ it 'accepts a bearer key with extra whitespace' do
+ post_rpc('tools/list', headers: { 'Authorization' => "Bearer #{token}" })
+
+ expect(response).to have_http_status(:ok)
+ end
+
it 'refuses a request with no key, naming the header to set' do
body = post_rpc('tools/list')