@middag-io/n8n-nodes-middag 0.3.0 → 0.3.1

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.
package/README.md CHANGED
@@ -20,10 +20,18 @@ All nodes use the **MIDDAG Connector** credential:
20
20
 
21
21
  ### Version 2 (CloudEvents, default)
22
22
 
23
- Receives the CloudEvents (`application/cloudevents+json`, structured mode) of the Connector.
23
+ Receives the CloudEvents (structured mode) of the Connector. The Connector sends them as
24
+ `application/json`. `application/cloudevents+json` also works: current n8n keeps the raw body
25
+ for every non-multipart content type, and the node does not check the content type.
24
26
  For each request the node does this, in order:
25
27
 
26
28
  1. Verifies `X-Middag-Signature` over the raw body. If it fails, it answers 401.
29
+ If n8n did not keep the raw body, the node checks the signature over `JSON.stringify` of the
30
+ body n8n parsed. This rebuilds the signed bytes exactly, because the Connector signs and
31
+ sends `JSON.stringify(event)`. A body sent in any other form fails with 401 `mismatch`, so the
32
+ fallback never accepts an unsigned body. With neither a raw body nor a parsed JSON object
33
+ (for example `{}`, which n8n leaves for a content type it does not parse), the node answers
34
+ 400 `missing_body`.
27
35
  2. Checks the CloudEvent envelope. If the body is not a CloudEvent, it answers 400.
28
36
  3. Filters by **Event Types** (catalog `<entity>.<action>`, e.g. `form.submitted`) and
29
37
  **Providers** (e.g. `hubspot`). Empty means all. Other events get 200 `{ignored: true}`.
