@yaks/hook 0.0.0-stage → 0.2.36
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/LICENSE +202 -0
- package/README.md +126 -2
- package/_dist/hooked.d.ts +28 -0
- package/_dist/hooked.d.ts.map +1 -0
- package/_dist/mod.d.ts +14 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/signed.d.ts +51 -0
- package/_dist/signed.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/deno.json +1 -0
- package/hooked.js +94 -0
- package/hooked.js.map +1 -0
- package/hooked.ts +105 -0
- package/mod.js +14 -0
- package/mod.js.map +1 -0
- package/mod.ts +24 -0
- package/package.json +23 -4
- package/signed.js +214 -0
- package/signed.js.map +1 -0
- package/signed.ts +320 -0
- package/vocab.js +16 -0
- package/vocab.js.map +1 -0
- package/vocab.json +73 -0
- package/vocab.ts +17 -0
package/mod.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An event another system delivered. The payload is kept as it arrived —
|
|
3
|
+
* body, method, path, headers — beside the result of checking its signature,
|
|
4
|
+
* because an unsigned hook is still something that happened, and whoever reads
|
|
5
|
+
* it decides what to trust.
|
|
6
|
+
*
|
|
7
|
+
* The components; `hooked()`: a captured request in, the bundles that record
|
|
8
|
+
* it out; and `checked()`/`refusal()`/`ours()`: whether a request is signed
|
|
9
|
+
* under a scheme and secret the receiver names, and whether a refusal can mean
|
|
10
|
+
* the receiver's secret is wrong.
|
|
11
|
+
*/ export { hookDoc } from "./vocab.js";
|
|
12
|
+
export { event, hooked, hookEid } from "./hooked.js";
|
|
13
|
+
export { checked, ours, refusal, SKEW } from "./signed.js";
|
|
14
|
+
//# sourceMappingURL=mod.js.map
|
package/mod.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mod.js","sources":["./mod.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;CAUC,GAED,SAAS,OAAO,qBAAoB;AACpC,SAAS,KAAK,EAAE,MAAM,EAAE,OAAO,sBAAmC;AAClE,SACE,OAAO,EAGP,IAAI,EACJ,OAAO,EAGP,IAAI,sBACc"}
|
package/mod.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An event another system delivered. The payload is kept as it arrived —
|
|
3
|
+
* body, method, path, headers — beside the result of checking its signature,
|
|
4
|
+
* because an unsigned hook is still something that happened, and whoever reads
|
|
5
|
+
* it decides what to trust.
|
|
6
|
+
*
|
|
7
|
+
* The components; `hooked()`: a captured request in, the bundles that record
|
|
8
|
+
* it out; and `checked()`/`refusal()`/`ours()`: whether a request is signed
|
|
9
|
+
* under a scheme and secret the receiver names, and whether a refusal can mean
|
|
10
|
+
* the receiver's secret is wrong.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
export { hookDoc } from './vocab.js'
|
|
14
|
+
export { event, hooked, hookEid, type Request } from './hooked.js'
|
|
15
|
+
export {
|
|
16
|
+
checked,
|
|
17
|
+
type HeaderLookup,
|
|
18
|
+
type Hmac,
|
|
19
|
+
ours,
|
|
20
|
+
refusal,
|
|
21
|
+
type Scheme,
|
|
22
|
+
type SecretHeader,
|
|
23
|
+
SKEW,
|
|
24
|
+
} from './signed.js'
|
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yaks/hook",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.36",
|
|
4
|
+
"homepage": "https://jsr.io/@yaks/hook",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./_dist/mod.d.ts",
|
|
9
|
+
"default": "./mod.js"
|
|
10
|
+
},
|
|
11
|
+
"./vocab": {
|
|
12
|
+
"types": "./_dist/vocab.d.ts",
|
|
13
|
+
"default": "./vocab.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"license": "Apache-2.0",
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"@yaks/doc": "0.2.36",
|
|
19
|
+
"@yaks/edge": "0.2.36",
|
|
20
|
+
"@yaks/graph": "0.2.36",
|
|
21
|
+
"@yaks/vocab": "0.2.36"
|
|
22
|
+
},
|
|
23
|
+
"description": "An event another system delivered, kept as it arrived: the payload, the route it came in on, and whether it was signed.",
|
|
24
|
+
"repository": "https://github.com/yak-sh/yak.git"
|
|
25
|
+
}
|
package/signed.js
ADDED
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// Whether a delivered request is signed by whoever holds the secret.
|
|
2
|
+
//
|
|
3
|
+
// A scheme is data, so the receiver can keep it beside the secret it names:
|
|
4
|
+
// `'stripe'`, `'github'`, `'telnyx'`, `'standard'` (Standard Webhooks, as
|
|
5
|
+
// OpenAI signs), an HMAC-SHA256 of the raw body in a
|
|
6
|
+
// header of the sender's choosing, or an Ed25519 signature over a timestamp
|
|
7
|
+
// and the body, checked under the sender's public key. The answer is a sentence: '' when the signature holds,
|
|
8
|
+
// else why it does not, because a refusal someone reads in a log should say
|
|
9
|
+
// what was wrong rather than only that something was.
|
|
10
|
+
//
|
|
11
|
+
// The body is the RAW body, the exact string that arrived: a signature covers
|
|
12
|
+
// bytes, and parsing then re-serializing would verify something the sender
|
|
13
|
+
// never signed. Every compare runs through WebCrypto's `verify`, so it is
|
|
14
|
+
// constant-time without a compare of our own.
|
|
15
|
+
//
|
|
16
|
+
// What secret to use, and what a refusal means for the request, are the
|
|
17
|
+
// receiver's to say. This module never learns where a secret came from, but it
|
|
18
|
+
// does say which refusal can be the receiver's own fault (`ours`): a sender
|
|
19
|
+
// holding the secret always sends a complete, fresh, well-formed signature, so
|
|
20
|
+
// only one of those that fails to match can mean the receiver's secret is
|
|
21
|
+
// wrong. Anything short of that (no header, no timestamp, a stale one, a digest
|
|
22
|
+
// that is not a SHA-256) is the caller's own bad input, which anyone on the
|
|
23
|
+
// internet can send, and is refused without anybody being told.
|
|
24
|
+
import { header } from "./hooked.js";
|
|
25
|
+
// How far a Stripe signature's timestamp may be from now, in seconds. Stripe's
|
|
26
|
+
// own libraries default to five minutes, and the point of the timestamp is
|
|
27
|
+
// that a body captured off the wire cannot be replayed later under its own
|
|
28
|
+
// signature.
|
|
29
|
+
export let SKEW = 300;
|
|
30
|
+
// The one refusal a receiver holding the wrong secret would see.
|
|
31
|
+
let MISMATCH = 'the signature does not match';
|
|
32
|
+
let MALFORMED = 'the signature is not well formed';
|
|
33
|
+
let GITHUB = {
|
|
34
|
+
header: 'X-Hub-Signature-256',
|
|
35
|
+
prefix: 'sha256='
|
|
36
|
+
};
|
|
37
|
+
// https://developers.telnyx.com/docs/messaging/messages/receiving-webhooks
|
|
38
|
+
let TELNYX = {
|
|
39
|
+
signature: 'telnyx-signature-ed25519',
|
|
40
|
+
timestamp: 'telnyx-timestamp',
|
|
41
|
+
join: '|'
|
|
42
|
+
};
|
|
43
|
+
let enc = new TextEncoder();
|
|
44
|
+
let hex = (s)=>{
|
|
45
|
+
if (!/^[0-9a-f]+$/i.test(s) || s.length % 2) return null;
|
|
46
|
+
let out = new Uint8Array(s.length / 2);
|
|
47
|
+
for(let i = 0; i < out.length; i++){
|
|
48
|
+
out[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16);
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
};
|
|
52
|
+
let base64 = (s)=>{
|
|
53
|
+
try {
|
|
54
|
+
return Uint8Array.from(atob(s), (c)=>c.charCodeAt(0));
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
let key = (secret)=>crypto.subtle.importKey('raw', typeof secret == 'string' ? enc.encode(secret) : secret, {
|
|
60
|
+
name: 'HMAC',
|
|
61
|
+
hash: 'SHA-256'
|
|
62
|
+
}, false, [
|
|
63
|
+
'verify'
|
|
64
|
+
]);
|
|
65
|
+
// Why none of these digests is the HMAC of the payload, or '' when one is. A
|
|
66
|
+
// digest that is not 32 bytes cannot be a SHA-256 and is simply skipped: it
|
|
67
|
+
// must never throw its way past the check, and when none is well formed there
|
|
68
|
+
// is nothing to take the secret out for.
|
|
69
|
+
let matches = async (secret, payload, digests)=>{
|
|
70
|
+
let sent = digests.filter((d)=>d?.length == 32);
|
|
71
|
+
if (!sent.length) return MALFORMED;
|
|
72
|
+
let k = await key(secret);
|
|
73
|
+
let bytes = enc.encode(payload);
|
|
74
|
+
for (let d of sent){
|
|
75
|
+
if (await crypto.subtle.verify('HMAC', k, d, bytes)) return '';
|
|
76
|
+
}
|
|
77
|
+
return MISMATCH;
|
|
78
|
+
};
|
|
79
|
+
// The Stripe header as its pairs, in order. A Map would not do: a header
|
|
80
|
+
// carries several `v1=` signatures while a secret is being rolled, and every
|
|
81
|
+
// one of them has to be tried.
|
|
82
|
+
let pairs = (line)=>line.split(',').map((p)=>{
|
|
83
|
+
let at = p.indexOf('=');
|
|
84
|
+
return at < 0 ? [
|
|
85
|
+
'',
|
|
86
|
+
p.trim()
|
|
87
|
+
] : [
|
|
88
|
+
p.slice(0, at).trim(),
|
|
89
|
+
p.slice(at + 1).trim()
|
|
90
|
+
];
|
|
91
|
+
});
|
|
92
|
+
// A provider that sends a bearer secret in a header (Pinnacle). Compare
|
|
93
|
+
// through WebCrypto, just as digests are compared, before the body is decoded.
|
|
94
|
+
let bearer = async (scheme, secret, headers)=>{
|
|
95
|
+
let sent = headers.get(scheme.header);
|
|
96
|
+
if (!sent) return `no ${scheme.header} header`;
|
|
97
|
+
let k = await crypto.subtle.importKey('raw', enc.encode(secret), {
|
|
98
|
+
name: 'HMAC',
|
|
99
|
+
hash: 'SHA-256'
|
|
100
|
+
}, false, [
|
|
101
|
+
'sign',
|
|
102
|
+
'verify'
|
|
103
|
+
]);
|
|
104
|
+
let digest = await crypto.subtle.sign('HMAC', k, enc.encode(secret));
|
|
105
|
+
return await crypto.subtle.verify('HMAC', k, digest, enc.encode(sent)) ? '' : MISMATCH;
|
|
106
|
+
};
|
|
107
|
+
// Stripe: `t=<unix>,v1=<hex>` over `<t>.<raw body>`.
|
|
108
|
+
let stripe = async (secret, body, headers, now)=>{
|
|
109
|
+
let line = headers.get('Stripe-Signature');
|
|
110
|
+
if (!line) return 'no Stripe-Signature header';
|
|
111
|
+
let said = pairs(line);
|
|
112
|
+
let t = Number(said.find(([k])=>k == 't')?.[1]);
|
|
113
|
+
if (!Number.isFinite(t)) return 'no timestamp on the signature';
|
|
114
|
+
if (Math.abs(now / 1000 - t) > SKEW) return 'the signature is too old';
|
|
115
|
+
let digests = said.filter(([k])=>k == 'v1').map(([, v])=>hex(v));
|
|
116
|
+
return await matches(secret, `${t}.${body}`, digests);
|
|
117
|
+
};
|
|
118
|
+
// Standard Webhooks (https://www.standardwebhooks.com): `v1,<base64>` in
|
|
119
|
+
// `webhook-signature`, several space apart while a secret is rolled, each an
|
|
120
|
+
// HMAC of `<webhook-id>.<webhook-timestamp>.<raw body>` under the secret's
|
|
121
|
+
// bytes, the base64 after its `whsec_`.
|
|
122
|
+
let standard = async (secret, body, headers, now)=>{
|
|
123
|
+
let line = headers.get('webhook-signature');
|
|
124
|
+
if (!line) return 'no webhook-signature header';
|
|
125
|
+
let id = headers.get('webhook-id') ?? '';
|
|
126
|
+
let at = headers.get('webhook-timestamp') ?? '';
|
|
127
|
+
if (!/^\d+$/.test(at)) return 'no timestamp on the signature';
|
|
128
|
+
if (Math.abs(now / 1000 - Number(at)) > SKEW) {
|
|
129
|
+
return 'the signature is too old';
|
|
130
|
+
}
|
|
131
|
+
let raw = base64(secret.replace(/^whsec_/, ''));
|
|
132
|
+
if (!raw?.length) return MISMATCH;
|
|
133
|
+
let digests = line.split(' ').flatMap((s)=>s.startsWith('v1,') ? [
|
|
134
|
+
base64(s.slice(3))
|
|
135
|
+
] : []);
|
|
136
|
+
return await matches(raw, `${id}.${at}.${body}`, digests);
|
|
137
|
+
};
|
|
138
|
+
let hmac = async (scheme, secret, body, headers, now)=>{
|
|
139
|
+
let line = headers.get(scheme.header);
|
|
140
|
+
if (!line) return `no ${scheme.header} header`;
|
|
141
|
+
let prefix = scheme.prefix ?? '';
|
|
142
|
+
if (!line.startsWith(prefix)) return MALFORMED;
|
|
143
|
+
let digest = line.slice(prefix.length).trim();
|
|
144
|
+
let decoded = scheme.encoding == 'base64' ? base64(digest) : hex(digest);
|
|
145
|
+
let stamp = scheme.timestamp;
|
|
146
|
+
if (!stamp) return matches(secret, body, [
|
|
147
|
+
decoded
|
|
148
|
+
]);
|
|
149
|
+
let at = headers.get(stamp.header) ?? '';
|
|
150
|
+
if (!/^\d+$/.test(at)) return 'no timestamp on the signature';
|
|
151
|
+
if (Math.abs(now / 1000 - Number(at)) > SKEW) {
|
|
152
|
+
return 'the signature is too old';
|
|
153
|
+
}
|
|
154
|
+
let signed = [
|
|
155
|
+
...stamp.version ? [
|
|
156
|
+
stamp.version
|
|
157
|
+
] : [],
|
|
158
|
+
at,
|
|
159
|
+
body
|
|
160
|
+
].join(stamp.separator);
|
|
161
|
+
return await matches(secret, signed, [
|
|
162
|
+
decoded
|
|
163
|
+
]);
|
|
164
|
+
};
|
|
165
|
+
// Ed25519: a 64-byte signature over `<t><join><raw body>`, `t` within SKEW of
|
|
166
|
+
// now, under a 32-byte public key. A key that is not one is the receiver's own
|
|
167
|
+
// mistake, so it reads as a mismatch.
|
|
168
|
+
let ed25519 = async (scheme, publicKey, body, headers, now)=>{
|
|
169
|
+
let line = headers.get(scheme.signature);
|
|
170
|
+
if (!line) return `no ${scheme.signature} header`;
|
|
171
|
+
let at = headers.get(scheme.timestamp) ?? '';
|
|
172
|
+
if (!/^\d+$/.test(at)) return 'no timestamp on the signature';
|
|
173
|
+
if (Math.abs(now / 1000 - Number(at)) > SKEW) {
|
|
174
|
+
return 'the signature is too old';
|
|
175
|
+
}
|
|
176
|
+
let sent = base64(line.trim());
|
|
177
|
+
if (sent?.length != 64) return MALFORMED;
|
|
178
|
+
let raw = base64(publicKey);
|
|
179
|
+
if (raw?.length != 32) return MISMATCH;
|
|
180
|
+
let k = await crypto.subtle.importKey('raw', raw, 'Ed25519', false, [
|
|
181
|
+
'verify'
|
|
182
|
+
]);
|
|
183
|
+
let ok = await crypto.subtle.verify('Ed25519', k, sent, enc.encode(`${at}${scheme.join}${body}`));
|
|
184
|
+
return ok ? '' : MISMATCH;
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Why a request is not signed under this scheme and secret, or '' when it is.
|
|
188
|
+
*
|
|
189
|
+
* ```ts
|
|
190
|
+
* import { refusal } from '@yaks/hook'
|
|
191
|
+
* await refusal('github', 'a secret', '{}', new Headers())
|
|
192
|
+
* // 'no X-Hub-Signature-256 header'
|
|
193
|
+
* ```
|
|
194
|
+
*/ export let refusal = (scheme, secret, body, headers, now = Date.now())=>scheme == 'stripe' ? stripe(secret, body, headers, now) : scheme == 'telnyx' ? ed25519(TELNYX, secret, body, headers, now) : scheme == 'standard' ? standard(secret, body, headers, now) : typeof scheme == 'object' && 'token' in scheme ? bearer(scheme, secret, headers) : scheme != 'github' && 'signature' in scheme ? ed25519(scheme, secret, body, headers, now) : hmac(scheme == 'github' ? GITHUB : scheme, secret, body, headers, now);
|
|
195
|
+
/**
|
|
196
|
+
* Whether a {@link refusal} can mean the receiver's own secret is wrong, and so
|
|
197
|
+
* is worth reporting: only a complete, fresh, well-formed signature that does
|
|
198
|
+
* not match. Every other refusal is the caller's own bad input.
|
|
199
|
+
*
|
|
200
|
+
* ```ts
|
|
201
|
+
* import { ours, refusal } from '@yaks/hook'
|
|
202
|
+
* ours(await refusal('stripe', 'a secret', '{}', new Headers())) // false
|
|
203
|
+
* ```
|
|
204
|
+
*/ export let ours = (why)=>why == MISMATCH;
|
|
205
|
+
/**
|
|
206
|
+
* A captured request with its `verified` set by checking it under this scheme
|
|
207
|
+
* and secret, ready for {@link hooked}.
|
|
208
|
+
*/ export let checked = async (r, scheme, secret, now = Date.now())=>({
|
|
209
|
+
...r,
|
|
210
|
+
verified: await refusal(scheme, secret, r.body ?? '', {
|
|
211
|
+
get: (name)=>header(r, name) ?? null
|
|
212
|
+
}, now) == ''
|
|
213
|
+
});
|
|
214
|
+
//# sourceMappingURL=signed.js.map
|
package/signed.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signed.js","sources":["./signed.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,EAAE;AACF,4EAA4E;AAC5E,0EAA0E;AAC1E,qDAAqD;AACrD,4EAA4E;AAC5E,8GAA8G;AAC9G,4EAA4E;AAC5E,sDAAsD;AACtD,EAAE;AACF,8EAA8E;AAC9E,2EAA2E;AAC3E,0EAA0E;AAC1E,8CAA8C;AAC9C,EAAE;AACF,wEAAwE;AACxE,+EAA+E;AAC/E,4EAA4E;AAC5E,+EAA+E;AAC/E,0EAA0E;AAC1E,gFAAgF;AAChF,4EAA4E;AAC5E,gEAAgE;AAEhE,SAAS,MAAM,sBAAmC;AA0ClD,+EAA+E;AAC/E,2EAA2E;AAC3E,2EAA2E;AAC3E,aAAa;AACb,OAAO,IAAI,OAAO,IAAG;AAErB,iEAAiE;AACjE,IAAI,WAAW;AACf,IAAI,YAAY;AAEhB,IAAI,SAAe;EAAE,QAAQ;EAAuB,QAAQ;AAAU;AACtE,2EAA2E;AAC3E,IAAI,SAAkB;EACpB,WAAW;EACX,WAAW;EACX,MAAM;AACR;AAEA,IAAI,MAAM,IAAI;AAEd,IAAI,MAAM,CAAC;EACT,IAAI,CAAC,eAAe,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,GAAG,OAAO;EACpD,IAAI,MAAM,IAAI,WAAW,EAAE,MAAM,GAAG;EACpC,IAAK,IAAI,IAAI,GAAG,IAAI,IAAI,MAAM,EAAE,IAAK;IACnC,GAAG,CAAC,EAAE,GAAG,SAAS,EAAE,KAAK,CAAC,IAAI,GAAG,IAAI,IAAI,IAAI;EAC/C;E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,IAAI,UAAU,OACnB,GACA,QACA,QACA,MAAc,KAAK,GAAG,EAAE,GACH,CAAC;IACtB,GAAG,CAAC;IACJ,UAAU,MAAM,QAAQ,QAAQ,QAAQ,EAAE,IAAI,IAAI,IAAI;MACpD,KAAK,CAAC,OAAS,OAAO,GAAG,SAAS;IACpC,GAAG,QAAQ;EACb,CAAC,EAAC"}
|
package/signed.ts
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
// Whether a delivered request is signed by whoever holds the secret.
|
|
2
|
+
//
|
|
3
|
+
// A scheme is data, so the receiver can keep it beside the secret it names:
|
|
4
|
+
// `'stripe'`, `'github'`, `'telnyx'`, `'standard'` (Standard Webhooks, as
|
|
5
|
+
// OpenAI signs), an HMAC-SHA256 of the raw body in a
|
|
6
|
+
// header of the sender's choosing, or an Ed25519 signature over a timestamp
|
|
7
|
+
// and the body, checked under the sender's public key. The answer is a sentence: '' when the signature holds,
|
|
8
|
+
// else why it does not, because a refusal someone reads in a log should say
|
|
9
|
+
// what was wrong rather than only that something was.
|
|
10
|
+
//
|
|
11
|
+
// The body is the RAW body, the exact string that arrived: a signature covers
|
|
12
|
+
// bytes, and parsing then re-serializing would verify something the sender
|
|
13
|
+
// never signed. Every compare runs through WebCrypto's `verify`, so it is
|
|
14
|
+
// constant-time without a compare of our own.
|
|
15
|
+
//
|
|
16
|
+
// What secret to use, and what a refusal means for the request, are the
|
|
17
|
+
// receiver's to say. This module never learns where a secret came from, but it
|
|
18
|
+
// does say which refusal can be the receiver's own fault (`ours`): a sender
|
|
19
|
+
// holding the secret always sends a complete, fresh, well-formed signature, so
|
|
20
|
+
// only one of those that fails to match can mean the receiver's secret is
|
|
21
|
+
// wrong. Anything short of that (no header, no timestamp, a stale one, a digest
|
|
22
|
+
// that is not a SHA-256) is the caller's own bad input, which anyone on the
|
|
23
|
+
// internet can send, and is refused without anybody being told.
|
|
24
|
+
|
|
25
|
+
import { header, type Request } from './hooked.js'
|
|
26
|
+
|
|
27
|
+
/** An HMAC-SHA256 of the raw body, in the named header. */
|
|
28
|
+
export type Hmac = {
|
|
29
|
+
/** the header carrying the signature */
|
|
30
|
+
header: string
|
|
31
|
+
/** what precedes the digest in the header, such as `sha256=` */
|
|
32
|
+
prefix?: string
|
|
33
|
+
/** how the digest is written: hex (the default) or base64 */
|
|
34
|
+
encoding?: 'hex' | 'base64'
|
|
35
|
+
/** A timestamp signed as `[version + separator] + time + separator + body`. */
|
|
36
|
+
timestamp?: { header: string; separator: string; version?: string }
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** An Ed25519 signature over `<timestamp><join><raw body>`, base64 in one
|
|
40
|
+
* header, the timestamp (unix seconds) in another. The secret it is checked
|
|
41
|
+
* under is the sender's public key, base64. */
|
|
42
|
+
export type Ed25519 = {
|
|
43
|
+
/** the header carrying the signature */
|
|
44
|
+
signature: string
|
|
45
|
+
/** the header carrying the unix time it was signed */
|
|
46
|
+
timestamp: string
|
|
47
|
+
/** what stands between the timestamp and the body in what was signed */
|
|
48
|
+
join: string
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** A provider that authenticates with a secret header rather than a signature. */
|
|
52
|
+
export type SecretHeader = { header: string; token: true }
|
|
53
|
+
|
|
54
|
+
/** Signature verification or bearer-secret authentication, specified as data. */
|
|
55
|
+
export type Scheme =
|
|
56
|
+
| 'stripe'
|
|
57
|
+
| 'github'
|
|
58
|
+
| 'telnyx'
|
|
59
|
+
| 'standard'
|
|
60
|
+
| Hmac
|
|
61
|
+
| Ed25519
|
|
62
|
+
| SecretHeader
|
|
63
|
+
|
|
64
|
+
/** Where a signature's header is read from: a fetch `Headers` is one. */
|
|
65
|
+
export type HeaderLookup = { get(name: string): string | null }
|
|
66
|
+
|
|
67
|
+
// How far a Stripe signature's timestamp may be from now, in seconds. Stripe's
|
|
68
|
+
// own libraries default to five minutes, and the point of the timestamp is
|
|
69
|
+
// that a body captured off the wire cannot be replayed later under its own
|
|
70
|
+
// signature.
|
|
71
|
+
export let SKEW = 300
|
|
72
|
+
|
|
73
|
+
// The one refusal a receiver holding the wrong secret would see.
|
|
74
|
+
let MISMATCH = 'the signature does not match'
|
|
75
|
+
let MALFORMED = 'the signature is not well formed'
|
|
76
|
+
|
|
77
|
+
let GITHUB: Hmac = { header: 'X-Hub-Signature-256', prefix: 'sha256=' }
|
|
78
|
+
// https://developers.telnyx.com/docs/messaging/messages/receiving-webhooks
|
|
79
|
+
let TELNYX: Ed25519 = {
|
|
80
|
+
signature: 'telnyx-signature-ed25519',
|
|
81
|
+
timestamp: 'telnyx-timestamp',
|
|
82
|
+
join: '|',
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
let enc = new TextEncoder()
|
|
86
|
+
|
|
87
|
+
let hex = (s: string) => {
|
|
88
|
+
if (!/^[0-9a-f]+$/i.test(s) || s.length % 2) return null
|
|
89
|
+
let out = new Uint8Array(s.length / 2)
|
|
90
|
+
for (let i = 0; i < out.length; i++) {
|
|
91
|
+
out[i] = parseInt(s.slice(i * 2, i * 2 + 2), 16)
|
|
92
|
+
}
|
|
93
|
+
return out
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
let base64 = (s: string) => {
|
|
97
|
+
try {
|
|
98
|
+
return Uint8Array.from(atob(s), (c) => c.charCodeAt(0))
|
|
99
|
+
} catch {
|
|
100
|
+
return null
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
let key = (secret: string | Uint8Array<ArrayBuffer>) =>
|
|
105
|
+
crypto.subtle.importKey(
|
|
106
|
+
'raw',
|
|
107
|
+
typeof secret == 'string' ? enc.encode(secret) : secret,
|
|
108
|
+
{ name: 'HMAC', hash: 'SHA-256' },
|
|
109
|
+
false,
|
|
110
|
+
['verify'],
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
// Why none of these digests is the HMAC of the payload, or '' when one is. A
|
|
114
|
+
// digest that is not 32 bytes cannot be a SHA-256 and is simply skipped: it
|
|
115
|
+
// must never throw its way past the check, and when none is well formed there
|
|
116
|
+
// is nothing to take the secret out for.
|
|
117
|
+
let matches = async (
|
|
118
|
+
secret: string | Uint8Array<ArrayBuffer>,
|
|
119
|
+
payload: string,
|
|
120
|
+
digests: (Uint8Array<ArrayBuffer> | null)[],
|
|
121
|
+
) => {
|
|
122
|
+
let sent = digests.filter((d) => d?.length == 32) as Uint8Array<ArrayBuffer>[]
|
|
123
|
+
if (!sent.length) return MALFORMED
|
|
124
|
+
let k = await key(secret)
|
|
125
|
+
let bytes = enc.encode(payload)
|
|
126
|
+
for (let d of sent) {
|
|
127
|
+
if (await crypto.subtle.verify('HMAC', k, d, bytes)) return ''
|
|
128
|
+
}
|
|
129
|
+
return MISMATCH
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// The Stripe header as its pairs, in order. A Map would not do: a header
|
|
133
|
+
// carries several `v1=` signatures while a secret is being rolled, and every
|
|
134
|
+
// one of them has to be tried.
|
|
135
|
+
let pairs = (line: string) =>
|
|
136
|
+
line.split(',').map((p) => {
|
|
137
|
+
let at = p.indexOf('=')
|
|
138
|
+
return at < 0
|
|
139
|
+
? ['', p.trim()]
|
|
140
|
+
: [p.slice(0, at).trim(), p.slice(at + 1).trim()]
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
// A provider that sends a bearer secret in a header (Pinnacle). Compare
|
|
144
|
+
// through WebCrypto, just as digests are compared, before the body is decoded.
|
|
145
|
+
let bearer = async (
|
|
146
|
+
scheme: SecretHeader,
|
|
147
|
+
secret: string,
|
|
148
|
+
headers: HeaderLookup,
|
|
149
|
+
) => {
|
|
150
|
+
let sent = headers.get(scheme.header)
|
|
151
|
+
if (!sent) return `no ${scheme.header} header`
|
|
152
|
+
let k = await crypto.subtle.importKey(
|
|
153
|
+
'raw',
|
|
154
|
+
enc.encode(secret),
|
|
155
|
+
{ name: 'HMAC', hash: 'SHA-256' },
|
|
156
|
+
false,
|
|
157
|
+
['sign', 'verify'],
|
|
158
|
+
)
|
|
159
|
+
let digest = await crypto.subtle.sign('HMAC', k, enc.encode(secret))
|
|
160
|
+
return await crypto.subtle.verify('HMAC', k, digest, enc.encode(sent))
|
|
161
|
+
? ''
|
|
162
|
+
: MISMATCH
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Stripe: `t=<unix>,v1=<hex>` over `<t>.<raw body>`.
|
|
166
|
+
let stripe = async (
|
|
167
|
+
secret: string,
|
|
168
|
+
body: string,
|
|
169
|
+
headers: HeaderLookup,
|
|
170
|
+
now: number,
|
|
171
|
+
) => {
|
|
172
|
+
let line = headers.get('Stripe-Signature')
|
|
173
|
+
if (!line) return 'no Stripe-Signature header'
|
|
174
|
+
let said = pairs(line)
|
|
175
|
+
let t = Number(said.find(([k]) => k == 't')?.[1])
|
|
176
|
+
if (!Number.isFinite(t)) return 'no timestamp on the signature'
|
|
177
|
+
if (Math.abs(now / 1000 - t) > SKEW) return 'the signature is too old'
|
|
178
|
+
let digests = said.filter(([k]) => k == 'v1').map(([, v]) => hex(v))
|
|
179
|
+
return await matches(secret, `${t}.${body}`, digests)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// Standard Webhooks (https://www.standardwebhooks.com): `v1,<base64>` in
|
|
183
|
+
// `webhook-signature`, several space apart while a secret is rolled, each an
|
|
184
|
+
// HMAC of `<webhook-id>.<webhook-timestamp>.<raw body>` under the secret's
|
|
185
|
+
// bytes, the base64 after its `whsec_`.
|
|
186
|
+
let standard = async (
|
|
187
|
+
secret: string,
|
|
188
|
+
body: string,
|
|
189
|
+
headers: HeaderLookup,
|
|
190
|
+
now: number,
|
|
191
|
+
) => {
|
|
192
|
+
let line = headers.get('webhook-signature')
|
|
193
|
+
if (!line) return 'no webhook-signature header'
|
|
194
|
+
let id = headers.get('webhook-id') ?? ''
|
|
195
|
+
let at = headers.get('webhook-timestamp') ?? ''
|
|
196
|
+
if (!/^\d+$/.test(at)) return 'no timestamp on the signature'
|
|
197
|
+
if (Math.abs(now / 1000 - Number(at)) > SKEW) {
|
|
198
|
+
return 'the signature is too old'
|
|
199
|
+
}
|
|
200
|
+
let raw = base64(secret.replace(/^whsec_/, ''))
|
|
201
|
+
if (!raw?.length) return MISMATCH
|
|
202
|
+
let digests = line.split(' ').flatMap((s) =>
|
|
203
|
+
s.startsWith('v1,') ? [base64(s.slice(3))] : []
|
|
204
|
+
)
|
|
205
|
+
return await matches(raw, `${id}.${at}.${body}`, digests)
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
let hmac = async (
|
|
209
|
+
scheme: Hmac,
|
|
210
|
+
secret: string,
|
|
211
|
+
body: string,
|
|
212
|
+
headers: HeaderLookup,
|
|
213
|
+
now: number,
|
|
214
|
+
) => {
|
|
215
|
+
let line = headers.get(scheme.header)
|
|
216
|
+
if (!line) return `no ${scheme.header} header`
|
|
217
|
+
let prefix = scheme.prefix ?? ''
|
|
218
|
+
if (!line.startsWith(prefix)) return MALFORMED
|
|
219
|
+
let digest = line.slice(prefix.length).trim()
|
|
220
|
+
let decoded = scheme.encoding == 'base64' ? base64(digest) : hex(digest)
|
|
221
|
+
let stamp = scheme.timestamp
|
|
222
|
+
if (!stamp) return matches(secret, body, [decoded])
|
|
223
|
+
let at = headers.get(stamp.header) ?? ''
|
|
224
|
+
if (!/^\d+$/.test(at)) return 'no timestamp on the signature'
|
|
225
|
+
if (Math.abs(now / 1000 - Number(at)) > SKEW) {
|
|
226
|
+
return 'the signature is too old'
|
|
227
|
+
}
|
|
228
|
+
let signed = [...stamp.version ? [stamp.version] : [], at, body]
|
|
229
|
+
.join(stamp.separator)
|
|
230
|
+
return await matches(secret, signed, [decoded])
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Ed25519: a 64-byte signature over `<t><join><raw body>`, `t` within SKEW of
|
|
234
|
+
// now, under a 32-byte public key. A key that is not one is the receiver's own
|
|
235
|
+
// mistake, so it reads as a mismatch.
|
|
236
|
+
let ed25519 = async (
|
|
237
|
+
scheme: Ed25519,
|
|
238
|
+
publicKey: string,
|
|
239
|
+
body: string,
|
|
240
|
+
headers: HeaderLookup,
|
|
241
|
+
now: number,
|
|
242
|
+
) => {
|
|
243
|
+
let line = headers.get(scheme.signature)
|
|
244
|
+
if (!line) return `no ${scheme.signature} header`
|
|
245
|
+
let at = headers.get(scheme.timestamp) ?? ''
|
|
246
|
+
if (!/^\d+$/.test(at)) return 'no timestamp on the signature'
|
|
247
|
+
if (Math.abs(now / 1000 - Number(at)) > SKEW) {
|
|
248
|
+
return 'the signature is too old'
|
|
249
|
+
}
|
|
250
|
+
let sent = base64(line.trim())
|
|
251
|
+
if (sent?.length != 64) return MALFORMED
|
|
252
|
+
let raw = base64(publicKey)
|
|
253
|
+
if (raw?.length != 32) return MISMATCH
|
|
254
|
+
let k = await crypto.subtle.importKey('raw', raw, 'Ed25519', false, [
|
|
255
|
+
'verify',
|
|
256
|
+
])
|
|
257
|
+
let ok = await crypto.subtle.verify(
|
|
258
|
+
'Ed25519',
|
|
259
|
+
k,
|
|
260
|
+
sent,
|
|
261
|
+
enc.encode(`${at}${scheme.join}${body}`),
|
|
262
|
+
)
|
|
263
|
+
return ok ? '' : MISMATCH
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Why a request is not signed under this scheme and secret, or '' when it is.
|
|
268
|
+
*
|
|
269
|
+
* ```ts
|
|
270
|
+
* import { refusal } from '@yaks/hook'
|
|
271
|
+
* await refusal('github', 'a secret', '{}', new Headers())
|
|
272
|
+
* // 'no X-Hub-Signature-256 header'
|
|
273
|
+
* ```
|
|
274
|
+
*/
|
|
275
|
+
export let refusal = (
|
|
276
|
+
scheme: Scheme,
|
|
277
|
+
secret: string,
|
|
278
|
+
body: string,
|
|
279
|
+
headers: HeaderLookup,
|
|
280
|
+
now: number = Date.now(),
|
|
281
|
+
): Promise<string> =>
|
|
282
|
+
scheme == 'stripe'
|
|
283
|
+
? stripe(secret, body, headers, now)
|
|
284
|
+
: scheme == 'telnyx'
|
|
285
|
+
? ed25519(TELNYX, secret, body, headers, now)
|
|
286
|
+
: scheme == 'standard'
|
|
287
|
+
? standard(secret, body, headers, now)
|
|
288
|
+
: typeof scheme == 'object' && 'token' in scheme
|
|
289
|
+
? bearer(scheme, secret, headers)
|
|
290
|
+
: scheme != 'github' && 'signature' in scheme
|
|
291
|
+
? ed25519(scheme, secret, body, headers, now)
|
|
292
|
+
: hmac(scheme == 'github' ? GITHUB : scheme, secret, body, headers, now)
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Whether a {@link refusal} can mean the receiver's own secret is wrong, and so
|
|
296
|
+
* is worth reporting: only a complete, fresh, well-formed signature that does
|
|
297
|
+
* not match. Every other refusal is the caller's own bad input.
|
|
298
|
+
*
|
|
299
|
+
* ```ts
|
|
300
|
+
* import { ours, refusal } from '@yaks/hook'
|
|
301
|
+
* ours(await refusal('stripe', 'a secret', '{}', new Headers())) // false
|
|
302
|
+
* ```
|
|
303
|
+
*/
|
|
304
|
+
export let ours = (why: string): boolean => why == MISMATCH
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* A captured request with its `verified` set by checking it under this scheme
|
|
308
|
+
* and secret, ready for {@link hooked}.
|
|
309
|
+
*/
|
|
310
|
+
export let checked = async (
|
|
311
|
+
r: Request,
|
|
312
|
+
scheme: Scheme,
|
|
313
|
+
secret: string,
|
|
314
|
+
now: number = Date.now(),
|
|
315
|
+
): Promise<Request> => ({
|
|
316
|
+
...r,
|
|
317
|
+
verified: await refusal(scheme, secret, r.body ?? '', {
|
|
318
|
+
get: (name) => header(r, name) ?? null,
|
|
319
|
+
}, now) == '',
|
|
320
|
+
})
|
package/vocab.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// The component declarations, and nothing else: the module a server or a
|
|
2
|
+
// browser page imports at `@yaks/hook/vocab`. It reaches no storage, no SQL
|
|
3
|
+
// and no runtime, so a browser tab loading this vocabulary loads nothing else.
|
|
4
|
+
import doc from './vocab.json' with {
|
|
5
|
+
type: 'json'
|
|
6
|
+
};
|
|
7
|
+
import manifest from './package.json' with {
|
|
8
|
+
type: 'json'
|
|
9
|
+
};
|
|
10
|
+
/** The hook vocabulary, in the form `loadVocab` accepts. */ export let hookDoc = doc;
|
|
11
|
+
/** What this package is: its manifest's description, the one place it is
|
|
12
|
+
* written, which a host stamps on each document here. */ export let description = manifest.description;
|
|
13
|
+
/** Every document this plugin declares. */ export let docs = [
|
|
14
|
+
hookDoc
|
|
15
|
+
];
|
|
16
|
+
//# sourceMappingURL=vocab.js.map
|
package/vocab.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"vocab.js","sources":["./vocab.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,4EAA4E;AAC5E,+EAA+E;AAG/E,OAAO,SAAS,oBAAoB;EAAE,MAAM;AAAO,EAAC;AACpD,OAAO,cAAc,sBAAsB;EAAE,MAAM;AAAO,EAAC;AAE3D,0DAA0D,GAC1D,OAAO,IAAI,UAAoB,IAAe;AAE9C;uDACuD,GACvD,OAAO,IAAI,cAAsB,SAAS,WAAW,CAAA;AAErD,yCAAyC,GACzC,OAAO,IAAI,OAAmB;EAAC;CAAQ,CAAA"}
|