2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-30 11:11:17 -04:00
|
|
|
require "openssl"
|
|
|
|
|
|
|
|
class WebhooksController < ActionController::Base
|
2022-06-13 10:36:45 -04:00
|
|
|
skip_before_action :verify_authenticity_token
|
2016-05-30 11:11:17 -04:00
|
|
|
|
|
|
|
def mailgun
|
2023-01-16 12:16:17 -05:00
|
|
|
return signature_failure if SiteSetting.mailgun_api_key.blank?
|
2016-05-30 11:11:17 -04:00
|
|
|
|
2019-01-31 11:52:33 -05:00
|
|
|
params["event-data"] ? handle_mailgun_new(params) : handle_mailgun_legacy(params)
|
2016-05-30 11:11:17 -04:00
|
|
|
end
|
|
|
|
|
2016-06-01 15:48:06 -04:00
|
|
|
def sendgrid
|
2023-01-16 12:16:17 -05:00
|
|
|
if SiteSetting.sendgrid_verification_key.present?
|
|
|
|
return signature_failure if !valid_sendgrid_signature?
|
|
|
|
else
|
|
|
|
Rails.logger.warn(
|
|
|
|
"Received a Sendgrid webhook, but no verification key has been configured. This is unsafe behaviour and will be disallowed in the future.",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-06-06 13:47:45 -04:00
|
|
|
events = params["_json"] || [params]
|
|
|
|
events.each do |event|
|
2021-12-05 19:34:39 -05:00
|
|
|
message_id = Email::MessageIdService.message_id_clean((event["smtp-id"] || ""))
|
2017-02-05 13:06:35 -05:00
|
|
|
to_address = event["email"]
|
2022-02-14 23:17:26 -05:00
|
|
|
error_code = event["status"]
|
2020-04-30 02:48:34 -04:00
|
|
|
if event["event"] == "bounce"
|
2022-02-14 23:17:26 -05:00
|
|
|
if error_code[Email::SMTP_STATUS_TRANSIENT_FAILURE]
|
|
|
|
process_bounce(message_id, to_address, SiteSetting.soft_bounce_score, error_code)
|
2016-06-01 15:48:06 -04:00
|
|
|
else
|
2022-02-14 23:17:26 -05:00
|
|
|
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score, error_code)
|
2016-06-01 15:48:06 -04:00
|
|
|
end
|
2020-04-30 02:48:34 -04:00
|
|
|
elsif event["event"] == "dropped"
|
2022-02-14 23:17:26 -05:00
|
|
|
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score, error_code)
|
2016-06-01 15:48:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-13 15:26:40 -05:00
|
|
|
success
|
2016-06-01 15:48:06 -04:00
|
|
|
end
|
|
|
|
|
2016-06-06 13:47:45 -04:00
|
|
|
def mailjet
|
2023-01-16 12:16:17 -05:00
|
|
|
if SiteSetting.mailjet_webhook_token.present?
|
|
|
|
return signature_failure if !valid_mailjet_token?
|
|
|
|
else
|
|
|
|
Rails.logger.warn(
|
|
|
|
"Received a Mailjet webhook, but no token has been configured. This is unsafe behaviour and will be disallowed in the future.",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-06-06 13:47:45 -04:00
|
|
|
events = params["_json"] || [params]
|
|
|
|
events.each do |event|
|
2016-06-08 16:38:38 -04:00
|
|
|
message_id = event["CustomID"]
|
2017-02-05 13:06:35 -05:00
|
|
|
to_address = event["email"]
|
2020-04-30 02:48:34 -04:00
|
|
|
if event["event"] == "bounce"
|
2016-06-06 13:47:45 -04:00
|
|
|
if event["hard_bounce"]
|
2017-02-05 13:06:35 -05:00
|
|
|
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
|
2016-06-06 13:47:45 -04:00
|
|
|
else
|
2017-02-05 13:06:35 -05:00
|
|
|
process_bounce(message_id, to_address, SiteSetting.soft_bounce_score)
|
2016-06-06 13:47:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-13 15:26:40 -05:00
|
|
|
success
|
2016-06-06 13:47:45 -04:00
|
|
|
end
|
|
|
|
|
2023-06-08 13:06:20 -04:00
|
|
|
def mailpace
|
|
|
|
# see https://docs.mailpace.com/guide/webhooks#email-events
|
|
|
|
|
|
|
|
message_id = Email::MessageIdService.message_id_clean(params["payload"]["message_id"])
|
|
|
|
to_address = params["payload"]["to"]
|
|
|
|
status = params["payload"]["status"]
|
|
|
|
|
|
|
|
case status
|
|
|
|
when "bounced"
|
|
|
|
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
|
|
|
|
when "deferred"
|
|
|
|
process_bounce(message_id, to_address, SiteSetting.soft_bounce_score)
|
|
|
|
end
|
|
|
|
|
|
|
|
success
|
|
|
|
end
|
|
|
|
|
2016-06-13 06:31:01 -04:00
|
|
|
def mandrill
|
2023-01-16 12:16:17 -05:00
|
|
|
if SiteSetting.mandrill_authentication_key.present?
|
|
|
|
return signature_failure if !valid_mandrill_signature?
|
|
|
|
else
|
|
|
|
Rails.logger.warn(
|
|
|
|
"Received a Mandrill webhook, but no authentication key has been configured. This is unsafe behaviour and will be disallowed in the future.",
|
|
|
|
)
|
|
|
|
end
|
2016-06-13 06:31:01 -04:00
|
|
|
|
2023-01-16 12:16:17 -05:00
|
|
|
JSON
|
|
|
|
.parse(params["mandrill_events"])
|
|
|
|
.each do |event|
|
|
|
|
message_id = event.dig("msg", "metadata", "message_id")
|
|
|
|
to_address = event.dig("msg", "email")
|
|
|
|
error_code = event.dig("msg", "diag")
|
|
|
|
|
|
|
|
case event["event"]
|
|
|
|
when "hard_bounce"
|
|
|
|
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score, error_code)
|
|
|
|
when "soft_bounce"
|
|
|
|
process_bounce(message_id, to_address, SiteSetting.soft_bounce_score, error_code)
|
|
|
|
end
|
2016-06-13 06:31:01 -04:00
|
|
|
end
|
|
|
|
|
2019-02-13 15:26:40 -05:00
|
|
|
success
|
2016-06-13 06:31:01 -04:00
|
|
|
end
|
|
|
|
|
2022-07-29 02:26:31 -04:00
|
|
|
def mandrill_head
|
|
|
|
# Mandrill sends a HEAD request to validate the webhook before saving
|
|
|
|
# Rails interprets it as a GET request
|
|
|
|
success
|
|
|
|
end
|
|
|
|
|
2020-02-11 10:09:07 -05:00
|
|
|
def postmark
|
2023-01-16 12:16:17 -05:00
|
|
|
if SiteSetting.postmark_webhook_token.present?
|
|
|
|
return signature_failure if !valid_postmark_token?
|
|
|
|
else
|
|
|
|
Rails.logger.warn(
|
|
|
|
"Received a Postmark webhook, but no token has been configured. This is unsafe behaviour and will be disallowed in the future.",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-02-11 10:09:07 -05:00
|
|
|
# see https://postmarkapp.com/developer/webhooks/bounce-webhook#bounce-webhook-data
|
|
|
|
# and https://postmarkapp.com/developer/api/bounce-api#bounce-types
|
|
|
|
|
|
|
|
message_id = params["MessageID"]
|
|
|
|
to_address = params["Email"]
|
2020-02-11 11:21:03 -05:00
|
|
|
type = params["Type"]
|
2020-02-11 10:09:07 -05:00
|
|
|
case type
|
|
|
|
when "HardBounce", "SpamNotification", "SpamComplaint"
|
|
|
|
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
|
|
|
|
when "SoftBounce"
|
|
|
|
process_bounce(message_id, to_address, SiteSetting.soft_bounce_score)
|
|
|
|
end
|
|
|
|
|
|
|
|
success
|
|
|
|
end
|
|
|
|
|
2016-09-27 01:13:34 -04:00
|
|
|
def sparkpost
|
2023-01-16 12:16:17 -05:00
|
|
|
if SiteSetting.sparkpost_webhook_token.present?
|
|
|
|
return signature_failure if !valid_sparkpost_token?
|
|
|
|
else
|
|
|
|
Rails.logger.warn(
|
|
|
|
"Received a Sparkpost webhook, but no token has been configured. This is unsafe behaviour and will be disallowed in the future.",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-09-27 01:13:34 -04:00
|
|
|
events = params["_json"] || [params]
|
|
|
|
events.each do |event|
|
2018-03-28 04:20:08 -04:00
|
|
|
message_event = event.dig("msys", "message_event")
|
2017-02-05 13:06:35 -05:00
|
|
|
next unless message_event
|
|
|
|
|
2018-03-28 04:20:08 -04:00
|
|
|
message_id = message_event.dig("rcpt_meta", "message_id")
|
|
|
|
to_address = message_event["rcpt_to"]
|
|
|
|
bounce_class = message_event["bounce_class"]
|
2017-02-05 13:06:35 -05:00
|
|
|
next unless bounce_class
|
2016-09-27 01:13:34 -04:00
|
|
|
|
|
|
|
bounce_class = bounce_class.to_i
|
|
|
|
|
|
|
|
# bounce class definitions: https://support.sparkpost.com/customer/portal/articles/1929896
|
|
|
|
if bounce_class < 80
|
|
|
|
if bounce_class == 10 || bounce_class == 25 || bounce_class == 30
|
2017-02-05 13:06:35 -05:00
|
|
|
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score)
|
2016-09-27 01:13:34 -04:00
|
|
|
else
|
2017-02-05 13:06:35 -05:00
|
|
|
process_bounce(message_id, to_address, SiteSetting.soft_bounce_score)
|
2016-09-27 01:13:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-13 15:26:40 -05:00
|
|
|
success
|
|
|
|
end
|
|
|
|
|
|
|
|
def aws
|
|
|
|
raw = request.raw_post
|
|
|
|
json = JSON.parse(raw)
|
|
|
|
|
|
|
|
case json["Type"]
|
|
|
|
when "SubscriptionConfirmation"
|
|
|
|
Jobs.enqueue(:confirm_sns_subscription, raw: raw, json: json)
|
|
|
|
when "Notification"
|
|
|
|
Jobs.enqueue(:process_sns_notification, raw: raw, json: json)
|
|
|
|
end
|
|
|
|
|
|
|
|
success
|
2016-09-27 01:13:34 -04:00
|
|
|
end
|
|
|
|
|
2016-05-30 11:11:17 -04:00
|
|
|
private
|
|
|
|
|
2023-01-16 12:16:17 -05:00
|
|
|
def signature_failure
|
2017-08-31 00:06:56 -04:00
|
|
|
render body: nil, status: 406
|
2016-05-30 11:11:17 -04:00
|
|
|
end
|
|
|
|
|
2019-02-13 15:26:40 -05:00
|
|
|
def success
|
2017-08-31 00:06:56 -04:00
|
|
|
render body: nil, status: 200
|
2016-05-30 11:11:17 -04:00
|
|
|
end
|
|
|
|
|
2019-01-31 11:52:33 -05:00
|
|
|
def valid_mailgun_signature?(token, timestamp, signature)
|
|
|
|
# token is a random 50 characters string
|
|
|
|
return false if token.blank? || token.size != 50
|
|
|
|
|
|
|
|
# prevent replay attacks
|
|
|
|
key = "mailgun_token_#{token}"
|
2019-12-03 04:05:53 -05:00
|
|
|
return false unless Discourse.redis.setnx(key, 1)
|
|
|
|
Discourse.redis.expire(key, 10.minutes)
|
2019-01-31 11:52:33 -05:00
|
|
|
|
|
|
|
# ensure timestamp isn't too far from current time
|
|
|
|
return false if (Time.at(timestamp.to_i) - Time.now).abs > 12.hours.to_i
|
|
|
|
|
|
|
|
# check the signature
|
|
|
|
signature ==
|
|
|
|
OpenSSL::HMAC.hexdigest("SHA256", SiteSetting.mailgun_api_key, "#{timestamp}#{token}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_mailgun_legacy(params)
|
|
|
|
unless valid_mailgun_signature?(params["token"], params["timestamp"], params["signature"])
|
2023-01-16 12:16:17 -05:00
|
|
|
return signature_failure
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2019-01-31 11:52:33 -05:00
|
|
|
|
|
|
|
event = params["event"]
|
2021-12-05 19:34:39 -05:00
|
|
|
message_id = Email::MessageIdService.message_id_clean(params["Message-Id"])
|
2019-01-31 11:52:33 -05:00
|
|
|
to_address = params["recipient"]
|
2022-02-14 23:17:26 -05:00
|
|
|
error_code = params["code"]
|
2019-01-31 11:52:33 -05:00
|
|
|
|
|
|
|
# only handle soft bounces, because hard bounces are also handled
|
|
|
|
# by the "dropped" event and we don't want to increase bounce score twice
|
|
|
|
# for the same message
|
2022-02-14 23:17:26 -05:00
|
|
|
if event == "bounced" && params["error"][Email::SMTP_STATUS_TRANSIENT_FAILURE]
|
|
|
|
process_bounce(message_id, to_address, SiteSetting.soft_bounce_score, error_code)
|
2020-04-30 02:48:34 -04:00
|
|
|
elsif event == "dropped"
|
2022-02-14 23:17:26 -05:00
|
|
|
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score, error_code)
|
2019-01-31 11:52:33 -05:00
|
|
|
end
|
|
|
|
|
2019-02-13 15:26:40 -05:00
|
|
|
success
|
2019-01-31 11:52:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def handle_mailgun_new(params)
|
|
|
|
signature = params["signature"]
|
|
|
|
unless valid_mailgun_signature?(
|
|
|
|
signature["token"],
|
|
|
|
signature["timestamp"],
|
|
|
|
signature["signature"],
|
|
|
|
)
|
2023-01-16 12:16:17 -05:00
|
|
|
return signature_failure
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2019-01-31 11:52:33 -05:00
|
|
|
|
|
|
|
data = params["event-data"]
|
2022-02-14 23:17:26 -05:00
|
|
|
error_code = params.dig("delivery-status", "code")
|
2019-01-31 11:52:33 -05:00
|
|
|
message_id = data.dig("message", "headers", "message-id")
|
|
|
|
to_address = data["recipient"]
|
|
|
|
severity = data["severity"]
|
|
|
|
|
2020-04-30 02:48:34 -04:00
|
|
|
if data["event"] == "failed"
|
|
|
|
if severity == "temporary"
|
2022-02-14 23:17:26 -05:00
|
|
|
process_bounce(message_id, to_address, SiteSetting.soft_bounce_score, error_code)
|
2020-04-30 02:48:34 -04:00
|
|
|
elsif severity == "permanent"
|
2022-02-14 23:17:26 -05:00
|
|
|
process_bounce(message_id, to_address, SiteSetting.hard_bounce_score, error_code)
|
2019-01-31 11:52:33 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-13 15:26:40 -05:00
|
|
|
success
|
2016-05-30 11:11:17 -04:00
|
|
|
end
|
|
|
|
|
2023-01-16 12:16:17 -05:00
|
|
|
def valid_sendgrid_signature?
|
|
|
|
signature = request.headers["X-Twilio-Email-Event-Webhook-Signature"]
|
|
|
|
timestamp = request.headers["X-Twilio-Email-Event-Webhook-Timestamp"]
|
|
|
|
request.body.rewind
|
|
|
|
payload = request.body.read
|
|
|
|
|
|
|
|
hashed_payload = Digest::SHA256.digest("#{timestamp}#{payload}")
|
|
|
|
decoded_signature = Base64.decode64(signature)
|
|
|
|
|
|
|
|
begin
|
|
|
|
public_key = OpenSSL::PKey::EC.new(Base64.decode64(SiteSetting.sendgrid_verification_key))
|
|
|
|
rescue StandardError => err
|
|
|
|
Rails.logger.error("Invalid Sendgrid verification key")
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
public_key.dsa_verify_asn1(hashed_payload, decoded_signature)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_mailjet_token?
|
|
|
|
ActiveSupport::SecurityUtils.secure_compare(params[:t], SiteSetting.mailjet_webhook_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_mandrill_signature?
|
|
|
|
signature = request.headers["X-Mandrill-Signature"]
|
|
|
|
|
|
|
|
payload = "#{Discourse.base_url}/webhooks/mandrill"
|
|
|
|
params
|
|
|
|
.permit(:mandrill_events)
|
|
|
|
.to_h
|
|
|
|
.sort_by(&:first)
|
|
|
|
.each do |key, value|
|
|
|
|
payload += key.to_s
|
|
|
|
payload += value
|
|
|
|
end
|
|
|
|
|
|
|
|
payload_signature =
|
|
|
|
OpenSSL::HMAC.digest("sha1", SiteSetting.mandrill_authentication_key, payload)
|
|
|
|
ActiveSupport::SecurityUtils.secure_compare(
|
|
|
|
signature,
|
|
|
|
Base64.strict_encode64(payload_signature),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_postmark_token?
|
|
|
|
ActiveSupport::SecurityUtils.secure_compare(params[:t], SiteSetting.postmark_webhook_token)
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_sparkpost_token?
|
|
|
|
ActiveSupport::SecurityUtils.secure_compare(params[:t], SiteSetting.sparkpost_webhook_token)
|
|
|
|
end
|
|
|
|
|
2022-02-14 23:17:26 -05:00
|
|
|
def process_bounce(message_id, to_address, bounce_score, bounce_error_code = nil)
|
2017-02-05 13:06:35 -05:00
|
|
|
return if message_id.blank? || to_address.blank?
|
2016-06-06 13:47:45 -04:00
|
|
|
|
2017-02-05 13:06:35 -05:00
|
|
|
email_log = EmailLog.find_by(message_id: message_id, to_address: to_address)
|
2016-06-06 13:47:45 -04:00
|
|
|
return if email_log.nil?
|
|
|
|
|
2022-02-14 23:17:26 -05:00
|
|
|
email_log.update_columns(bounced: true, bounce_error_code: bounce_error_code)
|
2016-10-26 04:13:05 -04:00
|
|
|
return if email_log.user.nil? || email_log.user.email.blank?
|
|
|
|
|
2016-06-06 13:47:45 -04:00
|
|
|
Email::Receiver.update_bounce_score(email_log.user.email, bounce_score)
|
|
|
|
end
|
2016-05-30 11:11:17 -04:00
|
|
|
end
|