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 destroy workflow was authorized as an update

Damian Legawieca7423c66e559
diff --git a/spree/api/spec/lib/spree/agent_tools/authorization_spec.rb b/spree/api/spec/lib/spree/agent_tools/authorization_spec.rb
index 74af1f6559..8be73eb591 100644
--- a/spree/api/spec/lib/spree/agent_tools/authorization_spec.rb
+++ b/spree/api/spec/lib/spree/agent_tools/authorization_spec.rb
@@ -116,6 +116,42 @@ RSpec.describe 'assistant authorization' do
end
end
+ # The stock catalog grants writes as `:manage`, which covers deletion —
+ # but an ability can be replaced, and a host app that grants `:update`
+ # without `:destroy` means it. A deletion checked as an update would go
+ # straight through.
+ context 'when the admin may edit a product but not delete one' do
+ let(:ability) do
+ Class.new do
+ include CanCan::Ability
+
+ def initialize(*)
+ can :read, Spree::Product
+ can :update, Spree::Product
+ end
+
+ def permission_keys
+ Spree.permissions.catalog_keys
+ end
+ end.new
+ end
+
+ it 'refuses the destroy workflow' do
+ destroy = Spree.agent_tools.available_for(context).
+ find { |candidate| candidate.tool_name == 'products_destroy' }
+ result = destroy.call(product: visible.prefixed_id)
+
+ expect(result[:error]).to be_present
+ expect(visible.reload).to be_present
+ end
+
+ it 'still allows the edit workflows' do
+ result = tool.call(product: visible.prefixed_id)
+
+ expect(result[:error]).to be_nil
+ end
+ end
+
# The rule that matters, and the one a `can :manage` fixture hides:
# reading every product and editing one is an ordinary shape for a
# marketplace role, and a write tool that only checks readability would
diff --git a/spree/core/lib/spree/agent_tools/workflow_tool.rb b/spree/core/lib/spree/agent_tools/workflow_tool.rb
index b445a22606..757cdbfc28 100644
--- a/spree/core/lib/spree/agent_tools/workflow_tool.rb
+++ b/spree/core/lib/spree/agent_tools/workflow_tool.rb
@@ -202,14 +202,26 @@ module Spree
# subject, since a cancellation reason from another seller is as much a
# leak as the order itself.
def unauthorized_record(arguments)
+ action = ability_action
+
records(arguments).each do |record|
- refusal = unauthorized(:update, record)
+ refusal = unauthorized(action, record)
return refusal if refusal
end
nil
end
+ # Which CanCanCan action this workflow amounts to. The stock catalog
+ # grants writes as `:manage`, which covers everything — but an ability
+ # may be replaced, and a host app that grants `:update` without
+ # `:destroy` means it: a deletion checked as an update would go through.
+ #
+ # @return [Symbol]
+ def ability_action
+ workflow_class.workflow_key.split('.').last == 'destroy' ? :destroy : :update
+ end
+
# Only what the caller named. The store and the principal are injected
# from the credential, so checking them would ask whether the caller may
# "update" their own store or their own user — which an order-desk role