@open-workspace/email 0.3.0 → 0.4.0

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.
Files changed (221) hide show
  1. package/README.md +22 -22
  2. package/alchemy/index.ts +273 -0
  3. package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
  4. package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
  5. package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
  6. package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
  7. package/drizzle/mailbox/meta/_journal.json +20 -0
  8. package/drizzle/mailbox/migrations.js +12 -0
  9. package/index.ts +3 -1
  10. package/migrations/{0000_email.sql → 0000_dry_moon_knight.sql} +26 -20
  11. package/migrations/meta/0000_snapshot.json +125 -109
  12. package/migrations/meta/_journal.json +2 -2
  13. package/package.json +74 -68
  14. package/shared/attachment-preview.ts +37 -27
  15. package/shared/folder-order.ts +106 -0
  16. package/shared/mail-authentication.ts +8 -3
  17. package/shared/mail-feedback.ts +18 -25
  18. package/shared/mail-headers.ts +114 -39
  19. package/shared/mail-import.ts +38 -0
  20. package/shared/mail-path.ts +42 -0
  21. package/shared/mail-received.ts +174 -31
  22. package/shared/mail-remote.ts +59 -2
  23. package/shared/mail-sender-intel.ts +22 -4
  24. package/shared/mail.ts +77 -28
  25. package/shared/module.ts +33 -0
  26. package/shared/office-preview.ts +14 -2
  27. package/shared/pdfjs-assets.ts +35 -0
  28. package/shared/permissions.ts +73 -0
  29. package/shared/webhooks.ts +41 -0
  30. package/src/admin/blocked-senders.tsx +189 -0
  31. package/src/client.tsx +97 -0
  32. package/src/lib/mail-api.ts +9 -0
  33. package/src/mail/attachment-preview-dialog.tsx +161 -0
  34. package/src/mail/blocked-senders.tsx +138 -0
  35. package/src/mail/compose-window-chrome.tsx +194 -0
  36. package/src/mail/compose-window.tsx +486 -0
  37. package/src/mail/composer-asset-bookmarks.ts +409 -0
  38. package/src/mail/composer-asset.tsx +229 -0
  39. package/src/mail/composer-attachment-list.tsx +138 -0
  40. package/src/mail/composer-editor-document.ts +65 -0
  41. package/src/mail/composer-editor.tsx +284 -0
  42. package/src/mail/composer-linked-attachment-text.ts +12 -0
  43. package/src/mail/composer-recipient-fields.tsx +165 -0
  44. package/src/mail/composer-recipients.ts +208 -0
  45. package/src/mail/composer-session.ts +562 -0
  46. package/src/mail/composer-toolbar.tsx +303 -0
  47. package/src/mail/composer-upload-client.ts +105 -0
  48. package/src/mail/composer-upload-progress.ts +53 -0
  49. package/src/mail/conversation-list.tsx +550 -0
  50. package/src/mail/conversation-loading-skeleton.tsx +119 -0
  51. package/src/mail/conversation-message.tsx +346 -0
  52. package/src/mail/conversation-reply.tsx +470 -0
  53. package/src/mail/conversation-selection-toolbar.tsx +322 -0
  54. package/src/mail/conversation-view.tsx +437 -0
  55. package/src/mail/delivery-indicator.tsx +272 -0
  56. package/src/mail/email-html-body.tsx +203 -0
  57. package/src/mail/folder-glyph.tsx +46 -0
  58. package/src/mail/folder-manager.tsx +532 -0
  59. package/src/mail/format-bytes.ts +7 -0
  60. package/src/mail/mail-app-bar.tsx +253 -0
  61. package/src/mail/mail-background.tsx +12 -0
  62. package/src/mail/mail-dock.tsx +137 -0
  63. package/src/mail/mail-folders.ts +44 -0
  64. package/src/mail/mail-layout.css +236 -0
  65. package/src/mail/mail-location.ts +73 -0
  66. package/src/mail/mail-mailbox-sheet.tsx +275 -0
  67. package/src/mail/mail-nav-rail.tsx +129 -0
  68. package/src/mail/mail-nav.tsx +324 -0
  69. package/src/mail/mail-prefetch.ts +59 -0
  70. package/src/mail/mail-primitives.tsx +66 -0
  71. package/src/mail/mail-queries.ts +209 -0
  72. package/src/mail/mail-query-cache.ts +486 -0
  73. package/src/mail/mail-seed.ts +47 -0
  74. package/src/mail/mail-shell.tsx +126 -0
  75. package/src/mail/mailbox-ai-settings.tsx +185 -0
  76. package/src/mail/mailbox-realtime.tsx +251 -0
  77. package/src/mail/mailbox-route.tsx +15 -0
  78. package/src/mail/message-actions.tsx +353 -0
  79. package/src/mail/message-attachments.tsx +200 -0
  80. package/src/mail/message-format.ts +75 -0
  81. package/src/mail/message-header.tsx +260 -0
  82. package/src/mail/message-translation.tsx +225 -0
  83. package/src/mail/original-message-sheet.tsx +619 -0
  84. package/src/mail/original-message.ts +191 -0
  85. package/src/mail/outbound-notification.ts +53 -0
  86. package/src/mail/pane-splitter.tsx +117 -0
  87. package/src/mail/pdf-preview-toolbar.tsx +152 -0
  88. package/src/mail/pdf-preview.css +44 -0
  89. package/src/mail/pdf-preview.tsx +156 -0
  90. package/src/mail/pdf-translation-layout.ts +95 -0
  91. package/src/mail/print-message.ts +92 -0
  92. package/src/mail/reader-pane.tsx +535 -0
  93. package/src/mail/recipient-combobox.tsx +335 -0
  94. package/src/mail/recipient-field-row.tsx +34 -0
  95. package/src/mail/reply-to-warning.tsx +50 -0
  96. package/src/mail/sanitize-email-html.ts +430 -0
  97. package/src/mail/sender-actions.tsx +149 -0
  98. package/src/mail/sender-authentication.tsx +485 -0
  99. package/src/mail/sender-dialogs.tsx +384 -0
  100. package/src/mail/sender-security.tsx +545 -0
  101. package/src/mail/toolbar-tooltip.tsx +29 -0
  102. package/src/mail/translation-html.ts +49 -0
  103. package/src/mail/translation-language.ts +21 -0
  104. package/src/mail/types.ts +53 -0
  105. package/src/mail/unread-document-indicator.tsx +60 -0
  106. package/src/mail/use-compose-submission.ts +208 -0
  107. package/src/mail/use-composer-attachment-preflight.ts +184 -0
  108. package/src/mail/use-composer-submission.ts +117 -0
  109. package/src/mail/use-conversation-selection.ts +186 -0
  110. package/src/mail/use-mail-controller.ts +427 -0
  111. package/src/mail/use-mail-data.ts +279 -0
  112. package/src/mail/use-mail-safety.ts +155 -0
  113. package/src/mail/use-mail-send.ts +142 -0
  114. package/src/mail/use-pdf-translation-overlay.ts +156 -0
  115. package/src/mail/use-pdf-translation.ts +100 -0
  116. package/src/mail/use-pdf-viewer.ts +215 -0
  117. package/src/mail/use-reply-submission.ts +114 -0
  118. package/src/mail/use-sanitized-email-html.ts +20 -0
  119. package/src/mail/use-visible-message-read.ts +138 -0
  120. package/src/pwa/push-subscription.ts +151 -0
  121. package/src/settings/mail-apps-page.tsx +235 -0
  122. package/src/settings/notifications-page.tsx +291 -0
  123. package/worker/admin/blocked-sender-routes.ts +166 -0
  124. package/worker/admin/schemas.ts +11 -0
  125. package/worker/bindings.d.ts +14 -10
  126. package/worker/db/schema.ts +63 -40
  127. package/worker/jmap/eventsource.ts +84 -16
  128. package/worker/jmap/methods.ts +1630 -524
  129. package/worker/jmap/push-types.ts +27 -0
  130. package/worker/jmap/push.ts +140 -56
  131. package/worker/jmap/routes.ts +366 -105
  132. package/worker/mail/access.ts +258 -21
  133. package/worker/mail/ai-classification.ts +36 -20
  134. package/worker/mail/ai-translation.ts +13 -6
  135. package/worker/mail/auth-headers.ts +395 -137
  136. package/worker/mail/bimi.ts +200 -73
  137. package/worker/mail/bootstrap.ts +79 -0
  138. package/worker/mail/capped-body.ts +36 -0
  139. package/worker/mail/classification-signals.ts +35 -25
  140. package/worker/mail/conversation-cursor.ts +30 -19
  141. package/worker/mail/delivery-events.ts +88 -62
  142. package/worker/mail/delivery-facts.ts +46 -10
  143. package/worker/mail/demo-routes.ts +34 -10
  144. package/worker/mail/doh.ts +62 -23
  145. package/worker/mail/downloads.ts +75 -40
  146. package/worker/mail/edge-cache.ts +155 -0
  147. package/worker/mail/header-metadata.ts +88 -22
  148. package/worker/mail/impersonation.ts +160 -38
  149. package/worker/mail/import-routes.ts +313 -0
  150. package/worker/mail/inbound-delivery.ts +19 -9
  151. package/worker/mail/inbound-threading.ts +18 -8
  152. package/worker/mail/inbound.ts +82 -40
  153. package/worker/mail/ip-intel.ts +110 -27
  154. package/worker/mail/isolate-cache.ts +35 -0
  155. package/worker/mail/link-domains.ts +29 -7
  156. package/worker/mail/mailbox-deletion.ts +10 -4
  157. package/worker/mail/mailbox-directory.ts +16 -6
  158. package/worker/mail/mailing-list.ts +94 -39
  159. package/worker/mail/message-security.ts +90 -15
  160. package/worker/mail/message-view.ts +18 -6
  161. package/worker/mail/office-conversion.ts +31 -9
  162. package/worker/mail/office-preview-container.ts +40 -13
  163. package/worker/mail/office-preview-queue.ts +76 -32
  164. package/worker/mail/org-domain.ts +8 -1
  165. package/worker/mail/outbound-content.ts +71 -26
  166. package/worker/mail/outbound-delivery.ts +31 -17
  167. package/worker/mail/outbound-service.ts +48 -30
  168. package/worker/mail/outbound-threading.ts +30 -17
  169. package/worker/mail/outbound.ts +97 -70
  170. package/worker/mail/outgoing-context.ts +25 -15
  171. package/worker/mail/participants.ts +44 -29
  172. package/worker/mail/push-notifications.ts +271 -173
  173. package/worker/mail/quote-parser.ts +21 -10
  174. package/worker/mail/r2-presigned-urls.ts +31 -25
  175. package/worker/mail/receive.ts +23 -11
  176. package/worker/mail/recipients.ts +6 -1
  177. package/worker/mail/remote-proxy.ts +187 -31
  178. package/worker/mail/remote-routes.ts +87 -0
  179. package/worker/mail/remote-token.ts +104 -0
  180. package/worker/mail/repeat.ts +19 -0
  181. package/worker/mail/reply-plan.ts +62 -33
  182. package/worker/mail/rfc.ts +32 -7
  183. package/worker/mail/routes.ts +870 -541
  184. package/worker/mail/safety-routes.ts +328 -200
  185. package/worker/mail/schemas.ts +84 -105
  186. package/worker/mail/search.ts +1 -0
  187. package/worker/mail/security-settings.ts +41 -9
  188. package/worker/mail/sender-intel.ts +206 -51
  189. package/worker/mail/spamhaus.ts +92 -45
  190. package/worker/mail/unsubscribe.ts +76 -18
  191. package/worker/mail/uploads.ts +126 -93
  192. package/worker/mail/webhook-source.ts +35 -12
  193. package/worker/mail/workspace-safety.ts +97 -38
  194. package/worker/mailbox/demo-html.ts +7 -7
  195. package/worker/mailbox/demo-seed.ts +10 -44
  196. package/worker/mailbox/demo.ts +595 -225
  197. package/worker/mailbox/email-index.ts +53 -0
  198. package/worker/mailbox/folder-model.ts +53 -17
  199. package/worker/mailbox/import-placement.ts +88 -0
  200. package/worker/mailbox/inbox-counts.ts +106 -0
  201. package/worker/mailbox/index.ts +1734 -721
  202. package/worker/mailbox/jmap-store.ts +139 -35
  203. package/worker/mailbox/model.ts +8 -6
  204. package/worker/mailbox/office-preview-jobs.ts +56 -25
  205. package/worker/mailbox/schema.ts +87 -66
  206. package/worker/mailbox/senders.ts +176 -70
  207. package/worker/mcp/mail-action-tools.ts +128 -87
  208. package/worker/mcp/mail-compose-tools.ts +133 -82
  209. package/worker/mcp/mail-query-tools.ts +120 -71
  210. package/worker/mcp/mail-safety-tools.ts +321 -248
  211. package/worker/mcp/schemas.ts +43 -0
  212. package/worker/mcp/tools.ts +13 -10
  213. package/worker/module.ts +43 -18
  214. package/worker/sql.d.ts +8 -0
  215. package/alchemy.ts +0 -142
  216. package/containers/office-preview/Dockerfile +0 -10
  217. package/shared/mail-rules.ts +0 -156
  218. package/worker/admin/report-routes.ts +0 -164
  219. package/worker/mail/reported-messages.ts +0 -68
  220. package/worker/mailbox/migrations.ts +0 -10
  221. package/worker/mailbox/rules.ts +0 -110
