@open-workspace/email 0.3.0 → 0.4.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/README.md +22 -22
- package/alchemy/index.ts +273 -0
- package/drizzle/mailbox/0000_puzzling_dracula.sql +149 -0
- package/drizzle/mailbox/0001_mute_jack_flag.sql +12 -0
- package/drizzle/mailbox/meta/0000_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/0001_snapshot.json +1002 -0
- package/drizzle/mailbox/meta/_journal.json +20 -0
- package/drizzle/mailbox/migrations.js +12 -0
- package/index.ts +3 -1
- package/migrations/{0000_email.sql → 0000_dry_moon_knight.sql} +26 -20
- package/migrations/meta/0000_snapshot.json +125 -109
- package/migrations/meta/_journal.json +2 -2
- package/package.json +74 -68
- package/shared/attachment-preview.ts +37 -27
- package/shared/folder-order.ts +106 -0
- package/shared/mail-authentication.ts +8 -3
- package/shared/mail-feedback.ts +18 -25
- package/shared/mail-headers.ts +114 -39
- package/shared/mail-import.ts +38 -0
- package/shared/mail-path.ts +42 -0
- package/shared/mail-received.ts +174 -31
- package/shared/mail-remote.ts +59 -2
- package/shared/mail-sender-intel.ts +22 -4
- package/shared/mail.ts +77 -28
- package/shared/module.ts +33 -0
- package/shared/office-preview.ts +14 -2
- package/shared/pdfjs-assets.ts +35 -0
- package/shared/permissions.ts +73 -0
- package/shared/webhooks.ts +41 -0
- package/src/admin/blocked-senders.tsx +189 -0
- package/src/client.tsx +97 -0
- package/src/lib/mail-api.ts +9 -0
- package/src/mail/attachment-preview-dialog.tsx +161 -0
- package/src/mail/blocked-senders.tsx +138 -0
- package/src/mail/compose-window-chrome.tsx +194 -0
- package/src/mail/compose-window.tsx +486 -0
- package/src/mail/composer-asset-bookmarks.ts +409 -0
- package/src/mail/composer-asset.tsx +229 -0
- package/src/mail/composer-attachment-list.tsx +138 -0
- package/src/mail/composer-editor-document.ts +65 -0
- package/src/mail/composer-editor.tsx +284 -0
- package/src/mail/composer-linked-attachment-text.ts +12 -0
- package/src/mail/composer-recipient-fields.tsx +165 -0
- package/src/mail/composer-recipients.ts +208 -0
- package/src/mail/composer-session.ts +562 -0
- package/src/mail/composer-toolbar.tsx +303 -0
- package/src/mail/composer-upload-client.ts +105 -0
- package/src/mail/composer-upload-progress.ts +53 -0
- package/src/mail/conversation-list.tsx +550 -0
- package/src/mail/conversation-loading-skeleton.tsx +119 -0
- package/src/mail/conversation-message.tsx +346 -0
- package/src/mail/conversation-reply.tsx +470 -0
- package/src/mail/conversation-selection-toolbar.tsx +322 -0
- package/src/mail/conversation-view.tsx +437 -0
- package/src/mail/delivery-indicator.tsx +272 -0
- package/src/mail/email-html-body.tsx +203 -0
- package/src/mail/folder-glyph.tsx +46 -0
- package/src/mail/folder-manager.tsx +532 -0
- package/src/mail/format-bytes.ts +7 -0
- package/src/mail/mail-app-bar.tsx +253 -0
- package/src/mail/mail-background.tsx +12 -0
- package/src/mail/mail-dock.tsx +137 -0
- package/src/mail/mail-folders.ts +44 -0
- package/src/mail/mail-layout.css +236 -0
- package/src/mail/mail-location.ts +73 -0
- package/src/mail/mail-mailbox-sheet.tsx +275 -0
- package/src/mail/mail-nav-rail.tsx +129 -0
- package/src/mail/mail-nav.tsx +324 -0
- package/src/mail/mail-prefetch.ts +59 -0
- package/src/mail/mail-primitives.tsx +66 -0
- package/src/mail/mail-queries.ts +209 -0
- package/src/mail/mail-query-cache.ts +486 -0
- package/src/mail/mail-seed.ts +47 -0
- package/src/mail/mail-shell.tsx +126 -0
- package/src/mail/mailbox-ai-settings.tsx +185 -0
- package/src/mail/mailbox-realtime.tsx +251 -0
- package/src/mail/mailbox-route.tsx +15 -0
- package/src/mail/message-actions.tsx +353 -0
- package/src/mail/message-attachments.tsx +200 -0
- package/src/mail/message-format.ts +75 -0
- package/src/mail/message-header.tsx +260 -0
- package/src/mail/message-translation.tsx +225 -0
- package/src/mail/original-message-sheet.tsx +619 -0
- package/src/mail/original-message.ts +191 -0
- package/src/mail/outbound-notification.ts +53 -0
- package/src/mail/pane-splitter.tsx +117 -0
- package/src/mail/pdf-preview-toolbar.tsx +152 -0
- package/src/mail/pdf-preview.css +44 -0
- package/src/mail/pdf-preview.tsx +156 -0
- package/src/mail/pdf-translation-layout.ts +95 -0
- package/src/mail/print-message.ts +92 -0
- package/src/mail/reader-pane.tsx +535 -0
- package/src/mail/recipient-combobox.tsx +335 -0
- package/src/mail/recipient-field-row.tsx +34 -0
- package/src/mail/reply-to-warning.tsx +50 -0
- package/src/mail/sanitize-email-html.ts +430 -0
- package/src/mail/sender-actions.tsx +149 -0
- package/src/mail/sender-authentication.tsx +485 -0
- package/src/mail/sender-dialogs.tsx +384 -0
- package/src/mail/sender-security.tsx +545 -0
- package/src/mail/toolbar-tooltip.tsx +29 -0
- package/src/mail/translation-html.ts +49 -0
- package/src/mail/translation-language.ts +21 -0
- package/src/mail/types.ts +53 -0
- package/src/mail/unread-document-indicator.tsx +60 -0
- package/src/mail/use-compose-submission.ts +208 -0
- package/src/mail/use-composer-attachment-preflight.ts +184 -0
- package/src/mail/use-composer-submission.ts +117 -0
- package/src/mail/use-conversation-selection.ts +186 -0
- package/src/mail/use-mail-controller.ts +427 -0
- package/src/mail/use-mail-data.ts +279 -0
- package/src/mail/use-mail-safety.ts +155 -0
- package/src/mail/use-mail-send.ts +142 -0
- package/src/mail/use-pdf-translation-overlay.ts +156 -0
- package/src/mail/use-pdf-translation.ts +100 -0
- package/src/mail/use-pdf-viewer.ts +215 -0
- package/src/mail/use-reply-submission.ts +114 -0
- package/src/mail/use-sanitized-email-html.ts +20 -0
- package/src/mail/use-visible-message-read.ts +138 -0
- package/src/pwa/push-subscription.ts +151 -0
- package/src/settings/mail-apps-page.tsx +235 -0
- package/src/settings/notifications-page.tsx +291 -0
- package/worker/admin/blocked-sender-routes.ts +166 -0
- package/worker/admin/schemas.ts +11 -0
- package/worker/bindings.d.ts +14 -10
- package/worker/db/schema.ts +63 -40
- package/worker/jmap/eventsource.ts +84 -16
- package/worker/jmap/methods.ts +1630 -524
- package/worker/jmap/push-types.ts +27 -0
- package/worker/jmap/push.ts +140 -56
- package/worker/jmap/routes.ts +366 -105
- package/worker/mail/access.ts +258 -21
- package/worker/mail/ai-classification.ts +36 -20
- package/worker/mail/ai-translation.ts +13 -6
- package/worker/mail/auth-headers.ts +395 -137
- package/worker/mail/bimi.ts +200 -73
- package/worker/mail/bootstrap.ts +79 -0
- package/worker/mail/capped-body.ts +36 -0
- package/worker/mail/classification-signals.ts +35 -25
- package/worker/mail/conversation-cursor.ts +30 -19
- package/worker/mail/delivery-events.ts +88 -62
- package/worker/mail/delivery-facts.ts +46 -10
- package/worker/mail/demo-routes.ts +34 -10
- package/worker/mail/doh.ts +62 -23
- package/worker/mail/downloads.ts +75 -40
- package/worker/mail/edge-cache.ts +155 -0
- package/worker/mail/header-metadata.ts +88 -22
- package/worker/mail/impersonation.ts +160 -38
- package/worker/mail/import-routes.ts +313 -0
- package/worker/mail/inbound-delivery.ts +19 -9
- package/worker/mail/inbound-threading.ts +18 -8
- package/worker/mail/inbound.ts +82 -40
- package/worker/mail/ip-intel.ts +110 -27
- package/worker/mail/isolate-cache.ts +35 -0
- package/worker/mail/link-domains.ts +29 -7
- package/worker/mail/mailbox-deletion.ts +10 -4
- package/worker/mail/mailbox-directory.ts +16 -6
- package/worker/mail/mailing-list.ts +94 -39
- package/worker/mail/message-security.ts +90 -15
- package/worker/mail/message-view.ts +18 -6
- package/worker/mail/office-conversion.ts +31 -9
- package/worker/mail/office-preview-container.ts +40 -13
- package/worker/mail/office-preview-queue.ts +76 -32
- package/worker/mail/org-domain.ts +8 -1
- package/worker/mail/outbound-content.ts +71 -26
- package/worker/mail/outbound-delivery.ts +31 -17
- package/worker/mail/outbound-service.ts +48 -30
- package/worker/mail/outbound-threading.ts +30 -17
- package/worker/mail/outbound.ts +97 -70
- package/worker/mail/outgoing-context.ts +25 -15
- package/worker/mail/participants.ts +44 -29
- package/worker/mail/push-notifications.ts +271 -173
- package/worker/mail/quote-parser.ts +21 -10
- package/worker/mail/r2-presigned-urls.ts +31 -25
- package/worker/mail/receive.ts +23 -11
- package/worker/mail/recipients.ts +6 -1
- package/worker/mail/remote-proxy.ts +187 -31
- package/worker/mail/remote-routes.ts +87 -0
- package/worker/mail/remote-token.ts +104 -0
- package/worker/mail/repeat.ts +19 -0
- package/worker/mail/reply-plan.ts +62 -33
- package/worker/mail/rfc.ts +32 -7
- package/worker/mail/routes.ts +870 -541
- package/worker/mail/safety-routes.ts +328 -200
- package/worker/mail/schemas.ts +84 -105
- package/worker/mail/search.ts +1 -0
- package/worker/mail/security-settings.ts +41 -9
- package/worker/mail/sender-intel.ts +206 -51
- package/worker/mail/spamhaus.ts +92 -45
- package/worker/mail/unsubscribe.ts +76 -18
- package/worker/mail/uploads.ts +126 -93
- package/worker/mail/webhook-source.ts +35 -12
- package/worker/mail/workspace-safety.ts +97 -38
- package/worker/mailbox/demo-html.ts +7 -7
- package/worker/mailbox/demo-seed.ts +10 -44
- package/worker/mailbox/demo.ts +595 -225
- package/worker/mailbox/email-index.ts +53 -0
- package/worker/mailbox/folder-model.ts +53 -17
- package/worker/mailbox/import-placement.ts +88 -0
- package/worker/mailbox/inbox-counts.ts +106 -0
- package/worker/mailbox/index.ts +1734 -721
- package/worker/mailbox/jmap-store.ts +139 -35
- package/worker/mailbox/model.ts +8 -6
- package/worker/mailbox/office-preview-jobs.ts +56 -25
- package/worker/mailbox/schema.ts +87 -66
- package/worker/mailbox/senders.ts +176 -70
- package/worker/mcp/mail-action-tools.ts +128 -87
- package/worker/mcp/mail-compose-tools.ts +133 -82
- package/worker/mcp/mail-query-tools.ts +120 -71
- package/worker/mcp/mail-safety-tools.ts +321 -248
- package/worker/mcp/schemas.ts +43 -0
- package/worker/mcp/tools.ts +13 -10
- package/worker/module.ts +43 -18
- package/worker/sql.d.ts +8 -0
- package/alchemy.ts +0 -142
- package/containers/office-preview/Dockerfile +0 -10
- package/shared/mail-rules.ts +0 -156
- package/worker/admin/report-routes.ts +0 -164
- package/worker/mail/reported-messages.ts +0 -68
- package/worker/mailbox/migrations.ts +0 -10
- package/worker/mailbox/rules.ts +0 -110
|
@@ -15,36 +15,47 @@ const ATTRIBUTION_LINE = [
|
|
|
15
15
|
const OUTLOOK_HEADER = /^from:\s+.+$/iu;
|
|
16
16
|
const OUTLOOK_FOLLOWUP = /^(?:sent|date|to|subject):\s+.+$/iu;
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
/** Whether `text` starts a quote: an attribution line, or an Outlook header block (with the lines after it). */
|
|
19
|
+
function isAttribution(text: string, following: string[]) {
|
|
20
|
+
const line = text.trim();
|
|
21
|
+
|
|
20
22
|
if (ATTRIBUTION_LINE.some((pattern) => pattern.test(line))) return true;
|
|
23
|
+
|
|
21
24
|
if (!OUTLOOK_HEADER.test(line)) return false;
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
|
|
26
|
+
return following
|
|
27
|
+
.slice(0, 4)
|
|
24
28
|
.some((candidate) => OUTLOOK_FOLLOWUP.test(candidate.trim()));
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
function quotedBlockStart(lines: string[]) {
|
|
28
|
-
for (
|
|
29
|
-
if (isAttribution(
|
|
32
|
+
for (const [index, text] of lines.entries()) {
|
|
33
|
+
if (isAttribution(text, lines.slice(index + 1))) return index;
|
|
30
34
|
}
|
|
31
|
-
|
|
32
|
-
|
|
35
|
+
|
|
36
|
+
for (const [index, text] of lines.entries()) {
|
|
37
|
+
if (!/^\s*>/u.test(text)) continue;
|
|
33
38
|
const remaining = lines.slice(index).filter((line) => line.trim());
|
|
34
39
|
const quoted = remaining.filter((line) => /^\s*>/u.test(line)).length;
|
|
40
|
+
|
|
35
41
|
if (remaining.length && quoted / remaining.length >= 0.6) return index;
|
|
36
42
|
}
|
|
43
|
+
|
|
37
44
|
return -1;
|
|
38
45
|
}
|
|
39
46
|
|
|
40
|
-
export function parseReplyText(
|
|
47
|
+
export function parseReplyText(
|
|
48
|
+
value: string | undefined | null
|
|
49
|
+
): ParsedReplyText {
|
|
41
50
|
const normalized = (value ?? "").replaceAll("\r\n", "\n").trim();
|
|
51
|
+
|
|
42
52
|
if (!normalized) return { bodyText: "", quotedText: null };
|
|
43
53
|
const lines = normalized.split("\n");
|
|
44
54
|
const splitAt = quotedBlockStart(lines);
|
|
55
|
+
|
|
45
56
|
if (splitAt < 0) return { bodyText: normalized, quotedText: null };
|
|
46
57
|
const bodyText = lines.slice(0, splitAt).join("\n").trim();
|
|
47
58
|
const quotedText = lines.slice(splitAt).join("\n").trim();
|
|
59
|
+
|
|
48
60
|
return { bodyText, quotedText: quotedText || null };
|
|
49
61
|
}
|
|
50
|
-
|
|
@@ -1,32 +1,41 @@
|
|
|
1
|
+
import type { AppEnv } from "@open-workspace/core/module";
|
|
1
2
|
import { AwsClient } from "aws4fetch";
|
|
2
|
-
import
|
|
3
|
+
import { z } from "zod";
|
|
3
4
|
|
|
4
5
|
type Bindings = AppEnv["Bindings"];
|
|
5
6
|
|
|
6
7
|
const PRESIGN_TTL_SECONDS = 15 * 60;
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
9
|
+
const MailS3 = z.object({
|
|
10
|
+
endpoint: z.string(),
|
|
11
|
+
bucketName: z.string(),
|
|
12
|
+
region: z.string(),
|
|
13
|
+
accessKeyId: z.string(),
|
|
14
|
+
secretAccessKey: z.string(),
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
/** The mail bucket's S3 endpoint, name and credentials (`MAIL_S3`, from Alchemy's `S3Credentials`). */
|
|
18
|
+
export function mailS3(env: Pick<Bindings, "MAIL_S3">) {
|
|
19
|
+
return MailS3.parse(JSON.parse(env.MAIL_S3));
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
function r2Signer(env: Bindings, r2Key: string) {
|
|
18
|
-
const
|
|
19
|
-
|
|
23
|
+
const s3 = mailS3(env);
|
|
24
|
+
|
|
25
|
+
const url = new URL(
|
|
26
|
+
`${s3.endpoint}/${encodeURIComponent(s3.bucketName)}/${r2Key}`
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
url.searchParams.set("X-Amz-Expires", String(PRESIGN_TTL_SECONDS));
|
|
30
|
+
|
|
20
31
|
return {
|
|
21
32
|
client: new AwsClient({
|
|
22
|
-
accessKeyId:
|
|
23
|
-
secretAccessKey:
|
|
33
|
+
accessKeyId: s3.accessKeyId,
|
|
34
|
+
secretAccessKey: s3.secretAccessKey,
|
|
24
35
|
service: "s3",
|
|
25
|
-
region:
|
|
36
|
+
region: s3.region,
|
|
26
37
|
}),
|
|
27
|
-
url
|
|
28
|
-
`https://${credentials.accountId}.r2.cloudflarestorage.com/${credentials.bucketName}/${r2Key}`,
|
|
29
|
-
),
|
|
38
|
+
url,
|
|
30
39
|
};
|
|
31
40
|
}
|
|
32
41
|
|
|
@@ -36,8 +45,7 @@ export async function signR2PutUrl(input: {
|
|
|
36
45
|
contentType: string;
|
|
37
46
|
}) {
|
|
38
47
|
const signer = r2Signer(input.env, input.r2Key);
|
|
39
|
-
|
|
40
|
-
signer.url.searchParams.set("X-Amz-Expires", String(PRESIGN_TTL_SECONDS));
|
|
48
|
+
|
|
41
49
|
const signed = await signer.client.sign(signer.url, {
|
|
42
50
|
method: "PUT",
|
|
43
51
|
headers: {
|
|
@@ -48,22 +56,20 @@ export async function signR2PutUrl(input: {
|
|
|
48
56
|
signQuery: true,
|
|
49
57
|
},
|
|
50
58
|
});
|
|
59
|
+
|
|
51
60
|
return signed.url;
|
|
52
61
|
}
|
|
53
62
|
|
|
54
|
-
export async function signR2GetUrl(input: {
|
|
55
|
-
env: Bindings;
|
|
56
|
-
r2Key: string;
|
|
57
|
-
}) {
|
|
63
|
+
export async function signR2GetUrl(input: { env: Bindings; r2Key: string }) {
|
|
58
64
|
const signer = r2Signer(input.env, input.r2Key);
|
|
59
|
-
|
|
60
|
-
signer.url.searchParams.set("X-Amz-Expires", String(PRESIGN_TTL_SECONDS));
|
|
65
|
+
|
|
61
66
|
const signed = await signer.client.sign(signer.url, {
|
|
62
67
|
method: "GET",
|
|
63
68
|
aws: { signQuery: true },
|
|
64
69
|
});
|
|
70
|
+
|
|
65
71
|
return {
|
|
66
72
|
url: signed.url,
|
|
67
|
-
expiresAt: Date.now() + PRESIGN_TTL_SECONDS *
|
|
73
|
+
expiresAt: Date.now() + PRESIGN_TTL_SECONDS * 1000,
|
|
68
74
|
};
|
|
69
75
|
}
|
package/worker/mail/receive.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import {
|
|
2
|
+
createDb,
|
|
3
|
+
mailboxAddressPredicate,
|
|
4
|
+
mailboxes,
|
|
5
|
+
normalizeMailboxAddress,
|
|
6
|
+
} from "@open-workspace/core/module";
|
|
7
|
+
|
|
5
8
|
import { mailboxStub } from "../mailbox";
|
|
6
9
|
import { inboundDeliveryId } from "./inbound-delivery";
|
|
7
10
|
import { deliverToList } from "./mailing-list";
|
|
@@ -10,6 +13,7 @@ import { deliverToList } from "./mailing-list";
|
|
|
10
13
|
export async function receiveEmail(message: ForwardableEmailMessage, env: Env) {
|
|
11
14
|
const receivedAt = Date.now();
|
|
12
15
|
const recipient = normalizeMailboxAddress(message.to);
|
|
16
|
+
|
|
13
17
|
const [mailbox] = await createDb(env.DB)
|
|
14
18
|
.select({
|
|
15
19
|
id: mailboxes.id,
|
|
@@ -18,30 +22,40 @@ export async function receiveEmail(message: ForwardableEmailMessage, env: Env) {
|
|
|
18
22
|
listPosting: mailboxes.listPosting,
|
|
19
23
|
})
|
|
20
24
|
.from(mailboxes)
|
|
21
|
-
.
|
|
25
|
+
.where(mailboxAddressPredicate(recipient))
|
|
22
26
|
.limit(1);
|
|
27
|
+
|
|
23
28
|
if (!mailbox) {
|
|
24
29
|
message.setReject(`Mailbox ${recipient} does not exist`);
|
|
30
|
+
|
|
25
31
|
return;
|
|
26
32
|
}
|
|
27
33
|
|
|
28
34
|
const raw = await new Response(message.raw).arrayBuffer();
|
|
35
|
+
|
|
29
36
|
if (mailbox.delivery === "list" && mailbox.teamId) {
|
|
30
37
|
await deliverToList({
|
|
31
38
|
env,
|
|
32
|
-
list: {
|
|
39
|
+
list: {
|
|
40
|
+
id: mailbox.id,
|
|
41
|
+
teamId: mailbox.teamId,
|
|
42
|
+
listPosting: mailbox.listPosting,
|
|
43
|
+
},
|
|
33
44
|
message,
|
|
34
45
|
raw,
|
|
35
46
|
receivedAt,
|
|
36
47
|
});
|
|
48
|
+
|
|
37
49
|
return;
|
|
38
50
|
}
|
|
51
|
+
|
|
39
52
|
const deliveryId = await inboundDeliveryId({
|
|
40
53
|
mailboxId: mailbox.id,
|
|
41
54
|
envelopeFrom: message.from,
|
|
42
55
|
envelopeTo: message.to,
|
|
43
56
|
raw,
|
|
44
57
|
});
|
|
58
|
+
|
|
45
59
|
const objectKey = `mailboxes/${mailbox.id}/raw/${deliveryId}.eml`;
|
|
46
60
|
await mailboxStub(env, mailbox.id).enqueueInbound({
|
|
47
61
|
id: deliveryId,
|
|
@@ -51,9 +65,7 @@ export async function receiveEmail(message: ForwardableEmailMessage, env: Env) {
|
|
|
51
65
|
envelopeTo: message.to,
|
|
52
66
|
receivedAt,
|
|
53
67
|
});
|
|
54
|
-
await env.MAIL_STORAGE.put(
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
{ httpMetadata: { contentType: "message/rfc822" } },
|
|
58
|
-
);
|
|
68
|
+
await env.MAIL_STORAGE.put(objectKey, raw, {
|
|
69
|
+
httpMetadata: { contentType: "message/rfc822" },
|
|
70
|
+
});
|
|
59
71
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { normalizeEmail } from "@open-workspace/core/
|
|
1
|
+
import { normalizeEmail } from "@open-workspace/core/module";
|
|
2
2
|
|
|
3
3
|
export type RecipientFields = {
|
|
4
4
|
to: string[];
|
|
@@ -12,16 +12,21 @@ export type RecipientFields = {
|
|
|
12
12
|
*/
|
|
13
13
|
export function dedupeRecipientFields(input: RecipientFields): RecipientFields {
|
|
14
14
|
const seen = new Set<string>();
|
|
15
|
+
|
|
15
16
|
const take = (values: string[]) => {
|
|
16
17
|
const result: string[] = [];
|
|
18
|
+
|
|
17
19
|
for (const value of values) {
|
|
18
20
|
const normalized = normalizeEmail(value);
|
|
21
|
+
|
|
19
22
|
if (!normalized || seen.has(normalized)) continue;
|
|
20
23
|
seen.add(normalized);
|
|
21
24
|
result.push(normalized);
|
|
22
25
|
}
|
|
26
|
+
|
|
23
27
|
return result;
|
|
24
28
|
};
|
|
29
|
+
|
|
25
30
|
return {
|
|
26
31
|
to: take(input.to),
|
|
27
32
|
cc: take(input.cc),
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
+
import { required } from "@open-workspace/core/shared";
|
|
2
|
+
|
|
3
|
+
import { repeatUntil } from "./repeat";
|
|
4
|
+
|
|
1
5
|
const BLOCKED_HOST_SUFFIXES = [".localhost", ".local", ".internal"];
|
|
6
|
+
|
|
2
7
|
const BLOCKED_HOSTS = new Set([
|
|
3
8
|
"localhost",
|
|
4
9
|
"metadata.google.internal",
|
|
5
10
|
"metadata",
|
|
6
11
|
]);
|
|
7
12
|
|
|
8
|
-
const PRIVATE_IPV4 =
|
|
9
|
-
/^(?:0\.|10\.|127\.|169\.254\.|172\.(?:1[6-9]|2\d|3[01])\.|192\.168\.|100\.(?:6[4-9]|[7-9]\d|1[01]\d|12[0-7])\.)/u;
|
|
10
|
-
|
|
11
13
|
export const PROXYABLE_REMOTE_MEDIA_TYPES = new Set([
|
|
12
14
|
"image/apng",
|
|
13
15
|
"image/avif",
|
|
@@ -22,60 +24,198 @@ export const MAX_REMOTE_PROXY_BYTES = 5 * 1024 * 1024;
|
|
|
22
24
|
export const REMOTE_PROXY_TIMEOUT_MS = 12_000;
|
|
23
25
|
export const REMOTE_PROXY_MAX_REDIRECTS = 3;
|
|
24
26
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
/** An IPv4 address as a 32-bit number, or null. The URL parser has already turned 0x7f.1 and 2130706433 into dotted form. */
|
|
28
|
+
function ipv4Number(hostname: string) {
|
|
29
|
+
const parts = hostname.split(".");
|
|
30
|
+
|
|
31
|
+
if (
|
|
32
|
+
parts.length !== 4 ||
|
|
33
|
+
!parts.every((part) => /^\d{1,3}$/u.test(part) && Number(part) <= 255)
|
|
34
|
+
) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return parts.reduce((value, part) => value * 256 + Number(part), 0);
|
|
27
39
|
}
|
|
28
40
|
|
|
29
|
-
|
|
30
|
-
|
|
41
|
+
/** IPv4 networks a proxied fetch must never reach: private, loopback, link-local, shared, reserved, multicast. */
|
|
42
|
+
const BLOCKED_IPV4: [network: string, bits: number][] = [
|
|
43
|
+
["0.0.0.0", 8],
|
|
44
|
+
["10.0.0.0", 8],
|
|
45
|
+
["100.64.0.0", 10],
|
|
46
|
+
["127.0.0.0", 8],
|
|
47
|
+
["169.254.0.0", 16],
|
|
48
|
+
["172.16.0.0", 12],
|
|
49
|
+
["192.0.0.0", 24],
|
|
50
|
+
["192.0.2.0", 24],
|
|
51
|
+
["192.88.99.0", 24],
|
|
52
|
+
["192.168.0.0", 16],
|
|
53
|
+
["198.18.0.0", 15],
|
|
54
|
+
["198.51.100.0", 24],
|
|
55
|
+
["203.0.113.0", 24],
|
|
56
|
+
["224.0.0.0", 4],
|
|
57
|
+
["240.0.0.0", 4],
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
const BLOCKED_IPV4_RANGES = BLOCKED_IPV4.map(([network, bits]) => ({
|
|
61
|
+
start: required(ipv4Number(network), `${network} is not an IPv4 address`),
|
|
62
|
+
size: 2 ** (32 - bits),
|
|
63
|
+
}));
|
|
64
|
+
|
|
65
|
+
function isBlockedIpv4(address: number) {
|
|
66
|
+
return BLOCKED_IPV4_RANGES.some(
|
|
67
|
+
({ start, size }) => address >= start && address < start + size
|
|
68
|
+
);
|
|
31
69
|
}
|
|
32
70
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
71
|
+
type Ipv6Groups = [
|
|
72
|
+
number,
|
|
73
|
+
number,
|
|
74
|
+
number,
|
|
75
|
+
number,
|
|
76
|
+
number,
|
|
77
|
+
number,
|
|
78
|
+
number,
|
|
79
|
+
number,
|
|
80
|
+
];
|
|
81
|
+
|
|
82
|
+
/** The eight 16-bit groups of an IPv6 address (brackets, a zone, an IPv4 tail allowed), or null. */
|
|
83
|
+
function ipv6Groups(hostname: string): Ipv6Groups | null {
|
|
84
|
+
let value = hostname
|
|
85
|
+
.toLowerCase()
|
|
86
|
+
.replaceAll(/^\[|\]$/gu, "")
|
|
87
|
+
.split("%")[0];
|
|
88
|
+
|
|
89
|
+
const tail = /:(\d+\.\d+\.\d+\.\d+)$/u.exec(value);
|
|
90
|
+
|
|
91
|
+
if (tail) {
|
|
92
|
+
const v4 = ipv4Number(tail[1]);
|
|
93
|
+
|
|
94
|
+
if (v4 === null) return null;
|
|
95
|
+
value = `${value.slice(0, tail.index)}:${(v4 >>> 16).toString(16)}:${(v4 & 0xff_ff).toString(16)}`;
|
|
52
96
|
}
|
|
97
|
+
|
|
98
|
+
const halves = value.split("::");
|
|
99
|
+
|
|
100
|
+
if (halves.length > 2) return null;
|
|
101
|
+
const head = halves[0] ? halves[0].split(":") : [];
|
|
102
|
+
const rest = halves.length === 2 && halves[1] ? halves[1].split(":") : [];
|
|
103
|
+
const missing = 8 - head.length - rest.length;
|
|
104
|
+
|
|
105
|
+
if (missing < 0 || (halves.length === 1 && missing !== 0)) return null;
|
|
106
|
+
|
|
107
|
+
const groups = [
|
|
108
|
+
...head,
|
|
109
|
+
...Array.from({ length: missing }, () => "0"),
|
|
110
|
+
...rest,
|
|
111
|
+
];
|
|
112
|
+
|
|
113
|
+
if (!groups.every((group) => /^[0-9a-f]{1,4}$/u.test(group))) return null;
|
|
114
|
+
|
|
115
|
+
const [g0, g1, g2, g3, g4, g5, g6, g7] = groups.map((group) =>
|
|
116
|
+
Number.parseInt(group, 16)
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
if (groups.length !== 8) return null;
|
|
120
|
+
|
|
121
|
+
return [g0, g1, g2, g3, g4, g5, g6, g7];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Whether an IPv6 address is anything but a global unicast address on the
|
|
126
|
+
* public internet: loopback, unspecified, IPv4-mapped or -compatible
|
|
127
|
+
* (::ffff:7f00:1 is 127.0.0.1), NAT64 or 6to4 around a blocked IPv4
|
|
128
|
+
* address, unique local, link- and site-local, documentation, discard,
|
|
129
|
+
* Teredo, or multicast.
|
|
130
|
+
*/
|
|
131
|
+
function isBlockedIpv6(groups: Ipv6Groups) {
|
|
132
|
+
const [g0, g1, g2, g3, g4, g5, g6, g7] = groups;
|
|
133
|
+
|
|
134
|
+
const embedded = (high: number, low: number) =>
|
|
135
|
+
isBlockedIpv4(high * 65_536 + low);
|
|
136
|
+
|
|
137
|
+
if (groups.slice(0, 6).every((group) => group === 0)) return true; // ::, ::1, ::a.b.c.d
|
|
138
|
+
|
|
139
|
+
if (groups.slice(0, 5).every((group) => group === 0) && g5 === 0xff_ff) {
|
|
140
|
+
return true;
|
|
141
|
+
} // ::ffff:a.b.c.d
|
|
142
|
+
|
|
143
|
+
if (
|
|
144
|
+
g0 === 0 &&
|
|
145
|
+
g1 === 0 &&
|
|
146
|
+
g2 === 0 &&
|
|
147
|
+
g3 === 0 &&
|
|
148
|
+
g4 === 0xff_ff &&
|
|
149
|
+
g5 === 0
|
|
150
|
+
) {
|
|
151
|
+
return true;
|
|
152
|
+
} // ::ffff:0:a.b.c.d
|
|
153
|
+
|
|
154
|
+
if (g0 === 0x64 && g1 === 0xff_9b) return g2 === 1 || embedded(g6, g7); // NAT64
|
|
155
|
+
|
|
156
|
+
if (g0 === 0x20_02) return embedded(g1, g2); // 6to4
|
|
157
|
+
|
|
158
|
+
if (g0 === 0x20_01 && g1 === 0) return true; // Teredo
|
|
159
|
+
|
|
160
|
+
if (g0 === 0x20_01 && g1 === 0xd_b8) return true; // documentation
|
|
161
|
+
|
|
162
|
+
if (g0 === 0x1_00 && g1 === 0 && g2 === 0 && g3 === 0) return true; // discard
|
|
163
|
+
|
|
164
|
+
if ((g0 & 0xfe_00) === 0xfc_00) return true; // unique local
|
|
165
|
+
|
|
166
|
+
if ((g0 & 0xff_c0) === 0xfe_80 || (g0 & 0xff_c0) === 0xfe_c0) return true; // link- and site-local
|
|
167
|
+
|
|
168
|
+
if ((g0 & 0xff_00) === 0xff_00) return true; // multicast
|
|
169
|
+
|
|
53
170
|
return false;
|
|
54
171
|
}
|
|
55
172
|
|
|
173
|
+
/** Whether a URL's (already normalized) host is an IP address the proxy must not reach. */
|
|
174
|
+
function isBlockedIp(hostname: string) {
|
|
175
|
+
const v4 = ipv4Number(hostname);
|
|
176
|
+
|
|
177
|
+
if (v4 !== null) return isBlockedIpv4(v4);
|
|
178
|
+
|
|
179
|
+
if (!hostname.includes(":")) return false;
|
|
180
|
+
const groups = ipv6Groups(hostname);
|
|
181
|
+
|
|
182
|
+
// Anything that looks like IPv6 but does not parse is refused too.
|
|
183
|
+
return !groups || isBlockedIpv6(groups);
|
|
184
|
+
}
|
|
185
|
+
|
|
56
186
|
export function assertProxyableRemoteUrl(value: string): URL {
|
|
57
187
|
let url: URL;
|
|
188
|
+
|
|
58
189
|
try {
|
|
59
190
|
url = new URL(value);
|
|
60
191
|
} catch {
|
|
61
192
|
throw new Error("Remote URL is invalid");
|
|
62
193
|
}
|
|
194
|
+
|
|
63
195
|
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
64
196
|
throw new Error("Only http(s) remote URLs are allowed");
|
|
65
197
|
}
|
|
198
|
+
|
|
66
199
|
if (url.username || url.password) {
|
|
67
200
|
throw new Error("Remote URLs must not include credentials");
|
|
68
201
|
}
|
|
69
|
-
|
|
202
|
+
|
|
203
|
+
// The URL parser has normalized the host: IPv4 in dotted decimal, IPv6
|
|
204
|
+
// compressed in brackets. A trailing dot names the same host.
|
|
205
|
+
const host = url.hostname.toLowerCase().replace(/\.$/u, "");
|
|
206
|
+
|
|
70
207
|
if (!host || BLOCKED_HOSTS.has(host)) {
|
|
71
208
|
throw new Error("Remote host is not allowed");
|
|
72
209
|
}
|
|
210
|
+
|
|
73
211
|
if (BLOCKED_HOST_SUFFIXES.some((suffix) => host.endsWith(suffix))) {
|
|
74
212
|
throw new Error("Remote host is not allowed");
|
|
75
213
|
}
|
|
214
|
+
|
|
76
215
|
if (isBlockedIp(host)) {
|
|
77
216
|
throw new Error("Remote host is not allowed");
|
|
78
217
|
}
|
|
218
|
+
|
|
79
219
|
return url;
|
|
80
220
|
}
|
|
81
221
|
|
|
@@ -85,17 +225,22 @@ export function mediaType(contentType: string | null) {
|
|
|
85
225
|
|
|
86
226
|
export function isProxyableRemoteMediaType(contentType: string | null) {
|
|
87
227
|
const type = mediaType(contentType);
|
|
228
|
+
|
|
88
229
|
if (PROXYABLE_REMOTE_MEDIA_TYPES.has(type)) return true;
|
|
230
|
+
|
|
89
231
|
// Some CDNs send image/jpg; normalize already in set. image/x-png etc. rejected.
|
|
90
232
|
return false;
|
|
91
233
|
}
|
|
92
234
|
|
|
93
235
|
export async function fetchProxiedRemoteMedia(remoteUrl: string) {
|
|
94
236
|
let current = assertProxyableRemoteUrl(remoteUrl);
|
|
95
|
-
|
|
237
|
+
|
|
238
|
+
// The first request and each redirect hop, one after another.
|
|
239
|
+
const media = await repeatUntil(REMOTE_PROXY_MAX_REDIRECTS + 1, async () => {
|
|
96
240
|
const controller = new AbortController();
|
|
97
241
|
const timer = setTimeout(() => controller.abort(), REMOTE_PROXY_TIMEOUT_MS);
|
|
98
242
|
let response: Response;
|
|
243
|
+
|
|
99
244
|
try {
|
|
100
245
|
response = await fetch(current.toString(), {
|
|
101
246
|
method: "GET",
|
|
@@ -112,29 +257,40 @@ export async function fetchProxiedRemoteMedia(remoteUrl: string) {
|
|
|
112
257
|
|
|
113
258
|
if ([301, 302, 303, 307, 308].includes(response.status)) {
|
|
114
259
|
const location = response.headers.get("location");
|
|
260
|
+
|
|
115
261
|
if (!location) throw new Error("Remote redirect is missing a location");
|
|
116
262
|
current = assertProxyableRemoteUrl(new URL(location, current).toString());
|
|
117
|
-
|
|
263
|
+
|
|
264
|
+
return undefined;
|
|
118
265
|
}
|
|
119
266
|
|
|
120
267
|
if (!response.ok) {
|
|
121
268
|
throw new Error(`Remote fetch failed (${response.status})`);
|
|
122
269
|
}
|
|
270
|
+
|
|
123
271
|
if (!isProxyableRemoteMediaType(response.headers.get("content-type"))) {
|
|
124
272
|
throw new Error("Remote content type is not an allowed image");
|
|
125
273
|
}
|
|
274
|
+
|
|
126
275
|
const lengthHeader = response.headers.get("content-length");
|
|
276
|
+
|
|
127
277
|
if (lengthHeader && Number(lengthHeader) > MAX_REMOTE_PROXY_BYTES) {
|
|
128
278
|
throw new Error("Remote content is too large");
|
|
129
279
|
}
|
|
280
|
+
|
|
130
281
|
const buffer = await response.arrayBuffer();
|
|
282
|
+
|
|
131
283
|
if (buffer.byteLength > MAX_REMOTE_PROXY_BYTES) {
|
|
132
284
|
throw new Error("Remote content is too large");
|
|
133
285
|
}
|
|
286
|
+
|
|
134
287
|
return {
|
|
135
288
|
body: buffer,
|
|
136
289
|
contentType: mediaType(response.headers.get("content-type")),
|
|
137
290
|
};
|
|
138
|
-
}
|
|
139
|
-
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
if (!media) throw new Error("Too many remote redirects");
|
|
294
|
+
|
|
295
|
+
return media;
|
|
140
296
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { AppEnv } from "@open-workspace/core/module";
|
|
2
|
+
import {
|
|
3
|
+
apiError,
|
|
4
|
+
rateLimitedResponse,
|
|
5
|
+
withinRateLimit,
|
|
6
|
+
timed,
|
|
7
|
+
zValidator,
|
|
8
|
+
} from "@open-workspace/core/module";
|
|
9
|
+
import { Hono } from "hono";
|
|
10
|
+
|
|
11
|
+
import { proxiedRemoteImage } from "./edge-cache";
|
|
12
|
+
import { assertProxyableRemoteUrl } from "./remote-proxy";
|
|
13
|
+
import { verifyRemoteToken } from "./remote-token";
|
|
14
|
+
import { remoteProxyQuerySchema } from "./schemas";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* GET /api/mail/remote: remote images in sanitized message HTML, fetched
|
|
18
|
+
* here so the sender never sees the reader. The URL's token (remote-token.ts)
|
|
19
|
+
* is the only credential: no session or mailbox lookup, nothing in D1.
|
|
20
|
+
* Origin fetches are rate limited per person; images any reader fetched
|
|
21
|
+
* come from the colo's cache.
|
|
22
|
+
*/
|
|
23
|
+
export const mailRemoteRoutes = new Hono<AppEnv>().get(
|
|
24
|
+
"/",
|
|
25
|
+
zValidator("query", remoteProxyQuerySchema),
|
|
26
|
+
async (c) => {
|
|
27
|
+
const { mailboxId, token, url } = c.req.valid("query");
|
|
28
|
+
|
|
29
|
+
const userId = await timed(c, "auth", "token", () =>
|
|
30
|
+
verifyRemoteToken(c.env.MAIL_REMOTE_KEY, token, mailboxId, url)
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
if (!userId) {
|
|
34
|
+
return apiError(
|
|
35
|
+
c,
|
|
36
|
+
403,
|
|
37
|
+
"FORBIDDEN",
|
|
38
|
+
"Remote content link is invalid or expired"
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
try {
|
|
43
|
+
assertProxyableRemoteUrl(url);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
return apiError(
|
|
46
|
+
c,
|
|
47
|
+
400,
|
|
48
|
+
"BAD_REQUEST",
|
|
49
|
+
error instanceof Error ? error.message : "Remote URL is invalid"
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
try {
|
|
54
|
+
// Origin fetches, per person.
|
|
55
|
+
const image = await proxiedRemoteImage(c, url, () =>
|
|
56
|
+
timed(c, "limit", "remote", () =>
|
|
57
|
+
withinRateLimit(c, "RATE_LIMIT_120", "mail-remote", userId)
|
|
58
|
+
)
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
if (image.outcome === "limited") {
|
|
62
|
+
return rateLimitedResponse(
|
|
63
|
+
c,
|
|
64
|
+
"Too many remote content requests. Try again shortly."
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return new Response(image.body, {
|
|
69
|
+
headers: {
|
|
70
|
+
// The token in the URL changes with each token window.
|
|
71
|
+
"cache-control": "private, max-age=43200",
|
|
72
|
+
"content-type": image.contentType,
|
|
73
|
+
"content-disposition": "inline",
|
|
74
|
+
"cross-origin-resource-policy": "same-origin",
|
|
75
|
+
"x-content-type-options": "nosniff",
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
} catch (error) {
|
|
79
|
+
return apiError(
|
|
80
|
+
c,
|
|
81
|
+
502,
|
|
82
|
+
"BAD_GATEWAY",
|
|
83
|
+
error instanceof Error ? error.message : "Remote content unavailable"
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
);
|