@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
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
type SenderVerdictReason,
|
|
11
|
-
type UpstreamAuthenticationClaim,
|
|
1
|
+
import { authResults } from "../../shared/mail-authentication";
|
|
2
|
+
import type {
|
|
3
|
+
ArcSealer,
|
|
4
|
+
AuthResult,
|
|
5
|
+
DkimResult,
|
|
6
|
+
DmarcPolicy,
|
|
7
|
+
InboundAuthentication,
|
|
8
|
+
ListUnsubscribe,
|
|
9
|
+
UpstreamAuthenticationClaim,
|
|
12
10
|
} from "../../shared/mail-authentication";
|
|
13
11
|
import {
|
|
14
12
|
addressDomain,
|
|
@@ -16,8 +14,8 @@ import {
|
|
|
16
14
|
scanComments,
|
|
17
15
|
splitTopLevel,
|
|
18
16
|
unquote,
|
|
19
|
-
type HeaderField,
|
|
20
17
|
} from "../../shared/mail-headers";
|
|
18
|
+
import type { HeaderField } from "../../shared/mail-headers";
|
|
21
19
|
import {
|
|
22
20
|
isCloudflareHop,
|
|
23
21
|
normalizeIp,
|
|
@@ -27,7 +25,8 @@ import { relaxedAligned } from "./org-domain";
|
|
|
27
25
|
|
|
28
26
|
export const CLOUDFLARE_AUTHSERV_ID = "mx.cloudflare.net";
|
|
29
27
|
const CLOUDFLARE_ARC_DOMAIN = "cloudflare-email.net";
|
|
30
|
-
const MAX_TRUSTED_HEADER_LENGTH =
|
|
28
|
+
const MAX_TRUSTED_HEADER_LENGTH = 4096;
|
|
29
|
+
|
|
31
30
|
/** Cloudflare prepends 7 headers; the cap only matters when the score header is missing. */
|
|
32
31
|
const MAX_TRUSTED_BLOCK_WITHOUT_SCORE = 10;
|
|
33
32
|
const MAX_UPSTREAM_CLAIMS = 5;
|
|
@@ -70,22 +69,38 @@ const VALUE = String.raw`"(?:[^"\\]|\\.)*"|[^\s;]+`;
|
|
|
70
69
|
function parseResInfo(segment: string): AuthResInfo | null {
|
|
71
70
|
const { text, comments } = scanComments(segment);
|
|
72
71
|
const method = /^\s*([a-z0-9_-]+)(?:\/\d+)?\s*=\s*([a-z0-9_-]+)/iu.exec(text);
|
|
72
|
+
|
|
73
73
|
if (!method) return null;
|
|
74
74
|
let rest = text.slice(method[0].length);
|
|
75
75
|
let reason: string | null = null;
|
|
76
|
-
|
|
76
|
+
|
|
77
|
+
const reasonMatch = new RegExp(
|
|
78
|
+
String.raw`\breason\s*=\s*(${VALUE})`,
|
|
79
|
+
"iu"
|
|
80
|
+
).exec(rest);
|
|
81
|
+
|
|
77
82
|
if (reasonMatch) {
|
|
78
|
-
reason = unquote(reasonMatch[1]
|
|
79
|
-
rest =
|
|
83
|
+
reason = unquote(reasonMatch[1]);
|
|
84
|
+
rest =
|
|
85
|
+
rest.slice(0, reasonMatch.index) +
|
|
86
|
+
rest.slice(reasonMatch.index + reasonMatch[0].length);
|
|
80
87
|
}
|
|
88
|
+
|
|
81
89
|
const props: Record<string, string> = {};
|
|
82
|
-
|
|
90
|
+
|
|
91
|
+
for (const match of rest.matchAll(
|
|
92
|
+
new RegExp(
|
|
93
|
+
String.raw`([a-z0-9_-]+)\s*\.\s*([a-z0-9_-]+)\s*=\s*(${VALUE})`,
|
|
94
|
+
"giu"
|
|
95
|
+
)
|
|
96
|
+
)) {
|
|
83
97
|
const key = `${match[1]}.${match[2]}`.toLocaleLowerCase();
|
|
84
|
-
props[key] ??= unquote(match[3]
|
|
98
|
+
props[key] ??= unquote(match[3]);
|
|
85
99
|
}
|
|
100
|
+
|
|
86
101
|
return {
|
|
87
|
-
method: method[1]
|
|
88
|
-
result: method[2]
|
|
102
|
+
method: method[1].toLocaleLowerCase(),
|
|
103
|
+
result: method[2].toLocaleLowerCase(),
|
|
89
104
|
reason,
|
|
90
105
|
comments,
|
|
91
106
|
props,
|
|
@@ -93,14 +108,23 @@ function parseResInfo(segment: string): AuthResInfo | null {
|
|
|
93
108
|
}
|
|
94
109
|
|
|
95
110
|
/** Parses an Authentication-Results value (RFC 8601 §2.2), comments and all. */
|
|
96
|
-
export function parseAuthenticationResults(
|
|
111
|
+
export function parseAuthenticationResults(
|
|
112
|
+
value: string
|
|
113
|
+
): ParsedAuthenticationResults | null {
|
|
97
114
|
const segments = splitTopLevel(value, ";");
|
|
98
|
-
|
|
115
|
+
|
|
116
|
+
const authservId = scanComments(segments[0] ?? "")
|
|
117
|
+
.text.trim()
|
|
118
|
+
.split(/\s+/u)[0]
|
|
119
|
+
?.toLocaleLowerCase();
|
|
120
|
+
|
|
99
121
|
if (!authservId) return null;
|
|
122
|
+
|
|
100
123
|
return {
|
|
101
124
|
authservId,
|
|
102
125
|
results: segments.slice(1).flatMap((segment) => {
|
|
103
126
|
const info = parseResInfo(segment);
|
|
127
|
+
|
|
104
128
|
return info ? [info] : [];
|
|
105
129
|
}),
|
|
106
130
|
};
|
|
@@ -109,8 +133,10 @@ export function parseAuthenticationResults(value: string): ParsedAuthenticationR
|
|
|
109
133
|
/** ARC-Authentication-Results carries `i=N;` before the A-R payload. */
|
|
110
134
|
export function parseArcAuthenticationResults(value: string) {
|
|
111
135
|
const instance = /^\s*i\s*=\s*(\d+)\s*;/u.exec(value);
|
|
136
|
+
|
|
112
137
|
if (!instance) return null;
|
|
113
138
|
const parsed = parseAuthenticationResults(value.slice(instance[0].length));
|
|
139
|
+
|
|
114
140
|
return parsed ? { instance: Number(instance[1]), ...parsed } : null;
|
|
115
141
|
}
|
|
116
142
|
|
|
@@ -124,15 +150,22 @@ export type ParsedReceivedSpf = {
|
|
|
124
150
|
|
|
125
151
|
export function parseReceivedSpf(value: string): ParsedReceivedSpf | null {
|
|
126
152
|
const result = /^\s*([a-z]+)/iu.exec(value);
|
|
153
|
+
|
|
127
154
|
if (!result) return null;
|
|
128
155
|
const { text, comments } = scanComments(value.slice(result[0].length));
|
|
129
156
|
const pairs: Record<string, string> = {};
|
|
157
|
+
|
|
130
158
|
for (const part of splitTopLevel(text, ";")) {
|
|
131
|
-
const pair = new RegExp(
|
|
132
|
-
|
|
159
|
+
const pair = new RegExp(
|
|
160
|
+
String.raw`^\s*([a-z-]+)\s*=\s*(${VALUE})\s*$`,
|
|
161
|
+
"iu"
|
|
162
|
+
).exec(part);
|
|
163
|
+
|
|
164
|
+
if (pair) pairs[pair[1].toLocaleLowerCase()] ??= unquote(pair[2]);
|
|
133
165
|
}
|
|
166
|
+
|
|
134
167
|
return {
|
|
135
|
-
result: result[1]
|
|
168
|
+
result: result[1].toLocaleLowerCase(),
|
|
136
169
|
comment: comments[0] ?? null,
|
|
137
170
|
pairs,
|
|
138
171
|
};
|
|
@@ -142,33 +175,49 @@ export function parseReceivedSpf(value: string): ParsedReceivedSpf | null {
|
|
|
142
175
|
|
|
143
176
|
export function parseTagList(value: string) {
|
|
144
177
|
const tags: Record<string, string> = {};
|
|
178
|
+
|
|
145
179
|
for (const part of value.split(";")) {
|
|
146
180
|
const equals = part.indexOf("=");
|
|
181
|
+
|
|
147
182
|
if (equals < 1) continue;
|
|
148
183
|
const name = part.slice(0, equals).trim().toLocaleLowerCase();
|
|
184
|
+
|
|
149
185
|
if (!/^[a-z][a-z0-9_]*$/u.test(name) || name in tags) continue;
|
|
150
|
-
tags[name] = part
|
|
186
|
+
tags[name] = part
|
|
187
|
+
.slice(equals + 1)
|
|
188
|
+
.replaceAll(/\s+/gu, name === "h" ? "" : " ")
|
|
189
|
+
.trim();
|
|
151
190
|
}
|
|
191
|
+
|
|
152
192
|
return tags;
|
|
153
193
|
}
|
|
154
194
|
|
|
155
|
-
type DkimSignatureTags = {
|
|
195
|
+
type DkimSignatureTags = {
|
|
196
|
+
domain: string;
|
|
197
|
+
selector: string | null;
|
|
198
|
+
b: string;
|
|
199
|
+
signedHeaders: string[];
|
|
200
|
+
};
|
|
156
201
|
|
|
157
202
|
function dkimSignatureTags(fields: HeaderField[]): DkimSignatureTags[] {
|
|
158
203
|
return fields.flatMap((field) => {
|
|
159
204
|
if (field.name !== "dkim-signature") return [];
|
|
160
205
|
const tags = parseTagList(field.value);
|
|
161
206
|
const domain = addressDomain(tags.d);
|
|
207
|
+
|
|
162
208
|
if (!domain) return [];
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
.
|
|
169
|
-
.
|
|
170
|
-
|
|
171
|
-
|
|
209
|
+
|
|
210
|
+
return [
|
|
211
|
+
{
|
|
212
|
+
domain,
|
|
213
|
+
selector: tags.s?.trim() ?? null,
|
|
214
|
+
b: (tags.b ?? "").replaceAll(/\s+/gu, ""),
|
|
215
|
+
signedHeaders: (tags.h ?? "")
|
|
216
|
+
.split(":")
|
|
217
|
+
.map((name) => name.trim().toLocaleLowerCase())
|
|
218
|
+
.filter(Boolean),
|
|
219
|
+
},
|
|
220
|
+
];
|
|
172
221
|
});
|
|
173
222
|
}
|
|
174
223
|
|
|
@@ -180,26 +229,50 @@ function dkimSignatureTags(fields: HeaderField[]): DkimSignatureTags[] {
|
|
|
180
229
|
*/
|
|
181
230
|
export function trustedHeaderBlock(fields: HeaderField[]) {
|
|
182
231
|
const first = fields[0];
|
|
183
|
-
|
|
232
|
+
|
|
233
|
+
if (first?.name !== "received") return null;
|
|
234
|
+
const hop = parseReceived(first.value);
|
|
235
|
+
|
|
236
|
+
if (!isCloudflareHop(hop)) return null;
|
|
237
|
+
|
|
184
238
|
const score = fields.findIndex((field) => field.name === "x-cf-spamh-score");
|
|
185
|
-
|
|
186
|
-
|
|
239
|
+
|
|
240
|
+
if (score !== -1) {
|
|
241
|
+
return {
|
|
242
|
+
hop,
|
|
243
|
+
trusted: fields.slice(0, score + 1),
|
|
244
|
+
below: fields.slice(score + 1),
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
const nextReceived = fields.findIndex(
|
|
249
|
+
(field, index) => index > 0 && field.name === "received"
|
|
250
|
+
);
|
|
251
|
+
|
|
187
252
|
const end = Math.min(
|
|
188
|
-
nextReceived
|
|
189
|
-
MAX_TRUSTED_BLOCK_WITHOUT_SCORE
|
|
253
|
+
nextReceived === -1 ? fields.length : nextReceived,
|
|
254
|
+
MAX_TRUSTED_BLOCK_WITHOUT_SCORE
|
|
190
255
|
);
|
|
191
|
-
|
|
256
|
+
|
|
257
|
+
return { hop, trusted: fields.slice(0, end), below: fields.slice(end) };
|
|
192
258
|
}
|
|
193
259
|
|
|
194
260
|
function authResult(value: string | null | undefined): AuthResult | null {
|
|
195
261
|
return authResults.find((result) => result === value) ?? null;
|
|
196
262
|
}
|
|
197
263
|
|
|
198
|
-
function claimFrom(
|
|
264
|
+
function claimFrom(
|
|
265
|
+
parsed: ParsedAuthenticationResults
|
|
266
|
+
): UpstreamAuthenticationClaim {
|
|
199
267
|
const first = (method: string) =>
|
|
200
|
-
authResult(
|
|
201
|
-
|
|
202
|
-
|
|
268
|
+
authResult(
|
|
269
|
+
parsed.results.find(
|
|
270
|
+
(result) =>
|
|
271
|
+
result.method === method &&
|
|
272
|
+
(method !== "spf" || !result.props["smtp.helo"])
|
|
273
|
+
)?.result
|
|
274
|
+
);
|
|
275
|
+
|
|
203
276
|
return {
|
|
204
277
|
authservId: parsed.authservId,
|
|
205
278
|
spf: first("spf"),
|
|
@@ -210,53 +283,83 @@ function claimFrom(parsed: ParsedAuthenticationResults): UpstreamAuthenticationC
|
|
|
210
283
|
|
|
211
284
|
function dmarcPolicy(value: string | undefined): DmarcPolicy | null {
|
|
212
285
|
const normalized = value?.toLocaleLowerCase();
|
|
213
|
-
|
|
286
|
+
|
|
287
|
+
return normalized === "none" ||
|
|
288
|
+
normalized === "quarantine" ||
|
|
289
|
+
normalized === "reject"
|
|
214
290
|
? normalized
|
|
215
291
|
: null;
|
|
216
292
|
}
|
|
217
293
|
|
|
218
|
-
function matchSignature(
|
|
219
|
-
|
|
294
|
+
function matchSignature(
|
|
295
|
+
result: AuthResInfo,
|
|
296
|
+
domain: string,
|
|
297
|
+
signatures: DkimSignatureTags[]
|
|
298
|
+
) {
|
|
299
|
+
const bPrefix = result.props["header.b"]?.replaceAll(/\s+/gu, "");
|
|
220
300
|
const selector = result.props["header.s"];
|
|
301
|
+
|
|
221
302
|
const sameDomain = (signature: DkimSignatureTags) =>
|
|
222
303
|
signature.domain === domain || domain.endsWith(`.${signature.domain}`);
|
|
304
|
+
|
|
223
305
|
if (bPrefix) {
|
|
224
|
-
const match = signatures.find(
|
|
306
|
+
const match = signatures.find(
|
|
307
|
+
(signature) => sameDomain(signature) && signature.b.startsWith(bPrefix)
|
|
308
|
+
);
|
|
309
|
+
|
|
225
310
|
if (match) return match;
|
|
226
311
|
}
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
312
|
+
|
|
313
|
+
return (
|
|
314
|
+
signatures.find(
|
|
315
|
+
(signature) =>
|
|
316
|
+
sameDomain(signature) && (!selector || signature.selector === selector)
|
|
317
|
+
) ?? null
|
|
318
|
+
);
|
|
230
319
|
}
|
|
231
320
|
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
321
|
+
type Verdict = Pick<InboundAuthentication, "verdict" | "verdictReason">;
|
|
322
|
+
|
|
323
|
+
function verdictFor(
|
|
324
|
+
input: Pick<InboundAuthentication, "spf" | "dkim" | "dmarc" | "arc">
|
|
325
|
+
): Verdict {
|
|
326
|
+
const alignedDkimPass = input.dkim.some(
|
|
327
|
+
(dkim) => dkim.result === "pass" && dkim.aligned
|
|
328
|
+
);
|
|
329
|
+
|
|
237
330
|
const alignedSpfPass = input.spf?.result === "pass" && input.spf.aligned;
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
331
|
+
|
|
332
|
+
if (input.dmarc?.result === "pass") {
|
|
333
|
+
return { verdict: "verified", verdictReason: "dmarc_pass" };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const failing =
|
|
337
|
+
input.dmarc?.result === "fail"
|
|
338
|
+
? ("dmarc_fail" as const)
|
|
339
|
+
: !alignedDkimPass && !alignedSpfPass
|
|
340
|
+
? ("no_aligned_pass" as const)
|
|
341
|
+
: null;
|
|
342
|
+
|
|
244
343
|
if (failing) {
|
|
245
|
-
const arc = input
|
|
344
|
+
const { arc } = input;
|
|
246
345
|
const oldestSealer = arc?.sealers[0]?.domain;
|
|
346
|
+
|
|
247
347
|
if (
|
|
248
|
-
arc?.result === "pass"
|
|
249
|
-
|
|
250
|
-
&&
|
|
251
|
-
|
|
348
|
+
arc?.result === "pass" &&
|
|
349
|
+
arc.oldest?.dmarc === "pass" &&
|
|
350
|
+
oldestSealer &&
|
|
351
|
+
TRUSTED_ARC_SEALERS.has(oldestSealer)
|
|
252
352
|
) {
|
|
253
353
|
return { verdict: "unverified", verdictReason: "forwarded_arc" };
|
|
254
354
|
}
|
|
355
|
+
|
|
255
356
|
return { verdict: "suspicious", verdictReason: failing };
|
|
256
357
|
}
|
|
358
|
+
|
|
257
359
|
if (alignedDkimPass && (!input.dmarc || input.dmarc.result === "none")) {
|
|
258
360
|
return { verdict: "verified", verdictReason: "aligned_dkim" };
|
|
259
361
|
}
|
|
362
|
+
|
|
260
363
|
return { verdict: "unverified", verdictReason: "partial" };
|
|
261
364
|
}
|
|
262
365
|
|
|
@@ -264,7 +367,7 @@ function emptyAuthentication(
|
|
|
264
367
|
now: number,
|
|
265
368
|
fromDomain: string | null,
|
|
266
369
|
reason: "not_cloudflare" | "missing_results",
|
|
267
|
-
envelopeFrom: string | null
|
|
370
|
+
envelopeFrom: string | null
|
|
268
371
|
): InboundAuthentication {
|
|
269
372
|
return {
|
|
270
373
|
source: "headers",
|
|
@@ -291,57 +394,98 @@ function emptyAuthentication(
|
|
|
291
394
|
|
|
292
395
|
function claimsCloudflare(field: HeaderField) {
|
|
293
396
|
if (field.name === "authentication-results") {
|
|
294
|
-
return
|
|
397
|
+
return (
|
|
398
|
+
parseAuthenticationResults(field.value)?.authservId ===
|
|
399
|
+
CLOUDFLARE_AUTHSERV_ID
|
|
400
|
+
);
|
|
295
401
|
}
|
|
402
|
+
|
|
296
403
|
if (field.name === "arc-authentication-results") {
|
|
297
|
-
return
|
|
404
|
+
return (
|
|
405
|
+
parseArcAuthenticationResults(field.value)?.authservId ===
|
|
406
|
+
CLOUDFLARE_AUTHSERV_ID
|
|
407
|
+
);
|
|
298
408
|
}
|
|
409
|
+
|
|
299
410
|
if (field.name === "received-spf") {
|
|
300
|
-
return
|
|
411
|
+
return (
|
|
412
|
+
parseReceivedSpf(field.value)?.pairs.receiver?.toLocaleLowerCase() ===
|
|
413
|
+
CLOUDFLARE_AUTHSERV_ID
|
|
414
|
+
);
|
|
301
415
|
}
|
|
416
|
+
|
|
302
417
|
if (field.name === "x-cf-spamh-score") return true;
|
|
418
|
+
|
|
303
419
|
return false;
|
|
304
420
|
}
|
|
305
421
|
|
|
306
422
|
function arcSummary(
|
|
307
423
|
trusted: HeaderField[],
|
|
308
424
|
all: HeaderField[],
|
|
309
|
-
arcResult: AuthResInfo | undefined
|
|
425
|
+
arcResult: AuthResInfo | undefined
|
|
310
426
|
): InboundAuthentication["arc"] {
|
|
311
|
-
const ownSeal = trusted.find(
|
|
312
|
-
|
|
427
|
+
const ownSeal = trusted.find(
|
|
428
|
+
(field) =>
|
|
429
|
+
field.name === "arc-seal" &&
|
|
430
|
+
addressDomain(parseTagList(field.value).d) === CLOUDFLARE_ARC_DOMAIN
|
|
313
431
|
);
|
|
314
|
-
|
|
315
|
-
const
|
|
316
|
-
?
|
|
317
|
-
|
|
432
|
+
|
|
433
|
+
const ownInstance = Number(
|
|
434
|
+
ownSeal ? parseTagList(ownSeal.value).i : Number.NaN
|
|
435
|
+
);
|
|
436
|
+
|
|
437
|
+
const result =
|
|
438
|
+
arcResult?.result === "pass" || arcResult?.result === "fail"
|
|
439
|
+
? arcResult.result
|
|
440
|
+
: "none";
|
|
441
|
+
|
|
318
442
|
if (!arcResult && !ownSeal) return null;
|
|
319
|
-
|
|
443
|
+
|
|
444
|
+
const chainLength =
|
|
445
|
+
Number.isInteger(ownInstance) && ownInstance > 0 ? ownInstance : 1;
|
|
446
|
+
|
|
320
447
|
const sealers = new Map<number, ArcSealer>();
|
|
448
|
+
|
|
321
449
|
for (const field of all) {
|
|
322
450
|
if (field.name !== "arc-seal") continue;
|
|
323
451
|
const tags = parseTagList(field.value);
|
|
324
452
|
const instance = Number(tags.i);
|
|
325
|
-
|
|
453
|
+
|
|
454
|
+
if (
|
|
455
|
+
!Number.isInteger(instance) ||
|
|
456
|
+
instance < 1 ||
|
|
457
|
+
instance >= chainLength ||
|
|
458
|
+
sealers.has(instance)
|
|
459
|
+
) {
|
|
460
|
+
continue;
|
|
461
|
+
}
|
|
462
|
+
|
|
326
463
|
sealers.set(instance, {
|
|
327
464
|
instance,
|
|
328
465
|
domain: addressDomain(tags.d),
|
|
329
466
|
cv: tags.cv?.toLocaleLowerCase() ?? null,
|
|
330
467
|
});
|
|
331
468
|
}
|
|
469
|
+
|
|
332
470
|
let oldest: UpstreamAuthenticationClaim | null = null;
|
|
471
|
+
|
|
333
472
|
if (result === "pass" && chainLength > 1) {
|
|
334
473
|
const first = all.flatMap((field) => {
|
|
335
474
|
if (field.name !== "arc-authentication-results") return [];
|
|
336
475
|
const parsed = parseArcAuthenticationResults(field.value);
|
|
476
|
+
|
|
337
477
|
return parsed?.instance === 1 ? [parsed] : [];
|
|
338
478
|
})[0];
|
|
479
|
+
|
|
339
480
|
oldest = first ? claimFrom(first) : null;
|
|
340
481
|
}
|
|
482
|
+
|
|
341
483
|
return {
|
|
342
484
|
result,
|
|
343
485
|
chainLength,
|
|
344
|
-
sealers: [...sealers.values()].sort(
|
|
486
|
+
sealers: [...sealers.values()].sort(
|
|
487
|
+
(left, right) => left.instance - right.instance
|
|
488
|
+
),
|
|
345
489
|
oldest,
|
|
346
490
|
};
|
|
347
491
|
}
|
|
@@ -361,37 +505,69 @@ export function parseInboundAuthentication(input: {
|
|
|
361
505
|
const fields = headerFields(input.raw);
|
|
362
506
|
const block = trustedHeaderBlock(fields);
|
|
363
507
|
const fromDomainHint = addressDomain(input.fromAddress);
|
|
508
|
+
|
|
364
509
|
if (!block) {
|
|
365
|
-
return emptyAuthentication(
|
|
510
|
+
return emptyAuthentication(
|
|
511
|
+
now,
|
|
512
|
+
fromDomainHint,
|
|
513
|
+
"not_cloudflare",
|
|
514
|
+
input.envelopeFrom ?? null
|
|
515
|
+
);
|
|
366
516
|
}
|
|
517
|
+
|
|
367
518
|
const authenticationResults = block.trusted.flatMap((field) => {
|
|
368
519
|
if (field.name !== "authentication-results") return [];
|
|
369
520
|
const parsed = parseAuthenticationResults(field.value);
|
|
370
|
-
|
|
521
|
+
|
|
522
|
+
return parsed?.authservId === CLOUDFLARE_AUTHSERV_ID
|
|
523
|
+
? [{ field, parsed }]
|
|
524
|
+
: [];
|
|
371
525
|
})[0];
|
|
526
|
+
|
|
372
527
|
const receivedSpf = block.trusted.flatMap((field) => {
|
|
373
528
|
if (field.name !== "received-spf") return [];
|
|
374
529
|
const parsed = parseReceivedSpf(field.value);
|
|
375
|
-
|
|
530
|
+
|
|
531
|
+
return parsed?.pairs.receiver?.toLocaleLowerCase() ===
|
|
532
|
+
CLOUDFLARE_AUTHSERV_ID
|
|
533
|
+
? [parsed]
|
|
534
|
+
: [];
|
|
376
535
|
})[0];
|
|
377
|
-
|
|
378
|
-
const
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
536
|
+
|
|
537
|
+
const scoreField = block.trusted.find(
|
|
538
|
+
(field) => field.name === "x-cf-spamh-score"
|
|
539
|
+
);
|
|
540
|
+
|
|
541
|
+
const score =
|
|
542
|
+
scoreField && /^\s*\d+(?:\.\d+)?\s*$/u.test(scoreField.value)
|
|
543
|
+
? Number(scoreField.value)
|
|
544
|
+
: null;
|
|
545
|
+
|
|
546
|
+
const upstream = block.below
|
|
547
|
+
.flatMap((field) => {
|
|
548
|
+
if (field.name !== "authentication-results") return [];
|
|
549
|
+
const parsed = parseAuthenticationResults(field.value);
|
|
550
|
+
|
|
551
|
+
return parsed ? [claimFrom(parsed)] : [];
|
|
552
|
+
})
|
|
553
|
+
.slice(0, MAX_UPSTREAM_CLAIMS);
|
|
554
|
+
|
|
386
555
|
const ignoredCloudflareClaims = block.below.filter(claimsCloudflare).length;
|
|
387
|
-
const cloudflareHop =
|
|
556
|
+
const cloudflareHop = block.hop;
|
|
388
557
|
|
|
389
558
|
if (!authenticationResults) {
|
|
390
559
|
return {
|
|
391
|
-
...emptyAuthentication(
|
|
392
|
-
|
|
560
|
+
...emptyAuthentication(
|
|
561
|
+
now,
|
|
562
|
+
fromDomainHint,
|
|
563
|
+
"missing_results",
|
|
564
|
+
input.envelopeFrom ?? null
|
|
565
|
+
),
|
|
566
|
+
remoteIp:
|
|
567
|
+
normalizeIp(receivedSpf?.pairs["client-ip"]) ?? cloudflareHop.ip,
|
|
393
568
|
helo: receivedSpf?.pairs.helo ?? cloudflareHop.from,
|
|
394
|
-
envelopeFrom:
|
|
569
|
+
envelopeFrom:
|
|
570
|
+
receivedSpf?.pairs["envelope-from"] || input.envelopeFrom || null,
|
|
395
571
|
spamScore: score,
|
|
396
572
|
upstream,
|
|
397
573
|
ignoredCloudflareClaims,
|
|
@@ -400,88 +576,138 @@ export function parseInboundAuthentication(input: {
|
|
|
400
576
|
|
|
401
577
|
const { results } = authenticationResults.parsed;
|
|
402
578
|
const dmarcResult = results.find((result) => result.method === "dmarc");
|
|
403
|
-
|
|
579
|
+
|
|
580
|
+
const fromDomain =
|
|
581
|
+
fromDomainHint ?? addressDomain(dmarcResult?.props["header.from"]);
|
|
582
|
+
|
|
404
583
|
const spfResults = results.filter((result) => result.method === "spf");
|
|
405
|
-
|
|
406
|
-
|
|
584
|
+
|
|
585
|
+
const spfMailFrom =
|
|
586
|
+
spfResults.find((result) => result.props["smtp.mailfrom"]) ??
|
|
587
|
+
spfResults.find((result) => !result.props["smtp.helo"]);
|
|
588
|
+
|
|
407
589
|
const spfHelo = spfResults.find((result) => result.props["smtp.helo"]);
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
||
|
|
411
|
-
||
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
590
|
+
|
|
591
|
+
const envelopeFrom =
|
|
592
|
+
spfMailFrom?.props["smtp.mailfrom"] ||
|
|
593
|
+
receivedSpf?.pairs["envelope-from"] ||
|
|
594
|
+
input.envelopeFrom ||
|
|
595
|
+
null;
|
|
596
|
+
|
|
597
|
+
const remoteIp =
|
|
598
|
+
normalizeIp(
|
|
599
|
+
results.find((result) => result.props["smtp.remote-ip"])?.props[
|
|
600
|
+
"smtp.remote-ip"
|
|
601
|
+
]
|
|
602
|
+
) ??
|
|
603
|
+
normalizeIp(receivedSpf?.pairs["client-ip"]) ??
|
|
604
|
+
cloudflareHop.ip;
|
|
415
605
|
|
|
416
606
|
const signatures = dkimSignatureTags(fields);
|
|
607
|
+
|
|
417
608
|
const dkim: DkimResult[] = results.flatMap((result) => {
|
|
418
609
|
if (result.method !== "dkim") return [];
|
|
419
610
|
const verdict = authResult(result.result);
|
|
420
611
|
const identity = result.props["header.i"] ?? null;
|
|
421
|
-
|
|
612
|
+
|
|
613
|
+
const domain =
|
|
614
|
+
addressDomain(result.props["header.d"]) ?? addressDomain(identity);
|
|
615
|
+
|
|
422
616
|
if (!verdict || !domain) return [];
|
|
423
617
|
const signature = matchSignature(result, domain, signatures);
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
618
|
+
|
|
619
|
+
return [
|
|
620
|
+
{
|
|
621
|
+
result: verdict,
|
|
622
|
+
domain,
|
|
623
|
+
identity,
|
|
624
|
+
selector: result.props["header.s"] ?? null,
|
|
625
|
+
bPrefix: result.props["header.b"] ?? null,
|
|
626
|
+
aligned: relaxedAligned(domain, fromDomain),
|
|
627
|
+
signedHeaders: signature?.signedHeaders ?? null,
|
|
628
|
+
},
|
|
629
|
+
];
|
|
433
630
|
});
|
|
434
631
|
|
|
435
632
|
const spfVerdict = authResult(spfMailFrom?.result);
|
|
436
633
|
const spfDomain = addressDomain(envelopeFrom);
|
|
634
|
+
|
|
437
635
|
const spf = spfVerdict
|
|
438
|
-
? {
|
|
636
|
+
? {
|
|
637
|
+
result: spfVerdict,
|
|
638
|
+
domain: spfDomain,
|
|
639
|
+
aligned: relaxedAligned(spfDomain, fromDomain),
|
|
640
|
+
}
|
|
439
641
|
: null;
|
|
642
|
+
|
|
440
643
|
const spfHeloVerdict = authResult(spfHelo?.result);
|
|
441
644
|
const dmarcVerdict = authResult(dmarcResult?.result);
|
|
645
|
+
|
|
442
646
|
const dmarc = dmarcVerdict
|
|
443
647
|
? {
|
|
444
648
|
result: dmarcVerdict,
|
|
445
649
|
headerFrom: addressDomain(dmarcResult?.props["header.from"]),
|
|
446
650
|
policy: dmarcPolicy(dmarcResult?.props["policy.dmarc"]),
|
|
447
651
|
alignedVia: [
|
|
448
|
-
...(dkim.some((item) => item.result === "pass" && item.aligned)
|
|
652
|
+
...(dkim.some((item) => item.result === "pass" && item.aligned)
|
|
653
|
+
? ["dkim" as const]
|
|
654
|
+
: []),
|
|
449
655
|
...(spf?.result === "pass" && spf.aligned ? ["spf" as const] : []),
|
|
450
656
|
],
|
|
451
657
|
}
|
|
452
658
|
: null;
|
|
453
|
-
|
|
659
|
+
|
|
660
|
+
const arc = arcSummary(
|
|
661
|
+
block.trusted,
|
|
662
|
+
fields,
|
|
663
|
+
results.find((result) => result.method === "arc")
|
|
664
|
+
);
|
|
665
|
+
|
|
454
666
|
const parts = { spf, dkim, dmarc, arc };
|
|
667
|
+
|
|
455
668
|
return {
|
|
456
669
|
source: "headers",
|
|
457
670
|
version: 1,
|
|
458
671
|
authservId: CLOUDFLARE_AUTHSERV_ID,
|
|
459
672
|
parsedAt: now,
|
|
460
673
|
remoteIp,
|
|
461
|
-
helo:
|
|
674
|
+
helo:
|
|
675
|
+
spfHelo?.props["smtp.helo"] ??
|
|
676
|
+
receivedSpf?.pairs.helo ??
|
|
677
|
+
cloudflareHop.from,
|
|
462
678
|
envelopeFrom,
|
|
463
679
|
fromDomain,
|
|
464
680
|
...parts,
|
|
465
681
|
spfHelo: spfHeloVerdict
|
|
466
|
-
? {
|
|
682
|
+
? {
|
|
683
|
+
result: spfHeloVerdict,
|
|
684
|
+
domain: addressDomain(spfHelo?.props["smtp.helo"]),
|
|
685
|
+
}
|
|
467
686
|
: null,
|
|
468
687
|
spamScore: score,
|
|
469
688
|
...verdictFor(parts),
|
|
470
689
|
upstream,
|
|
471
690
|
ignoredCloudflareClaims,
|
|
472
|
-
trustedHeader: authenticationResults.field.value.slice(
|
|
691
|
+
trustedHeader: authenticationResults.field.value.slice(
|
|
692
|
+
0,
|
|
693
|
+
MAX_TRUSTED_HEADER_LENGTH
|
|
694
|
+
),
|
|
473
695
|
};
|
|
474
696
|
}
|
|
475
697
|
|
|
476
698
|
// --- List-Unsubscribe (RFC 2369 / RFC 8058) --------------------------------
|
|
477
699
|
|
|
478
700
|
function httpsUnsubscribeUrl(value: string) {
|
|
479
|
-
if (value.length >
|
|
701
|
+
if (value.length > 2048) return null;
|
|
702
|
+
|
|
480
703
|
try {
|
|
481
704
|
const url = new URL(value);
|
|
705
|
+
|
|
482
706
|
if (url.protocol !== "https:" || url.username || url.password) return null;
|
|
483
|
-
const host = url.hostname.
|
|
707
|
+
const host = url.hostname.replaceAll(/^\[|\]$/gu, "");
|
|
708
|
+
|
|
484
709
|
if (normalizeIp(host) || !host.includes(".")) return null;
|
|
710
|
+
|
|
485
711
|
return url.toString();
|
|
486
712
|
} catch {
|
|
487
713
|
return null;
|
|
@@ -489,11 +715,15 @@ function httpsUnsubscribeUrl(value: string) {
|
|
|
489
715
|
}
|
|
490
716
|
|
|
491
717
|
function mailtoUnsubscribeUrl(value: string) {
|
|
492
|
-
if (value.length >
|
|
718
|
+
if (value.length > 2048 || !/^mailto:/iu.test(value)) return null;
|
|
719
|
+
|
|
493
720
|
try {
|
|
494
721
|
const url = new URL(value);
|
|
495
722
|
const recipient = decodeURIComponent(url.pathname);
|
|
496
|
-
|
|
723
|
+
|
|
724
|
+
return /^[^\s@,]+@[^\s@,]+\.[^\s@,]+$/u.test(recipient)
|
|
725
|
+
? url.toString()
|
|
726
|
+
: null;
|
|
497
727
|
} catch {
|
|
498
728
|
return null;
|
|
499
729
|
}
|
|
@@ -506,35 +736,62 @@ function mailtoUnsubscribeUrl(value: string) {
|
|
|
506
736
|
*/
|
|
507
737
|
export function parseListUnsubscribe(
|
|
508
738
|
raw: string,
|
|
509
|
-
authentication: Pick<InboundAuthentication, "dkim"
|
|
739
|
+
authentication: Pick<InboundAuthentication, "dkim">
|
|
510
740
|
): ListUnsubscribe | null {
|
|
511
741
|
const fields = headerFields(raw);
|
|
512
|
-
|
|
742
|
+
|
|
743
|
+
const header = fields.find(
|
|
744
|
+
(field) => field.name === "list-unsubscribe"
|
|
745
|
+
)?.value;
|
|
746
|
+
|
|
513
747
|
if (!header) return null;
|
|
514
|
-
|
|
748
|
+
|
|
749
|
+
const uris = [...header.matchAll(/<([^>]*)>/gu)].map((match) =>
|
|
750
|
+
match[1].replaceAll(/\s+/gu, "")
|
|
751
|
+
);
|
|
752
|
+
|
|
515
753
|
const httpsUrl = uris.map(httpsUnsubscribeUrl).find(Boolean) ?? null;
|
|
516
754
|
const mailto = uris.map(mailtoUnsubscribeUrl).find(Boolean) ?? null;
|
|
755
|
+
|
|
517
756
|
const result = (reason: ListUnsubscribe["reason"]): ListUnsubscribe => ({
|
|
518
757
|
httpsUrl,
|
|
519
758
|
mailto,
|
|
520
759
|
oneClick: reason === null,
|
|
521
760
|
reason,
|
|
522
761
|
});
|
|
762
|
+
|
|
523
763
|
if (!httpsUrl && !mailto) return result("invalid-uri");
|
|
764
|
+
|
|
524
765
|
if (!httpsUrl) return result("no-https");
|
|
525
|
-
|
|
526
|
-
|
|
766
|
+
|
|
767
|
+
const post = fields.find(
|
|
768
|
+
(field) => field.name === "list-unsubscribe-post"
|
|
769
|
+
)?.value;
|
|
770
|
+
|
|
771
|
+
if (
|
|
772
|
+
post?.replaceAll(/\s+/gu, "").toLocaleLowerCase() !==
|
|
773
|
+
"list-unsubscribe=one-click"
|
|
774
|
+
) {
|
|
527
775
|
return result("no-post");
|
|
528
776
|
}
|
|
529
|
-
|
|
530
|
-
|
|
777
|
+
|
|
778
|
+
const covering = authentication.dkim.filter(
|
|
779
|
+
(signature) =>
|
|
780
|
+
signature.result === "pass" &&
|
|
781
|
+
signature.signedHeaders?.includes("list-unsubscribe")
|
|
531
782
|
);
|
|
783
|
+
|
|
532
784
|
if (!covering.length) return result("unsigned");
|
|
785
|
+
|
|
533
786
|
const coveringBoth = covering.filter((signature) =>
|
|
534
787
|
signature.signedHeaders?.includes("list-unsubscribe-post")
|
|
535
788
|
);
|
|
789
|
+
|
|
536
790
|
if (!coveringBoth.length) return result("post-not-signed");
|
|
537
|
-
|
|
791
|
+
|
|
792
|
+
return result(
|
|
793
|
+
coveringBoth.some((signature) => signature.aligned) ? null : "unaligned"
|
|
794
|
+
);
|
|
538
795
|
}
|
|
539
796
|
|
|
540
797
|
/** Header-derived authentication and unsubscribe data stored on the email row. */
|
|
@@ -550,6 +807,7 @@ export function inboundHeaderMetadata(input: {
|
|
|
550
807
|
envelopeFrom: input.envelopeFrom,
|
|
551
808
|
now: input.now,
|
|
552
809
|
});
|
|
810
|
+
|
|
553
811
|
return {
|
|
554
812
|
authenticationResultsJson: authentication,
|
|
555
813
|
listUnsubscribeJson: parseListUnsubscribe(input.rawHeaders, authentication),
|