@pithy-sh/email 0.10.2 → 0.10.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.
@@ -16,7 +16,7 @@ function extractAddress(value) {
16
16
  function originalMessageId(raw) {
17
17
  const explicit = field(raw, "Original-Message-ID");
18
18
  if (explicit) return explicit.replace(/^<|>$/g, "");
19
- const ids = [...raw.matchAll(/^Message-ID:\s*<([^>]+)>/gim)].map((m) => m[1]);
19
+ const ids = [...raw.matchAll(/^Message-ID:[ \t]*<([^>\r\n]+)>/gim)].map((m) => m[1]);
20
20
  return ids.length > 0 ? ids[ids.length - 1] : void 0;
21
21
  }
22
22
  /** Classify an inbound message. */
@@ -41,6 +41,13 @@ export interface InboundSkip {
41
41
  * suppression database skips the suppression, and each skip is reported in `skipped` rather than thrown.
42
42
  */
43
43
  export declare function applyInbound(db: EmailDatabase | undefined, suppressionDb: EmailSuppressionDatabase | undefined, c: InboundClassification, now: Date, environment?: string): Promise<InboundOutcome>;
44
+ /**
45
+ * The largest inbound message the bounce handler reads, by its declared size. What comes back here is
46
+ * a DSN or a feedback report about a templated send with no attachments, so even a report that returns
47
+ * the whole original runs to tens of kilobytes; 2 MiB, support's default bound, leaves that a wide
48
+ * margin and refuses anything else before a byte of it is buffered.
49
+ */
50
+ export declare const MAX_BOUNCE_RAW_BYTES: number;
44
51
  /**
45
52
  * Build the inbound-email handler for the `Capability.email` seam. Reads `DB` + `EMAIL_SUPPRESSIONS` from env.
46
53
  *
@@ -1 +1 @@
1
- {"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/bounce/handler.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,0CAA0C,CAAC;AAIvF,OAAO,EACL,KAAK,aAAa,EAClB,KAAK,wBAAwB,EAG9B,MAAM,gBAAgB,CAAC;AAGxB,OAAO,EAAmB,KAAK,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEzE;;;;;;G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K,CAAC,EAAE,MAAM,CAAC;IACf,6DAA6D;IAC7D,UAAU,EAAE,OAAO,CAAC;IACpB,6GAA6G;IAC7G,OAAO,EAAE,WAAW,EAAE,CAAC;CACxB;AAED,yFAAyF;AACzF,MAAM,WAAW,WAAW;IAC1B,8GAA8G;IAC9G,IAAI,EAAE,KAAK,GAAG,aAAa,CAAC;IAC5B,mDAAmD;IACnD,OAAO,EAAE,IAAI,GAAG,oBAAoB,CAAC;CACtC;AAED;;;;;;;;;GASG;AACH,wBAAsB,YAAY,CAChC,EAAE,EAAE,aAAa,GAAG,SAAS,EAC7B,aAAa,EAAE,wBAAwB,GAAG,SAAS,EACnD,CAAC,EAAE,qBAAqB,EACxB,GAAG,EAAE,IAAI,EACT,WAAW,CAAC,EAAE,MAAM,GACnB,OAAO,CAAC,cAAc,CAAC,CAiEzB;AAED;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,QAAkB,CAAC;AAEpD;;;;;;;;GAQG;AACH,wBAAgB,mBAAmB,IAAI,sBAAsB,CAmD5D"}
@@ -78,6 +78,13 @@ async function applyInbound(db, suppressionDb, c, now, environment) {
78
78
  };
79
79
  }
80
80
  /**
81
+ * The largest inbound message the bounce handler reads, by its declared size. What comes back here is
82
+ * a DSN or a feedback report about a templated send with no attachments, so even a report that returns
83
+ * the whole original runs to tens of kilobytes; 2 MiB, support's default bound, leaves that a wide
84
+ * margin and refuses anything else before a byte of it is buffered.
85
+ */
86
+ const MAX_BOUNCE_RAW_BYTES = 2097152;
87
+ /**
81
88
  * Build the inbound-email handler for the `Capability.email` seam. Reads `DB` + `EMAIL_SUPPRESSIONS` from env.
82
89
  *
83
90
  * It never throws. `createEntrypoint` runs every capability's handler in turn, so a throw here would stop the ones
@@ -94,6 +101,15 @@ function createBounceHandler() {
94
101
  fields: { env: environment ?? "unknown" }
95
102
  });
96
103
  try {
104
+ const declared = message.rawSize;
105
+ if (typeof declared === "number" && declared > 2097152) {
106
+ log.warn("email inbound message refused before parsing", {
107
+ reason: "too_large",
108
+ rawSize: declared,
109
+ maxRawBytes: MAX_BOUNCE_RAW_BYTES
110
+ });
111
+ return;
112
+ }
97
113
  const raw = await new Response(message.raw).text();
98
114
  const parsed = await PostalMime.parse(raw);
99
115
  const headers = {};
@@ -123,4 +139,4 @@ function isD1(binding) {
123
139
  return typeof binding === "object" && binding !== null;
124
140
  }
125
141
  //#endregion
126
- export { applyInbound, createBounceHandler };
142
+ export { MAX_BOUNCE_RAW_BYTES, applyInbound, createBounceHandler };
@@ -3,5 +3,5 @@
3
3
  /** This package's npm name — the join key against a release feed. */
4
4
  export declare const PACKAGE_NAME = "@pithy-sh/email";
5
5
  /** This package's version, stamped from its own package.json at generation time. */
6
- export declare const PACKAGE_VERSION = "0.10.2";
6
+ export declare const PACKAGE_VERSION = "0.10.3";
7
7
  //# sourceMappingURL=version.generated.d.ts.map
@@ -4,6 +4,6 @@
4
4
  /** This package's npm name — the join key against a release feed. */
5
5
  const PACKAGE_NAME = "@pithy-sh/email";
6
6
  /** This package's version, stamped from its own package.json at generation time. */
7
- const PACKAGE_VERSION = "0.10.2";
7
+ const PACKAGE_VERSION = "0.10.3";
8
8
  //#endregion
9
9
  export { PACKAGE_NAME, PACKAGE_VERSION };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pithy-sh/email",
3
- "version": "0.10.2",
3
+ "version": "0.10.3",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -39,10 +39,10 @@
39
39
  "@cloudflare/workers-types": "^5.20260729.1",
40
40
  "@hono/zod-validator": "^0.9.0",
41
41
  "handlebars": "^4.7.9",
42
- "postal-mime": "^3.0.0"
42
+ "postal-mime": "^4.0.8"
43
43
  },
44
44
  "peerDependencies": {
45
- "@pithy-sh/core": "^0.20.0",
45
+ "@pithy-sh/core": "^0.20.1",
46
46
  "@pithy-sh/secrets": "^0.6.8",
47
47
  "hono": "^4.13.2",
48
48
  "kysely": "^0.29.0",
@@ -47,7 +47,7 @@ function extractAddress(value: string | undefined): string | undefined {
47
47
  function originalMessageId(raw: string): string | undefined {
48
48
  const explicit = field(raw, "Original-Message-ID");
49
49
  if (explicit) return explicit.replace(/^<|>$/g, "");
50
- const ids = [...raw.matchAll(/^Message-ID:\s*<([^>]+)>/gim)].map((m) => m[1]);
50
+ const ids = [...raw.matchAll(/^Message-ID:[ \t]*<([^>\r\n]+)>/gim)].map((m) => m[1]);
51
51
  return ids.length > 0 ? ids[ids.length - 1] : undefined;
52
52
  }
53
53
 
@@ -130,6 +130,14 @@ export async function applyInbound(
130
130
  return { acted: suppressed || Boolean(job), type: c.type, jobId: job?.id, suppressed, skipped };
131
131
  }
132
132
 
133
+ /**
134
+ * The largest inbound message the bounce handler reads, by its declared size. What comes back here is
135
+ * a DSN or a feedback report about a templated send with no attachments, so even a report that returns
136
+ * the whole original runs to tens of kilobytes; 2 MiB, support's default bound, leaves that a wide
137
+ * margin and refuses anything else before a byte of it is buffered.
138
+ */
139
+ export const MAX_BOUNCE_RAW_BYTES = 2 * 1024 * 1024;
140
+
133
141
  /**
134
142
  * Build the inbound-email handler for the `Capability.email` seam. Reads `DB` + `EMAIL_SUPPRESSIONS` from env.
135
143
  *
@@ -144,6 +152,16 @@ export function createBounceHandler(): CapabilityEmailHandler {
144
152
  const environment = typeof env.ENVIRONMENT === "string" ? env.ENVIRONMENT : undefined;
145
153
  const log = createWorkerLogger({ name: "email", fields: { env: environment ?? "unknown" } });
146
154
  try {
155
+ const declared = message.rawSize;
156
+ if (typeof declared === "number" && declared > MAX_BOUNCE_RAW_BYTES) {
157
+ log.warn("email inbound message refused before parsing", {
158
+ reason: "too_large",
159
+ rawSize: declared,
160
+ maxRawBytes: MAX_BOUNCE_RAW_BYTES,
161
+ });
162
+ return;
163
+ }
164
+
147
165
  const raw = await new Response(message.raw).text();
148
166
  const parsed = await PostalMime.parse(raw);
149
167
  const headers: Record<string, string> = {};
@@ -13,4 +13,4 @@
13
13
  export const PACKAGE_NAME = "@pithy-sh/email";
14
14
 
15
15
  /** This package's version, stamped from its own package.json at generation time. */
16
- export const PACKAGE_VERSION = "0.10.2";
16
+ export const PACKAGE_VERSION = "0.10.3";