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.

Separate auth rate-limit buckets and handle omitted email_verified

Damian Legawiec80619a46091c
diff --git a/docs/plans/6.0-platform-auth.md b/docs/plans/6.0-platform-auth.md
index 99173ea2b3..4935bc46fc 100644
--- a/docs/plans/6.0-platform-auth.md
+++ b/docs/plans/6.0-platform-auth.md
@@ -546,7 +546,7 @@ distinct records.
`AdminUser` in this order:
1. **Known identity** — a `Spree::UserIdentity` matching `(provider, uid, user_type)` already exists. Log in.
-2. **Existing admin, first SSO login** — no identity yet, but an `AdminUser` has the same email **and the IdP asserts `email_verified: true`**. Link a new `UserIdentity` to that account and log in. This is what makes the migration path work: a shop registers Entra and its existing staff simply sign in, with no password round-trip — which matters most for the shop that also removed `:email`. The verified-claim requirement is not optional; matching on an unverified email claim lets anyone who can set their IdP email to a staff address take over that account.
+2. **Existing admin, first SSO login** — no identity yet, but an `AdminUser` has the same email **and the IdP asserts `email_verified: true`**. Link a new `UserIdentity` to that account and log in. This is what makes the migration path work: a shop registers Entra and its existing staff simply sign in, with no password round-trip — which matters most for the shop that also removed `:email`. The verified-claim requirement is not optional; matching on an unverified email claim lets anyone who can set their IdP email to a staff address take over that account. **An explicit `email_verified: false` is always refused.** A *missing* claim is a separate case (2026-08-04): Entra omits it for work accounts, so a strict reading would block first-login linking on the reference provider. Absence therefore falls to per-provider `trust_unverified_email:` (default **off**), enabled only for a directory that owns its addresses — never for a provider where a person picks their own email.
3. **Unknown subject** — no identity, no email match. **Rejected.** Admin accounts never auto-provision, or every member of a corporate Entra tenant becomes store staff. The person must be invited through the existing invitation flow first. Integrators may opt into JIT provisioning per strategy.
Rule 3 rejects a person who authenticated successfully with their IdP, so a generic "authentication
diff --git a/spree/api/app/controllers/spree/api/v3/admin/auth_controller.rb b/spree/api/app/controllers/spree/api/v3/admin/auth_controller.rb
index 6882386948..ae919e0676 100644
--- a/spree/api/app/controllers/spree/api/v3/admin/auth_controller.rb
+++ b/spree/api/app/controllers/spree/api/v3/admin/auth_controller.rb
@@ -14,7 +14,11 @@ module Spree
# not be refused because the login form was submitted a few times.
RATE_LIMITED_CALLBACK_RESPONSE = -> { redirect_to_dashboard(error: 'rate_limit_exceeded') }
- rate_limit to: Spree::Api::Config[:rate_limit_login], within: Spree::Api::Config[:rate_limit_window].seconds, store: Rails.cache, only: [:create, :providers], with: RATE_LIMIT_RESPONSE
+ rate_limit to: Spree::Api::Config[:rate_limit_login], within: Spree::Api::Config[:rate_limit_window].seconds, store: Rails.cache, only: :create, with: RATE_LIMIT_RESPONSE
+ # Each of these gets its own counter. The login page fetches providers on
+ # every load, so sharing the login budget would let ordinary page views —
+ # or an attacker hitting providers alone — lock staff out of signing in.
+ rate_limit to: Spree::Api::Config[:rate_limit_refresh], within: Spree::Api::Config[:rate_limit_window].seconds, store: Rails.cache, only: :providers, with: RATE_LIMIT_RESPONSE
rate_limit to: Spree::Api::Config[:rate_limit_login], within: Spree::Api::Config[:rate_limit_window].seconds, store: Rails.cache, only: :callback, with: RATE_LIMITED_CALLBACK_RESPONSE
rate_limit to: Spree::Api::Config[:rate_limit_refresh], within: Spree::Api::Config[:rate_limit_window].seconds, store: Rails.cache, only: [:refresh, :logout], with: RATE_LIMIT_RESPONSE
diff --git a/spree/core/app/models/spree/authentication/strategies/oidc_strategy.rb b/spree/core/app/models/spree/authentication/strategies/oidc_strategy.rb
index 62d872ca05..d550b4c603 100644
--- a/spree/core/app/models/spree/authentication/strategies/oidc_strategy.rb
+++ b/spree/core/app/models/spree/authentication/strategies/oidc_strategy.rb
@@ -71,15 +71,24 @@ module Spree
# @param redirect_uri [String] must match the value registered with the IdP
# @param label [String] name shown on the dashboard login button
# @param scope [String] space-separated scopes; must include +openid+
+ # @param trust_unverified_email [Boolean] allow first-login account linking
+ # when the provider omits the +email_verified+ claim. Off by default.
+ # Only enable for a provider whose directory owns its email addresses —
+ # Microsoft Entra ID omits the claim for work accounts, for instance, and
+ # a tenant admin controls those addresses. Never enable it for a provider
+ # where a person can set their own email (a consumer Google or GitHub
+ # account), since that turns "claim any staff address" into a takeover.
# @return [Factory]
- def self.configure(issuer:, client_id:, client_secret:, redirect_uri:, label: nil, scope: 'openid email profile')
+ def self.configure(issuer:, client_id:, client_secret:, redirect_uri:, label: nil, scope: 'openid email profile',
+ trust_unverified_email: false)
config = {
issuer: issuer.to_s.chomp('/'),
client_id: client_id,
client_secret: client_secret,
redirect_uri: redirect_uri,
label: label,
- scope: scope
+ scope: scope,
+ trust_unverified_email: trust_unverified_email
}
missing = REQUIRED_OPTIONS.select { |option| config[option].blank? }
@@ -188,8 +197,13 @@ module Spree
# Only an email the provider asserts as verified may claim an existing
# account. Matching on an unverified claim would let anyone who can set
# their IdP email to a staff address take that account over.
+ #
+ # Some directories — Microsoft Entra ID among them — omit +email_verified+
+ # for work accounts, which would block first-login linking for every
+ # existing admin. Those providers opt in with +trust_unverified_email+; an
+ # explicitly false claim is still refused either way.
def find_linkable_user(claims)
- return nil unless claims['email_verified'].to_s == 'true'
+ return nil unless email_claim_trusted?(claims)
# find_user_by_email matches case-insensitively, which matters here: the
# models normalize email with +squish+ but never downcase, so a stored
@@ -197,6 +211,17 @@ module Spree
find_user_by_email(claims['email'])
end
+ # A present claim is always obeyed: "false" refuses linking even when the
+ # provider is configured as trusted. Only an *absent* claim falls back to
+ # the provider's configuration.
+ def email_claim_trusted?(claims)
+ verified = claims['email_verified']
+
+ return verified.to_s == 'true' unless verified.nil?
+
+ config[:trust_unverified_email].present?
+ end
+
def identity_info(claims)
claims.slice('email', 'name', 'given_name', 'family_name', 'picture')
end
diff --git a/spree/core/spec/models/spree/authentication/strategies/oidc_strategy_spec.rb b/spree/core/spec/models/spree/authentication/strategies/oidc_strategy_spec.rb
index 026d6b0f45..a53f350b17 100644
--- a/spree/core/spec/models/spree/authentication/strategies/oidc_strategy_spec.rb
+++ b/spree/core/spec/models/spree/authentication/strategies/oidc_strategy_spec.rb
@@ -114,6 +114,46 @@ describe Spree::Authentication::Strategies::OidcStrategy do
end
end
+ # Entra omits email_verified for work accounts, so the claim's absence must
+ # be a configurable decision rather than an implicit yes or no.
+ context 'when the provider omits the email_verified claim' do
+ let(:claims) { { 'sub' => 'idp-subject-1', 'email' => admin.email } }
+
+ it 'refuses to link by default' do
+ result = strategy.callback
+
+ expect(result).not_to be_success
+ expect(admin.identities).to be_empty
+ end
+
+ context 'and the provider is configured to trust its directory' do
+ let(:factory) do
+ described_class.configure(
+ issuer: issuer,
+ client_id: 'client-id',
+ client_secret: 'client-secret',
+ redirect_uri: 'https://shop.example.com/api/v3/admin/auth/callback/entra',
+ label: 'Microsoft Entra ID',
+ trust_unverified_email: true
+ )
+ end
+
+ it 'links the identity to the existing admin' do
+ result = strategy.callback
+
+ expect(result).to be_success
+ expect(result.value).to eq(admin)
+ end
+
+ # A present "false" is an explicit denial and outranks the configuration.
+ it 'still refuses an explicitly unverified email' do
+ allow(strategy).to receive(:verify_id_token).and_return(claims.merge('email_verified' => 'false'))
+
+ expect(strategy.callback).not_to be_success
+ end
+ end
+ end
+
context 'when the email is not verified by the provider' do
let(:claims) do
{ 'sub' => 'idp-subject-1', 'email' => admin.email, 'email_verified' => 'false' }