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: sent staff password reset links only to the dashboard

Damian Legawiec3da07053c514
diff --git a/packages/admin-sdk/src/admin-client.ts b/packages/admin-sdk/src/admin-client.ts
index cb48531f0b..41c8484052 100644
--- a/packages/admin-sdk/src/admin-client.ts
+++ b/packages/admin-sdk/src/admin-client.ts
@@ -862,8 +862,9 @@ export class AdminClient {
/**
* Public (unauthenticated) request for a password reset email. Always
* resolves (202) whether or not the email matches an account, to prevent
- * enumeration. The emailed link points at `redirect_url` (validated against
- * the store's allowed origins) with the reset token appended as `?token=`.
+ * enumeration. The emailed link points at `redirect_url` when it is on the
+ * dashboard's origin and the account is staff of the store, otherwise at the
+ * dashboard's reset page, with the reset token appended as `?token=`.
*/
requestPasswordReset: (
params: PasswordResetRequestParams,
diff --git a/spree/api/app/controllers/spree/api/v3/admin/password_resets_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/password_resets_controller.rb
index 336bcedfda..d671419d2e 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/password_resets_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/password_resets_controller.rb
@@ -17,20 +17,10 @@ module Spree
# POST /api/v3/admin/password_resets
def create
- redirect_url = params[:redirect_url]
-
- # Validate redirect_url against allowed origins (secure by default).
- # If no allowed origins are configured, redirect_url is silently ignored
- # to prevent open redirect / token exfiltration attacks.
- if redirect_url.present?
- unless current_store.allowed_origins.exists? && current_store.allowed_origin?(redirect_url)
- redirect_url = nil
- end
- end
-
user = Spree.admin_user_class.find_by(email: params[:email])
if user
+ redirect_url = trusted_redirect_url(user)
token = user.generate_token_for(:password_reset)
event_payload = { reset_token: token, email: user.email, store_id: current_store.prefixed_id }
event_payload[:redirect_url] = redirect_url if redirect_url.present?
@@ -71,6 +61,36 @@ module Spree
private
+ # The emailed link carries the reset token, so it may only point at
+ # the dashboard — never at a storefront origin, which anyone who can
+ # edit a store's allowed origins could add — and only when the
+ # account belongs to the store the request names. Anything else is
+ # ignored and the email links to the dashboard's own reset page.
+ #
+ # @return [String, nil]
+ def trusted_redirect_url(user)
+ redirect_url = params[:redirect_url].presence
+ return if redirect_url.blank?
+ return unless same_origin?(redirect_url, Spree::Stores::DashboardUrl.call(store: current_store))
+ return unless staff_of_current_store?(user)
+
+ redirect_url
+ end
+
+ def same_origin?(url, other)
+ first = URI.parse(url.to_s)
+ second = URI.parse(other.to_s)
+ first.host.present? && [first.scheme, first.host, first.port] == [second.scheme, second.host, second.port]
+ rescue URI::InvalidURIError
+ false
+ end
+
+ def staff_of_current_store?(user)
+ return false unless user.respond_to?(:spree_roles)
+
+ user.spree_roles.for_resource(current_store).exists?
+ end
+
def auth_response(user)
{
token: generate_jwt(user, audience: JWT_AUDIENCE_ADMIN),
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/password_resets_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/password_resets_controller_spec.rb
index a04fee8850..0459689b20 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/password_resets_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/password_resets_controller_spec.rb
@@ -42,6 +42,43 @@ RSpec.describe Spree::Api::V3::Admin::PasswordResetsController, type: :controlle
expect(payload).not_to have_key(:redirect_url)
end
end
+
+ # The link carries the reset token, so only the dashboard may receive it:
+ # a storefront origin can be added by anyone who edits allowed origins.
+ context 'with the dashboard hosted at a known origin' do
+ before do
+ allow(Spree::Stores::DashboardUrl).to receive(:call).and_return('https://admin.example.com')
+ allow(Spree::Events).to receive(:publish)
+ end
+
+ it 'keeps a redirect_url on the dashboard origin' do
+ post :create, params: { email: '[email protected]', redirect_url: 'https://admin.example.com/reset-password' }
+
+ expect(Spree::Events).to have_received(:publish) do |_name, payload, _meta|
+ expect(payload[:redirect_url]).to eq('https://admin.example.com/reset-password')
+ end
+ end
+
+ it 'ignores a redirect_url on an allowed storefront origin' do
+ create(:allowed_origin, store: store, origin: 'https://attacker.example.com')
+
+ post :create, params: { email: '[email protected]', redirect_url: 'https://attacker.example.com/reset' }
+
+ expect(Spree::Events).to have_received(:publish) do |_name, payload, _meta|
+ expect(payload).not_to have_key(:redirect_url)
+ end
+ end
+
+ it 'ignores a redirect_url for an account that is not staff of the store' do
+ create(:admin_user, :without_admin_role, email: '[email protected]')
+
+ post :create, params: { email: '[email protected]', redirect_url: 'https://admin.example.com/reset-password' }
+
+ expect(Spree::Events).to have_received(:publish) do |_name, payload, _meta|
+ expect(payload).not_to have_key(:redirect_url)
+ end
+ end
+ end
end
describe 'PATCH #update' do
diff --git a/spree/api/spec/integration/spree/api/v3/admin/password_resets_spec.rb b/spree/api/spec/integration/spree/api/v3/admin/password_resets_spec.rb
index b67d5ad34e..c08f860d85 100644
--- a/spree/api/spec/integration/spree/api/v3/admin/password_resets_spec.rb
+++ b/spree/api/spec/integration/spree/api/v3/admin/password_resets_spec.rb
@@ -18,9 +18,10 @@ RSpec.describe 'Admin Password Resets API', type: :request, swagger_doc: 'api-re
whether or not the email matches an account, to prevent enumeration.
`redirect_url` is where the emailed link should point (the reset token
- is appended as a `token` query param). It must match one of the store's
- allowed origins — otherwise it is silently ignored and the server-side
- default is used. The email is delivered by Spree itself via the
+ is appended as a `token` query param). It is honoured only on the
+ dashboard's origin and for an account that is staff of the store —
+ otherwise it is silently ignored and the link opens the dashboard's
+ reset page. The email is delivered by Spree itself via the
`admin_user.password_reset_requested` event; this event is never
forwarded to webhook endpoints.
DESC
diff --git a/spree/core/app/mailers/spree/admin_user_mailer.rb b/spree/core/app/mailers/spree/admin_user_mailer.rb
index 849d8e1543..c4fbea6653 100644
--- a/spree/core/app/mailers/spree/admin_user_mailer.rb
+++ b/spree/core/app/mailers/spree/admin_user_mailer.rb
@@ -34,9 +34,9 @@ module Spree
end
# The dashboard SPA passes a validated redirect URL; the token is appended as
- # a query param, falling back to the store URL.
+ # a query param, falling back to the dashboard's own reset page.
def password_reset_url(token, store, redirect_url)
- append_token(redirect_url.presence || store.formatted_url, token)
+ append_token(redirect_url.presence || "#{Spree::Stores::DashboardUrl.call(store: store)}/reset-password", token)
end
end
end
diff --git a/spree/core/spec/mailers/spree/admin_user_mailer_spec.rb b/spree/core/spec/mailers/spree/admin_user_mailer_spec.rb
index a3a1700164..5a7a4ab011 100644
--- a/spree/core/spec/mailers/spree/admin_user_mailer_spec.rb
+++ b/spree/core/spec/mailers/spree/admin_user_mailer_spec.rb
@@ -19,6 +19,16 @@ describe Spree::AdminUserMailer, type: :mailer do
expect(message.body.encoded).to include("token=#{token}")
end
+ # The token must never land on a storefront, which is where the store URL
+ # points.
+ it 'falls back to the dashboard reset page' do
+ allow(Spree::Stores::DashboardUrl).to receive(:call).with(store: store).and_return('https://admin.example.com')
+
+ message = described_class.password_reset_email(admin_user, token, store)
+
+ expect(message.body.encoded).to include("https://admin.example.com/reset-password?token=#{token}")
+ end
+
it 'prefers the redirect URL when the API validated one (dashboard SPA)' do
message = described_class.password_reset_email(admin_user, token, store, redirect_url: 'https://admin.example.com/reset-password')