Skip to content

A platform for community discussion. Free, open, simple.

SECURITY: harden webooks endpoints

Joffrey JAFFEUXb8e2c8a07af4
diff --git a/app/controllers/webhooks_controller.rb b/app/controllers/webhooks_controller.rb
index 763c31d07aa..0b13d9a6d0a 100644
--- a/app/controllers/webhooks_controller.rb
+++ b/app/controllers/webhooks_controller.rb
@@ -22,9 +22,7 @@ class WebhooksController < ActionController::Base
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.",
- )
+ return signature_failure
end
events = params["_json"] || [params]
@@ -55,9 +53,7 @@ class WebhooksController < ActionController::Base
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.",
- )
+ return signature_failure
end
events = params["_json"] || [params]
@@ -79,6 +75,12 @@ class WebhooksController < ActionController::Base
def mailpace
# see https://docs.mailpace.com/guide/webhooks#email-events
+ if SiteSetting.mailpace_webhook_token.present?
+ return signature_failure if !valid_mailpace_token?
+ else
+ return signature_failure
+ end
+
message_id = Email::MessageIdService.message_id_clean(params["payload"]["message_id"])
to_address = params["payload"]["to"]
status = params["payload"]["status"]
@@ -97,9 +99,7 @@ class WebhooksController < ActionController::Base
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.",
- )
+ return signature_failure
end
JSON
@@ -130,9 +130,7 @@ class WebhooksController < ActionController::Base
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.",
- )
+ return signature_failure
end
# see https://postmarkapp.com/developer/webhooks/bounce-webhook#bounce-webhook-data
@@ -155,9 +153,7 @@ class WebhooksController < ActionController::Base
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.",
- )
+ return signature_failure
end
events = params["_json"] || [params]
@@ -325,6 +321,10 @@ class WebhooksController < ActionController::Base
)
end
+ def valid_mailpace_token?
+ ActiveSupport::SecurityUtils.secure_compare(params[:t], SiteSetting.mailpace_webhook_token)
+ end
+
def valid_postmark_token?
return false if params[:t].blank?
ActiveSupport::SecurityUtils.secure_compare(params[:t], SiteSetting.postmark_webhook_token)
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index a220b285245..46d2c61e994 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2343,6 +2343,7 @@ en:
mailgun_api_key: "Mailgun Secret API key used to verify webhook messages."
sendgrid_verification_key: "Sendgrid verification key used to verify webhook messages."
mailjet_webhook_token: "Token used to verify webhook payload. It must be passed as the 't' query parameter of the webhook, for example: https://example.com/webhook/mailjet?t=supersecret"
+ mailpace_webhook_token: "Token used to verify webhook payload. It must be passed as the 't' query parameter of the webhook, for example: https://example.com/webhook/mailpace?t=supersecret"
mandrill_authentication_key: "Mandrill authentication key used to verify webhook messages."
postmark_webhook_token: "Token used to verify webhook payload. It must be passed as the 't' query parameter of the webhook, for example: https://example.com/webhook/postmark?t=supersecret"
sparkpost_webhook_token: "Token used to verify webhook payload. It must be passed as the 't' query parameter of the webhook, for example: https://example.com/webhook/sparkpost?t=supersecret"
diff --git a/config/site_settings.yml b/config/site_settings.yml
index c85bd4877d8..6b4dc96523e 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -2010,6 +2010,9 @@ email:
mailjet_webhook_token:
default: ""
secret: true
+ mailpace_webhook_token:
+ default: ""
+ secret: true
mandrill_authentication_key:
default: ""
secret: true
diff --git a/spec/requests/webhooks_controller_spec.rb b/spec/requests/webhooks_controller_spec.rb
index bd4a52ec0ee..efab54e5f17 100644
--- a/spec/requests/webhooks_controller_spec.rb
+++ b/spec/requests/webhooks_controller_spec.rb
@@ -110,7 +110,29 @@ RSpec.describe WebhooksController do
fab!(:user) { Fabricate(:user, email:) }
fab!(:email_log) { Fabricate(:email_log, user:, message_id: message_id, to_address: email) }
- it "works" do
+ it "returns an error when no verification key is configured" do
+ SiteSetting.sendgrid_verification_key = ""
+
+ post "/webhooks/sendgrid.json",
+ params: {
+ "_json" => [
+ {
+ "email" => email,
+ "smtp-id" => "<[email protected]>",
+ "event" => "bounce",
+ "status" => "5.0.0",
+ },
+ ],
+ }
+
+ expect(response.status).to eq(406)
+ expect(email_log.reload.bounced).to eq(false)
+ end
+
+ it "hard bounces" do
+ SiteSetting.sendgrid_verification_key = "test"
+ WebhooksController.any_instance.stubs(:valid_sendgrid_signature?).returns(true)
+
post "/webhooks/sendgrid.json",
params: {
"_json" => [
@@ -132,6 +154,9 @@ RSpec.describe WebhooksController do
end
it "sets the bounce error code to 5.1.2 when payload's `event` is `bounce`, `type` is `blocked` and `status` is blank" do
+ SiteSetting.sendgrid_verification_key = "test"
+ WebhooksController.any_instance.stubs(:valid_sendgrid_signature?).returns(true)
+
post "/webhooks/sendgrid.json",
params: {
"_json" => [
@@ -229,7 +254,8 @@ RSpec.describe WebhooksController do
end
describe "#mailjet" do
- it "works" do
+ it "returns an error when no webhook token is configured" do
+ SiteSetting.mailjet_webhook_token = ""
user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
@@ -241,6 +267,23 @@ RSpec.describe WebhooksController do
"CustomID" => message_id,
}
+ expect(response.status).to eq(406)
+ expect(email_log.reload.bounced).to eq(false)
+ end
+
+ it "hard bounces" do
+ SiteSetting.mailjet_webhook_token = "foo"
+ user = Fabricate(:user, email: email)
+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+
+ post "/webhooks/mailjet.json?t=foo",
+ params: {
+ "event" => "bounce",
+ "email" => email,
+ "hard_bounce" => true,
+ "CustomID" => message_id,
+ }
+
expect(response.status).to eq(200)
email_log.reload
@@ -302,11 +345,12 @@ RSpec.describe WebhooksController do
end
describe "#mailpace" do
- it "works" do
+ it "hard bounces" do
+ SiteSetting.mailpace_webhook_token = "foo"
user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
- post "/webhooks/mailpace.json",
+ post "/webhooks/mailpace.json?t=foo",
params: {
event: "email.bounced",
payload: {
@@ -325,10 +369,11 @@ RSpec.describe WebhooksController do
end
it "soft bounces" do
+ SiteSetting.mailpace_webhook_token = "foo"
user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
- post "/webhooks/mailpace.json",
+ post "/webhooks/mailpace.json?t=foo",
params: {
event: "email.deferred",
payload: {
@@ -345,14 +390,79 @@ RSpec.describe WebhooksController do
expect(email_log.bounce_error_code).to eq(nil) # mailpace doesn't give us this
expect(email_log.user.user_stat.bounce_score).to eq(SiteSetting.soft_bounce_score)
end
+
+ it "verifies webhook tokens" do
+ SiteSetting.mailpace_webhook_token = "foo"
+ user = Fabricate(:user, email: email)
+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+
+ post "/webhooks/mailpace.json?t=foo",
+ params: {
+ event: "email.bounced",
+ payload: {
+ status: "bounced",
+ to: email,
+ message_id: "<#{message_id}>",
+ },
+ }
+
+ expect(response.status).to eq(200)
+ expect(email_log.reload.bounced).to eq(true)
+ end
+
+ it "returns error if token verification fails" do
+ SiteSetting.mailpace_webhook_token = "foo"
+ user = Fabricate(:user, email: email)
+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+
+ post "/webhooks/mailpace.json?t=bar",
+ params: {
+ event: "email.bounced",
+ payload: {
+ status: "bounced",
+ to: email,
+ message_id: "<#{message_id}>",
+ },
+ }
+
+ expect(response.status).to eq(406)
+ expect(email_log.reload.bounced).to eq(false)
+ end
end
describe "#mandrill" do
+ it "returns an error when no authentication key is configured" do
+ SiteSetting.mandrill_authentication_key = ""
+ user = Fabricate(:user, email: email)
+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+
+ post "/webhooks/mandrill.json",
+ params: {
+ mandrill_events: [
+ {
+ "event" => "hard_bounce",
+ "msg" => {
+ "email" => email,
+ "diag" => "5.1.1",
+ "metadata" => {
+ "message_id" => message_id,
+ },
+ },
+ },
+ ].to_json,
+ }
+
+ expect(response.status).to eq(406)
+ expect(email_log.reload.bounced).to eq(false)
+ end
+
let(:payload) do
"mandrill_events=%5B%7B%22event%22%3A%22hard_bounce%22%2C%22msg%22%3A%7B%22email%22%3A%22em%40il.com%22%2C%22diag%22%3A%225.1.1%22%2C%22bounce_description%22%3A%22smtp%3B+550-5.1.1+The+email+account+that+you+tried+to+reach+does+not+exist.%22%2C%22metadata%22%3A%7B%22message_id%22%3A%2212345%40il.com%22%7D%7D%7D%5D"
end
- it "works" do
+ it "hard bounces" do
+ SiteSetting.mandrill_authentication_key = "test"
+ WebhooksController.any_instance.stubs(:valid_mandrill_signature?).returns(true)
user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
@@ -432,7 +542,8 @@ RSpec.describe WebhooksController do
end
describe "#postmark" do
- it "works" do
+ it "returns an error when no webhook token is configured" do
+ SiteSetting.postmark_webhook_token = ""
user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
@@ -442,6 +553,22 @@ RSpec.describe WebhooksController do
"MessageID" => message_id,
"Email" => email,
}
+
+ expect(response.status).to eq(406)
+ expect(email_log.reload.bounced).to eq(false)
+ end
+
+ it "hard bounces" do
+ SiteSetting.postmark_webhook_token = "foo"
+ user = Fabricate(:user, email: email)
+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+
+ post "/webhooks/postmark.json?t=foo",
+ params: {
+ "Type" => "HardBounce",
+ "MessageID" => message_id,
+ "Email" => email,
+ }
expect(response.status).to eq(200)
email_log.reload
@@ -451,10 +578,11 @@ RSpec.describe WebhooksController do
end
it "soft bounces" do
+ SiteSetting.postmark_webhook_token = "foo"
user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
- post "/webhooks/postmark.json",
+ post "/webhooks/postmark.json?t=foo",
params: {
"Type" => "SoftBounce",
"MessageID" => message_id,
@@ -518,7 +646,8 @@ RSpec.describe WebhooksController do
end
describe "#sparkpost" do
- it "works" do
+ it "returns an error when no webhook token is configured" do
+ SiteSetting.sparkpost_webhook_token = ""
user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
@@ -540,6 +669,33 @@ RSpec.describe WebhooksController do
],
}
+ expect(response.status).to eq(406)
+ expect(email_log.reload.bounced).to eq(false)
+ end
+
+ it "hard bounces" do
+ SiteSetting.sparkpost_webhook_token = "foo"
+ user = Fabricate(:user, email: email)
+ email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)
+
+ post "/webhooks/sparkpost.json?t=foo",
+ params: {
+ "_json" => [
+ {
+ "msys" => {
+ "message_event" => {
+ "bounce_class" => 10,
+ "error_code" => "554",
+ "rcpt_to" => email,
+ "rcpt_meta" => {
+ "message_id" => message_id,
+ },
+ },
+ },
+ },
+ ],
+ }
+
expect(response.status).to eq(200)
email_log.reload
@@ -690,7 +846,7 @@ RSpec.describe WebhooksController do
before { Jobs.run_immediately! }
- it "works" do
+ it "hard bounces" do
user = Fabricate(:user, email: email)
email_log = Fabricate(:email_log, user: user, message_id: message_id, to_address: email)