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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Close TOCTOU race in digital download authorization (F9)
Damian Legawiec11586c19787b
diff --git a/spree/core/app/models/spree/digital_link.rb b/spree/core/app/models/spree/digital_link.rbindex da001e29b5..0e79ce16d0 100644--- a/spree/core/app/models/spree/digital_link.rb+++ b/spree/core/app/models/spree/digital_link.rb@@ -46,9 +46,15 @@ module Spree# This method should be called when a download is initiated.# It returns +true+ or +false+ depending on whether the authorization is granted.+ #+ # The access-limit check and the counter increment run inside a row lock so+ # concurrent requests sharing the same token cannot each pass the cap before+ # any of them increments the counter (TOCTOU race).def authorize!ActiveRecord::Base.connected_to(role: :writing) do- authorizable? && increment!(:access_counter, touch: true)+ with_lock do+ authorizable? && increment!(:access_counter, touch: true)+ endendenddiff --git a/spree/core/spec/models/spree/digital_link_spec.rb b/spree/core/spec/models/spree/digital_link_spec.rbindex 2c365b31b0..da325b735f 100644--- a/spree/core/spec/models/spree/digital_link_spec.rb+++ b/spree/core/spec/models/spree/digital_link_spec.rb@@ -214,5 +214,51 @@ describe Spree::DigitalLink, type: :model doend.not_to change { digital_link_expired.updated_at.to_s }endend++ context 'when the access limit is reached' do+ let(:limit) { store.preferred_digital_asset_authorized_clicks }+ let!(:digital_link) { create(:digital_link, access_counter: limit - 1) }++ after do+ digital_link.update(access_counter: 2)+ digital_link.save!+ digital_link.reload+ end++ it 'allows exactly the configured number of downloads and denies the rest' do+ expect(digital_link.authorize!).to be_truthy+ expect(digital_link.reload.access_counter).to eq(limit)++ expect(digital_link.authorize!).to be_falsey+ expect(digital_link.reload.access_counter).to eq(limit)+ end+ end++ # Proves the check-and-increment is atomic: two concurrent authorize! calls+ # sharing the same token cannot both pass the cap. Requires a database with+ # real concurrent connections and row-level locking (PostgreSQL).+ context 'under concurrent access', if: ENV['DB'] == 'postgres' do+ let(:limit) { store.preferred_digital_asset_authorized_clicks }+ let!(:digital_link) { create(:digital_link, access_counter: limit - 1) }++ after do+ digital_link.update(access_counter: 2)+ digital_link.save!+ digital_link.reload+ end++ it 'never lets the counter exceed the limit' do+ results = Array.new(5) do+ Thread.new do+ ActiveRecord::Base.connection_pool.with_connection do+ Spree::DigitalLink.find(digital_link.id).authorize!+ end+ end+ end.map(&:value)++ expect(results.count { |granted| granted }).to eq(1)+ expect(digital_link.reload.access_counter).to eq(limit)+ end+ endendend