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.

Tighten `Store#allowed_origin?` check (#14188)

Damian Legawiec65a27734034d
diff --git a/spree/core/app/models/spree/allowed_origin.rb b/spree/core/app/models/spree/allowed_origin.rb
index 2927b84650..0904ec8e90 100644
--- a/spree/core/app/models/spree/allowed_origin.rb
+++ b/spree/core/app/models/spree/allowed_origin.rb
@@ -2,6 +2,10 @@
module Spree
class AllowedOrigin < Spree.base_class
+ # Loopback/development hosts that match any port, so storing `http://localhost`
+ # keeps matching `http://localhost:3000`, `:4000`, etc.
+ LOOPBACK_HOSTS = %w[localhost 127.0.0.1 ::1 0.0.0.0].freeze
+
has_prefix_id :ao
include Spree::SingleStoreResource
@@ -12,6 +16,42 @@ module Spree
validates :origin, uniqueness: { scope: [:store_id, *spree_base_uniqueness_scope] }
validate :origin_must_be_valid_http_url
+ # Parses a URL into its comparable origin components, or nil when the URL is
+ # invalid or not http(s). The host is downcased and has a single trailing dot
+ # stripped, and the port is the URI default (80/443) when not explicitly given.
+ #
+ # @param url [String] the URL to parse
+ # @return [Hash, nil] `{ scheme:, host:, port: }` or nil
+ def self.parse_origin(url)
+ uri = URI.parse(url.to_s)
+ return nil unless uri.is_a?(URI::HTTP)
+ return nil if uri.host.blank?
+
+ { scheme: uri.scheme.downcase, host: uri.host.downcase.chomp('.'), port: uri.port }
+ rescue URI::InvalidURIError
+ nil
+ end
+
+ # Returns true if the given URL's origin matches this stored origin.
+ #
+ # Scheme and host must match exactly (host comparison is case- and trailing-dot-
+ # insensitive). Port must also match, with the scheme default applied, so storing
+ # `https://shop.com` matches `https://shop.com:443`. Loopback/development hosts
+ # ({LOOPBACK_HOSTS}) are exempt from the port check, so `http://localhost` still
+ # matches `http://localhost:3000`, `:4000`, etc.
+ #
+ # @param url [String] the candidate URL to check
+ # @return [Boolean]
+ def matches?(url)
+ candidate = self.class.parse_origin(url)
+ allowed = self.class.parse_origin(origin)
+ return false if candidate.nil? || allowed.nil?
+ return false unless allowed[:scheme] == candidate[:scheme]
+ return false unless allowed[:host] == candidate[:host]
+
+ LOOPBACK_HOSTS.include?(allowed[:host]) || allowed[:port] == candidate[:port]
+ end
+
private
def origin_must_be_valid_http_url
@@ -19,12 +59,7 @@ module Spree
uri = URI.parse(origin)
- unless uri.is_a?(URI::HTTP) || uri.is_a?(URI::HTTPS)
- errors.add(:origin, :invalid)
- return
- end
-
- if uri.host.blank?
+ if self.class.parse_origin(origin).nil?
errors.add(:origin, :invalid)
return
end
diff --git a/spree/core/app/models/spree/store.rb b/spree/core/app/models/spree/store.rb
index d81736dfbd..4c118ca5c0 100644
--- a/spree/core/app/models/spree/store.rb
+++ b/spree/core/app/models/spree/store.rb
@@ -279,25 +279,14 @@ module Spree
end
# Returns true if the given URL's origin matches one of the store's allowed origins.
- # Comparison is port-less: only scheme + host are matched, so storing
- # `http://localhost` will match `http://localhost:3000`, `http://localhost:4000`, etc.
+ # See {Spree::AllowedOrigin#matches?} for the matching rules (scheme/host/port).
#
# @param url [String] the full URL to check
# @return [Boolean]
def allowed_origin?(url)
return false if url.blank?
- uri = URI.parse(url)
- request_origin = "#{uri.scheme}://#{uri.host}"
-
- allowed_origins.pluck(:origin).any? do |stored|
- stored_uri = URI.parse(stored)
- "#{stored_uri.scheme}://#{stored_uri.host}" == request_origin
- rescue URI::InvalidURIError
- false
- end
- rescue URI::InvalidURIError
- false
+ allowed_origins.any? { |allowed_origin| allowed_origin.matches?(url) }
end
# Returns the states available for checkout for the store
diff --git a/spree/core/spec/models/spree/allowed_origin_spec.rb b/spree/core/spec/models/spree/allowed_origin_spec.rb
index 934f75266b..645e298051 100644
--- a/spree/core/spec/models/spree/allowed_origin_spec.rb
+++ b/spree/core/spec/models/spree/allowed_origin_spec.rb
@@ -81,6 +81,73 @@ RSpec.describe Spree::AllowedOrigin, type: :model do
end
end
+ describe '#matches?' do
+ subject(:allowed_origin) { build(:allowed_origin, store: store, origin: stored) }
+
+ let(:stored) { 'https://myshop.com' }
+
+ it 'matches the exact origin regardless of path' do
+ expect(allowed_origin.matches?('https://myshop.com/reset-password')).to be true
+ end
+
+ it 'matches a non-loopback origin when the implicit default port is requested explicitly' do
+ expect(allowed_origin.matches?('https://myshop.com:443/checkout')).to be true
+ end
+
+ it 'does not match a non-loopback origin on a non-standard port' do
+ expect(allowed_origin.matches?('https://myshop.com:8080/checkout')).to be false
+ end
+
+ it 'does not match a different scheme' do
+ expect(allowed_origin.matches?('http://myshop.com/page')).to be false
+ end
+
+ it 'does not match a different host' do
+ expect(allowed_origin.matches?('https://evil.com/page')).to be false
+ end
+
+ it 'does not match a subdomain of the host' do
+ expect(allowed_origin.matches?('https://app.myshop.com/page')).to be false
+ end
+
+ it 'matches regardless of host casing' do
+ expect(allowed_origin.matches?('https://MyShop.com/page')).to be true
+ end
+
+ it 'ignores a trailing dot on the host' do
+ expect(allowed_origin.matches?('https://myshop.com./page')).to be true
+ end
+
+ it 'does not treat userinfo as the host' do
+ expect(allowed_origin.matches?('https://[email protected]/page')).to be false
+ end
+
+ it 'returns false for non-http(s) and blank candidates' do
+ expect(allowed_origin.matches?('ftp://myshop.com')).to be false
+ expect(allowed_origin.matches?('javascript:alert(1)')).to be false
+ expect(allowed_origin.matches?(nil)).to be false
+ end
+
+ context 'when the stored origin is a loopback host' do
+ let(:stored) { 'http://localhost' }
+
+ it 'matches any port' do
+ expect(allowed_origin.matches?('http://localhost:3000/reset-password')).to be true
+ expect(allowed_origin.matches?('http://localhost:5173')).to be true
+ end
+ end
+
+ context 'when the stored origin pins an explicit non-standard port' do
+ let(:stored) { 'https://staging.myshop.com:8443' }
+
+ it 'matches only that port' do
+ expect(allowed_origin.matches?('https://staging.myshop.com:8443/page')).to be true
+ expect(allowed_origin.matches?('https://staging.myshop.com/page')).to be false
+ expect(allowed_origin.matches?('https://staging.myshop.com:9000/page')).to be false
+ end
+ end
+ end
+
describe 'SingleStoreResource' do
it 'prevents changing store after creation' do
origin = create(:allowed_origin, store: store)
diff --git a/spree/core/spec/models/spree/store_spec.rb b/spree/core/spec/models/spree/store_spec.rb
index ea27f83e69..1e81d05e20 100644
--- a/spree/core/spec/models/spree/store_spec.rb
+++ b/spree/core/spec/models/spree/store_spec.rb
@@ -866,34 +866,22 @@ describe Spree::Store, type: :model, without_global_store: true do
create(:allowed_origin, store: store, origin: 'http://localhost')
end
- it 'matches exact origin' do
+ # Matching rules (scheme/host/port, loopback exemption, edge cases) are unit-tested
+ # on Spree::AllowedOrigin#matches?. These cover the store-level aggregation over
+ # multiple allowed origins and the public method's guards.
+ it 'matches when any allowed origin matches' do
expect(store.allowed_origin?('https://myshop.com/reset-password')).to be true
- end
-
- it 'matches origin regardless of port (port-less matching)' do
expect(store.allowed_origin?('http://localhost:3000/reset-password')).to be true
- expect(store.allowed_origin?('http://localhost:4000/anything')).to be true
- expect(store.allowed_origin?('http://localhost:5173')).to be true
- end
-
- it 'matches production origin with non-standard port' do
- expect(store.allowed_origin?('https://myshop.com:8080/checkout')).to be true
end
- it 'does not match different scheme' do
- expect(store.allowed_origin?('http://myshop.com/page')).to be false
- end
-
- it 'does not match different host' do
+ it 'does not match when no allowed origin matches' do
expect(store.allowed_origin?('https://evil.com/page')).to be false
+ expect(store.allowed_origin?('https://myshop.com:8080/checkout')).to be false
end
- it 'returns false for blank url' do
+ it 'returns false for blank or invalid url' do
expect(store.allowed_origin?(nil)).to be false
expect(store.allowed_origin?('')).to be false
- end
-
- it 'returns false for invalid URI' do
expect(store.allowed_origin?('not a url')).to be false
end
end