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.

Harden company and catalog pricing edges found in review

Damian Legawiec1e5e5097ae0d
diff --git a/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb
index aaf8d404c0..f8003866a5 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/orders_controller.rb
@@ -187,7 +187,7 @@ module Spree
end
def order_update_params
- normalize_params(
+ permitted = normalize_params(
params.permit(
:email, :customer_id, :user_id,
:customer_note, :internal_note,
@@ -200,6 +200,23 @@ module Spree
items: item_permitted_keys
)
)
+ resolve_company_param(permitted)
+ end
+
+ # An incidental lookup like any other: resolved through the store so
+ # another tenant's node 404s instead of surfacing as a validation
+ # error (which would confirm the id exists). Reads the raw param —
+ # normalize_params has already decoded the prefixed id.
+ def resolve_company_param(permitted)
+ return permitted unless permitted.key?(:company_id)
+
+ permitted[:company_id] =
+ if params[:company_id].present?
+ current_store.companies.find_by_prefix_id!(params[:company_id]).id
+ else
+ nil
+ end
+ permitted
end
def address_permitted_keys
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb
index 27b7b8e6f1..5ece2abaed 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/orders_controller_spec.rb
@@ -535,12 +535,14 @@ RSpec.describe Spree::Api::V3::Admin::OrdersController, type: :controller do
expect(order.reload.company).to eq(company)
end
- it 'refuses a node belonging to another store' do
+ # 404, not 422 — an existence answer for another tenant's id would be
+ # an oracle, and the standard scope-fetch already gives not-found.
+ it '404s a node belonging to another store' do
elsewhere = create(:company, store: create(:store))
patch :update, params: { id: order.prefixed_id, company_id: elsewhere.prefixed_id }, as: :json
- expect(response).to have_http_status(:unprocessable_content)
+ expect(response).to have_http_status(:not_found)
expect(order.reload.company_id).to be_nil
end
diff --git a/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb
index d37430ea1f..dcee82d48f 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb
@@ -635,6 +635,19 @@ RSpec.describe Spree::Api::V3::Store::CartsController, type: :controller do
expect(order.reload.company_id).to be_nil
end
+ # A guest holds only the cart token; company ids are not secrets, so a
+ # token-authorized cart must not be able to claim one.
+ it 'refuses a company on a guest cart' do
+ guest_cart = create(:cart_with_line_items, store: store, customer: nil)
+ request.headers['Authorization'] = nil
+ request.headers['X-Spree-Token'] = guest_cart.token
+
+ patch :update, params: { id: guest_cart.prefixed_id, company_id: company.prefixed_id }
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(guest_cart.reload.company_id).to be_nil
+ end
+
it '404s a node of another store' do
foreign = create(:company, store: create(:store))
diff --git a/spree/core/app/models/concerns/spree/purchase/company.rb b/spree/core/app/models/concerns/spree/purchase/company.rb
index 943afafd59..f52cf7cc22 100644
--- a/spree/core/app/models/concerns/spree/purchase/company.rb
+++ b/spree/core/app/models/concerns/spree/purchase/company.rb
@@ -18,9 +18,13 @@ module Spree
validate :company_belongs_to_store, if: :company_id_changing?
# The storefront's write path is the cart; an order's company arrives
# from completion (already validated on the cart) or from staff, whose
- # authority is the admin credential rather than a membership.
+ # authority is the admin credential rather than a membership. A guest
+ # cart has no standing at all, so naming a company on one is refused
+ # outright — company ids are not secrets, and a token-authorized cart
+ # must not be able to claim another business's tax exemptions and
+ # catalog prices.
validate :customer_has_standing_over_company,
- if: -> { is_a?(Spree::Cart) && company_id_changing? && customer_id.present? }
+ if: -> { is_a?(Spree::Cart) && company_id_changing? }
end
# @return [Boolean] whether this sale is for a business customer
@@ -72,7 +76,7 @@ module Spree
def customer_has_standing_over_company
return if company.nil?
- return if customer.standing_for?(company)
+ return if customer&.standing_for?(company)
errors.add(:company, :invalid)
end
diff --git a/spree/core/app/models/spree/company.rb b/spree/core/app/models/spree/company.rb
index 38f9dc2bed..f01915da10 100644
--- a/spree/core/app/models/spree/company.rb
+++ b/spree/core/app/models/spree/company.rb
@@ -223,12 +223,18 @@ module Spree
end
# A legal entity that holds registrations or certificates cannot become a
- # division — its purchases would silently lose their tax identity.
+ # division — its purchases would silently lose their tax identity. Nor can
+ # one that anchors division children: their legal_entity would silently
+ # re-resolve to a higher ancestor, changing the registration and
+ # certificates applied to their future purchases.
def kind_change_keeps_tax_anchor
return unless persisted? && will_save_change_to_kind? && kind == 'division'
- return unless tax_identifiers.exists? || tax_exemption_certificates.exists?
- errors.add(:kind, :holds_tax_registrations)
+ if tax_identifiers.exists? || tax_exemption_certificates.exists?
+ errors.add(:kind, :holds_tax_registrations)
+ elsif children.where(kind: 'division').exists?
+ errors.add(:kind, :anchors_descendant_divisions)
+ end
end
end
end
diff --git a/spree/core/app/models/spree/pricing_provider/internal.rb b/spree/core/app/models/spree/pricing_provider/internal.rb
index 32315e3bee..fcb327633d 100644
--- a/spree/core/app/models/spree/pricing_provider/internal.rb
+++ b/spree/core/app/models/spree/pricing_provider/internal.rb
@@ -54,7 +54,9 @@ module Spree
end
# Price lists reached through the buyer's effective catalogs, in
- # catalog resolution order. A catalog-attached list applies because
+ # catalog resolution order — company subtree first, then customer
+ # groups, then the channel's default catalog (the same fallback
+ # chain visibility uses). A catalog-attached list applies because
# the catalog applies — its own rules are not consulted, only its
# status and dates.
# @return [Array<Spree::PriceList>]
@@ -65,6 +67,9 @@ module Spree
groups = context.user.try(:customer_groups)&.where(store_id: context.store.id) || []
catalogs = Spree::Catalog.effective_for_customer_groups(groups)
end
+ if catalogs.empty?
+ catalogs = [context.channel&.default_catalog].compact.select(&:active?)
+ end
catalogs.filter_map(&:price_list).uniq.select(&:currently_active?)
end
@@ -81,10 +86,13 @@ module Spree
select { |list| list.applicable?(context) }
end
+ # Only ACTIVE catalogs claim their list: an inactive catalog is off,
+ # and blacklisting its list would silently kill a rule-based list
+ # that was working before the catalog draft existed.
def catalog_bound_price_list_ids
@catalog_bound_price_list_ids ||=
if context.store
- Spree::Catalog.where(store_id: context.store.id).where.not(price_list_id: nil).
+ Spree::Catalog.active.where(store_id: context.store.id).where.not(price_list_id: nil).
distinct.pluck(:price_list_id).to_set
else
Set.new
diff --git a/spree/core/app/services/spree/companies/add_member.rb b/spree/core/app/services/spree/companies/add_member.rb
index 69f50d0c8f..41b4bee7e4 100644
--- a/spree/core/app/services/spree/companies/add_member.rb
+++ b/spree/core/app/services/spree/companies/add_member.rb
@@ -14,7 +14,11 @@ module Spree
# @param email [String]
# @param inviter [Object, nil] the inviting customer, nil when staff
# @param role [String, nil] cosmetic membership label
- # @param metadata [Hash, nil] extension payload carried onto the record
+ # @param metadata [Hash, nil] extension payload — carried on the
+ # invitation only, where Enterprise reads it back on acceptance
+ # (memberships have no metadata column; a direct-membership payload is
+ # applied by the caller through the `company_membership.form_fields`
+ # contract, not stored here)
# @return [Spree::ServiceModule::Result] value is the created
# Spree::CompanyMembership or Spree::CompanyInvitation
def call(company:, email:, inviter: nil, role: nil, metadata: nil)
@@ -25,7 +29,6 @@ module Spree
if customer
attributes = { customer: customer }
attributes[:role] = role if role.present?
- attributes[:metadata] = metadata if metadata.present?
company.memberships.new(attributes)
else
company.invitations.new(email: normalized_email, inviter: inviter, metadata: metadata)
diff --git a/spree/core/config/locales/en.yml b/spree/core/config/locales/en.yml
index 416a24bbf7..46727c58c5 100644
--- a/spree/core/config/locales/en.yml
+++ b/spree/core/config/locales/en.yml
@@ -262,6 +262,7 @@ en:
spree/company:
attributes:
kind:
+ anchors_descendant_divisions: cannot become a division while divisions below it anchor their tax identity here
holds_tax_registrations: cannot become a division while holding tax registrations or exemption certificates
root_must_be_legal_entity: must be company for a root node
parent:
diff --git a/spree/core/spec/lib/spree/core/pricing/catalog_pricing_spec.rb b/spree/core/spec/lib/spree/core/pricing/catalog_pricing_spec.rb
index 9355928b2e..772254e910 100644
--- a/spree/core/spec/lib/spree/core/pricing/catalog_pricing_spec.rb
+++ b/spree/core/spec/lib/spree/core/pricing/catalog_pricing_spec.rb
@@ -68,4 +68,27 @@ describe 'catalog-aware pricing' do
expect(resolve(company: company).amount).to eq(100)
end
+
+ it 'prices through the channel default catalog when nothing narrower applies' do
+ catalog = create(:catalog, store: store, price_list: price_list_with_price(95))
+ channel = store.default_channel
+ channel.update!(default_catalog: catalog)
+
+ context = Spree::Pricing::Context.new(variant: variant, currency: 'USD', store: store,
+ channel: channel)
+
+ expect(Spree::Pricing::Resolver.new(context).resolve.amount).to eq(95)
+ end
+
+ # An inactive catalog is off: it must neither price its audience nor keep
+ # claiming its list away from the generic rule matcher — a rule-less list
+ # that worked before the catalog draft existed has to keep working.
+ it 'releases a price list back to rule matching while its catalog is inactive' do
+ price_list = price_list_with_price(80)
+ catalog = create(:catalog, store: store, price_list: price_list, active: false)
+ create(:catalog_assignment, catalog: catalog, assignable: company)
+
+ expect(resolve(company: company).amount).to eq(80)
+ expect(resolve.amount).to eq(80)
+ end
end
diff --git a/spree/core/spec/models/concerns/spree/purchase/company_spec.rb b/spree/core/spec/models/concerns/spree/purchase/company_spec.rb
index 6da2392efd..f72d796716 100644
--- a/spree/core/spec/models/concerns/spree/purchase/company_spec.rb
+++ b/spree/core/spec/models/concerns/spree/purchase/company_spec.rb
@@ -167,6 +167,17 @@ RSpec.describe Spree::Purchase::Company do
expect(cart).not_to be_valid
end
+ # Company ids are not secrets — they appear in responses and invite links
+ # — so a token-authorized guest cart must not be able to claim one and
+ # ride its tax exemptions and catalog prices through checkout.
+ it 'refuses a company on a guest cart' do
+ guest_cart = create(:cart, store: store, customer: nil)
+ guest_cart.company = company
+
+ expect(guest_cart).not_to be_valid
+ expect(guest_cart.errors[:company]).to be_present
+ end
+
it 'does not re-run once the node is stamped' do
create(:company_membership, company: company, customer: customer)
cart.update!(company: company)
diff --git a/spree/core/spec/models/spree/company_spec.rb b/spree/core/spec/models/spree/company_spec.rb
index 1e51525319..3f31dadb3c 100644
--- a/spree/core/spec/models/spree/company_spec.rb
+++ b/spree/core/spec/models/spree/company_spec.rb
@@ -174,6 +174,30 @@ describe Spree::Company, type: :model do
expect(child).not_to be_valid
expect(child.errors[:kind]).to be_present
end
+
+ # Even without registrations of its own, a company node anchors the
+ # divisions below it — demoting it would silently re-resolve their
+ # legal_entity to a higher ancestor.
+ it 'refuses demoting a company that anchors division children' do
+ root = create(:company, store: store)
+ child = create(:company, store: store, parent: root)
+ create(:company, store: store, kind: 'division', parent: child)
+
+ child.kind = 'division'
+
+ expect(child).not_to be_valid
+ expect(child.errors[:kind]).to be_present
+ end
+
+ it 'allows demoting a company whose children are all companies' do
+ root = create(:company, store: store)
+ child = create(:company, store: store, parent: root)
+ create(:company, store: store, parent: child)
+
+ child.kind = 'division'
+
+ expect(child).to be_valid
+ end
end
describe 'default addresses' do