@open-workspace/email 0.5.1 → 0.6.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.
- package/README.md +1 -2
- package/alchemy/index.ts +0 -16
- package/migrations/0003_after_0_5_2.sql +1 -0
- package/migrations/meta/0003_snapshot.json +703 -0
- package/migrations/meta/_journal.json +7 -0
- package/package.json +36 -35
- package/shared/mail.ts +3 -2
- package/shared/module.ts +1 -1
- package/src/client.tsx +23 -31
- package/src/extensions.ts +176 -0
- package/src/mail/compose-window-chrome.tsx +5 -10
- package/src/mail/compose-window.tsx +13 -2
- package/src/mail/composer-asset-bookmarks.ts +3 -3
- package/src/mail/composer-attach-button.tsx +106 -0
- package/src/mail/composer-editor.tsx +17 -0
- package/src/mail/composer-inserts.tsx +186 -0
- package/src/mail/composer-toolbar.tsx +3 -0
- package/src/mail/conversation-message.tsx +37 -1
- package/src/mail/conversation-reply.tsx +5 -10
- package/src/mail/mail-mailbox-sheet.tsx +2 -2
- package/src/mail/mail-prefetch.ts +1 -1
- package/src/mail/message-actions.tsx +30 -0
- package/src/mail/message-attachments.tsx +62 -0
- package/src/mail/message-panels.tsx +206 -0
- package/src/mail/module-extensions.ts +87 -0
- package/src/mail/reader-pane.tsx +75 -47
- package/src/mail/recipient-combobox.tsx +32 -12
- package/src/mail/shared-compose.ts +45 -0
- package/src/mail/unread-document-indicator.tsx +4 -3
- package/src/mail/use-composer-attachment-preflight.ts +2 -1
- package/src/mail/use-composer-core.ts +4 -6
- package/src/settings/mail-notifications.tsx +119 -0
- package/src/settings/settings-queries.ts +0 -7
- package/worker/backups/file-parts.ts +9 -6
- package/worker/backups/mail-backup.ts +21 -4
- package/worker/backups/settings-backup.ts +56 -24
- package/worker/db/schema.ts +0 -36
- package/worker/jmap/eventsource.ts +4 -1
- package/worker/jmap/method-core.ts +2 -3
- package/worker/jmap/push-subscription-methods.ts +5 -2
- package/worker/jmap/push.ts +10 -11
- package/worker/jmap/routes.ts +10 -49
- package/worker/mail/bimi.ts +2 -3
- package/worker/mail/person-erasure.ts +58 -0
- package/worker/mail/push-notifications.ts +59 -419
- package/worker/mail/remote-proxy.ts +3 -206
- package/worker/mail/remote-routes.ts +2 -2
- package/worker/mail/routes.ts +8 -2
- package/worker/mail/uploads.ts +14 -6
- package/worker/mail/webhook-source.ts +13 -1
- package/worker/mailbox/index.ts +27 -0
- package/worker/mailbox/mailbox-drafts.ts +5 -0
- package/worker/module-api.ts +239 -0
- package/worker/module.ts +11 -7
- package/src/pwa/push-key.ts +0 -18
- package/src/pwa/push-subscription.ts +0 -213
- package/src/settings/notifications-page.tsx +0 -306
- package/worker/mail/push-endpoint.ts +0 -17
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
|
+
backupObject,
|
|
3
|
+
holdsScope,
|
|
2
4
|
resolveBackedUpPeople,
|
|
3
5
|
createDb,
|
|
4
6
|
mailboxAddressPredicate,
|
|
@@ -8,6 +10,7 @@ import {
|
|
|
8
10
|
import type {
|
|
9
11
|
AppEnv,
|
|
10
12
|
BackupRestoreItem,
|
|
13
|
+
BackupRestoreObject,
|
|
11
14
|
BackupRestoreResult,
|
|
12
15
|
BackupSnapshot,
|
|
13
16
|
Database,
|
|
@@ -374,11 +377,10 @@ export async function writePieces(
|
|
|
374
377
|
continue;
|
|
375
378
|
}
|
|
376
379
|
|
|
377
|
-
// oxlint-disable-next-line eslint/no-await-in-loop -- see above
|
|
378
|
-
const sha256 = await snapshot.putObject(bytes, piece.type);
|
|
379
380
|
lines.push({
|
|
380
381
|
...piece.line,
|
|
381
|
-
|
|
382
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- see above
|
|
383
|
+
object: await snapshot.putObject(await backupObject(bytes), piece.type),
|
|
382
384
|
});
|
|
383
385
|
} catch (cause) {
|
|
384
386
|
snapshot.error(
|
|
@@ -560,10 +562,26 @@ const pieceLine = z.discriminatedUnion("kind", [
|
|
|
560
562
|
}),
|
|
561
563
|
]);
|
|
562
564
|
|
|
563
|
-
|
|
565
|
+
/**
|
|
566
|
+
* The largest settings or draft object read: what the restore route took
|
|
567
|
+
* whole before objects were streamed. A workspace's object holds every
|
|
568
|
+
* signature and blocked sender, so it isn't small.
|
|
569
|
+
*/
|
|
570
|
+
const MAX_JSON_OBJECT_BYTES = 26 * 1024 * 1024;
|
|
571
|
+
|
|
572
|
+
/** A JSON object's content (a piece's settings, a draft), read whole. */
|
|
573
|
+
async function parseObject<T>(
|
|
574
|
+
schema: z.ZodType<T>,
|
|
575
|
+
object: BackupRestoreObject | null
|
|
576
|
+
) {
|
|
577
|
+
if (!object || object.size > MAX_JSON_OBJECT_BYTES) return undefined;
|
|
578
|
+
|
|
564
579
|
try {
|
|
565
|
-
|
|
580
|
+
const text = await new Response(object.body).text();
|
|
581
|
+
|
|
582
|
+
return schema.safeParse(JSON.parse(text)).data;
|
|
566
583
|
} catch {
|
|
584
|
+
// Bytes that aren't the ones named error the stream; core refuses the line.
|
|
567
585
|
return undefined;
|
|
568
586
|
}
|
|
569
587
|
}
|
|
@@ -580,10 +598,12 @@ const TEAMS_SCOPE = "admin.teams";
|
|
|
580
598
|
* may write there. Folders, rules, the auto-reply, AI instructions, sender
|
|
581
599
|
* choices, people's signatures and notification choices: `mail.organize`;
|
|
582
600
|
* drafts and their files: `mail.send`; a team address's signature:
|
|
583
|
-
* `admin.teams
|
|
584
|
-
*
|
|
601
|
+
* `admin.teams`. The workspace's blocked senders (`admin.blocked_senders`)
|
|
602
|
+
* and signatures, and an address's own signature, are in the object, so
|
|
603
|
+
* `restorePiece` checks those (`lacking`). Null for a line that is no
|
|
604
|
+
* piece (a message).
|
|
585
605
|
*/
|
|
586
|
-
export function pieceScopes({ line
|
|
606
|
+
export function pieceScopes({ line }: Pick<BackupRestoreItem, "line">) {
|
|
587
607
|
const piece = pieceLine.safeParse(line).data;
|
|
588
608
|
|
|
589
609
|
if (!piece) return null;
|
|
@@ -602,21 +622,11 @@ export function pieceScopes({ line, object }: BackupRestoreItem) {
|
|
|
602
622
|
}
|
|
603
623
|
|
|
604
624
|
case "settings": {
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
return [
|
|
608
|
-
...anyMailbox(MAIL_ORGANIZE_SCOPE),
|
|
609
|
-
...(data?.signature ? [TEAMS_SCOPE] : []),
|
|
610
|
-
];
|
|
625
|
+
return anyMailbox(MAIL_ORGANIZE_SCOPE);
|
|
611
626
|
}
|
|
612
627
|
|
|
613
628
|
case "workspace": {
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
return [
|
|
617
|
-
...(data?.blockedSenders.length ? [BLOCK_SENDERS_SCOPE] : []),
|
|
618
|
-
...(data?.signatures.length ? anyMailbox(MAIL_ORGANIZE_SCOPE) : []),
|
|
619
|
-
];
|
|
629
|
+
return [];
|
|
620
630
|
}
|
|
621
631
|
|
|
622
632
|
default: {
|
|
@@ -637,6 +647,15 @@ const skipped = (reason: string) =>
|
|
|
637
647
|
reason,
|
|
638
648
|
}) satisfies BackupRestoreResult;
|
|
639
649
|
|
|
650
|
+
/** As core skips a line whose `restoreScopes` the sign-in lacks; null when it holds them. */
|
|
651
|
+
function lacking(c: Context<AppEnv>, scopes: string[]) {
|
|
652
|
+
const missing = [...new Set(scopes)].filter((scope) => !holdsScope(c, scope));
|
|
653
|
+
|
|
654
|
+
return missing.length
|
|
655
|
+
? skipped(`this sign-in lacks ${missing.join(", ")}`)
|
|
656
|
+
: null;
|
|
657
|
+
}
|
|
658
|
+
|
|
640
659
|
/**
|
|
641
660
|
* Puts a piece back; what exists is left as it is. Null for a line that
|
|
642
661
|
* is no piece (a message).
|
|
@@ -674,9 +693,16 @@ export async function restorePiece(
|
|
|
674
693
|
}
|
|
675
694
|
|
|
676
695
|
if (piece.kind === "workspace") {
|
|
677
|
-
const data =
|
|
696
|
+
const data = await parseObject(workspaceObject, object);
|
|
678
697
|
|
|
679
698
|
if (!data) return skipped("damaged");
|
|
699
|
+
|
|
700
|
+
const refused = lacking(c, [
|
|
701
|
+
...(data.blockedSenders.length ? [BLOCK_SENDERS_SCOPE] : []),
|
|
702
|
+
...(data.signatures.length ? anyMailbox(MAIL_ORGANIZE_SCOPE) : []),
|
|
703
|
+
]);
|
|
704
|
+
|
|
705
|
+
if (refused) return refused;
|
|
680
706
|
const actor = c.get("user").id;
|
|
681
707
|
|
|
682
708
|
const blocks = await Promise.all(
|
|
@@ -765,7 +791,9 @@ export async function restorePiece(
|
|
|
765
791
|
piece.file,
|
|
766
792
|
options
|
|
767
793
|
);
|
|
768
|
-
} else if (object)
|
|
794
|
+
} else if (object) {
|
|
795
|
+
await env.MAIL_STORAGE.put(target, object.body, options);
|
|
796
|
+
}
|
|
769
797
|
|
|
770
798
|
// A draft's file goes again unless a draft holds it: its draft line
|
|
771
799
|
// comes first, so a draft that was skipped (the drafts limit, one already
|
|
@@ -781,7 +809,7 @@ export async function restorePiece(
|
|
|
781
809
|
const stub = mailboxStub(env, mailbox.id);
|
|
782
810
|
|
|
783
811
|
if (piece.kind === "draft") {
|
|
784
|
-
const draft =
|
|
812
|
+
const draft = await parseObject(draftObject, object);
|
|
785
813
|
|
|
786
814
|
if (!draft || !owner) return skipped("damaged");
|
|
787
815
|
const { sourceMessageId, forwardedFiles, ...input } = draft;
|
|
@@ -814,9 +842,13 @@ export async function restorePiece(
|
|
|
814
842
|
: restored(status === "saved");
|
|
815
843
|
}
|
|
816
844
|
|
|
817
|
-
const data =
|
|
845
|
+
const data = await parseObject(settingsObject, object);
|
|
818
846
|
|
|
819
847
|
if (!data) return skipped("damaged");
|
|
848
|
+
// A team address's own signature is the Teams screen's to change.
|
|
849
|
+
const refused = data.signature ? lacking(c, [TEAMS_SCOPE]) : null;
|
|
850
|
+
|
|
851
|
+
if (refused) return refused;
|
|
820
852
|
const { signature: own, notifications, ...settings } = data;
|
|
821
853
|
|
|
822
854
|
const people = await resolveBackedUpPeople(
|
package/worker/db/schema.ts
CHANGED
|
@@ -74,42 +74,6 @@ export const signatures = sqliteTable(
|
|
|
74
74
|
]
|
|
75
75
|
);
|
|
76
76
|
|
|
77
|
-
/**
|
|
78
|
-
* Browsers that get new-mail notifications, one row per device, bound to
|
|
79
|
-
* the session that last registered it. Rows from before sessions were
|
|
80
|
-
* recorded have none and are removed only with all of a person's sessions.
|
|
81
|
-
*/
|
|
82
|
-
export const pushSubscriptions = sqliteTable(
|
|
83
|
-
"push_subscriptions",
|
|
84
|
-
{
|
|
85
|
-
id: text("id").primaryKey(),
|
|
86
|
-
userId: text("user_id")
|
|
87
|
-
.notNull()
|
|
88
|
-
.references(() => users.id, { onDelete: "cascade" }),
|
|
89
|
-
/**
|
|
90
|
-
* Not a foreign key: expiry must not take the device's notifications
|
|
91
|
-
* with them. Re-registering moves the row to the new session; a session
|
|
92
|
-
* that ends otherwise (signed out, revoked, pushed out) removes it (the
|
|
93
|
-
* `sessionsEnded` hook).
|
|
94
|
-
*/
|
|
95
|
-
sessionId: text("session_id"),
|
|
96
|
-
endpoint: text("endpoint").notNull(),
|
|
97
|
-
p256dh: text("p256dh").notNull(),
|
|
98
|
-
auth: text("auth").notNull(),
|
|
99
|
-
createdAt: integer("created_at", { mode: "timestamp_ms" })
|
|
100
|
-
.notNull()
|
|
101
|
-
.default(sql`(unixepoch() * 1000)`),
|
|
102
|
-
updatedAt: integer("updated_at", { mode: "timestamp_ms" })
|
|
103
|
-
.notNull()
|
|
104
|
-
.default(sql`(unixepoch() * 1000)`),
|
|
105
|
-
},
|
|
106
|
-
(table) => [
|
|
107
|
-
uniqueIndex("push_subscriptions_endpoint_unique").on(table.endpoint),
|
|
108
|
-
index("push_subscriptions_user_idx").on(table.userId),
|
|
109
|
-
index("push_subscriptions_session_idx").on(table.sessionId),
|
|
110
|
-
]
|
|
111
|
-
);
|
|
112
|
-
|
|
113
77
|
/**
|
|
114
78
|
* What people said about a sender in each mailbox (one row per message), so
|
|
115
79
|
* every mailbox's classifier hears about spam reported elsewhere.
|
|
@@ -80,7 +80,10 @@ export async function jmapEventStream(input: {
|
|
|
80
80
|
}
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
|
|
83
|
+
// Closing fails when the client has already gone.
|
|
84
|
+
writer
|
|
85
|
+
.close()
|
|
86
|
+
.catch((error) => console.debug("Event stream already closed", error));
|
|
84
87
|
finished();
|
|
85
88
|
};
|
|
86
89
|
|
|
@@ -4,12 +4,11 @@ import {
|
|
|
4
4
|
hashToken,
|
|
5
5
|
issueProperties,
|
|
6
6
|
} from "@open-workspace/core/module";
|
|
7
|
-
import type { Issue } from "@open-workspace/core/module";
|
|
7
|
+
import type { Issue, VapidConfiguration } from "@open-workspace/core/module";
|
|
8
8
|
import { z } from "zod";
|
|
9
9
|
|
|
10
10
|
import { MAIL_ORGANIZE_SCOPE, MAIL_SEND_SCOPE } from "../../shared/permissions";
|
|
11
11
|
import type { composeSchema } from "../mail/schemas";
|
|
12
|
-
import type { PushVapid } from "./push";
|
|
13
12
|
|
|
14
13
|
export const CORE = "urn:ietf:params:jmap:core";
|
|
15
14
|
export const MAX_OBJECTS = 500;
|
|
@@ -49,7 +48,7 @@ export type JmapContext = {
|
|
|
49
48
|
/** The token's mail scopes; each method needs its own (`scopes` of its definition). */
|
|
50
49
|
scopes: readonly string[];
|
|
51
50
|
/** Signs pushes to Web Push services; null when push is not configured. */
|
|
52
|
-
vapid:
|
|
51
|
+
vapid: VapidConfiguration | null;
|
|
53
52
|
accounts: JmapAccount[];
|
|
54
53
|
requestUrl: string;
|
|
55
54
|
};
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
/** PushSubscription/get and /set (RFC 8620 §7.2). */
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
inJsonList,
|
|
4
|
+
createDb,
|
|
5
|
+
isPublicPushEndpoint,
|
|
6
|
+
} from "@open-workspace/core/module";
|
|
3
7
|
import { waitUntil } from "cloudflare:workers";
|
|
4
8
|
import { and, eq } from "drizzle-orm";
|
|
5
9
|
import { z } from "zod";
|
|
6
10
|
|
|
7
11
|
import { jmapPushSubscriptions } from "../db/schema";
|
|
8
|
-
import { isPublicPushEndpoint } from "../mail/push-endpoint";
|
|
9
12
|
import type { JsonObject, RequestState } from "./method-core";
|
|
10
13
|
import {
|
|
11
14
|
MethodError,
|
package/worker/jmap/push.ts
CHANGED
|
@@ -3,12 +3,17 @@ import {
|
|
|
3
3
|
sendPushNotification,
|
|
4
4
|
topicFromString,
|
|
5
5
|
} from "@mmmike/web-push/send";
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
inJsonList,
|
|
8
|
+
createDb,
|
|
9
|
+
createId,
|
|
10
|
+
isPublicPushEndpoint,
|
|
11
|
+
} from "@open-workspace/core/module";
|
|
12
|
+
import type { VapidConfiguration } from "@open-workspace/core/module";
|
|
7
13
|
import { mailboxAccess, mailboxes, users } from "@open-workspace/core/schema";
|
|
8
14
|
import { and, eq, gt, isNotNull, lte } from "drizzle-orm";
|
|
9
15
|
|
|
10
16
|
import { jmapPushSubscriptions } from "../db/schema";
|
|
11
|
-
import { isPublicPushEndpoint } from "../mail/push-endpoint";
|
|
12
17
|
import { stateChange, wantsMailChanges } from "./push-types";
|
|
13
18
|
import type { JmapStateMessage, StateChange } from "./push-types";
|
|
14
19
|
|
|
@@ -20,12 +25,6 @@ const MAX_SUBSCRIPTION_MS = 7 * 24 * 60 * 60 * 1000;
|
|
|
20
25
|
/** State changes are worth little once a client resyncs anyway. */
|
|
21
26
|
const STATE_CHANGE_TTL_SECONDS = 24 * 60 * 60;
|
|
22
27
|
|
|
23
|
-
export type PushVapid = {
|
|
24
|
-
publicKey: string;
|
|
25
|
-
privateKey: string;
|
|
26
|
-
subject: string;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
28
|
type Subscription = typeof jmapPushSubscriptions.$inferSelect;
|
|
30
29
|
|
|
31
30
|
/** RFC 8620 §7.2.2: proves the client can read what is pushed to the URL. */
|
|
@@ -56,7 +55,7 @@ export function subscriptionExpiry(
|
|
|
56
55
|
export async function postToSubscription(
|
|
57
56
|
subscription: Pick<Subscription, "url" | "p256dh" | "auth">,
|
|
58
57
|
body: StateChange | PushVerification,
|
|
59
|
-
vapid:
|
|
58
|
+
vapid: VapidConfiguration | null,
|
|
60
59
|
options: { ttl: number; topic?: string }
|
|
61
60
|
) {
|
|
62
61
|
// Registered before the rule: gone, as a push service would say.
|
|
@@ -111,7 +110,7 @@ export function newVerificationCode() {
|
|
|
111
110
|
/** Sends the PushVerification object a new subscription is confirmed with. */
|
|
112
111
|
export async function sendVerification(
|
|
113
112
|
subscription: Subscription,
|
|
114
|
-
vapid:
|
|
113
|
+
vapid: VapidConfiguration | null
|
|
115
114
|
) {
|
|
116
115
|
await postToSubscription(
|
|
117
116
|
subscription,
|
|
@@ -132,7 +131,7 @@ export async function sendVerification(
|
|
|
132
131
|
export async function deliverJmapStateChange(
|
|
133
132
|
env: Env,
|
|
134
133
|
input: { mailboxId: string } & JmapStateMessage,
|
|
135
|
-
vapid:
|
|
134
|
+
vapid: VapidConfiguration
|
|
136
135
|
) {
|
|
137
136
|
const db = createDb(env.DB);
|
|
138
137
|
const now = new Date();
|
package/worker/jmap/routes.ts
CHANGED
|
@@ -4,10 +4,9 @@ import {
|
|
|
4
4
|
describeIssues,
|
|
5
5
|
mailboxAddressSql,
|
|
6
6
|
overRateLimit,
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
oidcIssuer,
|
|
7
|
+
resourceAccessToken,
|
|
8
|
+
resourceChallenge,
|
|
9
|
+
vapidConfiguration,
|
|
11
10
|
} from "@open-workspace/core/module";
|
|
12
11
|
import type { AppEnv } from "@open-workspace/core/module";
|
|
13
12
|
import { mailboxAccess, mailboxes } from "@open-workspace/core/schema";
|
|
@@ -24,7 +23,6 @@ import {
|
|
|
24
23
|
MAIL_SEND_SCOPE,
|
|
25
24
|
mailScopes,
|
|
26
25
|
} from "../../shared/permissions";
|
|
27
|
-
import { vapidConfiguration } from "../mail/push-notifications";
|
|
28
26
|
import {
|
|
29
27
|
createComposerUploadIntent,
|
|
30
28
|
finalizeComposerUpload,
|
|
@@ -96,13 +94,10 @@ function accountCapabilities(maySubmit: boolean) {
|
|
|
96
94
|
return capabilities;
|
|
97
95
|
}
|
|
98
96
|
|
|
99
|
-
const mailScopeNames: ReadonlySet<string> = new Set(mailScopes);
|
|
100
|
-
|
|
101
97
|
type JmapUser = {
|
|
102
98
|
id: string;
|
|
103
99
|
name: string;
|
|
104
100
|
clientId: string;
|
|
105
|
-
token: string;
|
|
106
101
|
/** The token's mail scopes the person may still use. */
|
|
107
102
|
scopes: readonly string[];
|
|
108
103
|
};
|
|
@@ -199,51 +194,16 @@ async function sessionObject(
|
|
|
199
194
|
};
|
|
200
195
|
}
|
|
201
196
|
|
|
202
|
-
/** RFC 8707: JMAP accepts only tokens issued for it, at the issuer's origin or this one. */
|
|
203
|
-
function jmapResources(c: Context<JmapEnv>) {
|
|
204
|
-
const origins = new Set([new URL(c.req.url).origin]);
|
|
205
|
-
|
|
206
|
-
try {
|
|
207
|
-
origins.add(new URL(oidcIssuer(c.env)).origin);
|
|
208
|
-
} catch {
|
|
209
|
-
// Without a configured issuer, only this origin.
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
return [...origins].map(jmapResourceUri);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/** Who a JMAP token signs in, with its mail scopes; null when it is not good here. */
|
|
216
|
-
async function jmapIdentity(c: Context<JmapEnv>, token: string) {
|
|
217
|
-
const identity = await authenticateAccessToken(
|
|
218
|
-
createDb(c.env.DB),
|
|
219
|
-
token,
|
|
220
|
-
jmapResources(c)
|
|
221
|
-
);
|
|
222
|
-
|
|
223
|
-
if (!identity) return null;
|
|
224
|
-
|
|
225
|
-
return {
|
|
226
|
-
...identity,
|
|
227
|
-
scopes: identity.scopes.filter((scope) => mailScopeNames.has(scope)),
|
|
228
|
-
};
|
|
229
|
-
}
|
|
230
|
-
|
|
231
197
|
const requireJmapAuth = async (
|
|
232
198
|
c: Context<JmapEnv>,
|
|
233
199
|
next: () => Promise<void>
|
|
234
200
|
) => {
|
|
201
|
+
// Only a token issued for JMAP, with its mail scopes (RFC 8707).
|
|
235
202
|
const token = bearerToken(c.req.raw);
|
|
236
|
-
const identity =
|
|
203
|
+
const identity = await resourceAccessToken(c, JMAP_PATH);
|
|
237
204
|
|
|
238
205
|
if (!token || !identity) {
|
|
239
|
-
c.header(
|
|
240
|
-
"www-authenticate",
|
|
241
|
-
[
|
|
242
|
-
`Bearer resource_metadata="${jmapProtectedResourceMetadataUrl(new URL(c.req.url).origin)}"`,
|
|
243
|
-
`scope="${mailScopes.join(" ")}"`,
|
|
244
|
-
...(token ? ['error="invalid_token"'] : []),
|
|
245
|
-
].join(", ")
|
|
246
|
-
);
|
|
206
|
+
c.header("www-authenticate", resourceChallenge(c, JMAP_PATH));
|
|
247
207
|
|
|
248
208
|
return problem(
|
|
249
209
|
c,
|
|
@@ -271,7 +231,6 @@ const requireJmapAuth = async (
|
|
|
271
231
|
id: identity.user.id,
|
|
272
232
|
name: identity.user.name,
|
|
273
233
|
clientId: identity.clientId,
|
|
274
|
-
token,
|
|
275
234
|
scopes: identity.scopes,
|
|
276
235
|
});
|
|
277
236
|
await next();
|
|
@@ -519,10 +478,12 @@ export const jmapRoutes = new Hono<JmapEnv>()
|
|
|
519
478
|
types: c.req.query("types") ?? "*",
|
|
520
479
|
closeAfterState: c.req.query("closeafter") === "state",
|
|
521
480
|
pingSeconds: Number(c.req.query("ping") ?? 0),
|
|
522
|
-
// The stream outlives the request check;
|
|
481
|
+
// The stream outlives the request check; its token is checked again on each ping.
|
|
523
482
|
stillAuthorized: async () =>
|
|
524
483
|
Boolean(
|
|
525
|
-
(await
|
|
484
|
+
(await resourceAccessToken(c, JMAP_PATH))?.scopes.includes(
|
|
485
|
+
MAIL_READ_SCOPE
|
|
486
|
+
)
|
|
526
487
|
),
|
|
527
488
|
currentAccounts: () => jmapAccounts(c.env, user.id),
|
|
528
489
|
});
|
package/worker/mail/bimi.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { readBounded } from "@open-workspace/core/module";
|
|
1
|
+
import { assertPublicUrl, readBounded } from "@open-workspace/core/module";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
|
|
4
4
|
import type { BimiResult, BimiStatus } from "../../shared/mail-sender-intel";
|
|
5
5
|
import { isAbortError } from "./doh";
|
|
6
6
|
import type { DnsResolver } from "./doh";
|
|
7
|
-
import { assertProxyableRemoteUrl } from "./remote-proxy";
|
|
8
7
|
|
|
9
8
|
/**
|
|
10
9
|
* BIMI logo lookup without VMC/CMC validation: we show the domain's own logo
|
|
@@ -162,7 +161,7 @@ async function lookup(input: {
|
|
|
162
161
|
let url: URL;
|
|
163
162
|
|
|
164
163
|
try {
|
|
165
|
-
url =
|
|
164
|
+
url = assertPublicUrl(record.location);
|
|
166
165
|
} catch {
|
|
167
166
|
return {
|
|
168
167
|
status: "invalid",
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { createDb } from "@open-workspace/core/module";
|
|
2
|
+
import { mailboxes } from "@open-workspace/core/schema";
|
|
3
|
+
import { eq } from "drizzle-orm";
|
|
4
|
+
|
|
5
|
+
import { mailboxReadCounts, signatures } from "../db/schema";
|
|
6
|
+
import { mailboxStub } from "../mailbox";
|
|
7
|
+
import { deleteRichImages } from "./rich-content";
|
|
8
|
+
import { signatureImagePrefix } from "./signatures";
|
|
9
|
+
import { composerUploadsOf, draftFileKey } from "./uploads";
|
|
10
|
+
|
|
11
|
+
/** Deletes every object under `prefix`, a page at a time. */
|
|
12
|
+
async function deletePrefix(bucket: R2Bucket, prefix: string) {
|
|
13
|
+
let cursor: string | undefined;
|
|
14
|
+
|
|
15
|
+
do {
|
|
16
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- R2 lists one page at a time
|
|
17
|
+
const page = await bucket.list({ prefix, limit: 1000, cursor });
|
|
18
|
+
const keys = page.objects.map((object) => object.key);
|
|
19
|
+
|
|
20
|
+
// oxlint-disable-next-line eslint/no-await-in-loop -- see above
|
|
21
|
+
if (keys.length) await bucket.delete(keys);
|
|
22
|
+
cursor = page.truncated ? page.cursor : undefined;
|
|
23
|
+
} while (cursor);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Core's `personErased` hook: what mail keeps for a person outside their own
|
|
28
|
+
* addresses (core deletes those next, through `mailboxDeleted`). In every
|
|
29
|
+
* mailbox that keeps mail: their drafts and files, uploads, read state and
|
|
30
|
+
* spam undo receipts; and their signature, read counts and its images.
|
|
31
|
+
* Push subscriptions and notification preferences go with their row. The
|
|
32
|
+
* mail archive is write-once and keeps what it has. Each step finds
|
|
33
|
+
* nothing the second time, so a retry after a failure is safe.
|
|
34
|
+
*/
|
|
35
|
+
export async function erasePersonMail(env: Env, userId: string) {
|
|
36
|
+
const db = createDb(env.DB);
|
|
37
|
+
|
|
38
|
+
const inboxes = await db
|
|
39
|
+
.select({ id: mailboxes.id })
|
|
40
|
+
.from(mailboxes)
|
|
41
|
+
.where(eq(mailboxes.delivery, "inbox"));
|
|
42
|
+
|
|
43
|
+
await Promise.all(
|
|
44
|
+
inboxes.map(({ id }) =>
|
|
45
|
+
Promise.all([
|
|
46
|
+
mailboxStub(env, id).forgetPerson(userId),
|
|
47
|
+
deletePrefix(env.MAIL_STORAGE, draftFileKey(id, userId, "")),
|
|
48
|
+
deletePrefix(env.MAIL_STORAGE, composerUploadsOf(id, userId)),
|
|
49
|
+
])
|
|
50
|
+
)
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
await db.batch([
|
|
54
|
+
db.delete(mailboxReadCounts).where(eq(mailboxReadCounts.userId, userId)),
|
|
55
|
+
db.delete(signatures).where(eq(signatures.userId, userId)),
|
|
56
|
+
]);
|
|
57
|
+
await deleteRichImages(env.MAIL_STORAGE, signatureImagePrefix(userId));
|
|
58
|
+
}
|