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: Stopped newsletter subscribe from handing another person's subscription to the caller

Damian Legawiecfca9b0e99e9c
diff --git a/spree/api/app/serializers/spree/api/v3/admin/newsletter_subscriber_serializer.rb b/spree/api/app/serializers/spree/api/v3/admin/newsletter_subscriber_serializer.rb
index 24f5dc76f8..0346eef464 100644
--- a/spree/api/app/serializers/spree/api/v3/admin/newsletter_subscriber_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/admin/newsletter_subscriber_serializer.rb
@@ -8,6 +8,7 @@ module Spree
# Back-office surface, nested under the admin customer serializer. Extends the
# customer-facing serializer so public fields stay in sync.
class NewsletterSubscriberSerializer < V3::NewsletterSubscriberSerializer
+ prefixed_id_attributes :customer
end
end
end
diff --git a/spree/api/app/serializers/spree/api/v3/newsletter_subscriber_serializer.rb b/spree/api/app/serializers/spree/api/v3/newsletter_subscriber_serializer.rb
index 8e2980d843..5ed3f6095b 100644
--- a/spree/api/app/serializers/spree/api/v3/newsletter_subscriber_serializer.rb
+++ b/spree/api/app/serializers/spree/api/v3/newsletter_subscriber_serializer.rb
@@ -16,7 +16,11 @@ module Spree
subscriber.verified_at&.iso8601
end
- prefixed_id_attributes :customer
+ # Shown to the account's owner only: for anyone else, posting an email
+ # must not tell them whether an account exists for it.
+ attribute :customer_id do |subscriber|
+ subscriber.customer&.prefixed_id if current_user && subscriber.customer_id == current_user.id
+ end
end
end
end
diff --git a/spree/api/spec/controllers/spree/api/v3/store/newsletter_subscribers_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/newsletter_subscribers_controller_spec.rb
index 002e260373..badbcf8045 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/newsletter_subscribers_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/newsletter_subscribers_controller_spec.rb
@@ -11,6 +11,36 @@ RSpec.describe Spree::Api::V3::Store::NewsletterSubscribersController, type: :co
request.headers['X-Spree-Api-Key'] = api_key.token
end
+ describe 'POST #create' do
+ let!(:account) { create(:user, email: '[email protected]') }
+
+ it 'does not tell a guest whether an account exists for the email' do
+ post :create, params: { email: account.email }, as: :json
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['customer_id']).to be_nil
+ end
+
+ context 'as a signed-in customer subscribing someone else\'s verified address' do
+ let!(:subscriber) { create(:newsletter_subscriber, :verified, customer: account, email: account.email, store: store) }
+
+ before { request.headers['Authorization'] = "Bearer #{jwt_token}" }
+
+ it 'does not take over the subscription' do
+ post :create, params: { email: account.email }, as: :json
+
+ expect(response).to have_http_status(:created)
+ expect(json_response['customer_id']).to be_nil
+ expect(subscriber.reload.customer).to eq(account)
+
+ delete :destroy, params: { id: subscriber.prefixed_id }, as: :json
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(Spree::NewsletterSubscriber.exists?(subscriber.id)).to be(true)
+ end
+ end
+ end
+
describe 'DELETE #destroy' do
# Opt in via update_column, not the factory — setting accepts_email_marketing
# at create time auto-subscribes the customer and collides with our subscriber.
diff --git a/spree/core/app/services/spree/newsletter/subscribe.rb b/spree/core/app/services/spree/newsletter/subscribe.rb
index d61afa0dd1..25d8fd181b 100644
--- a/spree/core/app/services/spree/newsletter/subscribe.rb
+++ b/spree/core/app/services/spree/newsletter/subscribe.rb
@@ -54,8 +54,16 @@ module Spree
@existed_subscription ||= Spree::NewsletterSubscriber.verified.find_by(email: email, store: current_store)
end
+ # The account the subscription belongs to: the signed-in customer only
+ # when the address is theirs. Linking it to whoever is signed in would
+ # hand them another person's subscription — and with it the right to
+ # cancel it.
def known_user
- @known_user ||= current_user || Spree.customer_class.find_by(email: email)
+ @known_user ||= if current_user&.email.to_s.casecmp?(email.to_s)
+ current_user
+ else
+ Spree.customer_class.find_by(email: email)
+ end
end
end
end
diff --git a/spree/core/spec/services/spree/newsletter/subscribe_spec.rb b/spree/core/spec/services/spree/newsletter/subscribe_spec.rb
index e9c437703c..d91984aa93 100644
--- a/spree/core/spec/services/spree/newsletter/subscribe_spec.rb
+++ b/spree/core/spec/services/spree/newsletter/subscribe_spec.rb
@@ -120,6 +120,17 @@ module Spree
expect(user.reload.accepts_email_marketing).to eq(true)
end
end
+
+ context 'when a logged in user with another email subscribes it' do
+ let(:owner) { create(:user, email: email) }
+ let(:user) { create(:user, email: '[email protected]') }
+
+ before { subscriber.update!(customer: owner) }
+
+ it 'leaves the subscription with its owner' do
+ expect(service.customer).to eq(owner)
+ end
+ end
end
context 'when unverified subscription has been already created' do