mailkube-rails 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51841915f7e8ab1cadf9811384560ef487bc599b9ca4ee5f8b7a58647c394063
4
- data.tar.gz: 9bd4f08e64b9b2c4ab360db7abe3125c6bcdac0b7f5111790e755d7462f71d98
3
+ metadata.gz: f2da3769f8ea62219d2fa8f410a8c270546e05d896a7bf58b1c6dec9da1a8c08
4
+ data.tar.gz: 8afbff78747a43ed99ca4c8194444e4e5655ae20c552d377dc101cdf19868a92
5
5
  SHA512:
6
- metadata.gz: c193d18bfdadbff588b0a3f83a601bcc2a2826592d27dedb2a8224bf47dcc3add0a62fbda34edcbc2d64c6ddd81715495980c27a9c161d018b6460dc6455d3ce
7
- data.tar.gz: 31f288311241e5b7f34a95296ec25d3896ef184033206168f240697a2e7cbd9a6fa48c79d81d5b8b3f64e541c96f9d957007d62d9bbc451ad7ba48465c37bdc2
6
+ metadata.gz: '0097ef0162c5a8f00b21233bcd99b0ddd1a0f4d28c67c7f24e4acd79b3907e130ec439315655a1e865ca36cf56b057bc7473343f49175a3e9d6b95d97a88f7d8'
7
+ data.tar.gz: a9a84927bd84a386cddc9faea70e988c9ca8b9e12ee8e79f1584264c818e7bf149477353755d584e35e5ddc105159249e9ca8f8157caf3d8e15563402a4d1614
data/README.md CHANGED
@@ -5,13 +5,15 @@
5
5
  [![License: Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE)
6
6
  [![Code of Conduct](https://img.shields.io/badge/Contributor%20Covenant-2.1-purple.svg)](CODE_OF_CONDUCT.md)
7
7
 
8
- ActionMailer delivery method for mailkube.
8
+ ActionMailer delivery method for [mailkube](https://mailkube.com).
9
9
 
10
10
  Send mail through mailkube using ActionMailer exactly as you already do, and receive webhooks as
11
11
  `ActiveSupport::Notifications`. This gem is a thin adapter over the
12
12
  [`mailkube`](https://rubygems.org/gems/mailkube) gem: the API, retries,
13
13
  errors and signature verification all live there.
14
14
 
15
+ Full product and API documentation: [docs.mailkube.com/sdks/rails](https://docs.mailkube.com/sdks/rails).
16
+
15
17
  Requires Ruby 3.4+ and Rails 7.2.3+.
16
18
 
17
19
  ## Install
@@ -124,13 +126,14 @@ any other delivery method.
124
126
 
125
127
  ## Webhooks
126
128
 
127
- You write the route. A mountable engine would force a path on your application and drag generator
129
+ You write the routes. A mountable engine would force a path on your application and drag generator
128
130
  machinery along for one endpoint, so this gem ships the controller and lets you mount it where you
129
131
  want:
130
132
 
131
133
  ```ruby
132
134
  # config/initializers/mailkube.rb
133
- Rails.application.config.mailkube.webhook_secret = ENV.fetch("MAILKUBE_WEBHOOK_SECRET")
135
+ # nil until you register the endpoint and mailkube shows the secret; deliveries get a 500 until then
136
+ Rails.application.config.mailkube.webhook_secret = ENV["MAILKUBE_WEBHOOK_SECRET"]
134
137
  ```
135
138
 
136
139
  ```ruby
@@ -138,15 +141,28 @@ Rails.application.config.mailkube.webhook_secret = ENV.fetch("MAILKUBE_WEBHOOK_S
138
141
  require "mailkube/rails/webhooks_controller"
139
142
 
140
143
  Rails.application.routes.draw do
144
+ get "/webhooks/mailkube",
145
+ to: Mailkube::Rails::WebhooksController.action(:handshake)
141
146
  post "/webhooks/mailkube",
142
147
  to: Mailkube::Rails::WebhooksController.action(:create)
143
148
  end
144
149
  ```
145
150
 
146
- The `require` is needed and the `.action(:create)` form is deliberate: a gem's `lib/` is on the load
151
+ The `require` is needed and the `.action(...)` form is deliberate: a gem's `lib/` is on the load
147
152
  path but is not an autoload path, so the `"controller#action"` string form would name a constant
148
153
  Zeitwerk has never been told about.
149
154
 
155
+ The `GET` route is the registration handshake, and the controller answers it. mailkube keeps an
156
+ endpoint URL only after `GET <url>?hub.mode=subscribe&hub.challenge=<token>` answers `200` with the
157
+ token as the body, so both routes take the same path. The handshake reads no secret, which lets it
158
+ succeed before mailkube has shown you one. Register the URL exactly as it is routed, scheme and
159
+ trailing slash included: mailkube follows no redirect, so a URL that redirects (to HTTPS, to
160
+ another host, to a login page) is refused at registration.
161
+
162
+ If you drew a `GET` route of your own from an earlier version of these docs, replace it with the
163
+ line above. Until you do, Rails answers the probe with whichever `GET` route is drawn first, and both
164
+ apply the same rule.
165
+
150
166
  Deliveries are verified against the **raw** request body and published as one notification:
151
167
 
152
168
  ```ruby
@@ -174,12 +190,18 @@ cannot present a token; the signature is what authenticates the request, and it
174
190
  stronger. Any middleware that decodes and re-encodes the request body breaks verification: it no
175
191
  longer covers the bytes that were signed.
176
192
 
193
+ Both routes must stay open to a caller with no session. The controller subclasses
194
+ `ActionController::Base` rather than your `ApplicationController`, so your authentication filters
195
+ and browser checks do not apply to it. Keep anything that challenges or redirects an anonymous
196
+ request (a constraint, a Rack middleware, HTTP basic auth in front of a staging host) off this path,
197
+ or the probe and every delivery after it are refused.
198
+
177
199
  ## Extending this gem
178
200
 
179
201
  Before adding a setting, a mapped field, or another entry point, read
180
202
  [`.rules/INTEGRATION_CONTRACT.md`](.rules/INTEGRATION_CONTRACT.md) (what every mailkube framework
181
203
  integration does identically) and [`.rules/RAILS_INTEGRATION.md`](.rules/RAILS_INTEGRATION.md)
182
- (how those rules land here). Both carry a checklist.
204
+ (how those rules land here). The contract ends with the checklist for exposing a new SDK capability.
183
205
 
184
206
  The short version: the capability has to exist in the SDK first, inputs are mapped in `payload.rb`
185
207
  and never at a call site, and configuration goes through `config.rb`.
@@ -8,6 +8,6 @@ module Mailkube
8
8
  # version to RubyGems and a different one to the API. It stays at 0.0.0 in the repository:
9
9
  # semantic-release rewrites this line in the release runner and never commits it back. See
10
10
  # `.rules/RELEASE.md`.
11
- VERSION = "1.0.1"
11
+ VERSION = "1.1.0"
12
12
  end
13
13
  end
@@ -6,21 +6,25 @@ require_relative "config"
6
6
 
7
7
  module Mailkube
8
8
  module Rails
9
- # The inbound entry point: verify a delivery, then publish it.
9
+ # The inbound entry point: answer the registration handshake, verify a delivery, then publish it.
10
10
  #
11
- # This file is **not** required by `lib/mailkube/rails.rb`. It is reached through the route an
11
+ # This file is **not** required by `lib/mailkube/rails.rb`. It is reached through the routes an
12
12
  # application writes, so a worker that only sends mail never loads ActionController.
13
13
  #
14
14
  # ## No engine, deliberately
15
15
  #
16
16
  # A mountable engine would force a path on every host application and drag `isolate_namespace`
17
- # plus generator machinery along for one endpoint. Consumers write two lines instead, which
17
+ # plus generator machinery along for one endpoint. Consumers write three lines instead, which
18
18
  # also means they choose the path, the constraints and the middleware:
19
19
  #
20
20
  # require "mailkube/rails/webhooks_controller"
21
+ # get "/webhooks/mailkube", to: Mailkube::Rails::WebhooksController.action(:handshake)
21
22
  # post "/webhooks/mailkube", to: Mailkube::Rails::WebhooksController.action(:create)
22
23
  #
23
- # The `.action(:create)` form rather than the `"controller#action"` string: a gem's `lib/` is
24
+ # Both routes name the same path: mailkube probes the URL it was given with a GET and follows
25
+ # no redirect, so the handshake cannot live anywhere else.
26
+ #
27
+ # The `.action(...)` form rather than the `"controller#action"` string: a gem's `lib/` is
24
28
  # on `$LOAD_PATH` but is not an autoload path, so the string form would resolve to a constant
25
29
  # Zeitwerk has never been told about and raise at the first request. The explicit `require` is
26
30
  # what makes the constant exist, and is the reason this file can stay off the boot path.
@@ -36,12 +40,35 @@ module Mailkube
36
40
  # The notification this controller publishes on a verified delivery.
37
41
  NOTIFICATION = "webhook.mailkube"
38
42
 
43
+ # The longest `hub.challenge` the handshake echoes. mailkube sends 32 hex characters.
44
+ MAX_CHALLENGE_LENGTH = 256
45
+
39
46
  # A webhook POST is machine-to-machine and cannot carry a CSRF token, so the check is skipped
40
47
  # rather than left to reject every delivery. The signature is what authenticates the request,
41
48
  # and it is strictly stronger: CSRF protection proves a browser session, while the HMAC
42
- # proves the sender holds the endpoint secret.
49
+ # proves the sender holds the endpoint secret. The skip names no action, so it covers the
50
+ # handshake too, which a GET would pass in any case.
51
+ #
52
+ # This class subclasses `ActionController::Base`, never the host's `ApplicationController`,
53
+ # so the host's authentication filters and browser checks do not reach either action.
43
54
  skip_forgery_protection
44
55
 
56
+ # Answer mailkube's registration handshake.
57
+ #
58
+ # mailkube keeps an endpoint URL only after `GET <url>?hub.mode=subscribe&hub.challenge=<token>`
59
+ # answers 200 with the token as the body. No signing secret is read: mailkube shows the secret
60
+ # only after registration succeeds, so the probe has to be answered before one can exist.
61
+ #
62
+ # Answers 200 with the challenge as `text/plain`, and 400 with `bad handshake` otherwise.
63
+ #
64
+ # @return [void]
65
+ def handshake
66
+ challenge = subscribe_challenge(request.query_parameters)
67
+ return render(plain: "bad handshake", status: :bad_request) if challenge.nil?
68
+
69
+ render plain: challenge
70
+ end
71
+
45
72
  # Receive one webhook delivery.
46
73
  #
47
74
  # Answers 204 on success, 400 when verification fails, and 500 when no secret is configured.
@@ -68,6 +95,21 @@ module Mailkube
68
95
 
69
96
  private
70
97
 
98
+ # The challenge to echo, or nil when the query is not a well-formed subscribe handshake.
99
+ #
100
+ # The one definition of the handshake rule: mode `subscribe` and a challenge of 1 to
101
+ # {MAX_CHALLENGE_LENGTH} characters. A challenge sent as a list or a nested hash is refused
102
+ # rather than stringified and echoed.
103
+ #
104
+ # @param query [Hash{String => Object}] the parsed query string.
105
+ # @return [String, nil] the challenge, or nil.
106
+ def subscribe_challenge(query)
107
+ challenge = query["hub.challenge"]
108
+ return nil unless query["hub.mode"] == "subscribe" && challenge.is_a?(String)
109
+
110
+ challenge.length.between?(1, MAX_CHALLENGE_LENGTH) ? challenge : nil
111
+ end
112
+
71
113
  # The freshness window, as a splattable hash, or empty to accept the SDK's default.
72
114
  #
73
115
  # Only the optional argument travels in a splat: the three required ones are named at the
@@ -82,13 +124,11 @@ module Mailkube
82
124
 
83
125
  # Convert the Rack environment into the plain Hash the SDK reads headers from.
84
126
  #
85
- # **`request.headers` cannot be passed through**, which is worth stating because it is the
86
- # obvious thing to write and it fails twice over. `ActionDispatch::Http::Headers` includes
87
- # `Enumerable` and nothing else, so it has no `transform_keys` and the SDK's normalization
88
- # raises `NoMethodError` on it. And its `each` delegates to the Rack environment, so
89
- # converting it to a Hash yields `HTTP_X_WEBHOOK_ID` rather than `X-Webhook-Id` — which
90
- # downcases to something the SDK's lookup will never match, turning every delivery into a
91
- # signature failure with no clue as to why.
127
+ # The SDK would also take `request.headers` as it is. `ActionDispatch::Http::Headers` is
128
+ # `Enumerable` rather than a Hash and yields CGI env names (`HTTP_X_WEBHOOK_ID`), and the
129
+ # SDK's normalization iterates any such mapping and strips the `HTTP_` prefix, in every
130
+ # `mailkube` release this gem allows. The conversion here passes the request headers only,
131
+ # never the rest of the Rack environment.
92
132
  #
93
133
  # The conversion is generic rather than a list of the three headers the SDK reads today.
94
134
  # Naming them would put a copy of the signature scheme's header set in this repository, and
@@ -54,8 +54,11 @@ module Mailkube
54
54
 
55
55
  class WebhooksController < ::ActionController::Base
56
56
  NOTIFICATION: String
57
+ MAX_CHALLENGE_LENGTH: Integer
57
58
 
59
+ def handshake: () -> void
58
60
  def create: () -> void
61
+ def subscribe_challenge: (untyped) -> String?
59
62
  def tolerance_argument: () -> Hash[Symbol, Integer]
60
63
  def header_hash: () -> Hash[String, String]
61
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailkube-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mail Tactic Corporation