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 bulk price row with a non-numeric amount could clear the price

Damian Legawiecdf59bbbcf57e
diff --git a/spree/core/app/services/spree/prices/bulk_upsert.rb b/spree/core/app/services/spree/prices/bulk_upsert.rb
index 12c1807835..b0d53d45f8 100644
--- a/spree/core/app/services/spree/prices/bulk_upsert.rb
+++ b/spree/core/app/services/spree/prices/bulk_upsert.rb
@@ -171,9 +171,8 @@ module Spree
Integer(raw.to_s.strip, 10, exception: false)
end
- # Rows pricing below zero. This path runs no model validations, so the
- # sign is checked here for every caller rather than in each of them.
- # Rows whose amount is text that is not a plain decimal ("1,599.99").
+ # Rows whose amount is not a number or plain decimal text ("1,599.99",
+ # `false`), refused before a malformed value can read as "clear".
def rows_with_malformed_amount(rows)
rows.each_with_index.filter_map do |row, index|
Spree::Money::Rounding.parse_decimal(row[:amount])
@@ -184,6 +183,8 @@ module Spree
end
end
+ # Rows pricing below zero. This path runs no model validations, so the
+ # sign is checked here for every caller rather than in each of them.
def rows_with_negative_amount(rows)
rows.each_with_index.filter_map do |row, index|
{ index: index } if row[:amount]&.negative? || row[:compare_at_amount]&.negative?
diff --git a/spree/core/lib/spree/money.rb b/spree/core/lib/spree/money.rb
index 542ab98a94..ee6e8edeab 100644
--- a/spree/core/lib/spree/money.rb
+++ b/spree/core/lib/spree/money.rb
@@ -124,10 +124,10 @@ module Spree
# @return [BigDecimal, nil]
# @raise [Spree::Money::InvalidFormat]
def parse_decimal(value)
- return if value.blank?
+ return if value.nil? || (value.is_a?(String) && value.strip.empty?)
return BigDecimal(value.to_s) if value.is_a?(Numeric)
- parse_canonical_decimal(value.to_s.strip)
+ parse_canonical_decimal(value.is_a?(String) ? value.strip : value)
end
# Writes an amount the way the API and exports carry it: exactly the
diff --git a/spree/core/spec/lib/spree/money_spec.rb b/spree/core/spec/lib/spree/money_spec.rb
index 4c434e7bc1..e2f32c5fbe 100644
--- a/spree/core/spec/lib/spree/money_spec.rb
+++ b/spree/core/spec/lib/spree/money_spec.rb
@@ -318,8 +318,10 @@ describe Spree::Money do
expect(described_class.parse_decimal('')).to be_nil
end
- it 'refuses text it would otherwise misread' do
- expect { described_class.parse_decimal('1,599.99') }.to raise_error(Spree::Money::InvalidFormat)
+ it 'refuses text it would otherwise misread, and values that are not numbers' do
+ ['1,599.99', false, true, []].each do |value|
+ expect { described_class.parse_decimal(value) }.to raise_error(Spree::Money::InvalidFormat)
+ end
end
end
diff --git a/spree/core/spec/services/spree/prices/bulk_upsert_spec.rb b/spree/core/spec/services/spree/prices/bulk_upsert_spec.rb
index b219d7f689..64c4c99130 100644
--- a/spree/core/spec/services/spree/prices/bulk_upsert_spec.rb
+++ b/spree/core/spec/services/spree/prices/bulk_upsert_spec.rb
@@ -136,6 +136,13 @@ RSpec.describe Spree::Prices::BulkUpsert do
expect(result.error.value).to eq(malformed_amounts: [{ index: 0 }])
end
+ it 'refuses an amount that is not a number rather than clearing the price' do
+ result = described_class.call(rows: [{ variant_id: variant.id, currency: 'USD', price_list_id: price_list.id, amount: false }])
+
+ expect(result).to be_failure
+ expect(override.reload.amount).to eq(BigDecimal('5.00'))
+ end
+
it 'drops rows missing variant_id or currency' do
result = described_class.call(rows: [{ amount: '9.99' }])