@@ -0,0 +1,430 @@
1
+ import DOMPurify from "dompurify";
2
+
3
+ import { isComposerInlineImageContentType } from "../../shared/mail";
4
+ import { isRemoteHttpUrl, mailRemoteProxyPath } from "../../shared/mail-remote";
5
+ import type { MessageAttachment } from "./types";
6
+
7
+ const FORBIDDEN_TAGS = [
8
+ "script",
9
+ "form",
10
+ "input",
11
+ "button",
12
+ "textarea",
13
+ "select",
14
+ "option",
15
+ "meta",
16
+ "link",
17
+ "base",
18
+ "object",
19
+ "embed",
20
+ "iframe",
21
+ "audio",
22
+ "video",
23
+ "source",
24
+ "svg",
25
+ "math",
26
+ ];
27
+
28
+ const FORBIDDEN_ATTRIBUTES = ["poster", "action", "formaction"];
29
+
30
+ const COMPOSER_TAGS = [
31
+ "a",
32
+ "b",
33
+ "blockquote",
34
+ "br",
35
+ "code",
36
+ "div",
37
+ "em",
38
+ "i",
39
+ "img",
40
+ "li",
41
+ "ol",
42
+ "p",
43
+ "pre",
44
+ "s",
45
+ "section",
46
+ "span",
47
+ "strong",
48
+ "u",
49
+ "ul",
50
+ ];
51
+
52
+ const COMPOSER_ATTRIBUTES = [
53
+ "alt",
54
+ "data-forwarded-message",
55
+ "data-linked-attachment-card",
56
+ "href",
57
+ "loading",
58
+ "referrerpolicy",
59
+ "rel",
60
+ "src",
61
+ "srcset",
62
+ "target",
63
+ "title",
64
+ ];
65
+
66
+ const DANGEROUS_CSS =
67
+ /expression\s*\(|-moz-binding|behavior\s*:|@import|javascript\s*:|vbscript\s*:|data\s*:\s*text\/html/giu;
68
+
69
+ /**
70
+ * Keep email layout CSS while blocking scriptable CSS.
71
+ * Remote `url(...)` values are rewritten through the Worker proxy when provided.
72
+ */
73
+ export function sanitizeEmailCss(
74
+ css: string,
75
+ rewriteRemoteUrl?: (url: string) => string | null
76
+ ) {
77
+ const withoutComments = css.replaceAll(/\/\*[\s\S]*?\*\//gu, "");
78
+
79
+ const withoutDanger = withoutComments.replaceAll(
80
+ DANGEROUS_CSS,
81
+ "/* blocked */"
82
+ );
83
+
84
+ return withoutDanger.replaceAll(
85
+ /url\s*\(\s*(['"]?)([^)'"]*?)\1\s*\)/giu,
86
+ (match, _quote: string, rawUrl: string) => {
87
+ const url = rawUrl.trim();
88
+
89
+ if (/^data:image\/(?:gif|jpeg|jpg|png|webp)/iu.test(url)) return match;
90
+
91
+ if (/^cid:/iu.test(url)) return match;
92
+
93
+ if (isRemoteHttpUrl(url) && rewriteRemoteUrl) {
94
+ const proxied = rewriteRemoteUrl(url);
95
+
96
+ if (proxied) return `url("${proxied}")`;
97
+ }
98
+
99
+ return "/* remote url blocked */";
100
+ }
101
+ );
102
+ }
103
+
104
+ function normalizedContentId(value: string) {
105
+ let decoded = value;
106
+
107
+ try {
108
+ decoded = decodeURIComponent(value);
109
+ } catch {
110
+ // Malformed percent escapes are treated as an opaque content ID.
111
+ }
112
+
113
+ return decoded
114
+ .replace(/^cid:/iu, "")
115
+ .replaceAll(/^<|>$/gu, "")
116
+ .trim()
117
+ .toLocaleLowerCase();
118
+ }
119
+
120
+ function safeLink(value: string) {
121
+ try {
122
+ const url = new URL(value);
123
+
124
+ return ["http:", "https:", "mailto:"].includes(url.protocol);
125
+ } catch {
126
+ return false;
127
+ }
128
+ }
129
+
130
+ function inlineAttachmentUrls(
131
+ mailboxId: string,
132
+ messageId: string,
133
+ attachments: MessageAttachment[]
134
+ ) {
135
+ const values = new Map<string, string>();
136
+
137
+ for (const attachment of attachments) {
138
+ if (
139
+ !attachment.contentId ||
140
+ !isComposerInlineImageContentType(attachment.contentType)
141
+ ) {
142
+ continue;
143
+ }
144
+
145
+ values.set(
146
+ normalizedContentId(attachment.contentId),
147
+ `/api/mail/messages/${encodeURIComponent(messageId)}/attachments/${encodeURIComponent(attachment.id)}/inline?mailboxId=${encodeURIComponent(mailboxId)}`
148
+ );
149
+ }
150
+
151
+ return values;
152
+ }
153
+
154
+ function rewriteSrcset(
155
+ value: string,
156
+ rewriteRemoteUrl: (url: string) => string | null
157
+ ) {
158
+ return value
159
+ .split(",")
160
+ .map((part) => {
161
+ const trimmed = part.trim();
162
+
163
+ if (!trimmed) return "";
164
+ const match = /^(\S+)(\s+.+)?$/u.exec(trimmed);
165
+
166
+ if (!match) return "";
167
+ const [, source = "", descriptor = ""] = match;
168
+
169
+ if (!isRemoteHttpUrl(source)) return trimmed;
170
+ const proxied = rewriteRemoteUrl(source);
171
+
172
+ return proxied ? `${proxied}${descriptor}` : "";
173
+ })
174
+ .filter(Boolean)
175
+ .join(", ");
176
+ }
177
+
178
+ function scrubStyles(
179
+ root: ParentNode,
180
+ rewriteRemoteUrl: (url: string) => string | null
181
+ ) {
182
+ for (const element of root.querySelectorAll("[style]")) {
183
+ const style = element.getAttribute("style");
184
+
185
+ if (!style) continue;
186
+ const next = sanitizeEmailCss(style, rewriteRemoteUrl);
187
+
188
+ if (next.trim()) element.setAttribute("style", next);
189
+ else element.removeAttribute("style");
190
+ }
191
+
192
+ for (const styleTag of root.querySelectorAll("style")) {
193
+ styleTag.textContent = sanitizeEmailCss(
194
+ styleTag.textContent ?? "",
195
+ rewriteRemoteUrl
196
+ );
197
+ }
198
+ }
199
+
200
+ function isRichEmailDocument(document: Document) {
201
+ const richElement = document.querySelector(
202
+ [
203
+ "a[href]",
204
+ "img",
205
+ "table",
206
+ "ul",
207
+ "ol",
208
+ "blockquote",
209
+ "pre",
210
+ "code",
211
+ "h1",
212
+ "h2",
213
+ "h3",
214
+ "h4",
215
+ "h5",
216
+ "h6",
217
+ "strong",
218
+ "b",
219
+ "em",
220
+ "i",
221
+ "u",
222
+ "s",
223
+ "mark",
224
+ "sub",
225
+ "sup",
226
+ "hr",
227
+ "[style]",
228
+ "[class]",
229
+ "[bgcolor]",
230
+ "[background]",
231
+ "[color]",
232
+ "[width]",
233
+ "[height]",
234
+ "[align]",
235
+ ].join(",")
236
+ );
237
+
238
+ if (richElement) return true;
239
+
240
+ return [...document.querySelectorAll("style")].some((style) =>
241
+ Boolean(style.textContent?.trim())
242
+ );
243
+ }
244
+
245
+ type SafeEmailDocument = {
246
+ composerHtml: string;
247
+ srcDoc: string;
248
+ proxiedRemoteImages: number;
249
+ renderAsHtml: boolean;
250
+ };
251
+
252
+ /**
253
+ * Message HTML made safe to show: the document for the message's iframe and
254
+ * the stricter HTML the app's own document may hold. Remote images go
255
+ * through the proxy with their token from `remoteTokens`; without one they
256
+ * are dropped.
257
+ */
258
+ export function sanitizeEmailHtml(input: {
259
+ html: string;
260
+ mailboxId: string;
261
+ messageId: string;
262
+ attachments: MessageAttachment[];
263
+ remoteTokens: Record<string, string> | null;
264
+ }): SafeEmailDocument {
265
+ const { remoteTokens } = input;
266
+
267
+ const rewriteRemoteUrl = (url: string) => {
268
+ const token =
269
+ remoteTokens && Object.hasOwn(remoteTokens, url)
270
+ ? remoteTokens[url]
271
+ : undefined;
272
+
273
+ return token ? mailRemoteProxyPath(input.mailboxId, token, url) : null;
274
+ };
275
+
276
+ const sanitized = DOMPurify.sanitize(input.html, {
277
+ USE_PROFILES: { html: true },
278
+ ADD_TAGS: ["style"],
279
+ ADD_ATTR: [
280
+ "style",
281
+ "bgcolor",
282
+ "color",
283
+ "width",
284
+ "height",
285
+ "align",
286
+ "valign",
287
+ "background",
288
+ "srcset",
289
+ ],
290
+ FORBID_TAGS: FORBIDDEN_TAGS,
291
+ FORBID_ATTR: FORBIDDEN_ATTRIBUTES,
292
+ });
293
+
294
+ const document = new DOMParser().parseFromString(sanitized, "text/html");
295
+ scrubStyles(document, rewriteRemoteUrl);
296
+ const renderAsHtml = isRichEmailDocument(document);
297
+
298
+ const inlineUrls = inlineAttachmentUrls(
299
+ input.mailboxId,
300
+ input.messageId,
301
+ input.attachments
302
+ );
303
+
304
+ let proxiedRemoteImages = 0;
305
+
306
+ for (const image of document.querySelectorAll("img")) {
307
+ const source = image.getAttribute("src")?.trim() ?? "";
308
+
309
+ const inlineUrl = source.toLocaleLowerCase().startsWith("cid:")
310
+ ? inlineUrls.get(normalizedContentId(source))
311
+ : undefined;
312
+
313
+ const safeDataImage =
314
+ /^data:image\/(?:gif|jpeg|png|webp);base64,[a-z0-9+/=\s]+$/iu.test(
315
+ source
316
+ );
317
+
318
+ const remoteImage = isRemoteHttpUrl(source)
319
+ ? rewriteRemoteUrl(source)
320
+ : null;
321
+
322
+ if (inlineUrl) {
323
+ image.setAttribute("src", inlineUrl);
324
+ image.setAttribute("loading", "lazy");
325
+ } else if (safeDataImage) {
326
+ // keep as-is
327
+ } else if (remoteImage) {
328
+ image.setAttribute("src", remoteImage);
329
+ image.setAttribute("loading", "lazy");
330
+ image.setAttribute("referrerpolicy", "no-referrer");
331
+ proxiedRemoteImages += 1;
332
+ } else if (source) {
333
+ image.removeAttribute("src");
334
+ }
335
+
336
+ const srcset = image.getAttribute("srcset")?.trim() ?? "";
337
+
338
+ if (srcset) {
339
+ const rewritten = rewriteSrcset(srcset, rewriteRemoteUrl);
340
+
341
+ if (rewritten) image.setAttribute("srcset", rewritten);
342
+ else image.removeAttribute("srcset");
343
+ }
344
+ }
345
+
346
+ for (const element of document.querySelectorAll("[background]")) {
347
+ const background = element.getAttribute("background")?.trim() ?? "";
348
+
349
+ const proxied = isRemoteHttpUrl(background)
350
+ ? rewriteRemoteUrl(background)
351
+ : null;
352
+
353
+ if (proxied) {
354
+ element.setAttribute("background", proxied);
355
+ proxiedRemoteImages += 1;
356
+ } else {
357
+ element.removeAttribute("background");
358
+ }
359
+ }
360
+
361
+ for (const link of document.querySelectorAll("a")) {
362
+ const href = link.getAttribute("href")?.trim() ?? "";
363
+
364
+ if (!safeLink(href)) {
365
+ link.removeAttribute("href");
366
+ continue;
367
+ }
368
+
369
+ if (!href.toLocaleLowerCase().startsWith("mailto:")) {
370
+ link.setAttribute("target", "_blank");
371
+ link.setAttribute("rel", "noopener noreferrer");
372
+ }
373
+ }
374
+
375
+ const headStyles = [...document.querySelectorAll("head style")]
376
+ .map((node) => node.outerHTML)
377
+ .join("\n");
378
+
379
+ const body = document.body.innerHTML;
380
+
381
+ // Composer HTML is rendered in the app document, so sender-controlled CSS,
382
+ // classes, and layout attributes must not cross that boundary. The iframe
383
+ // path below remains intentionally more permissive for external email.
384
+ const composerHtml = DOMPurify.sanitize(body, {
385
+ ALLOWED_TAGS: COMPOSER_TAGS,
386
+ ALLOWED_ATTR: COMPOSER_ATTRIBUTES,
387
+ ALLOW_ARIA_ATTR: false,
388
+ ALLOW_DATA_ATTR: false,
389
+ });
390
+
391
+ // Keep the layout guard after sender styles: email templates commonly use
392
+ // fixed 600px tables that otherwise overflow a narrow message iframe.
393
+ const srcDoc = `<!doctype html>
394
+ <html>
395
+ <head>
396
+ <meta charset="utf-8">
397
+ <meta http-equiv="Content-Security-Policy" content="default-src 'none'; img-src 'self' data:; style-src 'unsafe-inline'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; form-action 'none'; base-uri 'none'">
398
+ <meta name="viewport" content="width=device-width, initial-scale=1">
399
+ <style>
400
+ :root { color-scheme: light only; }
401
+ html, body {
402
+ margin: 0;
403
+ padding: 0;
404
+ background: #ffffff;
405
+ color: #111111;
406
+ }
407
+ body {
408
+ overflow-wrap: anywhere;
409
+ font: 400 15px/1.55 system-ui, -apple-system, sans-serif;
410
+ }
411
+ img { max-width: 100%; height: auto; }
412
+ pre { overflow: auto; white-space: pre-wrap; }
413
+ </style>
414
+ ${headStyles}
415
+ <style>
416
+ html, body {
417
+ width: 100% !important;
418
+ max-width: 100% !important;
419
+ overflow-x: hidden !important;
420
+ }
421
+ *, *::before, *::after { box-sizing: border-box; }
422
+ table { max-width: 100% !important; }
423
+ img { max-width: 100% !important; height: auto !important; }
424
+ </style>
425
+ </head>
426
+ <body>${body}</body>
427
+ </html>`;
428
+
429
+ return { composerHtml, srcDoc, proxiedRemoteImages, renderAsHtml };
430
+ }
@@ -0,0 +1,149 @@
1
+ import { useState } from "react";
2
+ import { toast } from "sonner";
3
+
4
+ import {
5
+ BlockSenderDialog,
6
+ senderLabel,
7
+ UnsubscribeDialog,
8
+ } from "./sender-dialogs";
9
+ import type { Mailbox, MessageDetail } from "./types";
10
+ import {
11
+ useNotSpam,
12
+ useReportMessage,
13
+ useTrustSender,
14
+ useUnblockSender,
15
+ } from "./use-mail-safety";
16
+
17
+ type SenderDialogKind = "unsubscribe" | "block";
18
+
19
+ /**
20
+ * Sender-level actions (unsubscribe, block, report, trust) and their
21
+ * confirmation dialogs, shared by the message menus and the command bar.
22
+ */
23
+ export function useSenderActions({
24
+ mailbox,
25
+ onLeaveConversation,
26
+ }: {
27
+ mailbox?: Mailbox;
28
+ /** Called after an action moved the open conversation out of the current folder. */
29
+ onLeaveConversation: () => void;
30
+ }) {
31
+ const [dialog, setDialog] = useState<{
32
+ kind: SenderDialogKind;
33
+ message: MessageDetail;
34
+ open: boolean;
35
+ } | null>(null);
36
+
37
+ const report = useReportMessage();
38
+ const unblock = useUnblockSender();
39
+ const trust = useTrustSender();
40
+ const notSpam = useNotSpam();
41
+
42
+ function setOpen(open: boolean) {
43
+ setDialog((current) => current && { ...current, open });
44
+ }
45
+
46
+ function reportSpam(message: MessageDetail) {
47
+ if (!mailbox) return;
48
+ report.mutate(
49
+ { mailboxId: mailbox.id, messageId: message.id },
50
+ {
51
+ onSuccess: (result) => {
52
+ toast.success("Reported as spam");
53
+
54
+ if (result.moved) onLeaveConversation();
55
+ },
56
+ onError: (error) => toast.error(error.message),
57
+ }
58
+ );
59
+ }
60
+
61
+ function unblockSender(message: MessageDetail) {
62
+ const block = message.sender?.block;
63
+
64
+ if (!mailbox || !block || block.workspace) return;
65
+ unblock.mutate(
66
+ { mailboxId: mailbox.id, pattern: block.pattern },
67
+ {
68
+ onSuccess: () => toast.success(`Unblocked ${block.pattern}`),
69
+ onError: (error) => toast.error(error.message),
70
+ }
71
+ );
72
+ }
73
+
74
+ function trustSender(message: MessageDetail) {
75
+ if (!mailbox) return;
76
+ trust.mutate(
77
+ { mailboxId: mailbox.id, address: message.fromAddress, trusted: true },
78
+ {
79
+ onSuccess: () =>
80
+ toast.success(`${senderLabel(message)} marked as safe`),
81
+ onError: (error) => toast.error(error.message),
82
+ }
83
+ );
84
+ }
85
+
86
+ function markNotSpam(message: MessageDetail) {
87
+ if (!mailbox) return;
88
+ notSpam.mutate(
89
+ { mailboxId: mailbox.id, messageId: message.id },
90
+ {
91
+ onSuccess: () => {
92
+ toast.success(
93
+ `Moved to Inbox. Mail from ${message.fromAddress} won't be treated as spam.`
94
+ );
95
+ onLeaveConversation();
96
+ },
97
+ onError: (error) => toast.error(error.message),
98
+ }
99
+ );
100
+ }
101
+
102
+ const dialogs =
103
+ dialog && mailbox ? (
104
+ <>
105
+ {dialog.kind === "unsubscribe" && (
106
+ <UnsubscribeDialog
107
+ message={dialog.message}
108
+ mailbox={mailbox}
109
+ open={dialog.open}
110
+ onOpenChange={setOpen}
111
+ />
112
+ )}
113
+ {dialog.kind === "block" && (
114
+ <BlockSenderDialog
115
+ message={dialog.message}
116
+ mailbox={mailbox}
117
+ open={dialog.open}
118
+ onOpenChange={setOpen}
119
+ onMoved={onLeaveConversation}
120
+ />
121
+ )}
122
+ </>
123
+ ) : null;
124
+
125
+ return {
126
+ open: (kind: SenderDialogKind, message: MessageDetail) =>
127
+ setDialog({ kind, message, open: true }),
128
+ reportSpam,
129
+ unblockSender,
130
+ trustSender,
131
+ markNotSpam,
132
+ pending:
133
+ report.isPending ||
134
+ unblock.isPending ||
135
+ trust.isPending ||
136
+ notSpam.isPending,
137
+ dialogs,
138
+ };
139
+ }
140
+
141
+ export type SenderActions = ReturnType<typeof useSenderActions>;
142
+
143
+ export function canUnsubscribe(message: MessageDetail) {
144
+ const methods = message.security?.listUnsubscribe;
145
+
146
+ return Boolean(
147
+ message.direction === "incoming" && (methods?.httpsUrl || methods?.mailto)
148
+ );
149
+ }