stream-chat 9.56.0 → 9.56.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.
@@ -64,6 +64,18 @@ export declare class InvalidWebhookError extends Error {
64
64
  name: string;
65
65
  constructor(message?: string);
66
66
  }
67
+ /**
68
+ * Webhook helper settings.
69
+ *
70
+ * `maxPayloadBytes` caps the decompressed size of a gzip webhook body
71
+ * (default 10 MiB). {@link gunzipPayload} runs before the signature
72
+ * check, so without a cap an unsigned gzip bomb could exhaust memory.
73
+ * Raise it if your events are larger. It must be a positive integer;
74
+ * values above `buffer.constants.MAX_LENGTH` are clamped to it.
75
+ */
76
+ export declare const webhookConfig: {
77
+ maxPayloadBytes: number;
78
+ };
67
79
  /**
68
80
  * Returns `body` as a `Buffer`, gzip-decompressed when its first two
69
81
  * bytes match the gzip magic (`1f 8b`, per RFC 1952). When the body is
@@ -73,6 +85,12 @@ export declare class InvalidWebhookError extends Error {
73
85
  * Magic-byte detection (rather than relying on a header) keeps the
74
86
  * same handler correct when middleware - Express, Next.js, AWS Lambda
75
87
  * - auto-decompresses the request before your code sees it.
88
+ *
89
+ * @throws {InvalidWebhookError} When the body has the gzip magic but is
90
+ * not valid gzip, or inflates to more than
91
+ * `webhookConfig.maxPayloadBytes`.
92
+ * @throws {RangeError} When the body has the gzip magic and
93
+ * `webhookConfig.maxPayloadBytes` is not a positive integer.
76
94
  */
77
95
  export declare function gunzipPayload(rawBody: string | Buffer): Buffer;
78
96
  /**
@@ -107,8 +125,8 @@ export declare function parseEvent(payload: Buffer | string): Event;
107
125
  * @param rawBody Raw HTTP request body bytes Stream signed
108
126
  * @param signature Value of the `X-Signature` header
109
127
  * @param secret Your app's API secret
110
- * @throws {InvalidWebhookError} When the signature does not match or
111
- * the gzip envelope is malformed.
128
+ * @throws {InvalidWebhookError} When the signature does not match, or
129
+ * the gzip envelope is malformed or exceeds `webhookConfig.maxPayloadBytes`.
112
130
  */
113
131
  export declare function verifyAndParseWebhook(rawBody: string | Buffer, signature: string, secret: string): Event;
114
132
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stream-chat",
3
- "version": "9.56.0",
3
+ "version": "9.56.1",
4
4
  "description": "JS SDK for the Stream Chat API",
5
5
  "homepage": "https://getstream.io/chat/",
6
6
  "author": {
@@ -29,6 +29,7 @@
29
29
  }
30
30
  },
31
31
  "browser": {
32
+ "buffer": false,
32
33
  "crypto": false,
33
34
  "https": false,
34
35
  "jsonwebtoken": false,
package/src/signing.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import jwt from 'jsonwebtoken';
2
2
  import crypto from 'crypto';
3
3
  import zlib from 'zlib';
4
+ import buffer from 'buffer';
4
5
  import { decodeBase64, encodeBase64 } from './base64';
5
6
  import type { Event, UR } from './types';
6
7
 
@@ -149,6 +150,19 @@ export class InvalidWebhookError extends Error {
149
150
  }
150
151
  }
151
152
 
153
+ /**
154
+ * Webhook helper settings.
155
+ *
156
+ * `maxPayloadBytes` caps the decompressed size of a gzip webhook body
157
+ * (default 10 MiB). {@link gunzipPayload} runs before the signature
158
+ * check, so without a cap an unsigned gzip bomb could exhaust memory.
159
+ * Raise it if your events are larger. It must be a positive integer;
160
+ * values above `buffer.constants.MAX_LENGTH` are clamped to it.
161
+ */
162
+ export const webhookConfig = {
163
+ maxPayloadBytes: 10 * 1024 * 1024,
164
+ };
165
+
152
166
  /**
153
167
  * Returns `body` as a `Buffer`, gzip-decompressed when its first two
154
168
  * bytes match the gzip magic (`1f 8b`, per RFC 1952). When the body is
@@ -158,15 +172,34 @@ export class InvalidWebhookError extends Error {
158
172
  * Magic-byte detection (rather than relying on a header) keeps the
159
173
  * same handler correct when middleware - Express, Next.js, AWS Lambda
160
174
  * - auto-decompresses the request before your code sees it.
175
+ *
176
+ * @throws {InvalidWebhookError} When the body has the gzip magic but is
177
+ * not valid gzip, or inflates to more than
178
+ * `webhookConfig.maxPayloadBytes`.
179
+ * @throws {RangeError} When the body has the gzip magic and
180
+ * `webhookConfig.maxPayloadBytes` is not a positive integer.
161
181
  */
162
182
  export function gunzipPayload(rawBody: string | Buffer): Buffer {
163
183
  const GZIP_MAGIC = Buffer.from([0x1f, 0x8b]);
164
184
 
165
185
  const body = Buffer.isBuffer(rawBody) ? rawBody : Buffer.from(rawBody);
166
186
  if (body.length >= 2 && body.subarray(0, 2).equals(GZIP_MAGIC)) {
187
+ const max = webhookConfig.maxPayloadBytes;
188
+ if (!Number.isInteger(max) || max < 1) {
189
+ const got = typeof max === 'string' ? JSON.stringify(max) : String(max);
190
+ throw new RangeError(
191
+ `webhookConfig.maxPayloadBytes must be a positive integer, got ${got}`,
192
+ );
193
+ }
194
+ const limit = Math.min(max, buffer.constants.MAX_LENGTH);
167
195
  try {
168
- return zlib.gunzipSync(body);
169
- } catch {
196
+ return zlib.gunzipSync(body, { maxOutputLength: limit });
197
+ } catch (err) {
198
+ if ((err as NodeJS.ErrnoException).code === 'ERR_BUFFER_TOO_LARGE') {
199
+ throw new InvalidWebhookError(
200
+ `${InvalidWebhookErrorMessages.gzipFailed}: payload exceeds ${limit} bytes`,
201
+ );
202
+ }
170
203
  throw new InvalidWebhookError(InvalidWebhookErrorMessages.gzipFailed);
171
204
  }
172
205
  }
@@ -261,8 +294,8 @@ function verifyAndParse(payload: Buffer, signature: string, secret: string): Eve
261
294
  * @param rawBody Raw HTTP request body bytes Stream signed
262
295
  * @param signature Value of the `X-Signature` header
263
296
  * @param secret Your app's API secret
264
- * @throws {InvalidWebhookError} When the signature does not match or
265
- * the gzip envelope is malformed.
297
+ * @throws {InvalidWebhookError} When the signature does not match, or
298
+ * the gzip envelope is malformed or exceeds `webhookConfig.maxPayloadBytes`.
266
299
  */
267
300
  export function verifyAndParseWebhook(
268
301
  rawBody: string | Buffer,