@segmentstream/ontology 0.169.0 → 0.170.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.170.0
4
+
5
+ An `hmac` webhook signature can sign the body as `{ body: "sorted-json" }`: the body parsed as a
6
+ JSON object and written again compactly with the keys of every object sorted, as `JSON.stringify`
7
+ writes it, for providers that sign such a re-serialization rather than the bytes they sent, such
8
+ as NOWPayments' IPN (`x-nowpayments-sig`, hex HMAC-SHA512). A delivery whose body is not a JSON
9
+ object fails the check. `{ body: true }` still signs the raw bytes. Adding a signature to a token
10
+ webhook changes its URL, since a signed webhook has no token: declare the signed one beside the
11
+ old one while objects created before still carry the old URL.
12
+
3
13
  ## 0.169.0
4
14
 
5
15
  An entity can declare `eraseFromWarehouseOnDelete: true`: deleting one of its records then erases
@@ -531,7 +531,7 @@ export declare const WebhookTriggerManifest: Schema.Struct<{
531
531
  readonly tolerance: Schema.optionalKey<Schema.Number>;
532
532
  }>>;
533
533
  readonly message: Schema.optionalKey<Schema.$Array<Schema.Union<readonly [Schema.String, Schema.Struct<{
534
- readonly body: Schema.Literal<true>;
534
+ readonly body: Schema.Literals<readonly [true, "sorted-json"]>;
535
535
  }>, Schema.Struct<{
536
536
  readonly header: Schema.String;
537
537
  }>, Schema.Struct<{
@@ -607,7 +607,7 @@ export declare const WorkflowTriggerManifest: Schema.Union<readonly [Schema.Stru
607
607
  readonly tolerance: Schema.optionalKey<Schema.Number>;
608
608
  }>>;
609
609
  readonly message: Schema.optionalKey<Schema.$Array<Schema.Union<readonly [Schema.String, Schema.Struct<{
610
- readonly body: Schema.Literal<true>;
610
+ readonly body: Schema.Literals<readonly [true, "sorted-json"]>;
611
611
  }>, Schema.Struct<{
612
612
  readonly header: Schema.String;
613
613
  }>, Schema.Struct<{
@@ -690,7 +690,7 @@ export declare const WorkflowManifest: Schema.Struct<{
690
690
  readonly tolerance: Schema.optionalKey<Schema.Number>;
691
691
  }>>;
692
692
  readonly message: Schema.optionalKey<Schema.$Array<Schema.Union<readonly [Schema.String, Schema.Struct<{
693
- readonly body: Schema.Literal<true>;
693
+ readonly body: Schema.Literals<readonly [true, "sorted-json"]>;
694
694
  }>, Schema.Struct<{
695
695
  readonly header: Schema.String;
696
696
  }>, Schema.Struct<{
@@ -1459,7 +1459,7 @@ export declare const OntologyManifestWire: Schema.encodeKeys<Schema.Struct<{
1459
1459
  readonly tolerance: Schema.optionalKey<Schema.Number>;
1460
1460
  }>>;
1461
1461
  readonly message: Schema.optionalKey<Schema.$Array<Schema.Union<readonly [Schema.String, Schema.Struct<{
1462
- readonly body: Schema.Literal<true>;
1462
+ readonly body: Schema.Literals<readonly [true, "sorted-json"]>;
1463
1463
  }>, Schema.Struct<{
1464
1464
  readonly header: Schema.String;
1465
1465
  }>, Schema.Struct<{
@@ -1739,7 +1739,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
1739
1739
  readonly signature?: {
1740
1740
  readonly scheme: string;
1741
1741
  readonly message?: readonly (string | {
1742
- readonly body: true;
1742
+ readonly body: true | "sorted-json";
1743
1743
  } | {
1744
1744
  readonly header: string;
1745
1745
  } | {
@@ -2117,7 +2117,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
2117
2117
  readonly signature?: {
2118
2118
  readonly scheme: string;
2119
2119
  readonly message?: readonly (string | {
2120
- readonly body: true;
2120
+ readonly body: true | "sorted-json";
2121
2121
  } | {
2122
2122
  readonly header: string;
2123
2123
  } | {
@@ -2497,7 +2497,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
2497
2497
  readonly signature?: {
2498
2498
  readonly scheme: string;
2499
2499
  readonly message?: readonly (string | {
2500
- readonly body: true;
2500
+ readonly body: true | "sorted-json";
2501
2501
  } | {
2502
2502
  readonly header: string;
2503
2503
  } | {
@@ -2875,7 +2875,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
2875
2875
  readonly signature?: {
2876
2876
  readonly scheme: string;
2877
2877
  readonly message?: readonly (string | {
2878
- readonly body: true;
2878
+ readonly body: true | "sorted-json";
2879
2879
  } | {
2880
2880
  readonly header: string;
2881
2881
  } | {
@@ -3503,7 +3503,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
3503
3503
  readonly tolerance: Schema.optionalKey<Schema.Number>;
3504
3504
  }>>;
3505
3505
  readonly message: Schema.optionalKey<Schema.$Array<Schema.Union<readonly [Schema.String, Schema.Struct<{
3506
- readonly body: Schema.Literal<true>;
3506
+ readonly body: Schema.Literals<readonly [true, "sorted-json"]>;
3507
3507
  }>, Schema.Struct<{
3508
3508
  readonly header: Schema.String;
3509
3509
  }>, Schema.Struct<{
@@ -4020,7 +4020,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
4020
4020
  readonly tolerance: Schema.optionalKey<Schema.Number>;
4021
4021
  }>>;
4022
4022
  readonly message: Schema.optionalKey<Schema.$Array<Schema.Union<readonly [Schema.String, Schema.Struct<{
4023
- readonly body: Schema.Literal<true>;
4023
+ readonly body: Schema.Literals<readonly [true, "sorted-json"]>;
4024
4024
  }>, Schema.Struct<{
4025
4025
  readonly header: Schema.String;
4026
4026
  }>, Schema.Struct<{
@@ -4292,7 +4292,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
4292
4292
  readonly signature?: {
4293
4293
  readonly scheme: string;
4294
4294
  readonly message?: readonly (string | {
4295
- readonly body: true;
4295
+ readonly body: true | "sorted-json";
4296
4296
  } | {
4297
4297
  readonly header: string;
4298
4298
  } | {
@@ -4670,7 +4670,7 @@ export declare const OntologyManifest: Schema.declareConstructor<{
4670
4670
  readonly signature?: {
4671
4671
  readonly scheme: string;
4672
4672
  readonly message?: readonly (string | {
4673
- readonly body: true;
4673
+ readonly body: true | "sorted-json";
4674
4674
  } | {
4675
4675
  readonly header: string;
4676
4676
  } | {
@@ -5054,7 +5054,7 @@ export declare const decodeManifestDocument: (value: unknown) => Effect.Effect<{
5054
5054
  readonly signature?: {
5055
5055
  readonly scheme: string;
5056
5056
  readonly message?: readonly (string | {
5057
- readonly body: true;
5057
+ readonly body: true | "sorted-json";
5058
5058
  } | {
5059
5059
  readonly header: string;
5060
5060
  } | {
@@ -5432,7 +5432,7 @@ export declare const decodeManifestDocument: (value: unknown) => Effect.Effect<{
5432
5432
  readonly signature?: {
5433
5433
  readonly scheme: string;
5434
5434
  readonly message?: readonly (string | {
5435
- readonly body: true;
5435
+ readonly body: true | "sorted-json";
5436
5436
  } | {
5437
5437
  readonly header: string;
5438
5438
  } | {
package/dist/manifest.js CHANGED
@@ -395,7 +395,7 @@ export const WebhookTriggerManifest = Schema.Struct({
395
395
  })),
396
396
  message: Schema.optionalKey(Schema.Array(Schema.Union([
397
397
  Schema.String,
398
- Schema.Struct({ body: Schema.Literal(true) }),
398
+ Schema.Struct({ body: Schema.Literals([true, "sorted-json"]) }),
399
399
  Schema.Struct({ header: Schema.String }),
400
400
  Schema.Struct({ timestamp: Schema.Literal(true) }),
401
401
  ]))),
@@ -479,7 +479,7 @@ export declare const MigrationPlan: Schema.Struct<{
479
479
  readonly signature?: {
480
480
  readonly scheme: string;
481
481
  readonly message?: readonly (string | {
482
- readonly body: true;
482
+ readonly body: true | "sorted-json";
483
483
  } | {
484
484
  readonly header: string;
485
485
  } | {
@@ -857,7 +857,7 @@ export declare const MigrationPlan: Schema.Struct<{
857
857
  readonly signature?: {
858
858
  readonly scheme: string;
859
859
  readonly message?: readonly (string | {
860
- readonly body: true;
860
+ readonly body: true | "sorted-json";
861
861
  } | {
862
862
  readonly header: string;
863
863
  } | {
@@ -1237,7 +1237,7 @@ export declare const MigrationPlan: Schema.Struct<{
1237
1237
  readonly signature?: {
1238
1238
  readonly scheme: string;
1239
1239
  readonly message?: readonly (string | {
1240
- readonly body: true;
1240
+ readonly body: true | "sorted-json";
1241
1241
  } | {
1242
1242
  readonly header: string;
1243
1243
  } | {
@@ -1615,7 +1615,7 @@ export declare const MigrationPlan: Schema.Struct<{
1615
1615
  readonly signature?: {
1616
1616
  readonly scheme: string;
1617
1617
  readonly message?: readonly (string | {
1618
- readonly body: true;
1618
+ readonly body: true | "sorted-json";
1619
1619
  } | {
1620
1620
  readonly header: string;
1621
1621
  } | {
@@ -2141,7 +2141,7 @@ export declare const MigrationPlan: Schema.Struct<{
2141
2141
  readonly tolerance: Schema.optionalKey<Schema.Number>;
2142
2142
  }>>;
2143
2143
  readonly message: Schema.optionalKey<Schema.$Array<Schema.Union<readonly [Schema.String, Schema.Struct<{
2144
- readonly body: Schema.Literal<true>;
2144
+ readonly body: Schema.Literals<readonly [true, "sorted-json"]>;
2145
2145
  }>, Schema.Struct<{
2146
2146
  readonly header: Schema.String;
2147
2147
  }>, Schema.Struct<{
@@ -2517,7 +2517,7 @@ export declare const MigrationPlan: Schema.Struct<{
2517
2517
  readonly tolerance: Schema.optionalKey<Schema.Number>;
2518
2518
  }>>;
2519
2519
  readonly message: Schema.optionalKey<Schema.$Array<Schema.Union<readonly [Schema.String, Schema.Struct<{
2520
- readonly body: Schema.Literal<true>;
2520
+ readonly body: Schema.Literals<readonly [true, "sorted-json"]>;
2521
2521
  }>, Schema.Struct<{
2522
2522
  readonly header: Schema.String;
2523
2523
  }>, Schema.Struct<{
@@ -2758,7 +2758,7 @@ export declare const MigrationPlan: Schema.Struct<{
2758
2758
  readonly signature?: {
2759
2759
  readonly scheme: string;
2760
2760
  readonly message?: readonly (string | {
2761
- readonly body: true;
2761
+ readonly body: true | "sorted-json";
2762
2762
  } | {
2763
2763
  readonly header: string;
2764
2764
  } | {
@@ -3136,7 +3136,7 @@ export declare const MigrationPlan: Schema.Struct<{
3136
3136
  readonly signature?: {
3137
3137
  readonly scheme: string;
3138
3138
  readonly message?: readonly (string | {
3139
- readonly body: true;
3139
+ readonly body: true | "sorted-json";
3140
3140
  } | {
3141
3141
  readonly header: string;
3142
3142
  } | {
@@ -641,7 +641,7 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
641
641
  readonly tolerance: Schema.optionalKey<Schema.Number>;
642
642
  }>>;
643
643
  readonly message: Schema.optionalKey<Schema.$Array<Schema.Union<readonly [Schema.String, Schema.Struct<{
644
- readonly body: Schema.Literal<true>;
644
+ readonly body: Schema.Literals<readonly [true, "sorted-json"]>;
645
645
  }>, Schema.Struct<{
646
646
  readonly header: Schema.String;
647
647
  }>, Schema.Struct<{
@@ -1032,7 +1032,7 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
1032
1032
  readonly tolerance: Schema.optionalKey<Schema.Number>;
1033
1033
  }>>;
1034
1034
  readonly message: Schema.optionalKey<Schema.$Array<Schema.Union<readonly [Schema.String, Schema.Struct<{
1035
- readonly body: Schema.Literal<true>;
1035
+ readonly body: Schema.Literals<readonly [true, "sorted-json"]>;
1036
1036
  }>, Schema.Struct<{
1037
1037
  readonly header: Schema.String;
1038
1038
  }>, Schema.Struct<{
@@ -107,6 +107,13 @@ export declare const STRIPE_TOLERANCE_SECONDS = 300;
107
107
  * is SegmentStream's, which holds the secret.
108
108
  */
109
109
  export declare function hmacSignature(format: WebhookHmacFormat): SignatureScheme;
110
+ /**
111
+ * The body re-serialized as a provider signs it with `{ body: "sorted-json" }`: parsed as a JSON
112
+ * object and written compactly with every object's keys sorted, exactly as
113
+ * `JSON.stringify(sortObject(JSON.parse(body)))` writes it in Node; arrays keep their order.
114
+ * `undefined` when the body is not UTF-8 JSON whose top level is an object.
115
+ */
116
+ export declare function sortedJson(body: Uint8Array): string | undefined;
110
117
  /**
111
118
  * Stripe's `Stripe-Signature`: `t=<unix seconds>` and one or more `v1=<hex>`. The signed bytes are
112
119
  * `<t>.` followed by the raw body; the HMAC is SHA-256 keyed with the whole `whsec_…` string.
@@ -197,7 +197,16 @@ export function hmacSignature(format) {
197
197
  parts.push(encoder.encode(part));
198
198
  }
199
199
  else if ("body" in part) {
200
- parts.push(body);
200
+ if (part.body === true) {
201
+ parts.push(body);
202
+ }
203
+ else {
204
+ const sorted = sortedJson(body);
205
+ if (sorted === undefined) {
206
+ return { ok: false, reason: "signature_malformed" };
207
+ }
208
+ parts.push(encoder.encode(sorted));
209
+ }
201
210
  }
202
211
  else if ("timestamp" in part) {
203
212
  parts.push(encoder.encode(timestamp ?? ""));
@@ -222,6 +231,35 @@ export function hmacSignature(format) {
222
231
  return { ok: true, message, signatures };
223
232
  };
224
233
  }
234
+ /**
235
+ * The body re-serialized as a provider signs it with `{ body: "sorted-json" }`: parsed as a JSON
236
+ * object and written compactly with every object's keys sorted, exactly as
237
+ * `JSON.stringify(sortObject(JSON.parse(body)))` writes it in Node; arrays keep their order.
238
+ * `undefined` when the body is not UTF-8 JSON whose top level is an object.
239
+ */
240
+ export function sortedJson(body) {
241
+ const text = utf8Text(body);
242
+ let parsed;
243
+ if (text === undefined) {
244
+ return undefined;
245
+ }
246
+ try {
247
+ parsed = JSON.parse(text);
248
+ }
249
+ catch {
250
+ return undefined;
251
+ }
252
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
253
+ return undefined;
254
+ }
255
+ // Object.fromEntries defines each key as an own property, `__proto__` included, and
256
+ // JSON.stringify writes integer keys first, as it does for the provider's own sorted object.
257
+ return JSON.stringify(parsed, (_key, value) => typeof value === "object" && value !== null && !Array.isArray(value)
258
+ ? Object.fromEntries(Object.keys(value)
259
+ .sort()
260
+ .map((key) => [key, value[key]]))
261
+ : value);
262
+ }
225
263
  /**
226
264
  * Stripe's `Stripe-Signature`: `t=<unix seconds>` and one or more `v1=<hex>`. The signed bytes are
227
265
  * `<t>.` followed by the raw body; the HMAC is SHA-256 keyed with the whole `whsec_…` string.
@@ -18,9 +18,16 @@ export declare const WEBHOOK_SIGNATURE_ENCODINGS: readonly ["hex", "base64"];
18
18
  export type WebhookSignatureEncoding = (typeof WEBHOOK_SIGNATURE_ENCODINGS)[number];
19
19
  /** The bytes of each algorithm's digest, which a signature must decode to. */
20
20
  export declare const WEBHOOK_HMAC_DIGEST_BYTES: Readonly<Record<WebhookHmacAlgorithm, number>>;
21
- /** One piece of the signed bytes: text, the raw body, a header's value or the timestamp. */
21
+ /**
22
+ * How a signed message includes the delivery's body: `true` its raw bytes, `"sorted-json"` the
23
+ * body parsed as a JSON object and written again compactly with the keys of every object sorted,
24
+ * as `JSON.stringify` writes it, for providers that sign such a re-serialization.
25
+ */
26
+ export declare const WEBHOOK_SIGNED_BODIES: readonly [true, "sorted-json"];
27
+ export type WebhookSignedBody = (typeof WEBHOOK_SIGNED_BODIES)[number];
28
+ /** One piece of the signed bytes: text, the body, a header's value or the timestamp. */
22
29
  export type WebhookSignedPart = string | {
23
- readonly body: true;
30
+ readonly body: WebhookSignedBody;
24
31
  } | {
25
32
  readonly header: string;
26
33
  } | {
package/dist/webhooks.js CHANGED
@@ -20,6 +20,12 @@ export const WEBHOOK_HMAC_DIGEST_BYTES = {
20
20
  sha256: 32,
21
21
  sha512: 64,
22
22
  };
23
+ /**
24
+ * How a signed message includes the delivery's body: `true` its raw bytes, `"sorted-json"` the
25
+ * body parsed as a JSON object and written again compactly with the keys of every object sorted,
26
+ * as `JSON.stringify` writes it, for providers that sign such a re-serialization.
27
+ */
28
+ export const WEBHOOK_SIGNED_BODIES = [true, "sorted-json"];
23
29
  /** How far a signed timestamp may be from a delivery's arrival by default, in seconds. */
24
30
  export const WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS = 300;
25
31
  /** The widest tolerance a format may declare, in seconds. */
@@ -241,8 +247,11 @@ export function webhookSignatureProblems(signature) {
241
247
  report(at, `A text part has 1 to ${MAX_WEBHOOK_MESSAGE_TEXT} characters.`);
242
248
  }
243
249
  }
244
- else if (isRecord(part) && Object.keys(part).length === 1 && part.body === true) {
250
+ else if (isRecord(part) && Object.keys(part).length === 1 && "body" in part) {
245
251
  bodies += 1;
252
+ if (!WEBHOOK_SIGNED_BODIES.includes(part.body)) {
253
+ report(at, 'The body is signed as { body: true } or { body: "sorted-json" }.');
254
+ }
246
255
  }
247
256
  else if (isRecord(part) && Object.keys(part).length === 1 && part.timestamp === true) {
248
257
  if (!timed) {
@@ -256,11 +265,11 @@ export function webhookSignatureProblems(signature) {
256
265
  }
257
266
  }
258
267
  else {
259
- report(at, "A part is text, { body: true }, { header } or { timestamp: true }.");
268
+ report(at, 'A part is text, { body: true }, { body: "sorted-json" }, { header } or { timestamp: true }.');
260
269
  }
261
270
  }
262
271
  if (bodies !== 1) {
263
- report("message", "message includes { body: true } exactly once.");
272
+ report("message", "message includes the body, as { body }, exactly once.");
264
273
  }
265
274
  }
266
275
  }
@@ -336,7 +345,7 @@ export function webhookSignatureManifest(signature) {
336
345
  message: message.map((part) => typeof part === "string"
337
346
  ? part
338
347
  : "body" in part
339
- ? { body: true }
348
+ ? { body: part.body }
340
349
  : "timestamp" in part
341
350
  ? { timestamp: true }
342
351
  : { header: part.header }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream/ontology",
3
- "version": "0.169.0",
3
+ "version": "0.170.0",
4
4
  "description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",