Module: Spree::WebhookPayloadRedaction

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/spree/webhook_payload_redaction.rb

Overview

Keeps single-use credentials out of the persisted webhook delivery log.

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, 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 outgoing request body is unchanged.

Constant Summary collapse

SENSITIVE_PAYLOAD_KEYS =

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.

token is the guest cart credential (X-Spree-Token), which a placed order inherits; with it anyone can read and change the cart.

%w[
  token
  reset_token
  unsubscribe_token
  verification_token
  confirmation_token
  download_url
  external_client_secret
  client_secret
  ephemeral_key_secret
].freeze
GIFT_CARD_CODE_KEY =

A gift card's code is what a shopper redeems, so it is spendable by whoever reads it. Only a gift card's own code is redacted — promotion, country and channel codes elsewhere in a payload are not credentials.

'code'
GIFT_CARD_NODE =
'gift_card'
GIFT_CARD_EVENT_PREFIX =
'gift_card.'
REDACTION_PLACEHOLDER =
'[REDACTED]'
PATH_SEPARATOR =

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.

'.'
ROOT_SEGMENT =

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.

'data'
PATH_ESCAPES =

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_SEPARATOR => "\\#{PATH_SEPARATOR}" }.freeze
PATH_ESCAPES_PATTERN =
/[\\#{Regexp.escape(PATH_SEPARATOR)}]/.freeze

Class Method Summary collapse

Class Method Details

.merge(payload, secrets) ⇒ Hash

Re-attaches previously extracted secrets to a redacted payload.

Parameters:

  • payload (Hash) —

    the redacted payload

  • secrets (Hash) —

    secrets returned by split

Returns:

  • (Hash) —

    the payload as it should go over the wire



88
89
90
91
92
93
94
# File 'app/models/concerns/spree/webhook_payload_redaction.rb', line 88

def self.merge(payload, secrets)
  return payload if secrets.blank?

  transform_data_hashes(payload) do |data|
    restore(data, [ROOT_SEGMENT], secrets)
  end
end

.split(payload) ⇒ Array(Hash, Hash)

Splits a payload into the version safe to persist and the secrets held back from it.

Parameters:

  • payload (Hash) —

    the full event payload

Returns:

  • (Array(Hash, Hash)) —

    redacted payload, and the extracted secrets keyed by their dotted path under data



72
73
74
75
76
77
78
79
80
81
# File 'app/models/concerns/spree/webhook_payload_redaction.rb', line 72

def self.split(payload)
  secrets = {}
  gift_card_event = event_name_of(payload).start_with?(GIFT_CARD_EVENT_PREFIX)

  redacted = transform_data_hashes(payload) do |data|
    redact(data, [ROOT_SEGMENT], secrets, gift_card_event)
  end

  secrets.empty? ? [payload, {}] : [redacted, secrets]
end