mailkube-rails 1.0.2 → 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 +4 -4
- data/README.md +24 -4
- data/lib/mailkube/rails/version.rb +1 -1
- data/lib/mailkube/rails/webhooks_controller.rb +52 -12
- data/sig/mailkube/rails.rbs +3 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f2da3769f8ea62219d2fa8f410a8c270546e05d896a7bf58b1c6dec9da1a8c08
|
|
4
|
+
data.tar.gz: 8afbff78747a43ed99ca4c8194444e4e5655ae20c552d377dc101cdf19868a92
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '0097ef0162c5a8f00b21233bcd99b0ddd1a0f4d28c67c7f24e4acd79b3907e130ec439315655a1e865ca36cf56b057bc7473343f49175a3e9d6b95d97a88f7d8'
|
|
7
|
+
data.tar.gz: a9a84927bd84a386cddc9faea70e988c9ca8b9e12ee8e79f1584264c818e7bf149477353755d584e35e5ddc105159249e9ca8f8157caf3d8e15563402a4d1614
|
data/README.md
CHANGED
|
@@ -126,13 +126,14 @@ any other delivery method.
|
|
|
126
126
|
|
|
127
127
|
## Webhooks
|
|
128
128
|
|
|
129
|
-
You write the
|
|
129
|
+
You write the routes. A mountable engine would force a path on your application and drag generator
|
|
130
130
|
machinery along for one endpoint, so this gem ships the controller and lets you mount it where you
|
|
131
131
|
want:
|
|
132
132
|
|
|
133
133
|
```ruby
|
|
134
134
|
# config/initializers/mailkube.rb
|
|
135
|
-
|
|
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"]
|
|
136
137
|
```
|
|
137
138
|
|
|
138
139
|
```ruby
|
|
@@ -140,15 +141,28 @@ Rails.application.config.mailkube.webhook_secret = ENV.fetch("MAILKUBE_WEBHOOK_S
|
|
|
140
141
|
require "mailkube/rails/webhooks_controller"
|
|
141
142
|
|
|
142
143
|
Rails.application.routes.draw do
|
|
144
|
+
get "/webhooks/mailkube",
|
|
145
|
+
to: Mailkube::Rails::WebhooksController.action(:handshake)
|
|
143
146
|
post "/webhooks/mailkube",
|
|
144
147
|
to: Mailkube::Rails::WebhooksController.action(:create)
|
|
145
148
|
end
|
|
146
149
|
```
|
|
147
150
|
|
|
148
|
-
The `require` is needed and the `.action(
|
|
151
|
+
The `require` is needed and the `.action(...)` form is deliberate: a gem's `lib/` is on the load
|
|
149
152
|
path but is not an autoload path, so the `"controller#action"` string form would name a constant
|
|
150
153
|
Zeitwerk has never been told about.
|
|
151
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
|
+
|
|
152
166
|
Deliveries are verified against the **raw** request body and published as one notification:
|
|
153
167
|
|
|
154
168
|
```ruby
|
|
@@ -176,12 +190,18 @@ cannot present a token; the signature is what authenticates the request, and it
|
|
|
176
190
|
stronger. Any middleware that decodes and re-encodes the request body breaks verification: it no
|
|
177
191
|
longer covers the bytes that were signed.
|
|
178
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
|
+
|
|
179
199
|
## Extending this gem
|
|
180
200
|
|
|
181
201
|
Before adding a setting, a mapped field, or another entry point, read
|
|
182
202
|
[`.rules/INTEGRATION_CONTRACT.md`](.rules/INTEGRATION_CONTRACT.md) (what every mailkube framework
|
|
183
203
|
integration does identically) and [`.rules/RAILS_INTEGRATION.md`](.rules/RAILS_INTEGRATION.md)
|
|
184
|
-
(how those rules land here).
|
|
204
|
+
(how those rules land here). The contract ends with the checklist for exposing a new SDK capability.
|
|
185
205
|
|
|
186
206
|
The short version: the capability has to exist in the SDK first, inputs are mapped in `payload.rb`
|
|
187
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
|
|
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
|
|
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
|
|
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
|
-
#
|
|
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
|
-
#
|
|
86
|
-
#
|
|
87
|
-
#
|
|
88
|
-
#
|
|
89
|
-
#
|
|
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
|
data/sig/mailkube/rails.rbs
CHANGED
|
@@ -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
|