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
Stop asking operators for credentials the system writes itself
Damian Legawiecaab76de59a23
diff --git a/spree/api/app/controllers/concerns/spree/api/v3/admin/subclassed_resource.rb b/spree/api/app/controllers/concerns/spree/api/v3/admin/subclassed_resource.rbindex 225a74ccc6..ce97f8ca47 100644--- a/spree/api/app/controllers/concerns/spree/api/v3/admin/subclassed_resource.rb+++ b/spree/api/app/controllers/concerns/spree/api/v3/admin/subclassed_resource.rb@@ -115,6 +115,12 @@ module Spreepreferences.each do |key, value|pref_name = key.to_symnext unless resource.has_preference?(pref_name)+ # Internal preferences are Spree's to write — a secret a+ # provider issued, an id it gave back. They are absent from the+ # schema, so no form offers them; ignoring them here is what+ # stops a client writing one anyway and breaking the signature+ # check that depends on it.+ next if resource.preference_internal(pref_name)# Round-trip guard: clients fetching a record see masked# `:password` values. Submitting the mask back unchanged# must NOT overwrite the real secret with `••••cret`.diff --git a/spree/api/spec/controllers/spree/api/v3/admin/payment_methods_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/payment_methods_controller_spec.rbindex 9e3d108157..98de3e328f 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/payment_methods_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/payment_methods_controller_spec.rb@@ -157,6 +157,25 @@ RSpec.describe Spree::Api::V3::Admin::PaymentMethodsController, type: :controlleexpect(payment_method.reload.preferred_dummy_secret_key).to eq('sk_live_existing_secret')end+ # Spree writes these itself — a secret the provider issued, an id it+ # gave back. They are absent from the schema so no form offers them,+ # and ignored here so a client cannot write one anyway and break the+ # signature check that depends on it.+ it 'ignores a preference the system owns' do+ allow_any_instance_of(Spree::Gateway::Bogus).to receive(:preference_internal).+ and_wrap_original { |original, name| name == :dummy_secret_key || original.call(name) }++ patch :update,+ params: {+ id: payment_method.prefixed_id,+ preferences: { dummy_secret_key: 'sk_live_written_by_a_client' }+ },+ as: :json++ expect(response).to have_http_status(:ok)+ expect(payment_method.reload.preferred_dummy_secret_key).to eq('sk_live_existing_secret')+ end+it 'replaces the secret when the submitted value is a new plaintext' dopatch :update,params: {diff --git a/spree/core/app/models/concerns/spree/preference_schema.rb b/spree/core/app/models/concerns/spree/preference_schema.rbindex bec9dff250..eef11d1c4b 100644--- a/spree/core/app/models/concerns/spree/preference_schema.rb+++ b/spree/core/app/models/concerns/spree/preference_schema.rb@@ -42,7 +42,8 @@ module Spreeclass_methods do# Returns `[{ key:, type:, default: }]` for every preference declared- # on this class (and its ancestors). Skips deprecated preferences.+ # on this class (and its ancestors). Skips deprecated and internal+ # preferences.## Memoized at class load — the schema is derived from the static# `preference :name, :type` declarations, so it can never change at@@ -82,6 +83,11 @@ module Spreeinstance = newinstance.defined_preferences.filter_map do |pref|next if instance.preference_deprecated(pref)+ # Written by Spree, not supplied by the operator — a value a+ # provider hands back after we register something with it. Offering+ # it as a field invites somebody to type over a secret that+ # signature verification depends on.+ next if instance.preference_internal(pref){key: pref,diff --git a/spree/core/lib/spree/core/preferences/preferable.rb b/spree/core/lib/spree/core/preferences/preferable.rbindex ef8762419f..85191ceef6 100644--- a/spree/core/lib/spree/core/preferences/preferable.rb+++ b/spree/core/lib/spree/core/preferences/preferable.rb@@ -67,6 +67,14 @@ module Spree::Preferences::Preferablesend(self.class.preference_deprecated_getter_method(name))end+ # Whether this preference is written by Spree rather than supplied by the+ # operator — a webhook secret a provider issues, say. Kept out of the+ # schema, so nothing offers it as a field to fill in.+ def preference_internal(name)+ has_preference! name+ send(self.class.preference_internal_getter_method(name))+ end+def has_preference!(name)raise NoMethodError, "#{name} preference not defined" unless has_preference? nameenddiff --git a/spree/core/lib/spree/core/preferences/preferable_class_methods.rb b/spree/core/lib/spree/core/preferences/preferable_class_methods.rbindex d2e7e9ffdb..a27e0e8e5a 100644--- a/spree/core/lib/spree/core/preferences/preferable_class_methods.rb+++ b/spree/core/lib/spree/core/preferences/preferable_class_methods.rb@@ -2,10 +2,11 @@ module Spree::Preferencesmodule PreferableClassMethodsdef preference(name, type, *args)options = args.extract_options!- options.assert_valid_keys(:default, :deprecated, :nullable, :parse_on_set)+ options.assert_valid_keys(:default, :deprecated, :internal, :nullable, :parse_on_set)default = options[:default]default = -> { options[:default] } unless default.is_a?(Proc)deprecated = options[:deprecated]+ internal = options[:internal]nullable = options[:nullable]parse_on_set = options[:parse_on_set]@@ -49,6 +50,13 @@ module Spree::Preferencesdeprecatedend+ # Whether this preference is the system's to write rather than the+ # operator's to supply — a value a provider hands back to us, not one+ # anybody could know in advance.+ define_method preference_internal_getter_method(name) do+ internal+ end+define_method prefers_query_method(name) dopreferences.fetch(name).to_bend@@ -98,6 +106,10 @@ module Spree::Preferences"preferred_#{name}_deprecated".to_symend+ def preference_internal_getter_method(name)+ "preferred_#{name}_internal".to_sym+ end+def preference_type_getter_method(name)"preferred_#{name}_type".to_symenddiff --git a/spree/core/spec/models/spree/payment_method_spec.rb b/spree/core/spec/models/spree/payment_method_spec.rbindex 1179a931e2..975605a118 100644--- a/spree/core/spec/models/spree/payment_method_spec.rb+++ b/spree/core/spec/models/spree/payment_method_spec.rb@@ -337,4 +337,37 @@ describe Spree::PaymentMethod, type: :model doActiveSupport::Notifications.unsubscribe(subscription)endend+ # A preference the system writes rather than the operator supplies — a+ # secret a provider issues, an id it gives back.+ describe 'internal preferences' do+ let(:gateway_class) do+ Class.new(Spree::Gateway) do+ def self.name = 'TestInternalGateway'++ preference :api_key, :password+ preference :issued_secret, :password, internal: true+ end+ end++ it 'keeps them out of the schema, so no form offers them' do+ keys = gateway_class.serialized_preference_schema.map { |field| field[:key] }++ expect(keys).to include(:api_key)+ expect(keys).not_to include(:issued_secret)+ end++ it 'still stores and reads one, since the system depends on the value' do+ gateway = gateway_class.new+ gateway.preferred_issued_secret = 'whsec_abc'++ expect(gateway.preferred_issued_secret).to eq('whsec_abc')+ end++ it 'reports which preferences are internal' do+ gateway = gateway_class.new++ expect(gateway.preference_internal(:issued_secret)).to be(true)+ expect(gateway.preference_internal(:api_key)).to be_nil+ end+ endenddiff --git a/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb b/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rbindex ccbf27578f..eb1142bbc1 100644--- a/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb+++ b/spree/providers/stripe/app/models/spree_stripe/gateway/connect.rb@@ -29,8 +29,10 @@ module SpreeStripe].freezeincluded do- preference :connect_webhook_signing_secret, :password- preference :connect_webhook_endpoint_id, :string+ # As with the payment endpoint's pair: Spree registers the endpoint and+ # stores what Stripe hands back.+ preference :connect_webhook_signing_secret, :password, internal: true+ preference :connect_webhook_endpoint_id, :string, internal: trueafter_commit :create_connect_webhook_endpoint_async, on: %i[create update]enddiff --git a/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb b/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rbindex 580e130d05..6f58716fa4 100644--- a/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb+++ b/spree/providers/stripe/app/models/spree_stripe/gateway/webhooks.rb@@ -28,8 +28,12 @@ module SpreeStripe}.freezeincluded do- preference :webhook_signing_secret, :password- preference :webhook_endpoint_id, :string+ # Both written by CreateGatewayWebhooks once Stripe has registered the+ # endpoint — Stripe issues the secret and only reveals it at creation.+ # An operator has nothing to type here, and a value typed over one+ # would break the signature check it exists to satisfy.+ preference :webhook_signing_secret, :password, internal: true+ preference :webhook_endpoint_id, :string, internal: trueafter_commit :create_webhook_endpoint_async, on: %i[create update]enddiff --git a/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb b/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rbindex 3a185b141e..96f226e766 100644--- a/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb+++ b/spree/providers/stripe/spec/models/spree_stripe/gateway_spec.rb@@ -1130,4 +1130,22 @@ RSpec.describe SpreeStripe::Gateway doendendend+ # Stripe issues these when Spree registers an endpoint, and only reveals a+ # signing secret at creation. An operator has nothing to type, and a value+ # typed over one would break the signature check it exists to satisfy.+ describe 'the credentials an operator supplies' do+ it 'asks for the two keys only' do+ keys = described_class.serialized_preference_schema.map { |field| field[:key] }++ expect(keys).to contain_exactly(:publishable_key, :secret_key)+ end++ it 'still holds what Stripe hands back' do+ gateway.preferred_webhook_signing_secret = 'whsec_payments'+ gateway.preferred_connect_webhook_signing_secret = 'whsec_connect'++ expect(gateway.preferred_webhook_signing_secret).to eq('whsec_payments')+ expect(gateway.preferred_connect_webhook_signing_secret).to eq('whsec_connect')+ end+ endend