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.

Fixed GHSA-g268-72p7-9j6j Authenticated Insecure Direct Object Refere… (#13423)

Damian Legawiec6591f4c9acce
diff --git a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
index af9a5c796d..a0eadade16 100644
--- a/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
+++ b/api/spec/requests/spree/api/v2/storefront/checkout_spec.rb
@@ -489,6 +489,104 @@ describe 'API V2 Storefront Checkout Spec', type: :request do
end
end
+ describe 'checkout#update address ownership validation (IDOR protection)' do
+ let!(:state) { create(:state) }
+ let!(:country) { state.country }
+ let(:other_user) { create(:user_with_addresses) }
+ let(:other_user_address) { other_user.bill_address }
+ let(:execute) { patch '/api/v2/storefront/checkout', params: params, headers: headers }
+
+ context 'as a signed in user' do
+ include_context 'creates order with line item'
+
+ context 'when attempting to use another user\'s address via bill_address_attributes' do
+ let(:params) do
+ {
+ order: {
+ bill_address_attributes: { id: other_user_address.id }
+ }
+ }
+ end
+
+ before { execute }
+
+ it 'returns 422 error' do
+ expect(response.status).to eq(422)
+ end
+
+ it 'does not associate the other user\'s address with the order' do
+ expect(order.reload.bill_address_id).not_to eq(other_user_address.id)
+ end
+
+ it 'returns an error message' do
+ expect(json_response['error']).to be_present
+ end
+ end
+
+ context 'when attempting to use another user\'s address via ship_address_attributes' do
+ let(:params) do
+ {
+ order: {
+ ship_address_attributes: { id: other_user_address.id }
+ }
+ }
+ end
+
+ before { execute }
+
+ it 'returns 422 error' do
+ expect(response.status).to eq(422)
+ end
+
+ it 'does not associate the other user\'s address with the order' do
+ expect(order.reload.ship_address_id).not_to eq(other_user_address.id)
+ end
+ end
+
+ context 'when using own address' do
+ let(:user) { create(:user_with_addresses) }
+ let(:user_address) { user.bill_address }
+ let(:params) do
+ {
+ order: {
+ bill_address_attributes: { id: user_address.id }
+ }
+ }
+ end
+
+ before { execute }
+
+ it 'returns success' do
+ expect(response.status).to eq(200)
+ end
+ end
+ end
+
+ context 'as a guest user' do
+ include_context 'creates guest order with guest token'
+
+ context 'when attempting to use another user\'s address' do
+ let(:params) do
+ {
+ order: {
+ bill_address_attributes: { id: other_user_address.id }
+ }
+ }
+ end
+
+ before { execute }
+
+ it 'returns 422 error' do
+ expect(response.status).to eq(422)
+ end
+
+ it 'does not associate the other user\'s address with the order' do
+ expect(order.reload.bill_address_id).not_to eq(other_user_address.id)
+ end
+ end
+ end
+ end
+
describe 'checkout#add_store_credit' do
let(:order_total) { 500.00 }
let(:params) { { order_token: order.token } }
diff --git a/core/app/services/spree/checkout/update.rb b/core/app/services/spree/checkout/update.rb
index 8aa1ac8390..93f5eff155 100644
--- a/core/app/services/spree/checkout/update.rb
+++ b/core/app/services/spree/checkout/update.rb
@@ -5,6 +5,10 @@ module Spree
include Spree::Addresses::Helper
def call(order:, params:, permitted_attributes:, request_env:)
+ # Validate address ownership to prevent IDOR attacks
+ address_ownership_error = validate_address_ownership(order, params)
+ return failure(order, address_ownership_error) if address_ownership_error
+
ship_changed = address_with_country_iso_present?(params, 'ship')
bill_changed = address_with_country_iso_present?(params, 'bill')
params[:order][:ship_address_attributes] = replace_country_iso_with_id(params[:order][:ship_address_attributes]) if ship_changed
@@ -26,6 +30,26 @@ module Spree
private
+ def validate_address_ownership(order, params)
+ return nil unless params[:order]
+
+ %w[bill ship].each do |address_kind|
+ address_id = params[:order].dig("#{address_kind}_address_attributes".to_sym, :id)
+ next unless address_id
+
+ address = Spree::Address.find_by(id: address_id)
+ next unless address
+
+ # Allow if address has no user (guest address) or belongs to the order's user
+ next if address.user_id.nil?
+ next if order.user_id.present? && address.user_id == order.user_id
+
+ return Spree.t(:address_not_owned_by_user)
+ end
+
+ nil
+ end
+
def address_with_country_iso_present?(params, address_kind = 'ship')
return false unless params.dig(:order, "#{address_kind}_address_attributes".to_sym, :country_iso)
return false if params.dig(:order, "#{address_kind}_address_attributes".to_sym, :country_id)
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 61a00c75a4..0247aefb8d 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -667,6 +667,7 @@ en:
successfully_updated: Updated successfully
unsuccessfully_saved: There was an error while trying to save your address.
unsuccessfully_updated: There was an update while trying to update your address.
+ address_not_owned_by_user: The specified address does not belong to this user.
address_settings: Address settings
addresses: Addresses
adjustable: Adjustable
diff --git a/core/spec/services/spree/checkout/update_spec.rb b/core/spec/services/spree/checkout/update_spec.rb
index 40a47a2467..52be391475 100644
--- a/core/spec/services/spree/checkout/update_spec.rb
+++ b/core/spec/services/spree/checkout/update_spec.rb
@@ -203,6 +203,118 @@ describe Spree::Checkout::Update, type: :service do
end
end
+ describe 'address ownership validation' do
+ let(:user) { create(:user_with_addresses) }
+ let(:other_user) { create(:user_with_addresses) }
+ let(:order) { create(:order_with_line_items, user: user, state: 'address') }
+ let(:permitted_attributes) do
+ Spree::PermittedAttributes.checkout_attributes + [
+ bill_address_attributes: Spree::PermittedAttributes.address_attributes,
+ ship_address_attributes: Spree::PermittedAttributes.address_attributes
+ ]
+ end
+
+ context 'when bill_address_attributes contains id of another user address' do
+ let(:other_user_address) { other_user.bill_address }
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ bill_address_attributes: { id: other_user_address.id }
+ }
+ )
+ end
+
+ it 'returns failure' do
+ result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(result).to be_failure
+ end
+
+ it 'does not associate the other user address with the order' do
+ described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(order.reload.bill_address_id).not_to eq other_user_address.id
+ end
+ end
+
+ context 'when ship_address_attributes contains id of another user address' do
+ let(:other_user_address) { other_user.ship_address }
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ ship_address_attributes: { id: other_user_address.id }
+ }
+ )
+ end
+
+ it 'returns failure' do
+ result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(result).to be_failure
+ end
+
+ it 'does not associate the other user address with the order' do
+ described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(order.reload.ship_address_id).not_to eq other_user_address.id
+ end
+ end
+
+ context 'when address_attributes contains id of the same user address' do
+ let(:user_address) { create(:address, user: user) }
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ bill_address_attributes: { id: user_address.id }
+ }
+ )
+ end
+
+ it 'returns success' do
+ result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(result).to be_success
+ end
+ end
+
+ context 'when address_attributes contains id of address with no user' do
+ let(:guest_address) { create(:address, user: nil) }
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ bill_address_attributes: { id: guest_address.id }
+ }
+ )
+ end
+
+ it 'returns success' do
+ result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(result).to be_success
+ end
+ end
+
+ context 'when address_attributes does not contain id' do
+ let(:state) { create(:state) }
+ let(:country) { state.country }
+ let(:order_params) do
+ ActionController::Parameters.new(
+ order: {
+ bill_address_attributes: {
+ firstname: 'John',
+ lastname: 'Doe',
+ address1: '123 Main St',
+ city: 'Anytown',
+ zipcode: '12345',
+ country_iso: country.iso,
+ state_id: state.id,
+ phone: '555-1234'
+ }
+ }
+ )
+ end
+
+ it 'returns success' do
+ result = described_class.call(order: order, params: order_params, permitted_attributes: permitted_attributes, request_env: nil)
+ expect(result).to be_success
+ end
+ end
+ end
+
describe 'update selected shipping rate' do
let(:update_service) { described_class.new }
let(:order) { create(:order_with_line_items) }