@@ -2,7 +2,8 @@ import type { IWebhookFunctions, IWebhookResponseData, INodeType, INodeTypeDescr
2
2
  /**
3
3
  * Version 2 (default for new nodes) receives the CloudEvents (`application/cloudevents+json`) of
4
4
  * the MIDDAG Connector worker (`worker-ts-middag-connector-hubspot`, packages/contracts): it
5
- * verifies `X-Middag-Signature` over the raw body, filters by `type` (catalog) and `provider`,
5
+ * verifies `X-Middag-Signature` over the raw body (or, when n8n did not keep it, over the
6
+ * re-serialized parsed body), filters by `type` (catalog) and `provider`,
6
7
  * dedupes on `X-Middag-Delivery-Id` and always answers the HTTP request itself. Its output item is
7
8
  * `{ deliveryId, event }`, with the CloudEvent unchanged (read `event.data`). See
8
9
  * cloudEventDelivery.ts.
@@ -11,7 +11,8 @@ const CLOUDEVENTS_VERSION = 2;
11
11
  /**
12
12
  * Version 2 (default for new nodes) receives the CloudEvents (`application/cloudevents+json`) of
13
13
  * the MIDDAG Connector worker (`worker-ts-middag-connector-hubspot`, packages/contracts): it
14
- * verifies `X-Middag-Signature` over the raw body, filters by `type` (catalog) and `provider`,
14
+ * verifies `X-Middag-Signature` over the raw body (or, when n8n did not keep it, over the
15
+ * re-serialized parsed body), filters by `type` (catalog) and `provider`,
15
16
  * dedupes on `X-Middag-Delivery-Id` and always answers the HTTP request itself. Its output item is
16
17
  * `{ deliveryId, event }`, with the CloudEvent unchanged (read `event.data`). See
17
18
  * cloudEventDelivery.ts.
@@ -168,6 +169,7 @@ async function cloudEventWebhook(ctx) {
168
169
  signatureHeader: firstValue(headers[middagSignature_1.MIDDAG_SIGNATURE_HEADER]),
169
170
  deliveryId: firstValue(headers[DELIVERY_ID_HEADER]),
170
171
  rawBody: rawBody === undefined ? undefined : rawBody.toString('utf8'),
172
+ parsedBody: rawBody === undefined ? ctx.getBodyData() : undefined,
171
173
  eventTypes: ctx.getNodeParameter('eventTypes', []),
172
174
  providers: ctx.getNodeParameter('providers', []),
173
175
  staticData: ctx.getWorkflowStaticData('node'),
@@ -4,8 +4,16 @@ export type CloudEventDeliveryInput = {
4
4
  readonly secret: string;
5
5
  readonly signatureHeader: string | undefined;
6
6
  readonly deliveryId: string | undefined;
7
- /** Exact request bytes. n8n does not parse `application/cloudevents+json`, so this is the only source. */
7
+ /**
8
+ * Exact request bytes (`req.rawBody`). Current n8n keeps them for every non-multipart content
9
+ * type, including `application/cloudevents+json` (which it does not parse into the body).
10
+ */
8
11
  readonly rawBody: string | undefined;
12
+ /**
13
+ * The body n8n already parsed (`getBodyData()`), used only when `rawBody` is missing. See
14
+ * `signedBodyOf` for why re-serializing it is a sound source for the signature check.
15
+ */
16
+ readonly parsedBody?: unknown;
9
17
  /** Selected CloudEvent types; empty means every type. */
10
18
  readonly eventTypes: readonly string[];
11
19
  /** Selected providers; empty means every provider. */
@@ -13,6 +13,30 @@ function failure(status, error) {
13
13
  function isSelected(selected, value) {
14
14
  return selected.length === 0 || selected.includes(value);
15
15
  }
16
+ function isNonEmptyPlainObject(value) {
17
+ return (typeof value === 'object' &&
18
+ value !== null &&
19
+ !Array.isArray(value) &&
20
+ Object.keys(value).length > 0);
21
+ }
22
+ /**
23
+ * The text the signature is checked against: the raw body when n8n kept it, otherwise
24
+ * `JSON.stringify` of the body n8n parsed.
25
+ *
26
+ * The signature is HMAC-SHA256 over `"<t>.<exact body text>"`, so a fallback is only sound if it
27
+ * rebuilds those exact bytes. It does for this sender: the Connector signs and sends
28
+ * `JSON.stringify(event)` (compact, insertion key order), and `JSON.stringify(JSON.parse(x))`
29
+ * returns `x` unchanged for any `x` that `JSON.stringify` produced. Any other body (extra
30
+ * whitespace, a different sender, a proxy that rewrote it) rebuilds different bytes and fails
31
+ * with 401 `mismatch`: the fallback can only reject, never accept something unsigned. A body that
32
+ * is missing, empty (`{}`, what n8n leaves for a content type it does not parse) or not a JSON
33
+ * object answers 400 `missing_body`.
34
+ */
35
+ function signedBodyOf(input) {
36
+ if (input.rawBody !== undefined)
37
+ return input.rawBody;
38
+ return isNonEmptyPlainObject(input.parsedBody) ? JSON.stringify(input.parsedBody) : undefined;
39
+ }
16
40
  /**
17
41
  * MIDDAG Trigger v2: one delivery from the connector worker. Order: signature, envelope,
18
42
  * type/provider filter, dedupe, emit. Every answer is final here (the node never defers the
@@ -20,12 +44,13 @@ function isSelected(selected, value) {
20
44
  * "Retry Delivery" operation, which asks the worker to redeliver.
21
45
  */
22
46
  function handleCloudEventDelivery(input) {
23
- if (input.rawBody === undefined)
47
+ const body = signedBodyOf(input);
48
+ if (body === undefined)
24
49
  return failure(HTTP_BAD_REQUEST, 'missing_body');
25
- const verification = (0, middagSignature_1.verifyMiddagSignature)(input.secret, input.signatureHeader, input.rawBody, input.nowMs);
50
+ const verification = (0, middagSignature_1.verifyMiddagSignature)(input.secret, input.signatureHeader, body, input.nowMs);
26
51
  if (!verification.valid)
27
52
  return failure(HTTP_UNAUTHORIZED, verification.reason);
28
- const parsed = (0, cloudEvent_1.parseCloudEvent)(input.rawBody);
53
+ const parsed = (0, cloudEvent_1.parseCloudEvent)(body);
29
54
  if (!parsed.ok)
30
55
  return failure(HTTP_BAD_REQUEST, parsed.reason);
31
56
  const { event } = parsed;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middag-io/n8n-nodes-middag",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "n8n community nodes for the MIDDAG Connector: a trigger for its signed CloudEvent deliveries and an action node for its operations.",
5
5
  "keywords": [
6
6
  "n8n-community-node-package",