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: declared the CSRF posture on the two public OAuth endpoints

Damian Legawiece2aa7d5dda58
diff --git a/spree/api/app/controllers/spree/api/v3/oauth/authorizations_controller.rb b/spree/api/app/controllers/spree/api/v3/oauth/authorizations_controller.rb
index 11c62d04c4..e663e7adbb 100644
--- a/spree/api/app/controllers/spree/api/v3/oauth/authorizations_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/oauth/authorizations_controller.rb
@@ -17,6 +17,13 @@ module Spree
class AuthorizationsController < ActionController::Base
include Spree::Core::ControllerHelpers::Store
+ # The authorization endpoint is reached by a cross-site GET on
+ # purpose — a client sends the browser here — so a forgery token
+ # cannot be required. It is safe to leave open because the action
+ # only redirects: nothing is granted until the merchant submits
+ # consent to the Admin API, which does carry its own session.
+ protect_from_forgery with: :null_session
+
# Carried across to the dashboard. Everything else is dropped, so a
# crafted link cannot smuggle extra parameters into the consent page.
FORWARDED_PARAMETERS = %w[
diff --git a/spree/api/app/controllers/spree/api/v3/oauth/metadata_controller.rb b/spree/api/app/controllers/spree/api/v3/oauth/metadata_controller.rb
index 7d00499d89..69853efa46 100644
--- a/spree/api/app/controllers/spree/api/v3/oauth/metadata_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/oauth/metadata_controller.rb
@@ -12,6 +12,14 @@ module Spree
class MetadataController < ActionController::API
include Spree::Core::ControllerHelpers::Store
+ # A public discovery document, read before any credential exists.
+ # The module has to be included because `ActionController::API`
+ # carries no forgery protection at all — declaring `:null_session`
+ # states that this endpoint changes nothing, rather than leaving
+ # that true only by accident of the base class.
+ include ActionController::RequestForgeryProtection
+ protect_from_forgery with: :null_session
+
# The `resource` value must equal the URL the client was pointed at,
# path and all, or a consumer client refuses to continue — so it is
# echoed from the registered identifier rather than rebuilt here.
diff --git a/spree/api/spec/tools/spree/agent_tools/import_tools_spec.rb b/spree/api/spec/tools/spree/agent_tools/import_tools_spec.rb
index 9baa5c9758..39ddd037bd 100644
--- a/spree/api/spec/tools/spree/agent_tools/import_tools_spec.rb
+++ b/spree/api/spec/tools/spree/agent_tools/import_tools_spec.rb
@@ -95,6 +95,33 @@ RSpec.describe 'assistant import tools' do
expect(result[:number]).to eq(import.number)
expect(result[:status]).to eq('mapping')
end
+
+ # A validation message quotes the value that failed — "Sku 'ABC' has
+ # already been taken" — so the failure reasons can carry uploaded data
+ # that was never persisted. Counting rows is reading the resource;
+ # reading back what someone typed into a spreadsheet is not.
+ context 'when the caller may read the resource but not write it' do
+ let(:ability) do
+ Class.new do
+ include CanCan::Ability
+ def initialize = can(:manage, :all)
+ def permission_keys = %w[read_products read_customers]
+ end.new
+ end
+
+ it 'still reports the counts' do
+ result = described_class.new(context).call(id: import.prefixed_id)
+
+ expect(result[:error]).to be_nil
+ expect(result[:status]).to be_present
+ end
+
+ it 'withholds the failure reasons' do
+ result = described_class.new(context).call(id: import.prefixed_id)
+
+ expect(result).not_to have_key(:failure_reasons)
+ end
+ end
end
# An import holds the uploaded file — its headings, a real sample row, the
diff --git a/spree/core/app/tools/spree/agent_tools/get_import_status.rb b/spree/core/app/tools/spree/agent_tools/get_import_status.rb
index 74d433be0b..ad5c806fbb 100644
--- a/spree/core/app/tools/spree/agent_tools/get_import_status.rb
+++ b/spree/core/app/tools/spree/agent_tools/get_import_status.rb
@@ -35,7 +35,12 @@ module Spree
rows: import.rows_status_counts,
# The reasons, not the rows: a merchant fixes a spreadsheet by
# learning "12 rows have no SKU", not by reading 12 rows back.
- failure_reasons: failure_reasons(import)
+ #
+ # Gated on the write scope rather than the read one, because a
+ # validation message quotes the value that failed ("Sku 'X' has
+ # already been taken") — so it can carry uploaded data that was
+ # never persisted, which reading the resource does not cover.
+ failure_reasons: (failure_reasons(import) if may_read_failures?(import))
}.compact
end
@@ -45,6 +50,14 @@ module Spree
private
+ # @return [Boolean]
+ def may_read_failures?(import)
+ scope = import.class.try(:required_scope)
+ return true if scope.blank?
+
+ context.holds?("write_#{scope}")
+ end
+
# Grouped so the assistant can say "12 rows failed because the SKU was
# missing" rather than reciting every row.
#