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: limited Store and Seller API filters to what those audiences may read

Damian Legawiecdaeec9965e1d
diff --git a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
index b7eb34ebf8..428fde5049 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/products_controller.rb
@@ -301,7 +301,7 @@ module Spree
end
def search_provider
- @search_provider ||= Spree::SearchProvider::Database.new(current_store)
+ @search_provider ||= Spree::SearchProvider::Database.new(current_store, audience: nil)
end
# Tag changes can flip automatic-collection matches, and `Tags::Bulk*`
diff --git a/spree/api/app/controllers/spree/api/v3/resource_controller.rb b/spree/api/app/controllers/spree/api/v3/resource_controller.rb
index 84466ea4fe..22d5f43890 100644
--- a/spree/api/app/controllers/spree/api/v3/resource_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/resource_controller.rb
@@ -254,7 +254,7 @@ module Spree
@search = scope.includes(collection_includes).
preload_associations_lazily.
- ransack(ransack_params)
+ ransack(ransack_params, auth_object: ransack_auth_object)
result = @search.result(distinct: collection_distinct?)
pagy_options = { limit: limit, page: page }
result = apply_collection_sort(result)
@@ -262,6 +262,16 @@ module Spree
@collection
end
+ # Who is filtering, as Ransack's auth object. Nil is the back office,
+ # which may use every allowlisted attribute; the Store and Seller
+ # branches name themselves so the models narrow what they may reach
+ # (see Spree::RansackableAttributes). Covers sorting too.
+ #
+ # @return [Symbol, nil]
+ def ransack_auth_object
+ nil
+ end
+
# Override in subclass to disable distinct (e.g., for custom sorting with computed columns)
# @return [Boolean] whether to apply distinct to the collection
def collection_distinct?
diff --git a/spree/api/app/controllers/spree/api/v3/seller/resource_controller.rb b/spree/api/app/controllers/spree/api/v3/seller/resource_controller.rb
index 43c84ef95e..16721a9542 100644
--- a/spree/api/app/controllers/spree/api/v3/seller/resource_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/seller/resource_controller.rb
@@ -18,6 +18,10 @@ module Spree
authenticate_seller!
end
+ def ransack_auth_object
+ :seller
+ end
+
# Every collection is rooted in the seller's own records.
#
# The inherited implementation roots at `for_store(current_store)`,
diff --git a/spree/api/app/controllers/spree/api/v3/store/resource_controller.rb b/spree/api/app/controllers/spree/api/v3/store/resource_controller.rb
index 4a551cb3c9..231b7c262b 100644
--- a/spree/api/app/controllers/spree/api/v3/store/resource_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/store/resource_controller.rb
@@ -28,6 +28,10 @@ module Spree
def authenticate_request!
authenticate_api_key!
end
+
+ def ransack_auth_object
+ :store
+ end
end
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/commission_lines_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/commission_lines_controller_spec.rb
index afc4d21fc6..d45a19c6a5 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/commission_lines_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/commission_lines_controller_spec.rb
@@ -40,6 +40,19 @@ RSpec.describe Spree::Api::V3::Admin::CommissionLinesController, type: :controll
expect(row['display_total']).to eq('$12.10')
end
+ # The order page lists an order's commission by its id; filtering through
+ # the order itself would reach the buyer, which a commissions-only caller
+ # may not read.
+ it 'filters by the order id but not through the order' do
+ get :index, params: { q: { order_id_eq: order.prefixed_id, order_email_start: 'zzz' } }, as: :json
+
+ expect(json_response['data'].map { |row| row['id'] }).to eq([commission_line.prefixed_id])
+
+ get :index, params: { q: { order_id_eq: create(:order, store: store).prefixed_id } }, as: :json
+
+ expect(json_response['data']).to be_empty
+ end
+
it "hides another marketplace's commission" do
other_store = create(:store)
other = create(:commission_line,
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/seller_transfers_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/seller_transfers_controller_spec.rb
index f8344f21fc..a7f9f18ea7 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/seller_transfers_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/seller_transfers_controller_spec.rb
@@ -27,6 +27,20 @@ RSpec.describe Spree::Api::V3::Admin::SellerTransfersController, type: :controll
expect(row['kind']).to eq('earning')
end
+ # Filtering through the order reaches the buyer's email and address,
+ # which a payouts-only caller may not read.
+ it 'ignores filters through the order' do
+ get :index, params: { q: { order_email_start: 'zzz' } }, as: :json
+
+ expect(json_response['data'].map { |row| row['id'] }).to eq([transfer.prefixed_id])
+ end
+
+ it 'filters by the order id' do
+ get :index, params: { q: { order_id_eq: create(:order, store: store).prefixed_id } }, as: :json
+
+ expect(json_response['data']).to be_empty
+ end
+
it "hides another marketplace's ledger" do
other_store = create(:store)
other_seller = create(:seller, store: other_store)
diff --git a/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb
index f3bdbfd736..feb00b4bee 100644
--- a/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/seller/orders_controller_spec.rb
@@ -33,6 +33,15 @@ RSpec.describe Spree::Api::V3::Seller::OrdersController, type: :controller do
expect(numbers).not_to include(theirs.number)
end
+ # The buyer's email is hidden from sellers, and the buyer's other orders
+ # belong to other sellers; a filter on either answers a yes/no question
+ # about them, so it is ignored.
+ it 'ignores filters on the buyer and the buyer\'s other orders' do
+ get :index, params: { q: { email_start: 'zzz', customer_orders_total_gt: 1_000_000, search: 'zzz' } }, as: :json
+
+ expect(json_response['data'].map { |row| row['number'] }).to eq([mine.number])
+ end
+
# A draft carrying its cart is a checkout still in flight; it is nobody's
# order yet and must not appear in a seller's list.
it 'leaves out a checkout still in flight' do
diff --git a/spree/api/spec/controllers/spree/api/v3/store/customer/orders_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/customer/orders_controller_spec.rb
index 4d9e307686..5cc5e02a91 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/customer/orders_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/customer/orders_controller_spec.rb
@@ -39,6 +39,21 @@ RSpec.describe Spree::Api::V3::Store::Customer::OrdersController, type: :control
expect(numbers).to eq([order.number])
end
+ # Filters are yes/no questions: one that could follow the order into its
+ # promotions, or test the email, would answer about data the customer
+ # never sees (another shopper's unused coupon codes, staff fields).
+ it 'ignores filters that reach beyond the order itself' do
+ get :index, params: { q: { promotions_coupon_codes_code_start: 'zzz', email_start: 'zzz', considered_risky_eq: true } }
+
+ expect(json_response['data'].map { |o| o['number'] }).to eq([order.number])
+ end
+
+ it 'still filters on the order itself' do
+ get :index, params: { q: { number_eq: 'nope' } }
+
+ expect(json_response['data']).to be_empty
+ end
+
it 'returns pagination metadata' do
get :index
diff --git a/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
index b6a7f14eeb..e27aa62a98 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/products_controller_spec.rb
@@ -122,6 +122,32 @@ RSpec.describe Spree::Api::V3::Store::ProductsController, type: :controller do
)
end
+ # The storefront may filter by tag, category and collection, never by
+ # back-office values (cost price, price-list prices, channel codes): each
+ # such filter answers a yes/no question about data it cannot read.
+ context 'with filters on back-office data' do
+ it 'ignores them' do
+ get :index, params: { q: { default_variant_cost_price_lt: -1, variants_prices_amount_lt: -1, channels_code_eq: 'nope' } }
+
+ expect(json_response['meta']['count']).to eq(2)
+ end
+
+ it 'ignores a sort through the variants' do
+ get :index, params: { sort: 'variants_cost_price' }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['meta']['count']).to eq(2)
+ end
+
+ it 'still filters by tag' do
+ product.update!(tag_list: ['sale'])
+
+ get :index, params: { q: { tags_name_cont: 'sale' } }
+
+ expect(json_response['data'].map { |p| p['id'] }).to eq([product.prefixed_id])
+ end
+ end
+
it 'respects max limit limit' do
get :index, params: { limit: 500 }
diff --git a/spree/core/app/models/concerns/spree/ransackable_attributes.rb b/spree/core/app/models/concerns/spree/ransackable_attributes.rb
index bc2d0c520a..ae9a81f068 100644
--- a/spree/core/app/models/concerns/spree/ransackable_attributes.rb
+++ b/spree/core/app/models/concerns/spree/ransackable_attributes.rb
@@ -1,10 +1,26 @@
module Spree::RansackableAttributes
extend ActiveSupport::Concern
+
+ # Ransack `auth_object` values for callers outside the back office. The
+ # allowlists below were written for staff; a shopper or a seller filtering
+ # on them would get a yes/no answer about data their responses never show.
+ RESTRICTED_AUDIENCES = %i[store seller].freeze
+
included do
class_attribute :whitelisted_ransackable_associations
class_attribute :whitelisted_ransackable_attributes
class_attribute :whitelisted_ransackable_scopes
+ # Associations the Store API may filter through. Empty by default: every
+ # hop is a join into data the storefront was never meant to query, and a
+ # join back to the model it came from lets one filter chain them forever.
+ class_attribute :storefront_ransackable_associations, default: []
+
+ # `{ store: [...], seller: [...] }` — attributes and scopes a restricted
+ # audience may not filter on, although staff may.
+ class_attribute :private_ransackable_attributes, default: {}
+ class_attribute :private_ransackable_scopes, default: {}
+
class_attribute :default_ransackable_attributes
# `position` is included so any `acts_as_list` model is sortable by it
# without each subclass having to opt in. Ransack ignores attributes
@@ -12,19 +28,35 @@ module Spree::RansackableAttributes
# without a position column.
self.default_ransackable_attributes = %w[id name updated_at created_at position]
- def self.ransackable_associations(*_args)
+ def self.ransackable_associations(auth_object = nil)
+ if Spree::RansackableAttributes.restricted_audience?(auth_object)
+ return auth_object.to_sym == :store ? storefront_ransackable_associations : []
+ end
+
base = whitelisted_ransackable_associations || []
base | Spree.ransack.custom_associations_for(self)
end
- def self.ransackable_attributes(*_args)
+ def self.ransackable_attributes(auth_object = nil)
base = default_ransackable_attributes | (whitelisted_ransackable_attributes || [])
- base | Spree.ransack.custom_attributes_for(self)
+ base |= Spree.ransack.custom_attributes_for(self)
+ return base unless Spree::RansackableAttributes.restricted_audience?(auth_object)
+
+ base - Array(private_ransackable_attributes[auth_object.to_sym])
end
- def self.ransackable_scopes(*_args)
- base = whitelisted_ransackable_scopes || []
- base | Spree.ransack.custom_scopes_for(self)
+ def self.ransackable_scopes(auth_object = nil)
+ base = (whitelisted_ransackable_scopes || []).map(&:to_s)
+ base |= Spree.ransack.custom_scopes_for(self).map(&:to_s)
+ return base unless Spree::RansackableAttributes.restricted_audience?(auth_object)
+
+ base - Array(private_ransackable_scopes[auth_object.to_sym])
end
end
+
+ # @param auth_object [Symbol, nil] the Ransack auth object a search ran with
+ # @return [Boolean] whether it names a caller outside the back office
+ def self.restricted_audience?(auth_object)
+ auth_object.respond_to?(:to_sym) && RESTRICTED_AUDIENCES.include?(auth_object.to_sym)
+ end
end
diff --git a/spree/core/app/models/spree/commission_line.rb b/spree/core/app/models/spree/commission_line.rb
index 9fb6131f59..085f3648e6 100644
--- a/spree/core/app/models/spree/commission_line.rb
+++ b/spree/core/app/models/spree/commission_line.rb
@@ -61,8 +61,10 @@ module Spree
scope :for_line_items, -> { where.not(line_item_id: nil) }
scope :for_fulfillments, -> { where.not(fulfillment_id: nil) }
- self.whitelisted_ransackable_attributes = %w[amount tax_amount total currency kind rate]
- self.whitelisted_ransackable_associations = %w[order seller commission_rate]
+ self.whitelisted_ransackable_attributes = %w[amount tax_amount total currency kind rate order_id]
+ # No `order`: filtering through it reaches the buyer's email and address,
+ # which a commissions-only caller may not read. `order_id` covers the lookup.
+ self.whitelisted_ransackable_associations = %w[seller commission_rate]
extend Spree::DisplayMoney
money_methods :amount, :tax_amount, :total
diff --git a/spree/core/app/models/spree/export.rb b/spree/core/app/models/spree/export.rb
index b75a77b24c..534b402a3b 100644
--- a/spree/core/app/models/spree/export.rb
+++ b/spree/core/app/models/spree/export.rb
@@ -219,7 +219,9 @@ module Spree
[scope, params]
end
- filtered_scope.ransack(params).result
+ # A seller's export filters as the seller's own listings do, so a
+ # condition on data the seller cannot read (the buyer's email) is ignored.
+ filtered_scope.ransack(params, auth_object: seller_id.present? ? :seller : nil).result
end
# `search_params` as a Hash — whether it is still the Hash a caller
diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rb
index 7fa8c5edbe..84c814ac26 100644
--- a/spree/core/app/models/spree/order.rb
+++ b/spree/core/app/models/spree/order.rb
@@ -170,6 +170,14 @@ module Spree
order_group_id po_number
]
self.whitelisted_ransackable_scopes = %w[complete incomplete refunded partially_refunded search multi_search]
+ # A seller never sees the buyer's email, and a company member filtering the
+ # company's orders must not learn a colleague's; the risk flag and coupon
+ # are back-office data. `search` matches on the email too.
+ self.private_ransackable_attributes = {
+ store: %w[email considered_risky coupon_code],
+ seller: %w[email considered_risky coupon_code]
+ }
+ self.private_ransackable_scopes = { seller: %w[search multi_search] }
# Set to false on admin-initiated flows to suppress customer-facing emails.
attr_accessor :notify_customer
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index 5033ed916f..dd2a1a6f9a 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -369,6 +369,10 @@ module Spree
self.whitelisted_ransackable_attributes = %w[description name slug discontinue_on status available_on created_at updated_at seller_id]
self.whitelisted_ransackable_associations = %w[categories collections store channels variants default_variant tags labels
product_type product_categories option_types seller]
+ # The storefront filters by tag, category and collection; every other hop
+ # (variants, prices, channels) reaches back-office data, and variants lead
+ # back to the product, which lets one filter chain joins without end.
+ self.storefront_ransackable_associations = %w[tags categories collections]
self.whitelisted_ransackable_scopes = %w[not_discontinued search_by_name in_taxon in_category in_categories in_collection price_between
price_lte price_gte
search multi_search in_stock out_of_stock with_option_value_ids
diff --git a/spree/core/app/models/spree/search_provider/database.rb b/spree/core/app/models/spree/search_provider/database.rb
index 10a67366db..e8e04ab33f 100644
--- a/spree/core/app/models/spree/search_provider/database.rb
+++ b/spree/core/app/models/spree/search_provider/database.rb
@@ -7,6 +7,14 @@ module Spree
'best_selling' => :by_best_selling
}.freeze
+ # @param store [Spree::Store]
+ # @param audience [Symbol, nil] who filters: `:store` (shoppers) limits
+ # filters to what the storefront may query; `nil` is the back office
+ def initialize(store, audience: :store)
+ super(store)
+ @audience = audience
+ end
+
def search_and_filter(scope:, query: nil, filters: {}, sort: nil, page: 1, limit: 25)
filters = filters.is_a?(Hash) ? filters.dup : {}
option_value_ids = filters.delete('with_option_value_ids') || filters.delete(:with_option_value_ids)
@@ -72,7 +80,7 @@ module Spree
ransack_filters = sanitize_filters(filters)
if ransack_filters.present?
- search = scope.ransack(ransack_filters)
+ search = scope.ransack(ransack_filters, auth_object: @audience)
scope = search.result(distinct: true)
end
@@ -127,7 +135,7 @@ module Spree
end
}.join(',')
- scope.ransack(s: ransack_sort).result
+ scope.ransack({ s: ransack_sort }, auth_object: @audience).result
end
end
diff --git a/spree/core/app/models/spree/seller_payout.rb b/spree/core/app/models/spree/seller_payout.rb
index e09f94362e..71ee18371f 100644
--- a/spree/core/app/models/spree/seller_payout.rb
+++ b/spree/core/app/models/spree/seller_payout.rb
@@ -58,7 +58,9 @@ module Spree
scope :owed, -> { where(status: %w[pending processing]) }
self.whitelisted_ransackable_attributes = %w[amount currency status provider reference period_start period_end created_at seller_id]
- self.whitelisted_ransackable_associations = %w[seller transfers]
+ # No `transfers`: they lead on to the order and its buyer, which a
+ # payouts-only caller may not read.
+ self.whitelisted_ransackable_associations = %w[seller]
extend Spree::DisplayMoney
money_methods :amount
diff --git a/spree/core/app/models/spree/seller_transfer.rb b/spree/core/app/models/spree/seller_transfer.rb
index e56e4dab8f..1890f25a8e 100644
--- a/spree/core/app/models/spree/seller_transfer.rb
+++ b/spree/core/app/models/spree/seller_transfer.rb
@@ -112,7 +112,9 @@ module Spree
end
self.whitelisted_ransackable_attributes = %w[amount currency kind status provider reference created_at seller_id order_id payout_id]
- self.whitelisted_ransackable_associations = %w[seller order payout refund]
+ # No `order`: filtering through it reaches the buyer's email and address,
+ # which a payouts-only caller may not read. `order_id` covers the lookup.
+ self.whitelisted_ransackable_associations = %w[seller payout refund]
extend Spree::DisplayMoney
money_methods :amount
diff --git a/spree/core/app/models/spree/store_credit.rb b/spree/core/app/models/spree/store_credit.rb
index 754604e807..ed5edc3cca 100644
--- a/spree/core/app/models/spree/store_credit.rb
+++ b/spree/core/app/models/spree/store_credit.rb
@@ -113,6 +113,8 @@ module Spree
self.whitelisted_ransackable_attributes = %w[customer_id created_by_id amount currency memo]
self.whitelisted_ransackable_associations = %w[customer created_by]
self.whitelisted_ransackable_scopes = %w[outstanding from_gift_card]
+ # Staff notes and who issued the credit are back-office data.
+ self.private_ransackable_attributes = { store: %w[memo created_by_id] }
# Two-state scopes: see Spree::Base.ransack_flag? for why the cast is
# opted out of here and done inside each scope instead.
diff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rb
index b01381c7b7..6647bf8d90 100644
--- a/spree/core/app/models/spree/variant.rb
+++ b/spree/core/app/models/spree/variant.rb
@@ -330,6 +330,7 @@ module Spree
carton_package_type_id carton_weight cartons_per_pallet]
self.whitelisted_ransackable_scopes = %i(product_name_or_sku_cont search_by_product_name_or_sku search
available_at_stock_location)
+ self.private_ransackable_attributes = { store: %w[cost_price cost_currency deleted_at] }
def self.product_name_or_sku_cont(query)
sanitized_query = ActiveRecord::Base.sanitize_sql_like(query.to_s.downcase.strip)
diff --git a/spree/core/spec/models/concerns/spree/ransackable_attributes_spec.rb b/spree/core/spec/models/concerns/spree/ransackable_attributes_spec.rb
new file mode 100644
index 0000000000..d6d0e72adf
--- /dev/null
+++ b/spree/core/spec/models/concerns/spree/ransackable_attributes_spec.rb
@@ -0,0 +1,37 @@
+require 'spec_helper'
+
+RSpec.describe Spree::RansackableAttributes do
+ describe 'for the back office' do
+ it 'allows every allowlisted association and attribute' do
+ expect(Spree::Order.ransackable_associations).to include('customer', 'promotions')
+ expect(Spree::Order.ransackable_attributes).to include('email')
+ expect(Spree::Variant.ransackable_attributes).to include('cost_price')
+ end
+ end
+
+ describe 'for the Store API' do
+ it 'reaches only the associations a storefront filters by' do
+ expect(Spree::Product.ransackable_associations(:store)).to match_array(%w[tags categories collections])
+ expect(Spree::Order.ransackable_associations(:store)).to be_empty
+ end
+
+ it 'drops private attributes' do
+ expect(Spree::Order.ransackable_attributes(:store)).not_to include('email', 'considered_risky')
+ expect(Spree::Order.ransackable_attributes(:store)).to include('number')
+ expect(Spree::Variant.ransackable_attributes(:store)).not_to include('cost_price')
+ expect(Spree::StoreCredit.ransackable_attributes(:store)).not_to include('memo')
+ end
+ end
+
+ describe 'for the Seller API' do
+ it 'reaches no associations' do
+ expect(Spree::Order.ransackable_associations(:seller)).to be_empty
+ expect(Spree::Product.ransackable_associations(:seller)).to be_empty
+ end
+
+ it "drops the buyer's email and the searches that match on it" do
+ expect(Spree::Order.ransackable_attributes(:seller)).not_to include('email')
+ expect(Spree::Order.ransackable_scopes(:seller)).not_to include('search', 'multi_search')
+ end
+ end
+end
diff --git a/spree/core/spec/models/spree/export_spec.rb b/spree/core/spec/models/spree/export_spec.rb
index 9f84f68717..35a4afbcd1 100644
--- a/spree/core/spec/models/spree/export_spec.rb
+++ b/spree/core/spec/models/spree/export_spec.rb
@@ -221,6 +221,15 @@ RSpec.describe Spree::Export, :job, type: :model do
expect(headers).to include('Notes')
end
+ # Which orders land in the file would otherwise answer whether a buyer's
+ # email matches the condition, one guess per export.
+ it "ignores a condition on the buyer's email" do
+ export = create(:order_export, store: store, seller: seller, user: nil,
+ search_params: { email_start: 'zzz' }.to_json)
+
+ expect(export.records_to_export).to include(order)
+ end
+
it "leaves the operator's own export untouched" do
expect(operator_export.csv_headers).to include('Email')
expect(rows_for(operator_export).first['Email']).to eq(order.email)