@stream-io/node-sdk 0.9.2 → 0.9.3

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.
@@ -25,6 +25,17 @@ export declare class InvalidWebhookError extends Error {
25
25
  name: string;
26
26
  constructor(message?: string);
27
27
  }
28
+ /**
29
+ * Webhook helper settings.
30
+ *
31
+ * `maxPayloadBytes` caps the decompressed size of a gzip webhook body
32
+ * (default 10 MiB). {@link gunzipPayload} runs before the signature
33
+ * check, so without a cap an unsigned gzip bomb could exhaust memory.
34
+ * Raise it if your events are larger.
35
+ */
36
+ export declare const webhookConfig: {
37
+ maxPayloadBytes: number;
38
+ };
28
39
  /**
29
40
  * Returns `body` as a `Buffer`, gzip-decompressed when its first two
30
41
  * bytes match the gzip magic (`1f 8b`, per RFC 1952). When the body is
@@ -34,6 +45,10 @@ export declare class InvalidWebhookError extends Error {
34
45
  * Magic-byte detection (rather than relying on a header) keeps the same
35
46
  * handler correct when middleware - Express, Next.js, AWS Lambda -
36
47
  * auto-decompresses the request before your code sees it.
48
+ *
49
+ * @throws {InvalidWebhookError} When the body has the gzip magic but is
50
+ * not valid gzip, or inflates to more than
51
+ * `webhookConfig.maxPayloadBytes`.
37
52
  */
38
53
  export declare function gunzipPayload(rawBody: string | Buffer): Buffer;
39
54
  /**
package/index.ts CHANGED
@@ -10,6 +10,7 @@ export {
10
10
  InvalidWebhookError,
11
11
  InvalidWebhookErrorMessages,
12
12
  verifyAndParseWebhook,
13
+ webhookConfig,
13
14
  } from './src/utils/webhook';
14
15
  export {
15
16
  convertTimestampToDate,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stream-io/node-sdk",
3
- "version": "0.9.2",
3
+ "version": "0.9.3",
4
4
  "description": "",
5
5
  "exports": {
6
6
  ".": {
@@ -35,6 +35,18 @@ export class InvalidWebhookError extends Error {
35
35
 
36
36
  const GZIP_MAGIC = Buffer.from([0x1f, 0x8b]);
37
37
 
38
+ /**
39
+ * Webhook helper settings.
40
+ *
41
+ * `maxPayloadBytes` caps the decompressed size of a gzip webhook body
42
+ * (default 10 MiB). {@link gunzipPayload} runs before the signature
43
+ * check, so without a cap an unsigned gzip bomb could exhaust memory.
44
+ * Raise it if your events are larger.
45
+ */
46
+ export const webhookConfig = {
47
+ maxPayloadBytes: 10 * 1024 * 1024,
48
+ };
49
+
38
50
  /**
39
51
  * Returns `body` as a `Buffer`, gzip-decompressed when its first two
40
52
  * bytes match the gzip magic (`1f 8b`, per RFC 1952). When the body is
@@ -44,13 +56,23 @@ const GZIP_MAGIC = Buffer.from([0x1f, 0x8b]);
44
56
  * Magic-byte detection (rather than relying on a header) keeps the same
45
57
  * handler correct when middleware - Express, Next.js, AWS Lambda -
46
58
  * auto-decompresses the request before your code sees it.
59
+ *
60
+ * @throws {InvalidWebhookError} When the body has the gzip magic but is
61
+ * not valid gzip, or inflates to more than
62
+ * `webhookConfig.maxPayloadBytes`.
47
63
  */
48
64
  export function gunzipPayload(rawBody: string | Buffer): Buffer {
49
65
  const body = Buffer.isBuffer(rawBody) ? rawBody : Buffer.from(rawBody);
50
66
  if (body.length >= 2 && body.subarray(0, 2).equals(GZIP_MAGIC)) {
67
+ const limit = webhookConfig.maxPayloadBytes;
51
68
  try {
52
- return zlib.gunzipSync(body);
53
- } catch {
69
+ return zlib.gunzipSync(body, { maxOutputLength: limit });
70
+ } catch (err) {
71
+ if ((err as NodeJS.ErrnoException).code === 'ERR_BUFFER_TOO_LARGE') {
72
+ throw new InvalidWebhookError(
73
+ `${InvalidWebhookErrorMessages.gzipFailed}: payload exceeds ${limit} bytes`,
74
+ );
75
+ }
54
76
  throw new InvalidWebhookError(InvalidWebhookErrorMessages.gzipFailed);
55
77
  }
56
78
  }