Class: Cloudflare::Email::IngressController

Inherits:
ActionMailbox::BaseController
  • Object
show all
Defined in:
app/controllers/cloudflare/email/ingress_controller.rb

Overview

ActionMailbox ingress for Cloudflare Email Worker forwards.

The shipped Worker template signs each forwarded message with HMAC-SHA256 over “timestamp.#raw_body” and sends:

X-CF-Email-Timestamp: <unix seconds>
X-CF-Email-Signature: <hex digest>

Set the shared secret in Rails credentials under cloudflare.ingress_secret (or in the CLOUDFLARE_INGRESS_SECRET env var) and as the Worker secret INGRESS_SECRET via ‘wrangler secret put INGRESS_SECRET`.

Instance Method Summary collapse

Instance Method Details

#createObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/cloudflare/email/ingress_controller.rb', line 18

def create
  ActiveSupport::Notifications.instrument(
    "cloudflare_email.ingress",
    bytes: raw_body.bytesize,
  ) do |payload|
    case Cloudflare::Email::Verification.verify(
          secret:    secret,
          body:      raw_body,
          timestamp: request.headers["X-CF-Email-Timestamp"],
          signature: request.headers["X-CF-Email-Signature"],
        )
    when :stale
      payload[:result] = :stale
      head :request_timeout
    when :bad_signature
      payload[:result] = :bad_signature
      head :unauthorized
    when :ok
      inbound = ActionMailbox::InboundEmail.create_and_extract_message_id!(raw_body)
      payload[:result]     = :ok
      payload[:message_id] = inbound.message_id
      head :ok
    end
  end
end