@segmentstream/ontology 0.125.0 → 0.126.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 +10 -0
- package/dist/analyze.js +4 -17
- package/dist/index.d.ts +1 -1
- package/dist/manifest.d.ts +405 -9
- package/dist/manifest.js +29 -2
- package/dist/migrate/model.d.ts +230 -6
- package/dist/service/schemas.d.ts +56 -0
- package/dist/service/webhooks.d.ts +22 -6
- package/dist/service/webhooks.js +131 -50
- package/dist/service/workflows.js +8 -6
- package/dist/webhooks.d.ts +105 -2
- package/dist/webhooks.js +293 -2
- package/dist/workflow.d.ts +10 -5
- package/dist/workflows.d.ts +1 -1
- package/package.json +1 -1
package/dist/service/webhooks.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Effect, Redacted } from "effect";
|
|
2
|
+
import { META_SIGNATURE_FORMAT, STRIPE_SIGNATURE_FORMAT, WEBHOOK_HMAC_DIGEST_BYTES, WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS, webhookHmacFormat, } from "../webhooks.js";
|
|
2
3
|
import { fixedWindowLimiter } from "./rate-limit.js";
|
|
3
4
|
/** How long the service waits for the backend's answer before answering 503. */
|
|
4
5
|
export const WEBHOOK_VERIFY_TIMEOUT_MS = 3_000;
|
|
@@ -12,6 +13,14 @@ export function deployedWebhookVerifier(environment, fetchImpl = fetch) {
|
|
|
12
13
|
return (request) => verify(request, {
|
|
13
14
|
algorithm: request.algorithm,
|
|
14
15
|
encoding: request.encoding,
|
|
16
|
+
...(request.key === undefined
|
|
17
|
+
? {}
|
|
18
|
+
: {
|
|
19
|
+
key: {
|
|
20
|
+
decode: request.key.decode,
|
|
21
|
+
...(request.key.prefix === undefined ? {} : { prefix: request.key.prefix }),
|
|
22
|
+
},
|
|
23
|
+
}),
|
|
15
24
|
message: Buffer.from(request.message).toString("base64"),
|
|
16
25
|
signatures: request.signatures,
|
|
17
26
|
});
|
|
@@ -89,64 +98,136 @@ export function deployedSecretVerifier(environment, operation, fetchImpl) {
|
|
|
89
98
|
};
|
|
90
99
|
}
|
|
91
100
|
/** How far Stripe's `t` may be from the time the delivery arrived, in seconds. */
|
|
92
|
-
export const STRIPE_TOLERANCE_SECONDS =
|
|
101
|
+
export const STRIPE_TOLERANCE_SECONDS = WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS;
|
|
93
102
|
/** The most signatures of one delivery that are sent to the backend. */
|
|
94
103
|
const MAX_SIGNATURES = 5;
|
|
104
|
+
const BASE64 = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
105
|
+
/** A signature written as the format says and as long as its digest, normalized; else undefined. */
|
|
106
|
+
function signatureCandidate(value, format) {
|
|
107
|
+
const bytes = WEBHOOK_HMAC_DIGEST_BYTES[format.algorithm];
|
|
108
|
+
if (format.encoding === "hex") {
|
|
109
|
+
return value.length === bytes * 2 && /^[0-9a-f]+$/i.test(value)
|
|
110
|
+
? value.toLowerCase()
|
|
111
|
+
: undefined;
|
|
112
|
+
}
|
|
113
|
+
const unpadded = value.replace(/=+$/, "");
|
|
114
|
+
return BASE64.test(value) &&
|
|
115
|
+
unpadded.length === Math.ceil((bytes * 4) / 3) &&
|
|
116
|
+
(value.length === unpadded.length || value.length === Math.ceil(bytes / 3) * 4)
|
|
117
|
+
? value
|
|
118
|
+
: undefined;
|
|
119
|
+
}
|
|
120
|
+
/** A header's entries, such as `t=1,v1=ab`, as name and value pairs in order. */
|
|
121
|
+
function headerEntries(header, entries) {
|
|
122
|
+
return header.split(entries.separator).flatMap((part) => {
|
|
123
|
+
const trimmed = part.trim();
|
|
124
|
+
const at = trimmed.indexOf(entries.assign);
|
|
125
|
+
return at <= 0 ? [] : [[trimmed.slice(0, at).trim(), trimmed.slice(at + 1).trim()]];
|
|
126
|
+
});
|
|
127
|
+
}
|
|
95
128
|
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
129
|
+
* The scheme a format describes: it finds the candidate signatures and the timestamp in the
|
|
130
|
+
* delivery's headers, refuses a stale timestamp, and assembles the signed bytes. The HMAC itself
|
|
131
|
+
* is SegmentStream's, which holds the secret.
|
|
98
132
|
*/
|
|
99
|
-
export
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
const
|
|
108
|
-
|
|
109
|
-
|
|
133
|
+
export function hmacSignature(format) {
|
|
134
|
+
const encoder = new TextEncoder();
|
|
135
|
+
return (headers, body, receivedAt) => {
|
|
136
|
+
const header = headers[format.signatures.header];
|
|
137
|
+
if (header === undefined || header.trim() === "") {
|
|
138
|
+
return { ok: false, reason: "signature_missing" };
|
|
139
|
+
}
|
|
140
|
+
const signatures = [];
|
|
141
|
+
const declared = format.signatures.entries;
|
|
142
|
+
const entries = declared === undefined ? undefined : headerEntries(header, declared);
|
|
143
|
+
if (entries !== undefined && declared !== undefined) {
|
|
144
|
+
for (const [name, value] of entries) {
|
|
145
|
+
const candidate = name === declared.name ? signatureCandidate(value, format) : undefined;
|
|
146
|
+
if (candidate !== undefined && signatures.length < MAX_SIGNATURES) {
|
|
147
|
+
signatures.push(candidate);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
110
150
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
151
|
+
else {
|
|
152
|
+
const prefix = format.signatures.prefix ?? "";
|
|
153
|
+
const value = header.trim();
|
|
154
|
+
if (value.slice(0, prefix.length).toLowerCase() !== prefix.toLowerCase()) {
|
|
155
|
+
return { ok: false, reason: "signature_malformed" };
|
|
156
|
+
}
|
|
157
|
+
const candidate = signatureCandidate(value.slice(prefix.length), format);
|
|
158
|
+
if (candidate !== undefined) {
|
|
159
|
+
signatures.push(candidate);
|
|
160
|
+
}
|
|
115
161
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
162
|
+
const declaredTimestamp = format.timestamp;
|
|
163
|
+
let timestamp;
|
|
164
|
+
if (declaredTimestamp !== undefined) {
|
|
165
|
+
timestamp =
|
|
166
|
+
declaredTimestamp.entry !== undefined
|
|
167
|
+
? entries?.find(([name]) => name === declaredTimestamp.entry)?.[1]
|
|
168
|
+
: headers[declaredTimestamp.header ?? ""]?.trim();
|
|
169
|
+
const milliseconds = declaredTimestamp.unit === "milliseconds";
|
|
170
|
+
if (timestamp === undefined ||
|
|
171
|
+
!(milliseconds ? /^\d{1,15}$/ : /^\d{1,12}$/).test(timestamp)) {
|
|
172
|
+
return { ok: false, reason: "signature_malformed" };
|
|
173
|
+
}
|
|
120
174
|
}
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
175
|
+
if (signatures.length === 0) {
|
|
176
|
+
return { ok: false, reason: "signature_malformed" };
|
|
177
|
+
}
|
|
178
|
+
if (declaredTimestamp !== undefined && timestamp !== undefined) {
|
|
179
|
+
const signedAt = Number(timestamp) / (declaredTimestamp.unit === "milliseconds" ? 1000 : 1);
|
|
180
|
+
const tolerance = declaredTimestamp.tolerance ?? WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS;
|
|
181
|
+
if (Math.abs(receivedAt.getTime() / 1000 - signedAt) > tolerance) {
|
|
182
|
+
return { ok: false, reason: "signature_stale" };
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const parts = [];
|
|
186
|
+
for (const part of format.message ?? [{ body: true }]) {
|
|
187
|
+
if (typeof part === "string") {
|
|
188
|
+
parts.push(encoder.encode(part));
|
|
189
|
+
}
|
|
190
|
+
else if ("body" in part) {
|
|
191
|
+
parts.push(body);
|
|
192
|
+
}
|
|
193
|
+
else if ("timestamp" in part) {
|
|
194
|
+
parts.push(encoder.encode(timestamp ?? ""));
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
const value = headers[part.header];
|
|
198
|
+
if (value === undefined) {
|
|
199
|
+
return { ok: false, reason: "signature_malformed" };
|
|
200
|
+
}
|
|
201
|
+
parts.push(encoder.encode(value));
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
if (parts.length === 1 && parts[0] === body) {
|
|
205
|
+
return { ok: true, message: body, signatures };
|
|
206
|
+
}
|
|
207
|
+
const message = new Uint8Array(parts.reduce((total, part) => total + part.byteLength, 0));
|
|
208
|
+
let offset = 0;
|
|
209
|
+
for (const part of parts) {
|
|
210
|
+
message.set(part, offset);
|
|
211
|
+
offset += part.byteLength;
|
|
212
|
+
}
|
|
213
|
+
return { ok: true, message, signatures };
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Stripe's `Stripe-Signature`: `t=<unix seconds>` and one or more `v1=<hex>`. The signed bytes are
|
|
218
|
+
* `<t>.` followed by the raw body; the HMAC is SHA-256 keyed with the whole `whsec_…` string.
|
|
219
|
+
*/
|
|
220
|
+
export const stripeSignature = hmacSignature(STRIPE_SIGNATURE_FORMAT);
|
|
134
221
|
/** Meta signs exactly the raw body; its app secret stays in the backend. */
|
|
135
|
-
export const metaSignature = (
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
};
|
|
145
|
-
/** Every scheme the SDK checks, by the name a declaration uses. */
|
|
146
|
-
export const SIGNATURE_SCHEMES = {
|
|
147
|
-
stripe: stripeSignature,
|
|
148
|
-
meta: metaSignature,
|
|
149
|
-
};
|
|
222
|
+
export const metaSignature = hmacSignature(META_SIGNATURE_FORMAT);
|
|
223
|
+
/**
|
|
224
|
+
* What checks a manifest signature: its format and that format's scheme, or `undefined` for a
|
|
225
|
+
* scheme this SDK does not know.
|
|
226
|
+
*/
|
|
227
|
+
export function signatureCheck(signature) {
|
|
228
|
+
const format = webhookHmacFormat(signature);
|
|
229
|
+
return format === undefined ? undefined : { format, scheme: hmacSignature(format) };
|
|
230
|
+
}
|
|
150
231
|
/**
|
|
151
232
|
* The request's body, read up to `limit` bytes: `too_large` without reading further, or
|
|
152
233
|
* `unreadable` when the client went away.
|
|
@@ -16,7 +16,7 @@ import { ActionRejected, PermissionDenied, } from "../errors.js";
|
|
|
16
16
|
import { isActionTrigger, isCronTrigger, isWebhookTrigger, } from "../manifest.js";
|
|
17
17
|
import { webhookTokenMatches } from "../webhook-token.js";
|
|
18
18
|
import { MAX_WEBHOOK_BODY_BYTES, MAX_META_WEBHOOK_BODY_BYTES, MAX_WEBHOOK_VERIFICATION_CHARS, WEBHOOK_BASE_PATH, webhookPath, } from "../webhooks.js";
|
|
19
|
-
import { readWebhookBody,
|
|
19
|
+
import { readWebhookBody, signatureCheck, signatureRefusals, utf8Text, } from "./webhooks.js";
|
|
20
20
|
import { clientAddress } from "./rate-limit.js";
|
|
21
21
|
import { isAllowed, workflowPermission } from "../permissions.js";
|
|
22
22
|
import { OntologyActions, OntologyReads, OntologyWrites, } from "../runtime.js";
|
|
@@ -1348,11 +1348,12 @@ export function workflowsLayer(options) {
|
|
|
1348
1348
|
if (wait !== undefined) {
|
|
1349
1349
|
return yield* refuse("signature_throttled", 429, { "retry-after": String(wait) });
|
|
1350
1350
|
}
|
|
1351
|
-
const
|
|
1352
|
-
if (
|
|
1351
|
+
const checked = signatureCheck(signature);
|
|
1352
|
+
if (checked === undefined || options.webhookVerifier === undefined) {
|
|
1353
1353
|
return yield* refuse("scheme_unavailable", 503);
|
|
1354
1354
|
}
|
|
1355
|
-
const
|
|
1355
|
+
const { format } = checked;
|
|
1356
|
+
const content = checked.scheme(request.headers, body, receivedAt);
|
|
1356
1357
|
if (!content.ok) {
|
|
1357
1358
|
refusals.refused(client);
|
|
1358
1359
|
return yield* refuse(content.reason, 401);
|
|
@@ -1360,8 +1361,9 @@ export function workflowsLayer(options) {
|
|
|
1360
1361
|
const verifier = options.webhookVerifier;
|
|
1361
1362
|
const verified = yield* Effect.promise(() => verifier({
|
|
1362
1363
|
secret: signature.secret,
|
|
1363
|
-
algorithm:
|
|
1364
|
-
encoding:
|
|
1364
|
+
algorithm: format.algorithm,
|
|
1365
|
+
encoding: format.encoding,
|
|
1366
|
+
...(format.key === undefined ? {} : { key: format.key }),
|
|
1365
1367
|
message: content.message,
|
|
1366
1368
|
signatures: content.signatures,
|
|
1367
1369
|
identityToken: requestIdentityToken(request),
|
package/dist/webhooks.d.ts
CHANGED
|
@@ -4,9 +4,112 @@ import { Schema } from "effect";
|
|
|
4
4
|
* Node built-in, so the workflows entry may re-export it; the token derivation, which needs
|
|
5
5
|
* `node:crypto`, is `webhook-token.ts`.
|
|
6
6
|
*/
|
|
7
|
-
/**
|
|
8
|
-
|
|
7
|
+
/**
|
|
8
|
+
* The signature schemes a webhook trigger can check: `hmac` declares a provider's HMAC format
|
|
9
|
+
* with the trigger; `stripe` and `meta` name two published formats.
|
|
10
|
+
*/
|
|
11
|
+
export declare const WEBHOOK_SIGNATURE_SCHEMES: readonly ["stripe", "meta", "hmac"];
|
|
9
12
|
export type WebhookSignatureScheme = (typeof WEBHOOK_SIGNATURE_SCHEMES)[number];
|
|
13
|
+
/** The hashes an HMAC signature may use. */
|
|
14
|
+
export declare const WEBHOOK_HMAC_ALGORITHMS: readonly ["sha1", "sha256", "sha512"];
|
|
15
|
+
export type WebhookHmacAlgorithm = (typeof WEBHOOK_HMAC_ALGORITHMS)[number];
|
|
16
|
+
/** How a provider writes its signatures. Hex compares in either case. */
|
|
17
|
+
export declare const WEBHOOK_SIGNATURE_ENCODINGS: readonly ["hex", "base64"];
|
|
18
|
+
export type WebhookSignatureEncoding = (typeof WEBHOOK_SIGNATURE_ENCODINGS)[number];
|
|
19
|
+
/** The bytes of each algorithm's digest, which a signature must decode to. */
|
|
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. */
|
|
22
|
+
export type WebhookSignedPart = string | {
|
|
23
|
+
readonly body: true;
|
|
24
|
+
} | {
|
|
25
|
+
readonly header: string;
|
|
26
|
+
} | {
|
|
27
|
+
readonly timestamp: true;
|
|
28
|
+
};
|
|
29
|
+
/**
|
|
30
|
+
* A provider's HMAC signature format: which bytes it signs, where its signatures travel and how
|
|
31
|
+
* they are written. The ontology service reads them from each delivery; SegmentStream computes
|
|
32
|
+
* the HMAC with the workspace secret, which never leaves it.
|
|
33
|
+
*/
|
|
34
|
+
export interface WebhookHmacFormat {
|
|
35
|
+
readonly algorithm: WebhookHmacAlgorithm;
|
|
36
|
+
readonly encoding: WebhookSignatureEncoding;
|
|
37
|
+
/**
|
|
38
|
+
* How the secret's value becomes the HMAC key. Omitted: its UTF-8 bytes. `decode: "base64"`
|
|
39
|
+
* decodes the value after `prefix`, when it starts with it, such as Svix's `whsec_…`.
|
|
40
|
+
*/
|
|
41
|
+
readonly key?: {
|
|
42
|
+
readonly decode: "base64";
|
|
43
|
+
readonly prefix?: string | undefined;
|
|
44
|
+
} | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* The header that carries the signatures: the whole value after an optional `prefix`, such as
|
|
47
|
+
* `sha256=`, or with `entries` a list such as `t=…,v1=…`, whose entries called `name` are the
|
|
48
|
+
* candidates.
|
|
49
|
+
*/
|
|
50
|
+
readonly signatures: {
|
|
51
|
+
readonly header: string;
|
|
52
|
+
readonly prefix?: string | undefined;
|
|
53
|
+
readonly entries?: {
|
|
54
|
+
readonly separator: "," | ";" | " ";
|
|
55
|
+
readonly assign: "=" | ",";
|
|
56
|
+
readonly name: string;
|
|
57
|
+
} | undefined;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* When the provider signed the delivery, in a `header` of its own or an `entry` of the signature
|
|
61
|
+
* header; a delivery further than `tolerance` seconds from its arrival is stale.
|
|
62
|
+
*/
|
|
63
|
+
readonly timestamp?: {
|
|
64
|
+
readonly header?: string | undefined;
|
|
65
|
+
readonly entry?: string | undefined;
|
|
66
|
+
readonly unit?: "seconds" | "milliseconds" | undefined;
|
|
67
|
+
readonly tolerance?: number | undefined;
|
|
68
|
+
} | undefined;
|
|
69
|
+
/** The signed bytes in order; the raw body alone when omitted. */
|
|
70
|
+
readonly message?: ReadonlyArray<WebhookSignedPart> | undefined;
|
|
71
|
+
}
|
|
72
|
+
/** How far a signed timestamp may be from a delivery's arrival by default, in seconds. */
|
|
73
|
+
export declare const WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS = 300;
|
|
74
|
+
/** The widest tolerance a format may declare, in seconds. */
|
|
75
|
+
export declare const MAX_WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS = 3600;
|
|
76
|
+
/** The most parts a format's signed message may have. */
|
|
77
|
+
export declare const MAX_WEBHOOK_MESSAGE_PARTS = 8;
|
|
78
|
+
/** The longest text part of a signed message. */
|
|
79
|
+
export declare const MAX_WEBHOOK_MESSAGE_TEXT = 64;
|
|
80
|
+
/** Stripe's `Stripe-Signature`: `t=<seconds>,v1=<hex>`, over `<t>.<raw body>`. */
|
|
81
|
+
export declare const STRIPE_SIGNATURE_FORMAT: WebhookHmacFormat;
|
|
82
|
+
/** Meta's `X-Hub-Signature-256: sha256=<hex>`, over the raw body. */
|
|
83
|
+
export declare const META_SIGNATURE_FORMAT: WebhookHmacFormat;
|
|
84
|
+
/**
|
|
85
|
+
* The format a declared or manifest signature checks: the named schemes' published formats, or
|
|
86
|
+
* the one an `hmac` signature declares. `undefined` for a scheme this SDK does not know.
|
|
87
|
+
*/
|
|
88
|
+
export declare function webhookHmacFormat(signature: {
|
|
89
|
+
readonly scheme: string;
|
|
90
|
+
}): WebhookHmacFormat | undefined;
|
|
91
|
+
/** A problem with a signature declaration: the key it concerns, under `signature`, and why. */
|
|
92
|
+
export interface WebhookSignatureProblem {
|
|
93
|
+
readonly path: string;
|
|
94
|
+
readonly message: string;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Every problem of a webhook trigger's `signature`, as `check` and SegmentStream's publication
|
|
98
|
+
* check report them: a known scheme and a workspace secret's name, and for `hmac` a complete
|
|
99
|
+
* format. Empty when the declaration is valid.
|
|
100
|
+
*/
|
|
101
|
+
export declare function webhookSignatureProblems(signature: unknown): ReadonlyArray<WebhookSignatureProblem>;
|
|
102
|
+
/**
|
|
103
|
+
* A valid signature declaration as the manifest records it: `{ scheme, secret }` for a named
|
|
104
|
+
* scheme, and for `hmac` its format's settings, each written only when declared.
|
|
105
|
+
*/
|
|
106
|
+
export declare function webhookSignatureManifest(signature: {
|
|
107
|
+
readonly scheme: string;
|
|
108
|
+
readonly secret: string;
|
|
109
|
+
} & Partial<WebhookHmacFormat>): {
|
|
110
|
+
readonly scheme: string;
|
|
111
|
+
readonly secret: string;
|
|
112
|
+
} & Partial<WebhookHmacFormat>;
|
|
10
113
|
/** The most webhook triggers one ontology may declare across its workflows. */
|
|
11
114
|
export declare const MAX_WEBHOOKS = 20;
|
|
12
115
|
/** The most request headers one webhook trigger may pass to its workflow. */
|
package/dist/webhooks.js
CHANGED
|
@@ -5,8 +5,299 @@ import { p } from "./property.js";
|
|
|
5
5
|
* Node built-in, so the workflows entry may re-export it; the token derivation, which needs
|
|
6
6
|
* `node:crypto`, is `webhook-token.ts`.
|
|
7
7
|
*/
|
|
8
|
-
/**
|
|
9
|
-
|
|
8
|
+
/**
|
|
9
|
+
* The signature schemes a webhook trigger can check: `hmac` declares a provider's HMAC format
|
|
10
|
+
* with the trigger; `stripe` and `meta` name two published formats.
|
|
11
|
+
*/
|
|
12
|
+
export const WEBHOOK_SIGNATURE_SCHEMES = ["stripe", "meta", "hmac"];
|
|
13
|
+
/** The hashes an HMAC signature may use. */
|
|
14
|
+
export const WEBHOOK_HMAC_ALGORITHMS = ["sha1", "sha256", "sha512"];
|
|
15
|
+
/** How a provider writes its signatures. Hex compares in either case. */
|
|
16
|
+
export const WEBHOOK_SIGNATURE_ENCODINGS = ["hex", "base64"];
|
|
17
|
+
/** The bytes of each algorithm's digest, which a signature must decode to. */
|
|
18
|
+
export const WEBHOOK_HMAC_DIGEST_BYTES = {
|
|
19
|
+
sha1: 20,
|
|
20
|
+
sha256: 32,
|
|
21
|
+
sha512: 64,
|
|
22
|
+
};
|
|
23
|
+
/** How far a signed timestamp may be from a delivery's arrival by default, in seconds. */
|
|
24
|
+
export const WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS = 300;
|
|
25
|
+
/** The widest tolerance a format may declare, in seconds. */
|
|
26
|
+
export const MAX_WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS = 3_600;
|
|
27
|
+
/** The most parts a format's signed message may have. */
|
|
28
|
+
export const MAX_WEBHOOK_MESSAGE_PARTS = 8;
|
|
29
|
+
/** The longest text part of a signed message. */
|
|
30
|
+
export const MAX_WEBHOOK_MESSAGE_TEXT = 64;
|
|
31
|
+
/** Stripe's `Stripe-Signature`: `t=<seconds>,v1=<hex>`, over `<t>.<raw body>`. */
|
|
32
|
+
export const STRIPE_SIGNATURE_FORMAT = {
|
|
33
|
+
algorithm: "sha256",
|
|
34
|
+
encoding: "hex",
|
|
35
|
+
signatures: {
|
|
36
|
+
header: "stripe-signature",
|
|
37
|
+
entries: { separator: ",", assign: "=", name: "v1" },
|
|
38
|
+
},
|
|
39
|
+
timestamp: { entry: "t", tolerance: WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS },
|
|
40
|
+
message: [{ timestamp: true }, ".", { body: true }],
|
|
41
|
+
};
|
|
42
|
+
/** Meta's `X-Hub-Signature-256: sha256=<hex>`, over the raw body. */
|
|
43
|
+
export const META_SIGNATURE_FORMAT = {
|
|
44
|
+
algorithm: "sha256",
|
|
45
|
+
encoding: "hex",
|
|
46
|
+
signatures: { header: "x-hub-signature-256", prefix: "sha256=" },
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* The format a declared or manifest signature checks: the named schemes' published formats, or
|
|
50
|
+
* the one an `hmac` signature declares. `undefined` for a scheme this SDK does not know.
|
|
51
|
+
*/
|
|
52
|
+
export function webhookHmacFormat(signature) {
|
|
53
|
+
switch (signature.scheme) {
|
|
54
|
+
case "stripe":
|
|
55
|
+
return STRIPE_SIGNATURE_FORMAT;
|
|
56
|
+
case "meta":
|
|
57
|
+
return META_SIGNATURE_FORMAT;
|
|
58
|
+
case "hmac":
|
|
59
|
+
return signature;
|
|
60
|
+
default:
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const FORMAT_KEYS = ["algorithm", "encoding", "key", "signatures", "timestamp", "message"];
|
|
65
|
+
const PRINTABLE = /^[\x21-\x7e]{1,16}$/;
|
|
66
|
+
const ENTRY_NAME = /^[A-Za-z0-9_.-]{1,16}$/;
|
|
67
|
+
function isRecord(value) {
|
|
68
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
69
|
+
}
|
|
70
|
+
function unknownKeys(value, known) {
|
|
71
|
+
return Object.keys(value).filter((key) => !known.includes(key));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Every problem of a webhook trigger's `signature`, as `check` and SegmentStream's publication
|
|
75
|
+
* check report them: a known scheme and a workspace secret's name, and for `hmac` a complete
|
|
76
|
+
* format. Empty when the declaration is valid.
|
|
77
|
+
*/
|
|
78
|
+
export function webhookSignatureProblems(signature) {
|
|
79
|
+
const problems = [];
|
|
80
|
+
const report = (path, message) => problems.push({ path, message });
|
|
81
|
+
if (!isRecord(signature)) {
|
|
82
|
+
report("", "signature is { scheme, secret }, or omitted for a provider that signs nothing.");
|
|
83
|
+
return problems;
|
|
84
|
+
}
|
|
85
|
+
const { scheme, secret } = signature;
|
|
86
|
+
if (!WEBHOOK_SIGNATURE_SCHEMES.includes(scheme)) {
|
|
87
|
+
report("scheme", `${JSON.stringify(scheme) ?? "undefined"} is not a signature scheme; use one of ` +
|
|
88
|
+
`${WEBHOOK_SIGNATURE_SCHEMES.join(", ")}, or omit signature for a provider that signs ` +
|
|
89
|
+
"nothing.");
|
|
90
|
+
}
|
|
91
|
+
if (typeof secret !== "string" || !WORKSPACE_SECRET_NAME.test(secret)) {
|
|
92
|
+
report("secret", `${JSON.stringify(secret) ?? "undefined"} is not a workspace secret name; name the ` +
|
|
93
|
+
"secret that holds the provider's signing secret, such as stripe-webhook-signing-secret.");
|
|
94
|
+
}
|
|
95
|
+
if (scheme !== "hmac") {
|
|
96
|
+
for (const key of unknownKeys(signature, ["scheme", "secret"])) {
|
|
97
|
+
report(key, FORMAT_KEYS.includes(key)
|
|
98
|
+
? `${key} declares a format, which only scheme "hmac" takes.`
|
|
99
|
+
: `${key} is not a signature setting.`);
|
|
100
|
+
}
|
|
101
|
+
return problems;
|
|
102
|
+
}
|
|
103
|
+
for (const key of unknownKeys(signature, ["scheme", "secret", ...FORMAT_KEYS])) {
|
|
104
|
+
report(key, `${key} is not a signature setting.`);
|
|
105
|
+
}
|
|
106
|
+
if (!WEBHOOK_HMAC_ALGORITHMS.includes(signature.algorithm)) {
|
|
107
|
+
report("algorithm", `algorithm is one of ${WEBHOOK_HMAC_ALGORITHMS.join(", ")}.`);
|
|
108
|
+
}
|
|
109
|
+
if (!WEBHOOK_SIGNATURE_ENCODINGS.includes(signature.encoding)) {
|
|
110
|
+
report("encoding", `encoding is one of ${WEBHOOK_SIGNATURE_ENCODINGS.join(", ")}.`);
|
|
111
|
+
}
|
|
112
|
+
const { key } = signature;
|
|
113
|
+
if (key !== undefined &&
|
|
114
|
+
(!isRecord(key) ||
|
|
115
|
+
key.decode !== "base64" ||
|
|
116
|
+
unknownKeys(key, ["decode", "prefix"]).length > 0 ||
|
|
117
|
+
(key.prefix !== undefined && (typeof key.prefix !== "string" || !PRINTABLE.test(key.prefix))))) {
|
|
118
|
+
report("key", 'key is { decode: "base64" }, with an optional prefix of up to 16 printable characters, ' +
|
|
119
|
+
"such as whsec_.");
|
|
120
|
+
}
|
|
121
|
+
const signatures = signature.signatures;
|
|
122
|
+
let entries = false;
|
|
123
|
+
if (!isRecord(signatures) ||
|
|
124
|
+
unknownKeys(signatures, ["header", "prefix", "entries"]).length > 0) {
|
|
125
|
+
report("signatures", "signatures is { header, prefix } or { header, entries }.");
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
const headerProblem = signedHeaderProblem(signatures.header);
|
|
129
|
+
if (headerProblem !== undefined) {
|
|
130
|
+
report("signatures.header", headerProblem);
|
|
131
|
+
}
|
|
132
|
+
if (signatures.prefix !== undefined &&
|
|
133
|
+
(typeof signatures.prefix !== "string" || !PRINTABLE.test(signatures.prefix))) {
|
|
134
|
+
report("signatures.prefix", "prefix is up to 16 printable characters, such as sha256=.");
|
|
135
|
+
}
|
|
136
|
+
if (signatures.entries !== undefined) {
|
|
137
|
+
const declared = signatures.entries;
|
|
138
|
+
entries = true;
|
|
139
|
+
if (signatures.prefix !== undefined) {
|
|
140
|
+
report("signatures", "signatures takes a prefix or entries, not both.");
|
|
141
|
+
}
|
|
142
|
+
if (!isRecord(declared) ||
|
|
143
|
+
unknownKeys(declared, ["separator", "assign", "name"]).length > 0 ||
|
|
144
|
+
![",", ";", " "].includes(declared.separator) ||
|
|
145
|
+
!["=", ","].includes(declared.assign) ||
|
|
146
|
+
declared.separator === declared.assign ||
|
|
147
|
+
typeof declared.name !== "string" ||
|
|
148
|
+
!ENTRY_NAME.test(declared.name)) {
|
|
149
|
+
report("signatures.entries", 'entries is { separator: ",", ";" or " ", assign: "=" or ",", name }, with a ' +
|
|
150
|
+
'different separator and assign, such as Stripe\'s { separator: ",", assign: "=", ' +
|
|
151
|
+
'name: "v1" }.');
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const timestamp = signature.timestamp;
|
|
156
|
+
const timed = timestamp !== undefined;
|
|
157
|
+
if (timed) {
|
|
158
|
+
if (!isRecord(timestamp) ||
|
|
159
|
+
unknownKeys(timestamp, ["header", "entry", "unit", "tolerance"]).length > 0 ||
|
|
160
|
+
(timestamp.header === undefined) === (timestamp.entry === undefined)) {
|
|
161
|
+
report("timestamp", "timestamp is { header } or { entry }, with an optional unit and tolerance.");
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
if (timestamp.header !== undefined) {
|
|
165
|
+
const headerProblem = signedHeaderProblem(timestamp.header);
|
|
166
|
+
if (headerProblem !== undefined) {
|
|
167
|
+
report("timestamp.header", headerProblem);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (timestamp.entry !== undefined &&
|
|
171
|
+
(typeof timestamp.entry !== "string" || !ENTRY_NAME.test(timestamp.entry) || !entries)) {
|
|
172
|
+
report("timestamp.entry", "entry names an entry of the signature header, which then declares entries.");
|
|
173
|
+
}
|
|
174
|
+
if (timestamp.unit !== undefined &&
|
|
175
|
+
timestamp.unit !== "seconds" &&
|
|
176
|
+
timestamp.unit !== "milliseconds") {
|
|
177
|
+
report("timestamp.unit", 'unit is "seconds" or "milliseconds".');
|
|
178
|
+
}
|
|
179
|
+
if (timestamp.tolerance !== undefined &&
|
|
180
|
+
(typeof timestamp.tolerance !== "number" ||
|
|
181
|
+
!Number.isInteger(timestamp.tolerance) ||
|
|
182
|
+
timestamp.tolerance < 1 ||
|
|
183
|
+
timestamp.tolerance > MAX_WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS)) {
|
|
184
|
+
report("timestamp.tolerance", `tolerance is a whole number of seconds from 1 to ${MAX_WEBHOOK_TIMESTAMP_TOLERANCE_SECONDS}.`);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const message = signature.message;
|
|
189
|
+
if (message !== undefined) {
|
|
190
|
+
if (!Array.isArray(message) ||
|
|
191
|
+
message.length === 0 ||
|
|
192
|
+
message.length > MAX_WEBHOOK_MESSAGE_PARTS) {
|
|
193
|
+
report("message", `message is a list of 1 to ${MAX_WEBHOOK_MESSAGE_PARTS} parts.`);
|
|
194
|
+
}
|
|
195
|
+
else {
|
|
196
|
+
let bodies = 0;
|
|
197
|
+
for (const [index, part] of message.entries()) {
|
|
198
|
+
const at = `message.${index}`;
|
|
199
|
+
if (typeof part === "string") {
|
|
200
|
+
if (part.length === 0 || part.length > MAX_WEBHOOK_MESSAGE_TEXT) {
|
|
201
|
+
report(at, `A text part has 1 to ${MAX_WEBHOOK_MESSAGE_TEXT} characters.`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else if (isRecord(part) && Object.keys(part).length === 1 && part.body === true) {
|
|
205
|
+
bodies += 1;
|
|
206
|
+
}
|
|
207
|
+
else if (isRecord(part) && Object.keys(part).length === 1 && part.timestamp === true) {
|
|
208
|
+
if (!timed) {
|
|
209
|
+
report(at, "{ timestamp: true } needs the signature's timestamp declared.");
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
else if (isRecord(part) && Object.keys(part).length === 1 && "header" in part) {
|
|
213
|
+
const headerProblem = signedHeaderProblem(part.header);
|
|
214
|
+
if (headerProblem !== undefined) {
|
|
215
|
+
report(at, headerProblem);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
report(at, "A part is text, { body: true }, { header } or { timestamp: true }.");
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
if (bodies !== 1) {
|
|
223
|
+
report("message", "message includes { body: true } exactly once.");
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
if (timed && isRecord(timestamp)) {
|
|
228
|
+
// A timestamp the provider does not sign proves nothing: a replayed delivery would carry a
|
|
229
|
+
// fresh one and pass.
|
|
230
|
+
const signedParts = Array.isArray(message) ? message : [];
|
|
231
|
+
const covered = signedParts.some((part) => isRecord(part) &&
|
|
232
|
+
(part.timestamp === true ||
|
|
233
|
+
(timestamp.header !== undefined && part.header === timestamp.header)));
|
|
234
|
+
if (!covered) {
|
|
235
|
+
report("timestamp", "The signed message must include the timestamp, as { timestamp: true } or its header; " +
|
|
236
|
+
"otherwise a replayed delivery passes with a fresh one.");
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
return problems;
|
|
240
|
+
}
|
|
241
|
+
function signedHeaderProblem(name) {
|
|
242
|
+
return typeof name === "string" && HEADER_NAME.test(name)
|
|
243
|
+
? undefined
|
|
244
|
+
: `${JSON.stringify(name) ?? "undefined"} is not a lowercase header name.`;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* A valid signature declaration as the manifest records it: `{ scheme, secret }` for a named
|
|
248
|
+
* scheme, and for `hmac` its format's settings, each written only when declared.
|
|
249
|
+
*/
|
|
250
|
+
export function webhookSignatureManifest(signature) {
|
|
251
|
+
if (signature.scheme !== "hmac") {
|
|
252
|
+
return { scheme: signature.scheme, secret: signature.secret };
|
|
253
|
+
}
|
|
254
|
+
const { signatures, timestamp, key, message } = signature;
|
|
255
|
+
return {
|
|
256
|
+
scheme: "hmac",
|
|
257
|
+
secret: signature.secret,
|
|
258
|
+
algorithm: signature.algorithm,
|
|
259
|
+
encoding: signature.encoding,
|
|
260
|
+
...(key === undefined
|
|
261
|
+
? {}
|
|
262
|
+
: {
|
|
263
|
+
key: { decode: key.decode, ...(key.prefix === undefined ? {} : { prefix: key.prefix }) },
|
|
264
|
+
}),
|
|
265
|
+
signatures: {
|
|
266
|
+
header: signatures.header,
|
|
267
|
+
...(signatures.prefix === undefined ? {} : { prefix: signatures.prefix }),
|
|
268
|
+
...(signatures.entries === undefined
|
|
269
|
+
? {}
|
|
270
|
+
: {
|
|
271
|
+
entries: {
|
|
272
|
+
separator: signatures.entries.separator,
|
|
273
|
+
assign: signatures.entries.assign,
|
|
274
|
+
name: signatures.entries.name,
|
|
275
|
+
},
|
|
276
|
+
}),
|
|
277
|
+
},
|
|
278
|
+
...(timestamp === undefined
|
|
279
|
+
? {}
|
|
280
|
+
: {
|
|
281
|
+
timestamp: {
|
|
282
|
+
...(timestamp.header === undefined ? {} : { header: timestamp.header }),
|
|
283
|
+
...(timestamp.entry === undefined ? {} : { entry: timestamp.entry }),
|
|
284
|
+
...(timestamp.unit === undefined ? {} : { unit: timestamp.unit }),
|
|
285
|
+
...(timestamp.tolerance === undefined ? {} : { tolerance: timestamp.tolerance }),
|
|
286
|
+
},
|
|
287
|
+
}),
|
|
288
|
+
...(message === undefined
|
|
289
|
+
? {}
|
|
290
|
+
: {
|
|
291
|
+
message: message.map((part) => typeof part === "string"
|
|
292
|
+
? part
|
|
293
|
+
: "body" in part
|
|
294
|
+
? { body: true }
|
|
295
|
+
: "timestamp" in part
|
|
296
|
+
? { timestamp: true }
|
|
297
|
+
: { header: part.header }),
|
|
298
|
+
}),
|
|
299
|
+
};
|
|
300
|
+
}
|
|
10
301
|
/** The most webhook triggers one ontology may declare across its workflows. */
|
|
11
302
|
export const MAX_WEBHOOKS = 20;
|
|
12
303
|
/** The most request headers one webhook trigger may pass to its workflow. */
|