Class: Spree::WebhookEndpoint

Inherits:
Object
  • Object
show all
Includes:
SingleStoreResource
Defined in:
app/models/spree/webhook_endpoint.rb

Constant Summary collapse

AUTO_DISABLE_THRESHOLD =

Number of consecutive failed deliveries before auto-disabling

15
MAX_SUBSCRIPTIONS =
200
MAX_SUBSCRIPTION_LENGTH =
100
SUBSCRIPTION_SEGMENT =
/\A[a-zA-Z0-9_-]+\z/

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allowed_internal_host?(url) ⇒ Boolean

Whether url targets a host the operator trusts despite it resolving to a private address — e.g. a storefront Service in the same Kubernetes cluster. Set via Spree::Api::Config.webhooks_allowed_internal_hosts (SPREE_WEBHOOKS_ALLOWED_INTERNAL_HOSTS): an entry matches that exact host, or with a leading dot (".svc.cluster.local") any host under it. Matching is case-insensitive. Operator config only, so a store admin can never point a webhook at an internal address.

Parameters:

  • url (String)

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
# File 'app/models/spree/webhook_endpoint.rb', line 53

def self.allowed_internal_host?(url)
  host = URI.parse(url.to_s).hostname&.downcase
  return false if host.blank?

  allowed_internal_hosts.any? do |entry|
    entry.start_with?('.') ? host.end_with?(entry) : host == entry
  end
rescue URI::InvalidURIError
  false
end

.event_pattern?(value) ⇒ Boolean

Whether value is a shape subscriptions accepts: *, an event name, or a name with * in place of whole segments (order.*, *.created). Anything else — regexp syntax above all — is refused before it is stored, so the matcher only ever sees names and segment wildcards.

Parameters:

  • value (Object)

Returns:

  • (Boolean)


101
102
103
104
105
# File 'app/models/spree/webhook_endpoint.rb', line 101

def self.event_pattern?(value)
  return false unless value.is_a?(String) && value.length.between?(1, MAX_SUBSCRIPTION_LENGTH)

  value.split('.', -1).all? { |segment| segment == '*' || segment.match?(SUBSCRIPTION_SEGMENT) }
end

Instance Method Details

#auto_disabled? ⇒ Boolean

Check if the endpoint was auto-disabled

Returns:

  • (Boolean)


176
177
178
# File 'app/models/spree/webhook_endpoint.rb', line 176

def auto_disabled?
  disabled_at.present?
end

#check_auto_disable! ⇒ Object

Check if auto-disable threshold has been reached and disable if so.



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'app/models/spree/webhook_endpoint.rb', line 182

def check_auto_disable!
  return if auto_disabled?

  consecutive_failures = webhook_deliveries
    .where(success: false)
    .where.not(delivered_at: nil)
    .order(delivered_at: :desc)
    .limit(AUTO_DISABLE_THRESHOLD)

  return if consecutive_failures.count < AUTO_DISABLE_THRESHOLD

  # Verify they're all failures (no successes interspersed)
  last_success = webhook_deliveries.successful.order(delivered_at: :desc).pick(:delivered_at)
  oldest_failure = consecutive_failures.last&.delivered_at

  if last_success.nil? || (oldest_failure && oldest_failure > last_success)
    disable!
  end
end

#credential_subscriptions ⇒ Array<Spree::Events::Catalog::Entry>

The credential-carrying events this endpoint names.

Returns:



125
126
127
# File 'app/models/spree/webhook_endpoint.rb', line 125

def credential_subscriptions
  Spree::Events.catalog.credential_events(subscriptions.to_a)
end

#disable!(reason: 'Automatically disabled after repeated delivery failures', notify: true) ⇒ Object

Disable this endpoint due to repeated failures. Sends a notification email to the store staff.

Parameters:

  • reason (String) (defaults to: 'Automatically disabled after repeated delivery failures')
  • notify (Boolean) (defaults to: true) —

    whether to send an email notification (default: true)



163
164
165
166
# File 'app/models/spree/webhook_endpoint.rb', line 163

def disable!(reason: 'Automatically disabled after repeated delivery failures', notify: true)
  update!(active: false, disabled_reason: reason, disabled_at: Time.current)
  Spree::WebhookMailer.endpoint_disabled(self).deliver_later if notify
end

#enable! ⇒ Object

Re-enable a previously disabled endpoint.



169
170
171
# File 'app/models/spree/webhook_endpoint.rb', line 169

def enable!
  update!(active: true, disabled_reason: nil, disabled_at: nil)
end

#secret_key_for_response ⇒ String?

Returns the plaintext secret_key only on the create response.

@reveal_secret_in_response is set by the after_create callback above — a per-instance flag, not derived from previous_changes, so a reload or any subsequent save can't accidentally re-expose the secret.

Returns:

  • (String, nil)


83
84
85
# File 'app/models/spree/webhook_endpoint.rb', line 83

def secret_key_for_response
  @reveal_secret_in_response ? secret_key : nil
end

#send_test! ⇒ Spree::WebhookDelivery

Send a test/ping webhook to verify the endpoint is reachable. Creates a delivery record and queues it for delivery.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/spree/webhook_endpoint.rb', line 142

def send_test!
  delivery = webhook_deliveries.create!(
    event_name: 'webhook.test',
    payload: {
      id: SecureRandom.uuid,
      name: 'webhook.test',
      created_at: Time.current.iso8601,
      data: { message: 'This is a test webhook from Spree.' },
      metadata: { spree_version: Spree.version }
    }
  )

  delivery.queue_for_delivery!
  delivery
end

#subscribed_events ⇒ Array<String>

Returns all events this endpoint is subscribed to

Returns:

  • (Array<String>)


132
133
134
135
136
# File 'app/models/spree/webhook_endpoint.rb', line 132

def subscribed_events
  return ['*'] if subscriptions.blank?

  subscriptions
end

#subscribed_to?(event_name) ⇒ Boolean

Check if this endpoint is subscribed to a specific event.

Events whose payload carries a live credential (a password reset token) reach only an endpoint that names them — never through * or a pattern — since receiving one is as good as holding the account.

Parameters:

  • event_name (String) —

    the event name to check

Returns:

  • (Boolean)


115
116
117
118
119
120
# File 'app/models/spree/webhook_endpoint.rb', line 115

def subscribed_to?(event_name)
  return subscriptions.to_a.include?(event_name) if Spree::Events.catalog.credential?(event_name)
  return true if subscriptions.blank?

  subscriptions.any? { |subscription| Spree::Event.matches?(event_name, subscription) }
end