@evumi/berth 0.2.4 → 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/dist/cjs/handler.d.ts +2 -0
- package/dist/cjs/handler.js +76 -10
- package/dist/cjs/signature.js +2 -0
- package/dist/esm/handler.d.ts +2 -0
- package/dist/esm/handler.js +75 -10
- package/dist/esm/signature.js +2 -0
- package/package.json +1 -1
package/dist/cjs/handler.d.ts
CHANGED
|
@@ -57,6 +57,8 @@ export interface Handlers {
|
|
|
57
57
|
toleranceSeconds?: number;
|
|
58
58
|
}
|
|
59
59
|
export type FetchHandler = (request: Request) => Promise<Response>;
|
|
60
|
+
/** Maximum webhook delivery body buffered before signature verification (64 MiB, matching Go SDK). */
|
|
61
|
+
export declare const MAX_DELIVERY_BODY: number;
|
|
60
62
|
/**
|
|
61
63
|
* A Fetch API handler for everything the platform pushes: queue batches, alarms and Cron HTTP
|
|
62
64
|
* targets, told apart by Berth-Source. It verifies Berth-Signature (401 when it fails), calls the
|
package/dist/cjs/handler.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MAX_DELIVERY_BODY = void 0;
|
|
3
4
|
exports.createHandler = createHandler;
|
|
4
5
|
exports.toNodeHandler = toNodeHandler;
|
|
5
6
|
const body_js_1 = require("./body.js");
|
|
@@ -54,6 +55,53 @@ function makeBatch(raw) {
|
|
|
54
55
|
};
|
|
55
56
|
return { batch, settle };
|
|
56
57
|
}
|
|
58
|
+
/** Maximum webhook delivery body buffered before signature verification (64 MiB, matching Go SDK). */
|
|
59
|
+
exports.MAX_DELIVERY_BODY = 64 * 1024 * 1024;
|
|
60
|
+
class BodyTooLargeError extends Error {
|
|
61
|
+
constructor() {
|
|
62
|
+
super('request body exceeds maximum size');
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
async function readRequestBody(request, maxBytes = exports.MAX_DELIVERY_BODY) {
|
|
66
|
+
const cl = request.headers.get('content-length');
|
|
67
|
+
if (cl !== null) {
|
|
68
|
+
const n = Number(cl);
|
|
69
|
+
if (Number.isFinite(n) && n > maxBytes)
|
|
70
|
+
throw new BodyTooLargeError();
|
|
71
|
+
}
|
|
72
|
+
if (!request.body) {
|
|
73
|
+
const raw = new Uint8Array(await request.arrayBuffer());
|
|
74
|
+
if (raw.byteLength > maxBytes)
|
|
75
|
+
throw new BodyTooLargeError();
|
|
76
|
+
return raw;
|
|
77
|
+
}
|
|
78
|
+
const reader = request.body.getReader();
|
|
79
|
+
const chunks = [];
|
|
80
|
+
let total = 0;
|
|
81
|
+
try {
|
|
82
|
+
while (true) {
|
|
83
|
+
const { done, value } = await reader.read();
|
|
84
|
+
if (done)
|
|
85
|
+
break;
|
|
86
|
+
total += value.byteLength;
|
|
87
|
+
if (total > maxBytes) {
|
|
88
|
+
await reader.cancel().catch(() => { });
|
|
89
|
+
throw new BodyTooLargeError();
|
|
90
|
+
}
|
|
91
|
+
chunks.push(value);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
reader.releaseLock();
|
|
96
|
+
}
|
|
97
|
+
const out = new Uint8Array(total);
|
|
98
|
+
let off = 0;
|
|
99
|
+
for (const c of chunks) {
|
|
100
|
+
out.set(c, off);
|
|
101
|
+
off += c.byteLength;
|
|
102
|
+
}
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
57
105
|
/**
|
|
58
106
|
* A Fetch API handler for everything the platform pushes: queue batches, alarms and Cron HTTP
|
|
59
107
|
* targets, told apart by Berth-Source. It verifies Berth-Signature (401 when it fails), calls the
|
|
@@ -63,9 +111,17 @@ function makeBatch(raw) {
|
|
|
63
111
|
function createHandler(handlers) {
|
|
64
112
|
return async (request) => {
|
|
65
113
|
const secret = handlers.secret ?? (0, util_js_1.env)('BERTH_SIGNING_SECRET') ?? '';
|
|
66
|
-
const raw = new Uint8Array(await request.arrayBuffer());
|
|
67
114
|
if (!secret)
|
|
68
115
|
return text(401, 'BERTH_SIGNING_SECRET is not set');
|
|
116
|
+
let raw;
|
|
117
|
+
try {
|
|
118
|
+
raw = await readRequestBody(request);
|
|
119
|
+
}
|
|
120
|
+
catch (e) {
|
|
121
|
+
if (e instanceof BodyTooLargeError)
|
|
122
|
+
return text(413, 'request body too large');
|
|
123
|
+
throw e;
|
|
124
|
+
}
|
|
69
125
|
const bad = (0, signature_js_1.checkSignature)(secret, request.headers.get('Berth-Signature'), raw, { toleranceSeconds: handlers.toleranceSeconds });
|
|
70
126
|
if (bad)
|
|
71
127
|
return text(401, `invalid signature: ${bad}`);
|
|
@@ -120,20 +176,30 @@ function createHandler(handlers) {
|
|
|
120
176
|
}
|
|
121
177
|
};
|
|
122
178
|
}
|
|
123
|
-
async function readNodeBody(req) {
|
|
124
|
-
if (req.body instanceof Uint8Array)
|
|
179
|
+
async function readNodeBody(req, maxBytes = exports.MAX_DELIVERY_BODY) {
|
|
180
|
+
if (req.body instanceof Uint8Array) {
|
|
181
|
+
if (req.body.byteLength > maxBytes)
|
|
182
|
+
throw new BodyTooLargeError();
|
|
125
183
|
return new Uint8Array(req.body); // Buffer, e.g. express.raw()
|
|
126
|
-
|
|
127
|
-
|
|
184
|
+
}
|
|
185
|
+
if (typeof req.body === 'string') {
|
|
186
|
+
const encoded = new TextEncoder().encode(req.body);
|
|
187
|
+
if (encoded.byteLength > maxBytes)
|
|
188
|
+
throw new BodyTooLargeError();
|
|
189
|
+
return encoded;
|
|
190
|
+
}
|
|
128
191
|
if (req.readableEnded) {
|
|
129
192
|
throw new Error('the request body was already read by a parser; mount this handler before express.json() or use express.raw()');
|
|
130
193
|
}
|
|
131
194
|
const chunks = [];
|
|
132
|
-
for await (const c of req)
|
|
133
|
-
chunks.push(typeof c === 'string' ? new TextEncoder().encode(c) : c);
|
|
134
195
|
let len = 0;
|
|
135
|
-
for (const c of
|
|
136
|
-
|
|
196
|
+
for await (const c of req) {
|
|
197
|
+
const chunk = typeof c === 'string' ? new TextEncoder().encode(c) : c;
|
|
198
|
+
len += chunk.length;
|
|
199
|
+
if (len > maxBytes)
|
|
200
|
+
throw new BodyTooLargeError();
|
|
201
|
+
chunks.push(chunk);
|
|
202
|
+
}
|
|
137
203
|
const out = new Uint8Array(len);
|
|
138
204
|
let off = 0;
|
|
139
205
|
for (const c of chunks) {
|
|
@@ -159,7 +225,7 @@ function toNodeHandler(handler) {
|
|
|
159
225
|
response = await handler(new Request(new URL(req.url ?? '/', `http://${host}`), { method, headers, body: method === 'GET' || method === 'HEAD' ? undefined : body }));
|
|
160
226
|
}
|
|
161
227
|
catch (e) {
|
|
162
|
-
response = text(400, e?.message ?? String(e));
|
|
228
|
+
response = e instanceof BodyTooLargeError ? text(413, 'request body too large') : text(400, e?.message ?? String(e));
|
|
163
229
|
}
|
|
164
230
|
res.statusCode = response.status;
|
|
165
231
|
response.headers.forEach((v, k) => void res.setHeader(k, v));
|
package/dist/cjs/signature.js
CHANGED
|
@@ -45,6 +45,8 @@ function checkSignature(secret, header, rawBody, opts = {}) {
|
|
|
45
45
|
if ((d < 0n ? -d : d) > tolerance)
|
|
46
46
|
return 'expired';
|
|
47
47
|
const key = typeof secret === 'string' ? utf8.encode(secret) : secret;
|
|
48
|
+
if (key.length === 0)
|
|
49
|
+
return 'mismatch';
|
|
48
50
|
const want = (0, sha256_js_1.toHex)((0, sha256_js_1.hmacSha256)(key, utf8.encode(t.toString() + '.'), bytes(rawBody)));
|
|
49
51
|
let ok = false;
|
|
50
52
|
for (const s of sigs)
|
package/dist/esm/handler.d.ts
CHANGED
|
@@ -57,6 +57,8 @@ export interface Handlers {
|
|
|
57
57
|
toleranceSeconds?: number;
|
|
58
58
|
}
|
|
59
59
|
export type FetchHandler = (request: Request) => Promise<Response>;
|
|
60
|
+
/** Maximum webhook delivery body buffered before signature verification (64 MiB, matching Go SDK). */
|
|
61
|
+
export declare const MAX_DELIVERY_BODY: number;
|
|
60
62
|
/**
|
|
61
63
|
* A Fetch API handler for everything the platform pushes: queue batches, alarms and Cron HTTP
|
|
62
64
|
* targets, told apart by Berth-Source. It verifies Berth-Signature (401 when it fails), calls the
|
package/dist/esm/handler.js
CHANGED
|
@@ -50,6 +50,53 @@ function makeBatch(raw) {
|
|
|
50
50
|
};
|
|
51
51
|
return { batch, settle };
|
|
52
52
|
}
|
|
53
|
+
/** Maximum webhook delivery body buffered before signature verification (64 MiB, matching Go SDK). */
|
|
54
|
+
export const MAX_DELIVERY_BODY = 64 * 1024 * 1024;
|
|
55
|
+
class BodyTooLargeError extends Error {
|
|
56
|
+
constructor() {
|
|
57
|
+
super('request body exceeds maximum size');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async function readRequestBody(request, maxBytes = MAX_DELIVERY_BODY) {
|
|
61
|
+
const cl = request.headers.get('content-length');
|
|
62
|
+
if (cl !== null) {
|
|
63
|
+
const n = Number(cl);
|
|
64
|
+
if (Number.isFinite(n) && n > maxBytes)
|
|
65
|
+
throw new BodyTooLargeError();
|
|
66
|
+
}
|
|
67
|
+
if (!request.body) {
|
|
68
|
+
const raw = new Uint8Array(await request.arrayBuffer());
|
|
69
|
+
if (raw.byteLength > maxBytes)
|
|
70
|
+
throw new BodyTooLargeError();
|
|
71
|
+
return raw;
|
|
72
|
+
}
|
|
73
|
+
const reader = request.body.getReader();
|
|
74
|
+
const chunks = [];
|
|
75
|
+
let total = 0;
|
|
76
|
+
try {
|
|
77
|
+
while (true) {
|
|
78
|
+
const { done, value } = await reader.read();
|
|
79
|
+
if (done)
|
|
80
|
+
break;
|
|
81
|
+
total += value.byteLength;
|
|
82
|
+
if (total > maxBytes) {
|
|
83
|
+
await reader.cancel().catch(() => { });
|
|
84
|
+
throw new BodyTooLargeError();
|
|
85
|
+
}
|
|
86
|
+
chunks.push(value);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
reader.releaseLock();
|
|
91
|
+
}
|
|
92
|
+
const out = new Uint8Array(total);
|
|
93
|
+
let off = 0;
|
|
94
|
+
for (const c of chunks) {
|
|
95
|
+
out.set(c, off);
|
|
96
|
+
off += c.byteLength;
|
|
97
|
+
}
|
|
98
|
+
return out;
|
|
99
|
+
}
|
|
53
100
|
/**
|
|
54
101
|
* A Fetch API handler for everything the platform pushes: queue batches, alarms and Cron HTTP
|
|
55
102
|
* targets, told apart by Berth-Source. It verifies Berth-Signature (401 when it fails), calls the
|
|
@@ -59,9 +106,17 @@ function makeBatch(raw) {
|
|
|
59
106
|
export function createHandler(handlers) {
|
|
60
107
|
return async (request) => {
|
|
61
108
|
const secret = handlers.secret ?? readEnv('BERTH_SIGNING_SECRET') ?? '';
|
|
62
|
-
const raw = new Uint8Array(await request.arrayBuffer());
|
|
63
109
|
if (!secret)
|
|
64
110
|
return text(401, 'BERTH_SIGNING_SECRET is not set');
|
|
111
|
+
let raw;
|
|
112
|
+
try {
|
|
113
|
+
raw = await readRequestBody(request);
|
|
114
|
+
}
|
|
115
|
+
catch (e) {
|
|
116
|
+
if (e instanceof BodyTooLargeError)
|
|
117
|
+
return text(413, 'request body too large');
|
|
118
|
+
throw e;
|
|
119
|
+
}
|
|
65
120
|
const bad = checkSignature(secret, request.headers.get('Berth-Signature'), raw, { toleranceSeconds: handlers.toleranceSeconds });
|
|
66
121
|
if (bad)
|
|
67
122
|
return text(401, `invalid signature: ${bad}`);
|
|
@@ -116,20 +171,30 @@ export function createHandler(handlers) {
|
|
|
116
171
|
}
|
|
117
172
|
};
|
|
118
173
|
}
|
|
119
|
-
async function readNodeBody(req) {
|
|
120
|
-
if (req.body instanceof Uint8Array)
|
|
174
|
+
async function readNodeBody(req, maxBytes = MAX_DELIVERY_BODY) {
|
|
175
|
+
if (req.body instanceof Uint8Array) {
|
|
176
|
+
if (req.body.byteLength > maxBytes)
|
|
177
|
+
throw new BodyTooLargeError();
|
|
121
178
|
return new Uint8Array(req.body); // Buffer, e.g. express.raw()
|
|
122
|
-
|
|
123
|
-
|
|
179
|
+
}
|
|
180
|
+
if (typeof req.body === 'string') {
|
|
181
|
+
const encoded = new TextEncoder().encode(req.body);
|
|
182
|
+
if (encoded.byteLength > maxBytes)
|
|
183
|
+
throw new BodyTooLargeError();
|
|
184
|
+
return encoded;
|
|
185
|
+
}
|
|
124
186
|
if (req.readableEnded) {
|
|
125
187
|
throw new Error('the request body was already read by a parser; mount this handler before express.json() or use express.raw()');
|
|
126
188
|
}
|
|
127
189
|
const chunks = [];
|
|
128
|
-
for await (const c of req)
|
|
129
|
-
chunks.push(typeof c === 'string' ? new TextEncoder().encode(c) : c);
|
|
130
190
|
let len = 0;
|
|
131
|
-
for (const c of
|
|
132
|
-
|
|
191
|
+
for await (const c of req) {
|
|
192
|
+
const chunk = typeof c === 'string' ? new TextEncoder().encode(c) : c;
|
|
193
|
+
len += chunk.length;
|
|
194
|
+
if (len > maxBytes)
|
|
195
|
+
throw new BodyTooLargeError();
|
|
196
|
+
chunks.push(chunk);
|
|
197
|
+
}
|
|
133
198
|
const out = new Uint8Array(len);
|
|
134
199
|
let off = 0;
|
|
135
200
|
for (const c of chunks) {
|
|
@@ -155,7 +220,7 @@ export function toNodeHandler(handler) {
|
|
|
155
220
|
response = await handler(new Request(new URL(req.url ?? '/', `http://${host}`), { method, headers, body: method === 'GET' || method === 'HEAD' ? undefined : body }));
|
|
156
221
|
}
|
|
157
222
|
catch (e) {
|
|
158
|
-
response = text(400, e?.message ?? String(e));
|
|
223
|
+
response = e instanceof BodyTooLargeError ? text(413, 'request body too large') : text(400, e?.message ?? String(e));
|
|
159
224
|
}
|
|
160
225
|
res.statusCode = response.status;
|
|
161
226
|
response.headers.forEach((v, k) => void res.setHeader(k, v));
|
package/dist/esm/signature.js
CHANGED
|
@@ -41,6 +41,8 @@ export function checkSignature(secret, header, rawBody, opts = {}) {
|
|
|
41
41
|
if ((d < 0n ? -d : d) > tolerance)
|
|
42
42
|
return 'expired';
|
|
43
43
|
const key = typeof secret === 'string' ? utf8.encode(secret) : secret;
|
|
44
|
+
if (key.length === 0)
|
|
45
|
+
return 'mismatch';
|
|
44
46
|
const want = toHex(hmacSha256(key, utf8.encode(t.toString() + '.'), bytes(rawBody)));
|
|
45
47
|
let ok = false;
|
|
46
48
|
for (const s of sigs)
|