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: a terminal client could not complete the OAuth flow

Damian Legawiec5940f4c8f924
diff --git a/packages/dashboard/src/components/spree/mcp-connect-sheet.tsx b/packages/dashboard/src/components/spree/mcp-connect-sheet.tsx
index eec815d8b4..dd5dd713e3 100644
--- a/packages/dashboard/src/components/spree/mcp-connect-sheet.tsx
+++ b/packages/dashboard/src/components/spree/mcp-connect-sheet.tsx
@@ -40,11 +40,23 @@ type McpConnectSheetProps = {
* `surfaces` are the ways that client connects. A desktop or web app takes
* the address pasted into its settings; a terminal client takes a command.
*/
+/**
+ * The loopback callback a terminal client listens on.
+ *
+ * Registered alongside the hosted one because Claude Code and Codex redirect
+ * to a local port rather than the vendor's servers, and the port is chosen
+ * at run time. RFC 8252 §7.3 says to compare loopback redirects ignoring the
+ * port, which Doorkeeper does — but only for an IP literal, since it decides
+ * by parsing the host as an address. `localhost` is a name, so it parses as
+ * nothing and every ephemeral port is refused.
+ */
+const LOOPBACK_CALLBACK = 'http://127.0.0.1/callback'
+
const KNOWN_CLIENTS = [
{
value: 'claude',
name: 'Claude',
- redirectUri: 'https://claude.ai/api/mcp/auth_callback',
+ redirectUris: ['https://claude.ai/api/mcp/auth_callback', LOOPBACK_CALLBACK],
surfaces: [
{ id: 'app', command: null },
{ id: 'cli', command: (url: string) => `claude mcp add --transport http spree ${url}` },
@@ -53,7 +65,7 @@ const KNOWN_CLIENTS = [
{
value: 'chatgpt',
name: 'ChatGPT',
- redirectUri: 'https://chatgpt.com/connector_platform_oauth_redirect',
+ redirectUris: ['https://chatgpt.com/connector_platform_oauth_redirect', LOOPBACK_CALLBACK],
surfaces: [
{ id: 'app', command: null },
{ id: 'cli', command: (url: string) => `codex mcp add spree --url ${url}` },
@@ -92,7 +104,9 @@ export function McpConnectSheet({ open, onOpenChange }: McpConnectSheetProps) {
async function connect() {
setFailure(null)
const name = selected ? selected.name : customName.trim()
- const redirectUri = selected ? selected.redirectUri : customUri.trim()
+ // Doorkeeper stores several callbacks newline-separated and accepts a
+ // request matching any one of them.
+ const redirectUri = selected ? selected.redirectUris.join('\n') : customUri.trim()
// Picking the same client twice is reconnecting it, not registering a
// second one — two rows with two ids would leave a merchant guessing
diff --git a/spree/api/lib/spree/api/oauth.rb b/spree/api/lib/spree/api/oauth.rb
index 719559ba7b..5e8901bc74 100644
--- a/spree/api/lib/spree/api/oauth.rb
+++ b/spree/api/lib/spree/api/oauth.rb
@@ -26,6 +26,24 @@ module Spree
# URL options otherwise. A store's own `url` column is deliberately not
# consulted — it describes the storefront, which is frequently neither
# the host nor the scheme the Admin API answers on.
+ # Whether a redirect URI points at the machine the client runs on.
+ #
+ # Decided by parsing the host as an address, the same way Doorkeeper
+ # does when it ignores the port of a loopback redirect — so `localhost`
+ # is deliberately not loopback here either, and a registration must use
+ # the IP literal for an ephemeral port to match.
+ #
+ # @param uri [URI, String]
+ # @return [Boolean]
+ def self.loopback_uri?(uri)
+ host = uri.respond_to?(:host) ? uri.host : URI.parse(uri.to_s).host
+ return false if host.blank?
+
+ IPAddr.new(host).loopback?
+ rescue StandardError
+ false
+ end
+
mattr_accessor :resources, default: {}
class << self
@@ -158,10 +176,19 @@ module Spree
pkce_code_challenge_methods %w[S256]
# A redirect URI is where authorization codes are delivered, so a
- # plain-http one hands them to anyone on the path. Off in
- # development, where a local client has no certificate and the
- # traffic never leaves the machine.
- force_ssl_in_redirect_uri !Rails.env.development?
+ # plain-http one hands them to anyone on the path — except a
+ # loopback one, which never leaves the machine. A terminal client
+ # listens on an ephemeral local port and has no certificate for
+ # it, so RFC 8252 §7.3 makes that the recommended shape, and
+ # refusing it would lock out Claude Code and Codex entirely.
+ #
+ # Passed as a callable so the exemption is per URI rather than
+ # per environment: everything else still has to be https.
+ force_ssl_in_redirect_uri do |uri|
+ next false if Rails.env.development?
+
+ !Spree::Api::Oauth.loopback_uri?(uri)
+ end
# RFC 8707. Registering the validator is what binds an audience
# into the token; left nil, the `resource` parameter is ignored
diff --git a/spree/api/spec/lib/spree/api/oauth_redirect_uri_spec.rb b/spree/api/spec/lib/spree/api/oauth_redirect_uri_spec.rb
new file mode 100644
index 0000000000..22313699f3
--- /dev/null
+++ b/spree/api/spec/lib/spree/api/oauth_redirect_uri_spec.rb
@@ -0,0 +1,71 @@
+require 'spec_helper'
+
+# Where authorization codes are delivered, and the one registration field a
+# merchant must not get wrong.
+RSpec.describe 'OAuth redirect URIs' do
+ let(:store) { @default_store }
+
+ def build_with(redirect_uri)
+ store.oauth_applications.new(name: "Client #{SecureRandom.hex(3)}",
+ confidential: false, redirect_uri: redirect_uri)
+ end
+
+ # A plain-http callback hands codes to anyone on the path — except a
+ # loopback one, which never leaves the machine. A terminal client listens on
+ # a local port and has no certificate for it.
+ describe 'the scheme' do
+ it 'accepts https' do
+ expect(build_with('https://claude.ai/api/mcp/auth_callback')).to be_valid
+ end
+
+ it 'accepts a loopback callback over plain http' do
+ expect(build_with('http://127.0.0.1/callback')).to be_valid
+ end
+
+ it 'accepts a hosted and a loopback callback together' do
+ expect(
+ build_with("https://claude.ai/api/mcp/auth_callback\nhttp://127.0.0.1/callback")
+ ).to be_valid
+ end
+
+ it 'refuses plain http anywhere else' do
+ expect(build_with('http://example.test/callback')).not_to be_valid
+ end
+
+ # Doorkeeper decides loopback by parsing the host as an address, so a name
+ # is not loopback — and a registration using one would have every
+ # ephemeral port refused at authorization anyway.
+ it 'refuses localhost by name' do
+ expect(build_with('http://localhost/callback')).not_to be_valid
+ end
+ end
+
+ # RFC 8252 §7.3: a terminal client picks its port at run time, so the
+ # registration cannot name it.
+ describe 'a terminal client calling back on an ephemeral port' do
+ let(:application) do
+ store.oauth_applications.create!(
+ name: 'Claude', confidential: false,
+ redirect_uri: "https://claude.ai/api/mcp/auth_callback\nhttp://127.0.0.1/callback"
+ )
+ end
+
+ def authorizable?(redirect_uri)
+ ::Doorkeeper::OAuth::PreAuthorization.new(
+ ::Doorkeeper.config,
+ { client_id: application.uid, response_type: 'code', redirect_uri: redirect_uri,
+ scope: 'read_products', code_challenge: 'c' * 43, code_challenge_method: 'S256' },
+ create(:admin_user)
+ ).authorizable?
+ end
+
+ it 'is authorized whatever port it chose' do
+ expect(authorizable?('http://127.0.0.1:49821/callback')).to be(true)
+ expect(authorizable?('http://127.0.0.1:60007/callback')).to be(true)
+ end
+
+ it 'still refuses a host that was never registered' do
+ expect(authorizable?('https://evil.test/callback')).to be(false)
+ end
+ end
+end