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
Fix: keep payment session credentials out of the webhook delivery log (#14680)
Damian Legawiec3d815e143028
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/webhook_deliveries_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/webhook_deliveries_controller_spec.rbindex 53a90af42f..81dc806bda 100644--- a/spree/api/spec/controllers/spree/api/v3/admin/webhook_deliveries_controller_spec.rb+++ b/spree/api/spec/controllers/spree/api/v3/admin/webhook_deliveries_controller_spec.rb@@ -48,6 +48,35 @@ RSpec.describe Spree::Api::V3::Admin::WebhookDeliveriesController, type: :controexpect(json_response['webhook_endpoint_id']).to eq(endpoint.prefixed_id)expect(json_response['payload']).to be_presentend++ # Rows written before payment session keys joined the redaction list still+ # hold live gateway credentials, so the read path redacts them again.+ context 'when a stored payload holds payment session credentials' do+ before do+ failed_delivery.update_columns(+ payload: {+ 'name' => 'payment_session.created',+ 'data' => {+ 'external_client_secret' => 'seti_live_secret',+ 'external_data' => {+ 'client_secret' => 'pi_live_secret',+ 'ephemeral_key_secret' => 'ek_live_secret'+ }+ }+ }+ )+ end++ it 'does not return them, at any depth' do+ subject++ data = json_response['payload']['data']+ expect(data['external_client_secret']).to eq(Spree::WebhookPayloadRedaction::REDACTION_PLACEHOLDER)+ expect(data['external_data']['client_secret']).to eq(Spree::WebhookPayloadRedaction::REDACTION_PLACEHOLDER)+ expect(data['external_data']['ephemeral_key_secret']).to eq(Spree::WebhookPayloadRedaction::REDACTION_PLACEHOLDER)+ expect(response.body).not_to include('pi_live_secret', 'ek_live_secret', 'seti_live_secret')+ end+ endenddescribe 'POST #redeliver' dodiff --git a/spree/core/app/models/concerns/spree/webhook_payload_redaction.rb b/spree/core/app/models/concerns/spree/webhook_payload_redaction.rbindex ce2285493e..46025435df 100644--- a/spree/core/app/models/concerns/spree/webhook_payload_redaction.rb+++ b/spree/core/app/models/concerns/spree/webhook_payload_redaction.rb@@ -5,10 +5,13 @@ module Spree## Some events must carry a live credential to their subscriber — a storefront# that owns its transactional emails needs the real password reset token to- # build the email. Delivering it over TLS to a merchant-configured endpoint is- # intended; keeping a readable copy in `spree_webhook_deliveries.payload` is- # not, because that column is queryable and is served back through the Admin- # API delivery log.+ # build the email, and a mobile app needs the payment session client secret to+ # confirm the payment. Delivering those over TLS to a merchant-configured+ # endpoint is intended; keeping a readable copy in+ # `spree_webhook_deliveries.payload` is not, because that column is queryable+ # and is served back through the Admin API delivery log to any key holding+ # `read_webhooks` — a scope an operator may grant precisely because it carries+ # no access to payments or customers.## Sensitive values are therefore replaced with {REDACTION_PLACEHOLDER} before# the record is written, and re-attached in memory at send time so the@@ -16,29 +19,49 @@ module Spreemodule WebhookPayloadRedactionextend ActiveSupport::Concern- # Payload keys under `data` whose values are live credentials.- SENSITIVE_PAYLOAD_KEYS = %w[reset_token unsubscribe_token verification_token download_url].freeze+ # Payload keys whose values are live credentials, at any depth under `data`.+ #+ # Payment session secrets are gateway-agnostic: `external_client_secret` is+ # a core column and `external_data` is a free-form provider hash, so every+ # provider that stores a confirmation credential there is covered.+ SENSITIVE_PAYLOAD_KEYS = %w[+ reset_token+ unsubscribe_token+ verification_token+ download_url+ external_client_secret+ client_secret+ ephemeral_key_secret+ ].freezeREDACTION_PLACEHOLDER = '[REDACTED]'+ # Separates the segments of a secret's path. Segments are escaped before+ # they are joined, so a key that itself contains the separator still maps+ # to one unambiguous path.+ PATH_SEPARATOR = '.'++ # The root both `data` keys share. A payload is persisted as JSON and read+ # back with string keys, so the symbol and string roots must key their+ # secrets identically or nothing restores after a round trip.+ ROOT_SEGMENT = 'data'++ # A path segment escapes the separator so it cannot be read as a boundary,+ # and the escape character itself so the escaping stays reversible.+ PATH_ESCAPES = { '\\' => '\\\\', PATH_SEPARATOR => "\\#{PATH_SEPARATOR}" }.freeze+ PATH_ESCAPES_PATTERN = /[\\#{Regexp.escape(PATH_SEPARATOR)}]/.freeze+# Splits a payload into the version safe to persist and the secrets held# back from it.## @param payload [Hash] the full event payload# @return [Array(Hash, Hash)] redacted payload, and the extracted secrets- # keyed as they appeared under `data`+ # keyed by their dotted path under `data`def self.split(payload)secrets = {}redacted = transform_data_hashes(payload) do |data|- data.to_h do |key, value|- if SENSITIVE_PAYLOAD_KEYS.include?(key.to_s) && value.present?- secrets[secret_key_for(key)] = value- [key, REDACTION_PLACEHOLDER]- else- [key, value]- end- end+ redact(data, [ROOT_SEGMENT], secrets)endsecrets.empty? ? [payload, {}] : [redacted, secrets]@@ -53,12 +76,62 @@ module Spreereturn payload if secrets.blank?transform_data_hashes(payload) do |data|- data.to_h do |key, value|- secret = secrets[secret_key_for(key)]- [key, secret.presence || value]+ restore(data, [ROOT_SEGMENT], secrets)+ end+ end++ # Walks a hash replacing sensitive values, recording each one against its+ # path so two secrets sharing a key name (`client_secret` at the top level+ # and inside `external_data`) cannot overwrite one another.+ def self.redact(node, path, secrets)+ if node.is_a?(Array)+ return node.each_with_index.map { |element, index| redact(element, path + [index.to_s], secrets) }+ end+ return node unless node.is_a?(Hash)++ node.to_h do |key, value|+ key_path = path + [key.to_s]++ if sensitive?(key, value)+ secrets[secret_key_for(key_path)] = value+ [key, REDACTION_PLACEHOLDER]+ else+ [key, redact(value, key_path, secrets)]endendend+ private_class_method :redact++ # The mirror of {redact}. Only a slot still holding the placeholder is+ # filled, so a real value the payload carries is never overwritten.+ #+ # Deliveries enqueued before path keying shipped carry secrets under a bare+ # key name. Those only ever came from the top level of `data`, so the+ # fallback is confined there — at depth it would fill every same-named slot+ # in the tree with one secret.+ def self.restore(node, path, secrets)+ if node.is_a?(Array)+ return node.each_with_index.map { |element, index| restore(element, path + [index.to_s], secrets) }+ end+ return node unless node.is_a?(Hash)++ node.to_h do |key, value|+ key_path = path + [key.to_s]+ next [key, restore(value, key_path, secrets)] unless value == REDACTION_PLACEHOLDER++ secret = secrets[secret_key_for(key_path)]+ secret ||= secrets[key.to_s] if path.one?++ [key, secret.presence || value]+ end+ end+ private_class_method :restore++ def self.sensitive?(key, value)+ SENSITIVE_PAYLOAD_KEYS.include?(key.to_s) && value.present? &&+ !value.is_a?(Hash) && !value.is_a?(Array)+ end+ private_class_method :sensitive?# Applies +block+ to every `data` hash on the payload.#@@ -77,11 +150,15 @@ module Spreeendprivate_class_method :transform_data_hashes- # Secrets are keyed by name alone. They cross an ActiveJob serialization+ # Secrets are keyed by path alone. They cross an ActiveJob serialization# boundary, which coerces symbol keys to strings, so the key form at split# time cannot be relied on to still match at merge time.- def self.secret_key_for(key)- key.to_s+ #+ # Each segment escapes the separator (and the escape character) before the+ # join, so `['a.b', 'c']` and `['a', 'b', 'c']` stay distinct keys.+ def self.secret_key_for(path)+ path.map { |segment| segment.gsub(PATH_ESCAPES_PATTERN) { |character| PATH_ESCAPES[character] } }.+ join(PATH_SEPARATOR)endprivate_class_method :secret_key_forenddiff --git a/spree/core/spec/models/concerns/spree/webhook_payload_redaction_spec.rb b/spree/core/spec/models/concerns/spree/webhook_payload_redaction_spec.rbindex 359266694c..c470da7e03 100644--- a/spree/core/spec/models/concerns/spree/webhook_payload_redaction_spec.rb+++ b/spree/core/spec/models/concerns/spree/webhook_payload_redaction_spec.rb@@ -14,7 +14,7 @@ describe Spree::WebhookPayloadRedaction doexpect(payload['data']['reset_token']).to eq(placeholder)expect(payload['data']['email']).to eq('[email protected]')- expect(secrets).to eq('reset_token' => 'live-token')+ expect(secrets).to eq('data.reset_token' => 'live-token')endit 'handles symbol keys' do@@ -38,10 +38,111 @@ describe Spree::WebhookPayloadRedaction doexpect(payload['data']['verification_token']).to eq(placeholder)expect(payload['data']['email']).to eq('[email protected]')- expect(secrets).to eq('verification_token' => 'live-token')+ expect(secrets).to eq('data.verification_token' => 'live-token')expect(described_class.merge(payload, secrets)).to eq(original)end+ # Payment session events carry live gateway credentials: a client secret+ # confirms a payment and an ephemeral key lists a customer's saved cards.+ it 'redacts payment session credentials nested inside external_data' do+ original = {+ 'name' => 'payment_session.created',+ 'data' => {+ 'status' => 'pending',+ 'external_client_secret' => 'seti_live_secret',+ 'external_data' => {+ 'client_secret' => 'pi_live_secret',+ 'ephemeral_key_secret' => 'ek_live_secret',+ 'payment_intent_id' => 'pi_123'+ }+ }+ }++ payload, secrets = described_class.split(original)++ expect(payload['data']['external_client_secret']).to eq(placeholder)+ expect(payload['data']['external_data']['client_secret']).to eq(placeholder)+ expect(payload['data']['external_data']['ephemeral_key_secret']).to eq(placeholder)+ expect(payload['data']['external_data']['payment_intent_id']).to eq('pi_123')+ expect(secrets).to eq(+ 'data.external_client_secret' => 'seti_live_secret',+ 'data.external_data.client_secret' => 'pi_live_secret',+ 'data.external_data.ephemeral_key_secret' => 'ek_live_secret'+ )+ expect(described_class.merge(payload, secrets)).to eq(original)+ end++ # Keying by path alone: two secrets sharing a key name must not overwrite+ # one another on the way out or back in.+ it 'keeps same-named secrets at different depths apart' do+ original = {+ 'data' => {+ 'client_secret' => 'top-level',+ 'external_data' => { 'client_secret' => 'nested' }+ }+ }++ payload, secrets = described_class.split(original)++ expect(secrets).to eq('data.client_secret' => 'top-level', 'data.external_data.client_secret' => 'nested')+ expect(described_class.merge(payload, secrets)).to eq(original)+ end++ it 'redacts secrets inside arrays of nested records' do+ original = { 'data' => { 'sessions' => [{ 'client_secret' => 'one' }, { 'client_secret' => 'two' }] } }++ payload, secrets = described_class.split(original)++ expect(payload['data']['sessions'].map { |session| session['client_secret'] }).to all(eq(placeholder))+ expect(described_class.merge(payload, secrets)).to eq(original)+ end++ # A key that itself contains the path separator must not be mistaken for+ # two nested keys, or `merge` restores the wrong credential.+ it 'keeps a dotted key name distinct from the nested path it resembles' do+ original = {+ 'data' => {+ 'external.data' => { 'client_secret' => 'from-dotted-parent' },+ 'external' => { 'data' => { 'client_secret' => 'from-nested' } }+ }+ }++ payload, secrets = described_class.split(original)++ expect(secrets.values).to contain_exactly('from-dotted-parent', 'from-nested')+ expect(described_class.merge(payload, secrets)).to eq(original)+ end++ # The escape character is itself escaped, so a key carrying a backslash+ # cannot forge the encoding of a different path.+ it 'keeps a key containing a backslash distinct from the path it imitates' do+ original = {+ 'data' => {+ 'a\\' => { 'b' => { 'client_secret' => 'from-backslash-parent' } },+ 'a' => { 'b' => { 'client_secret' => 'from-plain-parent' } }+ }+ }++ payload, secrets = described_class.split(original)++ expect(secrets.values).to contain_exactly('from-backslash-parent', 'from-plain-parent')+ expect(described_class.merge(payload, secrets)).to eq(original)+ end++ # Both roots key their secrets identically on purpose: the payload is+ # persisted as JSON and read back with string keys, so a symbol root that+ # keyed itself separately would restore nothing after the round trip.+ it 'keys both data roots the same way' do+ payload, secrets = described_class.split(+ :data => { 'client_secret' => 'sym-side' },+ 'data' => { 'client_secret' => 'str-side' }+ )++ expect(payload[:data]['client_secret']).to eq(placeholder)+ expect(payload['data']['client_secret']).to eq(placeholder)+ expect(secrets.keys).to eq(['data.client_secret'])+ end+it 'returns the payload untouched when nothing is sensitive' dooriginal = { 'data' => { 'number' => 'R123' } }@@ -68,7 +169,7 @@ describe Spree::WebhookPayloadRedaction do)expect(payload['data'].values).to all(eq(placeholder))- expect(secrets.keys).to eq(['reset_token'])+ expect(secrets.keys).to eq(['data.reset_token'])end# Secrets survive an ActiveJob round trip, which stringifies symbol keys.@@ -97,6 +198,33 @@ describe Spree::WebhookPayloadRedaction doexpect(described_class.merge(redacted, nil)).to eq(redacted)end++ # Only a slot still holding the placeholder is filled. A value the payload+ # legitimately carries is never replaced by a secret of the same name.+ it 'does not overwrite a value that is not the placeholder' do+ redacted = { 'data' => { 'external_data' => { 'client_secret' => 'a-real-value' } } }++ restored = described_class.merge(redacted, 'data.external_data.client_secret' => 'live-secret')++ expect(restored['data']['external_data']['client_secret']).to eq('a-real-value')+ end++ # Deliveries queued before path keying carry bare key names. That form only+ # ever came from the top level, so it must not fill same-named slots deeper+ # in the tree.+ it 'confines a legacy bare-keyed secret to the top level of data' do+ redacted = {+ 'data' => {+ 'reset_token' => placeholder,+ 'child' => { 'reset_token' => placeholder }+ }+ }++ restored = described_class.merge(redacted, 'reset_token' => 'live-token')++ expect(restored['data']['reset_token']).to eq('live-token')+ expect(restored['data']['child']['reset_token']).to eq(placeholder)+ endendit 'round-trips to the original payload' do