@remit/mailbox-service 0.0.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 +147 -0
- package/package.json +65 -0
- package/src/account-credentials.ts +128 -0
- package/src/adversarial-mime.e2e.test.ts +311 -0
- package/src/attribute-mapper.test.ts +78 -0
- package/src/attribute-mapper.ts +147 -0
- package/src/body-part-mapper.property.test.ts +354 -0
- package/src/body-part-mapper.ts +360 -0
- package/src/body-sync-queue.test.ts +124 -0
- package/src/body-sync-queue.ts +131 -0
- package/src/body-sync-terminal.test.ts +265 -0
- package/src/body-sync-terminal.ts +172 -0
- package/src/body-sync.ts +1202 -0
- package/src/connection-factory.ts +102 -0
- package/src/connection-test.ts +208 -0
- package/src/credentials.test.ts +181 -0
- package/src/filters/match.test.ts +181 -0
- package/src/filters/match.ts +143 -0
- package/src/filters/pipeline.ts +243 -0
- package/src/flag-push-terminal.test.ts +156 -0
- package/src/flag-push-terminal.ts +106 -0
- package/src/flag-push.test.ts +408 -0
- package/src/flag-push.ts +198 -0
- package/src/flag-queue.ts +500 -0
- package/src/heuristics/classifyByHeaders.test.ts +578 -0
- package/src/heuristics/classifyByHeaders.ts +369 -0
- package/src/heuristics/classifyPlacement.ts +96 -0
- package/src/heuristics/socialDomains.ts +23 -0
- package/src/heuristics/transactionalDomains.ts +26 -0
- package/src/imapflow-connect-retry.test.ts +108 -0
- package/src/imapflow-connection.e2e.test.ts +355 -0
- package/src/imapflow-connection.integ.test.ts +327 -0
- package/src/imapflow-connection.test.ts +161 -0
- package/src/imapflow-connection.ts +1374 -0
- package/src/index.ts +214 -0
- package/src/mailbox-cursor-rebuild.test.ts +171 -0
- package/src/mailbox-cursor-rebuild.ts +146 -0
- package/src/mailbox-cursor.test.ts +249 -0
- package/src/mailbox-cursor.ts +177 -0
- package/src/mailbox-management.test.ts +103 -0
- package/src/mailbox-management.ts +235 -0
- package/src/mailbox-queue.ts +258 -0
- package/src/mailbox-sync.test.ts +153 -0
- package/src/mailbox-sync.ts +590 -0
- package/src/message-move.ts +825 -0
- package/src/message-parser.ts +26 -0
- package/src/message-sync.ts +1021 -0
- package/src/mime-walker.test.ts +311 -0
- package/src/mime-walker.ts +305 -0
- package/src/outbox-queue.ts +294 -0
- package/src/pass-through-unit-of-work.ts +17 -0
- package/src/placement-move-terminal.test.ts +179 -0
- package/src/placement-move-terminal.ts +104 -0
- package/src/placement-move.ts +224 -0
- package/src/snippet.test.ts +213 -0
- package/src/snippet.ts +152 -0
- package/src/stale-message-reconcile.test.ts +82 -0
- package/src/stale-message-reconcile.ts +55 -0
- package/src/test-helpers/isolated-mailbox.ts +44 -0
- package/src/test-helpers/mailfuzz-connection.ts +27 -0
- package/src/test-helpers/mime-tree-generator.ts +461 -0
- package/src/text/normalizer.test.ts +223 -0
- package/src/text/normalizer.ts +140 -0
- package/src/types/mailparser-augment.d.ts +15 -0
- package/src/types/natural-porter-stemmer.d.ts +50 -0
- package/src/types/stopword.d.ts +69 -0
- package/src/types.ts +349 -0
- package/test/fixtures/mime/01-flat-text-plain.bodyparts.json +8 -0
- package/test/fixtures/mime/01-flat-text-plain.eml +10 -0
- package/test/fixtures/mime/01-flat-text-plain.expected.json +10 -0
- package/test/fixtures/mime/02-flat-text-html.bodyparts.json +8 -0
- package/test/fixtures/mime/02-flat-text-html.eml +10 -0
- package/test/fixtures/mime/02-flat-text-html.expected.json +10 -0
- package/test/fixtures/mime/03-alternative-text-html.bodyparts.json +20 -0
- package/test/fixtures/mime/03-alternative-text-html.eml +19 -0
- package/test/fixtures/mime/03-alternative-text-html.expected.json +16 -0
- package/test/fixtures/mime/04-mixed-html-pdf-attachment.bodyparts.json +22 -0
- package/test/fixtures/mime/04-mixed-html-pdf-attachment.eml +20 -0
- package/test/fixtures/mime/04-mixed-html-pdf-attachment.expected.json +16 -0
- package/test/fixtures/mime/05-mixed-html-octet-stream-pdf.bodyparts.json +22 -0
- package/test/fixtures/mime/05-mixed-html-octet-stream-pdf.eml +21 -0
- package/test/fixtures/mime/05-mixed-html-octet-stream-pdf.expected.json +16 -0
- package/test/fixtures/mime/06-related-html-cid-image.bodyparts.json +23 -0
- package/test/fixtures/mime/06-related-html-cid-image.eml +21 -0
- package/test/fixtures/mime/06-related-html-cid-image.expected.json +16 -0
- package/test/fixtures/mime/07-mixed-alternative-related-attachment.bodyparts.json +55 -0
- package/test/fixtures/mime/07-mixed-alternative-related-attachment.eml +47 -0
- package/test/fixtures/mime/07-mixed-alternative-related-attachment.expected.json +34 -0
- package/test/fixtures/mime/08-two-pdfs-same-filename.bodyparts.json +30 -0
- package/test/fixtures/mime/08-two-pdfs-same-filename.eml +26 -0
- package/test/fixtures/mime/08-two-pdfs-same-filename.expected.json +22 -0
- package/test/fixtures/mime/09-attachment-no-filename.bodyparts.json +21 -0
- package/test/fixtures/mime/09-attachment-no-filename.eml +20 -0
- package/test/fixtures/mime/09-attachment-no-filename.expected.json +16 -0
- package/test/fixtures/mime/10-calendar-invite.bodyparts.json +22 -0
- package/test/fixtures/mime/10-calendar-invite.eml +33 -0
- package/test/fixtures/mime/10-calendar-invite.expected.json +16 -0
- package/test/fixtures/mime/11-quoted-printable-text.bodyparts.json +8 -0
- package/test/fixtures/mime/11-quoted-printable-text.eml +10 -0
- package/test/fixtures/mime/11-quoted-printable-text.expected.json +10 -0
- package/test/fixtures/mime/12-base64-binary.bodyparts.json +22 -0
- package/test/fixtures/mime/12-base64-binary.eml +20 -0
- package/test/fixtures/mime/12-base64-binary.expected.json +16 -0
- package/test/fixtures/mime/13-message-rfc822-forward.bodyparts.json +22 -0
- package/test/fixtures/mime/13-message-rfc822-forward.eml +28 -0
- package/test/fixtures/mime/13-message-rfc822-forward.expected.json +16 -0
- package/test/fixtures/mime/14-deeply-nested-5-levels.bodyparts.json +38 -0
- package/test/fixtures/mime/14-deeply-nested-5-levels.eml +34 -0
- package/test/fixtures/mime/14-deeply-nested-5-levels.expected.json +10 -0
- package/test/fixtures/mime/15-empty-text-part.bodyparts.json +21 -0
- package/test/fixtures/mime/15-empty-text-part.eml +18 -0
- package/test/fixtures/mime/15-empty-text-part.expected.json +16 -0
- package/test/fixtures/mime/16-empty-non-text-part.bodyparts.json +30 -0
- package/test/fixtures/mime/16-empty-non-text-part.eml +24 -0
- package/test/fixtures/mime/16-empty-non-text-part.expected.json +22 -0
- package/test/fixtures/mime-adversarial-e2e/01-octet-stream-pdf.eml +21 -0
- package/test/fixtures/mime-adversarial-e2e/02-nested-related-cid-image.eml +31 -0
- package/test/fixtures/mime-adversarial-e2e/03-calendar-invite.eml +33 -0
- package/test/fixtures/mime-adversarial-e2e/04-forwarded-rfc822-with-attachments.eml +42 -0
- package/test/fixtures/mime-adversarial-e2e/05-tnef-winmail.eml +25 -0
- package/test/fixtures/mime-adversarial-e2e/06-8bit-non-ascii.eml +15 -0
- package/test/fixtures/mime-adversarial-e2e/07-quoted-printable-soft-breaks.eml +17 -0
- package/test/fixtures/mime-adversarial-e2e/08-alternative-empty-text.eml +18 -0
- package/tsconfig.json +8 -0
|
@@ -0,0 +1,578 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { describe, it } from "node:test";
|
|
3
|
+
import { MessageCategory } from "@remit/domain-enums";
|
|
4
|
+
import { simpleParser } from "mailparser";
|
|
5
|
+
import {
|
|
6
|
+
classifyByHeaders,
|
|
7
|
+
extractAuthenticity,
|
|
8
|
+
extractAuthResult,
|
|
9
|
+
extractHasListUnsubscribe,
|
|
10
|
+
extractProviderSpam,
|
|
11
|
+
} from "./classifyByHeaders.js";
|
|
12
|
+
|
|
13
|
+
const buildEml = (lines: string[]): Buffer => Buffer.from(lines.join("\r\n"));
|
|
14
|
+
|
|
15
|
+
const parse = async (lines: string[]) => simpleParser(buildEml(lines));
|
|
16
|
+
|
|
17
|
+
describe("classifyByHeaders", () => {
|
|
18
|
+
it("returns automated for Auto-Submitted: auto-generated", async () => {
|
|
19
|
+
const parsed = await parse([
|
|
20
|
+
"From: noreply@example.com",
|
|
21
|
+
"To: bob@example.com",
|
|
22
|
+
"Subject: Out of office",
|
|
23
|
+
"Auto-Submitted: auto-generated",
|
|
24
|
+
"",
|
|
25
|
+
"body",
|
|
26
|
+
]);
|
|
27
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("returns automated for Auto-Submitted: auto-replied", async () => {
|
|
31
|
+
const parsed = await parse([
|
|
32
|
+
"From: alice@example.com",
|
|
33
|
+
"To: bob@example.com",
|
|
34
|
+
"Subject: Re: vacation",
|
|
35
|
+
"Auto-Submitted: auto-replied",
|
|
36
|
+
"",
|
|
37
|
+
"body",
|
|
38
|
+
]);
|
|
39
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("ignores Auto-Submitted: no", async () => {
|
|
43
|
+
const parsed = await parse([
|
|
44
|
+
"From: alice@example.com",
|
|
45
|
+
"To: bob@example.com",
|
|
46
|
+
"Subject: hello",
|
|
47
|
+
"Auto-Submitted: no",
|
|
48
|
+
"",
|
|
49
|
+
"body",
|
|
50
|
+
]);
|
|
51
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.personal);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("returns automated for Precedence: bulk", async () => {
|
|
55
|
+
const parsed = await parse([
|
|
56
|
+
"From: alice@example.com",
|
|
57
|
+
"To: bob@example.com",
|
|
58
|
+
"Subject: announcement",
|
|
59
|
+
"Precedence: bulk",
|
|
60
|
+
"",
|
|
61
|
+
"body",
|
|
62
|
+
]);
|
|
63
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("returns automated for Precedence: list", async () => {
|
|
67
|
+
const parsed = await parse([
|
|
68
|
+
"From: alice@example.com",
|
|
69
|
+
"To: bob@example.com",
|
|
70
|
+
"Subject: list",
|
|
71
|
+
"Precedence: list",
|
|
72
|
+
"",
|
|
73
|
+
"body",
|
|
74
|
+
]);
|
|
75
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("returns automated for Precedence: junk", async () => {
|
|
79
|
+
const parsed = await parse([
|
|
80
|
+
"From: alice@example.com",
|
|
81
|
+
"To: bob@example.com",
|
|
82
|
+
"Subject: junk",
|
|
83
|
+
"Precedence: junk",
|
|
84
|
+
"",
|
|
85
|
+
"body",
|
|
86
|
+
]);
|
|
87
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("returns transactional for a calendar invite (text/calendar part)", async () => {
|
|
91
|
+
const parsed = await parse([
|
|
92
|
+
"From: alice@example.com",
|
|
93
|
+
"To: bob@example.com",
|
|
94
|
+
"Subject: Meeting invite",
|
|
95
|
+
'Content-Type: multipart/mixed; boundary="bnd"',
|
|
96
|
+
"",
|
|
97
|
+
"--bnd",
|
|
98
|
+
"Content-Type: text/plain",
|
|
99
|
+
"",
|
|
100
|
+
"see attached invite",
|
|
101
|
+
"--bnd",
|
|
102
|
+
'Content-Type: text/calendar; method=REQUEST; name="invite.ics"',
|
|
103
|
+
'Content-Disposition: attachment; filename="invite.ics"',
|
|
104
|
+
"",
|
|
105
|
+
"BEGIN:VCALENDAR",
|
|
106
|
+
"END:VCALENDAR",
|
|
107
|
+
"--bnd--",
|
|
108
|
+
]);
|
|
109
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.transactional);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it("returns transactional for a sender on the transactional allow-list (github.com)", async () => {
|
|
113
|
+
const parsed = await parse([
|
|
114
|
+
"From: noreply@github.com",
|
|
115
|
+
"To: bob@example.com",
|
|
116
|
+
"Subject: Security alert",
|
|
117
|
+
"",
|
|
118
|
+
"body",
|
|
119
|
+
]);
|
|
120
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.transactional);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("returns transactional for a subdomain of an allow-listed transactional domain", async () => {
|
|
124
|
+
const parsed = await parse([
|
|
125
|
+
"From: receipts@billing.stripe.com",
|
|
126
|
+
"To: bob@example.com",
|
|
127
|
+
"Subject: Receipt",
|
|
128
|
+
"",
|
|
129
|
+
"body",
|
|
130
|
+
]);
|
|
131
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.transactional);
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("returns newsletter when both List-Unsubscribe and List-Id are present", async () => {
|
|
135
|
+
const parsed = await parse([
|
|
136
|
+
"From: news@news.example.com",
|
|
137
|
+
"To: bob@example.com",
|
|
138
|
+
"Subject: Weekly digest",
|
|
139
|
+
"List-Id: <weekly.news.example.com>",
|
|
140
|
+
"List-Unsubscribe: <https://news.example.com/u>",
|
|
141
|
+
"",
|
|
142
|
+
"body",
|
|
143
|
+
]);
|
|
144
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.newsletter);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("returns marketing when only List-Unsubscribe is present (no List-Id)", async () => {
|
|
148
|
+
const parsed = await parse([
|
|
149
|
+
"From: deals@shop.example.com",
|
|
150
|
+
"To: bob@example.com",
|
|
151
|
+
"Subject: 50% off",
|
|
152
|
+
"List-Unsubscribe: <https://shop.example.com/u>",
|
|
153
|
+
"",
|
|
154
|
+
"body",
|
|
155
|
+
]);
|
|
156
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.marketing);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("returns automated when DKIM d= mismatches the From domain", async () => {
|
|
160
|
+
const parsed = await parse([
|
|
161
|
+
"From: alice@personal.example.com",
|
|
162
|
+
"To: bob@example.com",
|
|
163
|
+
"Subject: forwarded",
|
|
164
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=relay.example.net; s=sel; b=xxx",
|
|
165
|
+
"",
|
|
166
|
+
"body",
|
|
167
|
+
]);
|
|
168
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it("does NOT flag DKIM d= as mismatched when domains share a common root", async () => {
|
|
172
|
+
const parsed = await parse([
|
|
173
|
+
"From: alice@mail.example.com",
|
|
174
|
+
"To: bob@example.com",
|
|
175
|
+
"Subject: ok",
|
|
176
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=sel; b=xxx",
|
|
177
|
+
"",
|
|
178
|
+
"body",
|
|
179
|
+
]);
|
|
180
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.personal);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("returns social for a sender on the social allow-list (linkedin.com)", async () => {
|
|
184
|
+
const parsed = await parse([
|
|
185
|
+
"From: notifications@linkedin.com",
|
|
186
|
+
"To: bob@example.com",
|
|
187
|
+
"Subject: someone viewed your profile",
|
|
188
|
+
"",
|
|
189
|
+
"body",
|
|
190
|
+
]);
|
|
191
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.social);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("returns social for a subdomain of an allow-listed social domain", async () => {
|
|
195
|
+
const parsed = await parse([
|
|
196
|
+
"From: noreply@email.x.com",
|
|
197
|
+
"To: bob@example.com",
|
|
198
|
+
"Subject: New follower",
|
|
199
|
+
"",
|
|
200
|
+
"body",
|
|
201
|
+
]);
|
|
202
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.social);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("returns personal for plain mail with no special headers", async () => {
|
|
206
|
+
const parsed = await parse([
|
|
207
|
+
"From: alice@example.com",
|
|
208
|
+
"To: bob@example.com",
|
|
209
|
+
"Subject: lunch?",
|
|
210
|
+
"",
|
|
211
|
+
"hi bob, lunch on friday?",
|
|
212
|
+
]);
|
|
213
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.personal);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it("returns personal when From has no parseable address", async () => {
|
|
217
|
+
const parsed = await parse([
|
|
218
|
+
"From: ",
|
|
219
|
+
"To: bob@example.com",
|
|
220
|
+
"Subject: weird",
|
|
221
|
+
"",
|
|
222
|
+
"body",
|
|
223
|
+
]);
|
|
224
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.personal);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("category stays automated for DKIM mismatch even when extractAuthenticity also reports mismatch", async () => {
|
|
228
|
+
const parsed = await parse([
|
|
229
|
+
"From: alice@personal.example.com",
|
|
230
|
+
"To: bob@example.com",
|
|
231
|
+
"Subject: forwarded",
|
|
232
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=relay.example.net; s=sel; b=xxx",
|
|
233
|
+
"",
|
|
234
|
+
"body",
|
|
235
|
+
]);
|
|
236
|
+
// Both must agree — category heuristic and structured field
|
|
237
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
|
|
238
|
+
const auth = extractAuthenticity(parsed);
|
|
239
|
+
assert.ok(auth, "authenticity should be present");
|
|
240
|
+
assert.equal(auth.dkimMismatch, true);
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it("transactional rule wins over List-Unsubscribe", async () => {
|
|
244
|
+
// GitHub mail typically has List-Unsubscribe + List-Id (newsletter
|
|
245
|
+
// signal), but the EDD says transactional allow-list wins because
|
|
246
|
+
// receipts/security alerts are higher priority than newsletter framing.
|
|
247
|
+
const parsed = await parse([
|
|
248
|
+
"From: noreply@github.com",
|
|
249
|
+
"To: bob@example.com",
|
|
250
|
+
"Subject: Security alert",
|
|
251
|
+
"List-Id: <alerts.github.com>",
|
|
252
|
+
"List-Unsubscribe: <https://github.com/u>",
|
|
253
|
+
"",
|
|
254
|
+
"body",
|
|
255
|
+
]);
|
|
256
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.transactional);
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("Auto-Submitted wins over List-Unsubscribe", async () => {
|
|
260
|
+
const parsed = await parse([
|
|
261
|
+
"From: alice@example.com",
|
|
262
|
+
"To: bob@example.com",
|
|
263
|
+
"Subject: list bounce",
|
|
264
|
+
"Auto-Submitted: auto-generated",
|
|
265
|
+
"List-Unsubscribe: <https://example.com/u>",
|
|
266
|
+
"",
|
|
267
|
+
"body",
|
|
268
|
+
]);
|
|
269
|
+
assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
describe("extractAuthenticity", () => {
|
|
274
|
+
it("returns null when no DKIM-Signature header is present", async () => {
|
|
275
|
+
const parsed = await parse([
|
|
276
|
+
"From: alice@example.com",
|
|
277
|
+
"To: bob@example.com",
|
|
278
|
+
"Subject: no dkim",
|
|
279
|
+
"",
|
|
280
|
+
"body",
|
|
281
|
+
]);
|
|
282
|
+
assert.equal(extractAuthenticity(parsed), null);
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
it("returns null when From address is missing", async () => {
|
|
286
|
+
const parsed = await parse([
|
|
287
|
+
"From: ",
|
|
288
|
+
"To: bob@example.com",
|
|
289
|
+
"Subject: weird",
|
|
290
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=relay.example.net; s=sel; b=xxx",
|
|
291
|
+
"",
|
|
292
|
+
"body",
|
|
293
|
+
]);
|
|
294
|
+
assert.equal(extractAuthenticity(parsed), null);
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it("returns dkimMismatch: true when signing domain differs from From domain", async () => {
|
|
298
|
+
const parsed = await parse([
|
|
299
|
+
"From: alice@personal.example.com",
|
|
300
|
+
"To: bob@example.com",
|
|
301
|
+
"Subject: forwarded",
|
|
302
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=relay.example.net; s=sel; b=xxx",
|
|
303
|
+
"",
|
|
304
|
+
"body",
|
|
305
|
+
]);
|
|
306
|
+
const auth = extractAuthenticity(parsed);
|
|
307
|
+
assert.ok(auth, "expected authenticity object");
|
|
308
|
+
assert.equal(auth.fromDomain, "personal.example.com");
|
|
309
|
+
assert.equal(auth.dkimDomain, "relay.example.net");
|
|
310
|
+
assert.equal(auth.dkimMismatch, true);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it("returns dkimMismatch: false when signing domain equals From domain (aligned)", async () => {
|
|
314
|
+
const parsed = await parse([
|
|
315
|
+
"From: alice@example.com",
|
|
316
|
+
"To: bob@example.com",
|
|
317
|
+
"Subject: aligned",
|
|
318
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=sel; b=xxx",
|
|
319
|
+
"",
|
|
320
|
+
"body",
|
|
321
|
+
]);
|
|
322
|
+
const auth = extractAuthenticity(parsed);
|
|
323
|
+
assert.ok(auth, "expected authenticity object");
|
|
324
|
+
assert.equal(auth.fromDomain, "example.com");
|
|
325
|
+
assert.equal(auth.dkimDomain, "example.com");
|
|
326
|
+
assert.equal(auth.dkimMismatch, false);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("returns dkimMismatch: false for subdomain-aligned signing (d= is parent of From domain)", async () => {
|
|
330
|
+
const parsed = await parse([
|
|
331
|
+
"From: alice@mail.example.com",
|
|
332
|
+
"To: bob@example.com",
|
|
333
|
+
"Subject: subdomain ok",
|
|
334
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=sel; b=xxx",
|
|
335
|
+
"",
|
|
336
|
+
"body",
|
|
337
|
+
]);
|
|
338
|
+
const auth = extractAuthenticity(parsed);
|
|
339
|
+
assert.ok(auth, "expected authenticity object");
|
|
340
|
+
assert.equal(auth.fromDomain, "mail.example.com");
|
|
341
|
+
assert.equal(auth.dkimMismatch, false);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
it("returns dkimMismatch: false for subdomain-aligned signing (d= is child of From domain)", async () => {
|
|
345
|
+
const parsed = await parse([
|
|
346
|
+
"From: alice@example.com",
|
|
347
|
+
"To: bob@example.com",
|
|
348
|
+
"Subject: child ok",
|
|
349
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=mail.example.com; s=sel; b=xxx",
|
|
350
|
+
"",
|
|
351
|
+
"body",
|
|
352
|
+
]);
|
|
353
|
+
const auth = extractAuthenticity(parsed);
|
|
354
|
+
assert.ok(auth, "expected authenticity object");
|
|
355
|
+
assert.equal(auth.fromDomain, "example.com");
|
|
356
|
+
assert.equal(auth.dkimMismatch, false);
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
it("handles multiple DKIM signatures: aligned signature wins (no mismatch)", async () => {
|
|
360
|
+
const parsed = await parse([
|
|
361
|
+
"From: alice@example.com",
|
|
362
|
+
"To: bob@example.com",
|
|
363
|
+
"Subject: multi",
|
|
364
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=relay.example.net; s=s1; b=xxx",
|
|
365
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=s2; b=yyy",
|
|
366
|
+
"",
|
|
367
|
+
"body",
|
|
368
|
+
]);
|
|
369
|
+
const auth = extractAuthenticity(parsed);
|
|
370
|
+
assert.ok(auth, "expected authenticity object");
|
|
371
|
+
assert.equal(auth.dkimMismatch, false);
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
it("reports first mismatching domain when all signatures mismatch", async () => {
|
|
375
|
+
const parsed = await parse([
|
|
376
|
+
"From: alice@personal.example.com",
|
|
377
|
+
"To: bob@example.com",
|
|
378
|
+
"Subject: all mismatch",
|
|
379
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=relay-a.net; s=s1; b=xxx",
|
|
380
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=relay-b.net; s=s2; b=yyy",
|
|
381
|
+
"",
|
|
382
|
+
"body",
|
|
383
|
+
]);
|
|
384
|
+
const auth = extractAuthenticity(parsed);
|
|
385
|
+
assert.ok(auth, "expected authenticity object");
|
|
386
|
+
assert.equal(auth.dkimMismatch, true);
|
|
387
|
+
assert.equal(auth.dkimDomain, "relay-a.net");
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
it("category and dkimMismatch always agree (mismatch case)", async () => {
|
|
391
|
+
const parsed = await parse([
|
|
392
|
+
"From: alice@personal.example.com",
|
|
393
|
+
"To: bob@example.com",
|
|
394
|
+
"Subject: forwarded",
|
|
395
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=relay.example.net; s=sel; b=xxx",
|
|
396
|
+
"",
|
|
397
|
+
"body",
|
|
398
|
+
]);
|
|
399
|
+
const category = classifyByHeaders(parsed);
|
|
400
|
+
const auth = extractAuthenticity(parsed);
|
|
401
|
+
assert.equal(category, MessageCategory.automated);
|
|
402
|
+
assert.ok(auth, "expected authenticity object");
|
|
403
|
+
assert.equal(auth.dkimMismatch, true);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("category and dkimMismatch always agree (aligned case)", async () => {
|
|
407
|
+
const parsed = await parse([
|
|
408
|
+
"From: alice@example.com",
|
|
409
|
+
"To: bob@example.com",
|
|
410
|
+
"Subject: aligned",
|
|
411
|
+
"DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=sel; b=xxx",
|
|
412
|
+
"",
|
|
413
|
+
"body",
|
|
414
|
+
]);
|
|
415
|
+
const category = classifyByHeaders(parsed);
|
|
416
|
+
const auth = extractAuthenticity(parsed);
|
|
417
|
+
assert.equal(category, MessageCategory.personal);
|
|
418
|
+
assert.ok(auth, "expected authenticity object");
|
|
419
|
+
assert.equal(auth.dkimMismatch, false);
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
describe("extractAuthResult", () => {
|
|
424
|
+
it("returns null when Authentication-Results header is absent", async () => {
|
|
425
|
+
const parsed = await parse([
|
|
426
|
+
"From: alice@example.com",
|
|
427
|
+
"To: bob@example.com",
|
|
428
|
+
"Subject: no auth-results",
|
|
429
|
+
"",
|
|
430
|
+
"body",
|
|
431
|
+
]);
|
|
432
|
+
assert.equal(extractAuthResult(parsed), null);
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it("parses dmarc=pass spf=pass dkim=pass", async () => {
|
|
436
|
+
const parsed = await parse([
|
|
437
|
+
"From: alice@example.com",
|
|
438
|
+
"To: bob@example.com",
|
|
439
|
+
"Subject: auth results",
|
|
440
|
+
"Authentication-Results: mx.example.com; dmarc=pass; spf=pass; dkim=pass",
|
|
441
|
+
"",
|
|
442
|
+
"body",
|
|
443
|
+
]);
|
|
444
|
+
const result = extractAuthResult(parsed);
|
|
445
|
+
assert.ok(result);
|
|
446
|
+
assert.equal(result.dmarc, "Pass");
|
|
447
|
+
assert.equal(result.spf, "Pass");
|
|
448
|
+
assert.equal(result.dkim, "Pass");
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it("parses dmarc=fail", async () => {
|
|
452
|
+
const parsed = await parse([
|
|
453
|
+
"From: alice@example.com",
|
|
454
|
+
"To: bob@example.com",
|
|
455
|
+
"Subject: auth results",
|
|
456
|
+
"Authentication-Results: mx.example.com; dmarc=fail",
|
|
457
|
+
"",
|
|
458
|
+
"body",
|
|
459
|
+
]);
|
|
460
|
+
const result = extractAuthResult(parsed);
|
|
461
|
+
assert.ok(result);
|
|
462
|
+
assert.equal(result.dmarc, "Fail");
|
|
463
|
+
});
|
|
464
|
+
|
|
465
|
+
it("returns undefined for mechanism when absent", async () => {
|
|
466
|
+
const parsed = await parse([
|
|
467
|
+
"From: alice@example.com",
|
|
468
|
+
"To: bob@example.com",
|
|
469
|
+
"Subject: partial",
|
|
470
|
+
"Authentication-Results: mx.example.com; spf=softfail",
|
|
471
|
+
"",
|
|
472
|
+
"body",
|
|
473
|
+
]);
|
|
474
|
+
const result = extractAuthResult(parsed);
|
|
475
|
+
assert.ok(result);
|
|
476
|
+
assert.equal(result.spf, "Softfail");
|
|
477
|
+
assert.equal(result.dmarc, undefined);
|
|
478
|
+
assert.equal(result.dkim, undefined);
|
|
479
|
+
});
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
describe("extractProviderSpam", () => {
|
|
483
|
+
it("returns null when no spam headers are present", async () => {
|
|
484
|
+
const parsed = await parse([
|
|
485
|
+
"From: alice@example.com",
|
|
486
|
+
"To: bob@example.com",
|
|
487
|
+
"Subject: clean",
|
|
488
|
+
"",
|
|
489
|
+
"body",
|
|
490
|
+
]);
|
|
491
|
+
assert.equal(extractProviderSpam(parsed), null);
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
it("parses X-SpamExperts-Class: ham as classified=false", async () => {
|
|
495
|
+
const parsed = await parse([
|
|
496
|
+
"From: alice@example.com",
|
|
497
|
+
"To: bob@example.com",
|
|
498
|
+
"Subject: spamexperts ham",
|
|
499
|
+
"X-SpamExperts-Class: ham",
|
|
500
|
+
"",
|
|
501
|
+
"body",
|
|
502
|
+
]);
|
|
503
|
+
const result = extractProviderSpam(parsed);
|
|
504
|
+
assert.ok(result);
|
|
505
|
+
assert.equal(result.classified, false);
|
|
506
|
+
assert.equal(result.source, "x-spamexperts-class");
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
it("parses X-SpamExperts-Class: spam as classified=true", async () => {
|
|
510
|
+
const parsed = await parse([
|
|
511
|
+
"From: alice@example.com",
|
|
512
|
+
"To: bob@example.com",
|
|
513
|
+
"Subject: spamexperts spam",
|
|
514
|
+
"X-SpamExperts-Class: spam",
|
|
515
|
+
"",
|
|
516
|
+
"body",
|
|
517
|
+
]);
|
|
518
|
+
const result = extractProviderSpam(parsed);
|
|
519
|
+
assert.ok(result);
|
|
520
|
+
assert.equal(result.classified, true);
|
|
521
|
+
assert.equal(result.source, "x-spamexperts-class");
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
it("parses X-Spam-Status: Yes with score", async () => {
|
|
525
|
+
const parsed = await parse([
|
|
526
|
+
"From: alice@example.com",
|
|
527
|
+
"To: bob@example.com",
|
|
528
|
+
"Subject: spam status",
|
|
529
|
+
"X-Spam-Status: Yes, score=8.4 required=5.0",
|
|
530
|
+
"",
|
|
531
|
+
"body",
|
|
532
|
+
]);
|
|
533
|
+
const result = extractProviderSpam(parsed);
|
|
534
|
+
assert.ok(result);
|
|
535
|
+
assert.equal(result.classified, true);
|
|
536
|
+
assert.equal(result.score, "8.4");
|
|
537
|
+
assert.equal(result.source, "x-spam-status");
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
it("parses X-Spam-Status: No as classified=false", async () => {
|
|
541
|
+
const parsed = await parse([
|
|
542
|
+
"From: alice@example.com",
|
|
543
|
+
"To: bob@example.com",
|
|
544
|
+
"Subject: not spam",
|
|
545
|
+
"X-Spam-Status: No, score=1.2",
|
|
546
|
+
"",
|
|
547
|
+
"body",
|
|
548
|
+
]);
|
|
549
|
+
const result = extractProviderSpam(parsed);
|
|
550
|
+
assert.ok(result);
|
|
551
|
+
assert.equal(result.classified, false);
|
|
552
|
+
});
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
describe("extractHasListUnsubscribe", () => {
|
|
556
|
+
it("returns false when no List-Unsubscribe header", async () => {
|
|
557
|
+
const parsed = await parse([
|
|
558
|
+
"From: alice@example.com",
|
|
559
|
+
"To: bob@example.com",
|
|
560
|
+
"Subject: personal",
|
|
561
|
+
"",
|
|
562
|
+
"body",
|
|
563
|
+
]);
|
|
564
|
+
assert.equal(extractHasListUnsubscribe(parsed), false);
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
it("returns true when List-Unsubscribe header is present", async () => {
|
|
568
|
+
const parsed = await parse([
|
|
569
|
+
"From: newsletter@example.com",
|
|
570
|
+
"To: bob@example.com",
|
|
571
|
+
"Subject: our newsletter",
|
|
572
|
+
"List-Unsubscribe: <https://example.com/unsubscribe>",
|
|
573
|
+
"",
|
|
574
|
+
"body",
|
|
575
|
+
]);
|
|
576
|
+
assert.equal(extractHasListUnsubscribe(parsed), true);
|
|
577
|
+
});
|
|
578
|
+
});
|