@remit/mailbox-service 0.0.112 → 0.0.113

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (270) hide show
  1. package/dist/account-credentials.d.ts +83 -0
  2. package/dist/account-credentials.js +104 -0
  3. package/dist/attribute-mapper.d.ts +36 -0
  4. package/dist/attribute-mapper.js +119 -0
  5. package/dist/body-parse.d.ts +28 -0
  6. package/{src/body-parse.ts → dist/body-parse.js} +18 -26
  7. package/dist/body-part-mapper.d.ts +92 -0
  8. package/dist/body-part-mapper.js +301 -0
  9. package/dist/body-sync-queue.d.ts +59 -0
  10. package/dist/body-sync-queue.js +56 -0
  11. package/dist/body-sync-terminal.d.ts +58 -0
  12. package/dist/body-sync-terminal.js +95 -0
  13. package/dist/body-sync.d.ts +351 -0
  14. package/dist/body-sync.js +1204 -0
  15. package/dist/calendar-parts.d.ts +23 -0
  16. package/dist/calendar-parts.js +18 -0
  17. package/dist/classification-backfill.d.ts +91 -0
  18. package/dist/classification-backfill.js +180 -0
  19. package/dist/classify-message.d.ts +31 -0
  20. package/dist/classify-message.js +89 -0
  21. package/dist/connection-factory.d.ts +54 -0
  22. package/dist/connection-factory.js +54 -0
  23. package/dist/connection-test.d.ts +41 -0
  24. package/dist/connection-test.js +152 -0
  25. package/dist/external-move.d.ts +94 -0
  26. package/dist/external-move.js +173 -0
  27. package/dist/filter-operations.d.ts +42 -0
  28. package/dist/filter-operations.js +211 -0
  29. package/dist/filters/filter-config.d.ts +14 -0
  30. package/dist/filters/filter-config.js +10 -0
  31. package/{src/filters/list-id.ts → dist/filters/list-id.d.ts} +2 -16
  32. package/dist/filters/list-id.js +26 -0
  33. package/dist/filters/match.d.ts +69 -0
  34. package/dist/filters/match.js +165 -0
  35. package/dist/filters/pipeline.d.ts +64 -0
  36. package/dist/filters/pipeline.js +100 -0
  37. package/{src/flag-push-terminal.ts → dist/flag-push-terminal.d.ts} +16 -72
  38. package/dist/flag-push-terminal.js +63 -0
  39. package/dist/flag-push.d.ts +69 -0
  40. package/dist/flag-push.js +111 -0
  41. package/dist/flag-queue.d.ts +175 -0
  42. package/dist/flag-queue.js +291 -0
  43. package/dist/folder-operations.d.ts +39 -0
  44. package/dist/folder-operations.js +112 -0
  45. package/dist/folder-placement.d.ts +13 -0
  46. package/dist/folder-placement.js +23 -0
  47. package/dist/heuristics/authenticationResults.d.ts +10 -0
  48. package/dist/heuristics/authenticationResults.js +187 -0
  49. package/dist/heuristics/classifyByHeaders.d.ts +82 -0
  50. package/dist/heuristics/classifyByHeaders.js +365 -0
  51. package/dist/heuristics/classifyPlacement.d.ts +67 -0
  52. package/dist/heuristics/classifyPlacement.js +153 -0
  53. package/dist/heuristics/machineSenders.d.ts +21 -0
  54. package/{src/heuristics/machineSenders.ts → dist/heuristics/machineSenders.js} +34 -40
  55. package/dist/heuristics/senderMismatch.d.ts +81 -0
  56. package/dist/heuristics/senderMismatch.js +192 -0
  57. package/dist/heuristics/socialDomains.d.ts +14 -0
  58. package/{src/heuristics/socialDomains.ts → dist/heuristics/socialDomains.js} +9 -9
  59. package/dist/heuristics/transactionalDomains.d.ts +18 -0
  60. package/{src/heuristics/transactionalDomains.ts → dist/heuristics/transactionalDomains.js} +8 -8
  61. package/dist/imapflow-connection.d.ts +377 -0
  62. package/dist/imapflow-connection.js +1147 -0
  63. package/dist/index.d.ts +63 -0
  64. package/dist/index.js +65 -0
  65. package/dist/list-id-backfill.d.ts +83 -0
  66. package/dist/list-id-backfill.js +132 -0
  67. package/dist/mailbox-cursor-rebuild.d.ts +74 -0
  68. package/dist/mailbox-cursor-rebuild.js +71 -0
  69. package/{src/mailbox-cursor.ts → dist/mailbox-cursor.d.ts} +20 -98
  70. package/dist/mailbox-cursor.js +102 -0
  71. package/dist/mailbox-intent.d.ts +41 -0
  72. package/{src/mailbox-intent.ts → dist/mailbox-intent.js} +38 -64
  73. package/dist/mailbox-management.d.ts +240 -0
  74. package/dist/mailbox-management.js +576 -0
  75. package/dist/mailbox-presence.d.ts +52 -0
  76. package/{src/mailbox-presence.ts → dist/mailbox-presence.js} +19 -30
  77. package/dist/mailbox-queue.d.ts +126 -0
  78. package/dist/mailbox-queue.js +214 -0
  79. package/dist/mailbox-sync.d.ts +93 -0
  80. package/dist/mailbox-sync.js +490 -0
  81. package/dist/mailbox-upstream.d.ts +38 -0
  82. package/{src/mailbox-upstream.ts → dist/mailbox-upstream.js} +37 -47
  83. package/dist/message-category.d.ts +51 -0
  84. package/{src/message-category.ts → dist/message-category.js} +28 -71
  85. package/dist/message-move.d.ts +285 -0
  86. package/dist/message-move.js +858 -0
  87. package/dist/message-parser.d.ts +14 -0
  88. package/dist/message-parser.js +17 -0
  89. package/{src/message-presence.ts → dist/message-presence.d.ts} +1 -13
  90. package/dist/message-presence.js +29 -0
  91. package/dist/message-read.d.ts +51 -0
  92. package/dist/message-read.js +31 -0
  93. package/dist/message-sync.d.ts +345 -0
  94. package/dist/message-sync.js +1358 -0
  95. package/dist/mime-walker.d.ts +102 -0
  96. package/dist/mime-walker.js +198 -0
  97. package/dist/organize-job.d.ts +20 -0
  98. package/dist/organize-job.js +37 -0
  99. package/dist/outbox-attachment-content.d.ts +37 -0
  100. package/dist/outbox-attachment-content.js +50 -0
  101. package/dist/outbox-attachment-filename.d.ts +19 -0
  102. package/dist/outbox-attachment-filename.js +64 -0
  103. package/dist/outbox-attachment.d.ts +148 -0
  104. package/dist/outbox-attachment.js +273 -0
  105. package/dist/outbox-queue.d.ts +73 -0
  106. package/dist/outbox-queue.js +284 -0
  107. package/{src/outbox-status.ts → dist/outbox-status.d.ts} +1 -6
  108. package/dist/outbox-status.js +18 -0
  109. package/dist/ownership.d.ts +24 -0
  110. package/dist/ownership.js +83 -0
  111. package/dist/placement-move-terminal.d.ts +67 -0
  112. package/dist/placement-move-terminal.js +88 -0
  113. package/dist/placement-move.d.ts +83 -0
  114. package/dist/placement-move.js +216 -0
  115. package/{src/placement-settled.ts → dist/placement-settled.d.ts} +13 -68
  116. package/dist/placement-settled.js +75 -0
  117. package/dist/quarantine.d.ts +95 -0
  118. package/dist/quarantine.js +131 -0
  119. package/dist/refusal.d.ts +31 -0
  120. package/dist/refusal.js +3 -0
  121. package/{src/restore-source-placement.ts → dist/restore-source-placement.d.ts} +29 -97
  122. package/dist/restore-source-placement.js +79 -0
  123. package/dist/search-text-backfill.d.ts +15 -0
  124. package/dist/search-text-backfill.js +33 -0
  125. package/dist/snippet.d.ts +61 -0
  126. package/dist/snippet.js +129 -0
  127. package/dist/spam-report.d.ts +86 -0
  128. package/dist/spam-report.js +241 -0
  129. package/dist/sync-warning.d.ts +21 -0
  130. package/dist/sync-warning.js +26 -0
  131. package/dist/sync-watermarks.d.ts +138 -0
  132. package/dist/sync-watermarks.js +205 -0
  133. package/dist/test-helpers/fake-imap.d.ts +151 -0
  134. package/dist/test-helpers/fake-imap.js +508 -0
  135. package/dist/test-helpers/folder-roles.d.ts +34 -0
  136. package/dist/test-helpers/folder-roles.js +47 -0
  137. package/dist/test-helpers/mime-tree-generator.d.ts +82 -0
  138. package/dist/test-helpers/mime-tree-generator.js +329 -0
  139. package/dist/test-helpers/test-store.d.ts +69 -0
  140. package/dist/test-helpers/test-store.js +193 -0
  141. package/dist/text/normalizer.d.ts +20 -0
  142. package/dist/text/normalizer.js +102 -0
  143. package/dist/thread-message-rows.d.ts +69 -0
  144. package/dist/thread-message-rows.js +40 -0
  145. package/dist/thread-root.d.ts +7 -0
  146. package/dist/thread-root.js +25 -0
  147. package/dist/types.d.ts +343 -0
  148. package/dist/types.js +19 -0
  149. package/package.json +86 -88
  150. package/src/account-credentials.ts +0 -210
  151. package/src/attribute-mapper.test.ts +0 -104
  152. package/src/attribute-mapper.ts +0 -148
  153. package/src/body-parse.test.ts +0 -55
  154. package/src/body-part-mapper.property.test.ts +0 -354
  155. package/src/body-part-mapper.ts +0 -410
  156. package/src/body-sync-queue.test.ts +0 -64
  157. package/src/body-sync-queue.ts +0 -131
  158. package/src/body-sync-terminal.test.ts +0 -148
  159. package/src/body-sync-terminal.ts +0 -174
  160. package/src/body-sync.test.ts +0 -925
  161. package/src/body-sync.ts +0 -1891
  162. package/src/calendar-parts.ts +0 -41
  163. package/src/classification-backfill.test.ts +0 -285
  164. package/src/classification-backfill.ts +0 -363
  165. package/src/classify-message.test.ts +0 -142
  166. package/src/classify-message.ts +0 -116
  167. package/src/connection-factory.ts +0 -102
  168. package/src/connection-test.ts +0 -208
  169. package/src/credentials.test.ts +0 -181
  170. package/src/external-move.test.ts +0 -262
  171. package/src/external-move.ts +0 -285
  172. package/src/filter-operations.test.ts +0 -319
  173. package/src/filter-operations.ts +0 -377
  174. package/src/filter-update.test.ts +0 -313
  175. package/src/filters/filter-config.test.ts +0 -87
  176. package/src/filters/filter-config.ts +0 -34
  177. package/src/filters/list-id.test.ts +0 -50
  178. package/src/filters/match.test.ts +0 -392
  179. package/src/filters/match.ts +0 -225
  180. package/src/filters/pipeline.test.ts +0 -199
  181. package/src/filters/pipeline.ts +0 -170
  182. package/src/flag-push.test.ts +0 -112
  183. package/src/flag-push.ts +0 -198
  184. package/src/flag-queue.test.ts +0 -197
  185. package/src/flag-queue.ts +0 -509
  186. package/src/folder-operations.test.ts +0 -273
  187. package/src/folder-operations.ts +0 -217
  188. package/src/folder-placement.ts +0 -56
  189. package/src/forwarded-message.test.ts +0 -122
  190. package/src/heuristics/authenticationResults.test.ts +0 -189
  191. package/src/heuristics/authenticationResults.ts +0 -212
  192. package/src/heuristics/classifyByHeaders.test.ts +0 -889
  193. package/src/heuristics/classifyByHeaders.ts +0 -467
  194. package/src/heuristics/classifyPlacement.test.ts +0 -366
  195. package/src/heuristics/classifyPlacement.ts +0 -211
  196. package/src/heuristics/classifyRealMail.test.ts +0 -242
  197. package/src/heuristics/machineSenders.test.ts +0 -101
  198. package/src/heuristics/senderMismatch.test.ts +0 -346
  199. package/src/heuristics/senderMismatch.ts +0 -259
  200. package/src/imapflow-connect-retry.test.ts +0 -97
  201. package/src/imapflow-connection.test.ts +0 -503
  202. package/src/imapflow-connection.ts +0 -1476
  203. package/src/index.ts +0 -397
  204. package/src/list-id-backfill.test.ts +0 -207
  205. package/src/list-id-backfill.ts +0 -274
  206. package/src/mailbox-cursor-rebuild.test.ts +0 -171
  207. package/src/mailbox-cursor-rebuild.ts +0 -146
  208. package/src/mailbox-cursor.test.ts +0 -141
  209. package/src/mailbox-delete-intent.test.ts +0 -119
  210. package/src/mailbox-management.test.ts +0 -243
  211. package/src/mailbox-management.ts +0 -838
  212. package/src/mailbox-presence.test.ts +0 -74
  213. package/src/mailbox-queue.ts +0 -397
  214. package/src/mailbox-rename-intent.test.ts +0 -220
  215. package/src/mailbox-rename-subtree.test.ts +0 -287
  216. package/src/mailbox-sync.test.ts +0 -286
  217. package/src/mailbox-sync.ts +0 -724
  218. package/src/message-move.test.ts +0 -726
  219. package/src/message-move.ts +0 -1518
  220. package/src/message-parser.ts +0 -26
  221. package/src/message-presence.test.ts +0 -50
  222. package/src/message-read.ts +0 -87
  223. package/src/message-save-retry.test.ts +0 -90
  224. package/src/message-sync-flags.test.ts +0 -167
  225. package/src/message-sync.test.ts +0 -762
  226. package/src/message-sync.ts +0 -2133
  227. package/src/mime-walker.test.ts +0 -311
  228. package/src/mime-walker.ts +0 -306
  229. package/src/organize-job.ts +0 -93
  230. package/src/outbox-attachment-content.test.ts +0 -153
  231. package/src/outbox-attachment-content.ts +0 -104
  232. package/src/outbox-attachment-filename.test.ts +0 -132
  233. package/src/outbox-attachment-filename.ts +0 -76
  234. package/src/outbox-attachment.test.ts +0 -626
  235. package/src/outbox-attachment.ts +0 -536
  236. package/src/outbox-failed-recovery.test.ts +0 -299
  237. package/src/outbox-queue.test.ts +0 -315
  238. package/src/outbox-queue.ts +0 -525
  239. package/src/ownership.ts +0 -140
  240. package/src/placement-move-terminal.ts +0 -145
  241. package/src/placement-move.ts +0 -363
  242. package/src/placement-settled.test.ts +0 -147
  243. package/src/placement.test.ts +0 -404
  244. package/src/quarantine.test.ts +0 -133
  245. package/src/quarantine.ts +0 -229
  246. package/src/refusal.ts +0 -29
  247. package/src/snippet.test.ts +0 -213
  248. package/src/snippet.ts +0 -152
  249. package/src/spam-report.test.ts +0 -329
  250. package/src/spam-report.ts +0 -357
  251. package/src/sync-warning.test.ts +0 -213
  252. package/src/sync-warning.ts +0 -56
  253. package/src/sync-watermarks.test.ts +0 -409
  254. package/src/sync-watermarks.ts +0 -319
  255. package/src/system-flag-wire-format.test.ts +0 -77
  256. package/src/test-helpers/fake-imap.ts +0 -690
  257. package/src/test-helpers/folder-roles.ts +0 -66
  258. package/src/test-helpers/mime-tree-generator.ts +0 -461
  259. package/src/test-helpers/test-queue.ts +0 -56
  260. package/src/test-helpers/test-store.ts +0 -299
  261. package/src/text/normalizer.test.ts +0 -223
  262. package/src/text/normalizer.ts +0 -140
  263. package/src/thread-message-rows.ts +0 -77
  264. package/src/thread-root.test.ts +0 -29
  265. package/src/thread-root.ts +0 -51
  266. package/src/types/mailparser-augment.d.ts +0 -23
  267. package/src/types/natural-porter-stemmer.d.ts +0 -50
  268. package/src/types/stopword.d.ts +0 -69
  269. package/src/types.ts +0 -371
  270. package/tsconfig.json +0 -8
@@ -1,889 +0,0 @@
1
- import assert from "node:assert/strict";
2
- import { describe, it } from "node:test";
3
- import type { MessageItem } from "@remit/data-ports";
4
- import {
5
- MessageCategory,
6
- SenderOverride,
7
- SenderTrust,
8
- } from "@remit/domain-enums";
9
- import { type ParsedMail, simpleParser } from "mailparser";
10
- import {
11
- classifyByHeaders,
12
- extractAuthenticity,
13
- extractAuthResult,
14
- extractHasListUnsubscribe,
15
- extractProviderSpam,
16
- } from "./classifyByHeaders.js";
17
- import { classifyPlacement } from "./classifyPlacement.js";
18
-
19
- const buildEml = (lines: string[]): Buffer => Buffer.from(lines.join("\r\n"));
20
-
21
- const parse = async (lines: string[]) => simpleParser(buildEml(lines));
22
-
23
- describe("classifyByHeaders", () => {
24
- it("returns automated for Auto-Submitted: auto-generated", async () => {
25
- const parsed = await parse([
26
- "From: noreply@example.com",
27
- "To: bob@example.com",
28
- "Subject: Out of office",
29
- "Auto-Submitted: auto-generated",
30
- "",
31
- "body",
32
- ]);
33
- assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
34
- });
35
-
36
- it("returns automated for Auto-Submitted: auto-replied", async () => {
37
- const parsed = await parse([
38
- "From: alice@example.com",
39
- "To: bob@example.com",
40
- "Subject: Re: vacation",
41
- "Auto-Submitted: auto-replied",
42
- "",
43
- "body",
44
- ]);
45
- assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
46
- });
47
-
48
- it("ignores Auto-Submitted: no", async () => {
49
- const parsed = await parse([
50
- "From: alice@example.com",
51
- "To: bob@example.com",
52
- "Subject: hello",
53
- "Auto-Submitted: no",
54
- "",
55
- "body",
56
- ]);
57
- assert.equal(classifyByHeaders(parsed), MessageCategory.personal);
58
- });
59
-
60
- it("returns automated for Precedence: bulk", async () => {
61
- const parsed = await parse([
62
- "From: alice@example.com",
63
- "To: bob@example.com",
64
- "Subject: announcement",
65
- "Precedence: bulk",
66
- "",
67
- "body",
68
- ]);
69
- assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
70
- });
71
-
72
- it("returns automated for Precedence: list", async () => {
73
- const parsed = await parse([
74
- "From: alice@example.com",
75
- "To: bob@example.com",
76
- "Subject: list",
77
- "Precedence: list",
78
- "",
79
- "body",
80
- ]);
81
- assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
82
- });
83
-
84
- it("returns automated for Precedence: junk", async () => {
85
- const parsed = await parse([
86
- "From: alice@example.com",
87
- "To: bob@example.com",
88
- "Subject: junk",
89
- "Precedence: junk",
90
- "",
91
- "body",
92
- ]);
93
- assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
94
- });
95
-
96
- it("returns transactional for a calendar invite (text/calendar part)", async () => {
97
- const parsed = await parse([
98
- "From: alice@example.com",
99
- "To: bob@example.com",
100
- "Subject: Meeting invite",
101
- 'Content-Type: multipart/mixed; boundary="bnd"',
102
- "",
103
- "--bnd",
104
- "Content-Type: text/plain",
105
- "",
106
- "see attached invite",
107
- "--bnd",
108
- 'Content-Type: text/calendar; method=REQUEST; name="invite.ics"',
109
- 'Content-Disposition: attachment; filename="invite.ics"',
110
- "",
111
- "BEGIN:VCALENDAR",
112
- "END:VCALENDAR",
113
- "--bnd--",
114
- ]);
115
- assert.equal(classifyByHeaders(parsed), MessageCategory.transactional);
116
- });
117
-
118
- it("returns transactional for a sender on the transactional allow-list (github.com)", async () => {
119
- const parsed = await parse([
120
- "From: noreply@github.com",
121
- "To: bob@example.com",
122
- "Subject: Security alert",
123
- "",
124
- "body",
125
- ]);
126
- assert.equal(classifyByHeaders(parsed), MessageCategory.transactional);
127
- });
128
-
129
- it("returns transactional for a subdomain of an allow-listed transactional domain", async () => {
130
- const parsed = await parse([
131
- "From: receipts@billing.stripe.com",
132
- "To: bob@example.com",
133
- "Subject: Receipt",
134
- "",
135
- "body",
136
- ]);
137
- assert.equal(classifyByHeaders(parsed), MessageCategory.transactional);
138
- });
139
-
140
- it("returns newsletter when both List-Unsubscribe and List-Id are present", async () => {
141
- const parsed = await parse([
142
- "From: news@news.example.com",
143
- "To: bob@example.com",
144
- "Subject: Weekly digest",
145
- "List-Id: <weekly.news.example.com>",
146
- "List-Unsubscribe: <https://news.example.com/u>",
147
- "",
148
- "body",
149
- ]);
150
- assert.equal(classifyByHeaders(parsed), MessageCategory.newsletter);
151
- });
152
-
153
- it("returns marketing when only List-Unsubscribe is present (no List-Id)", async () => {
154
- const parsed = await parse([
155
- "From: deals@shop.example.com",
156
- "To: bob@example.com",
157
- "Subject: 50% off",
158
- "List-Unsubscribe: <https://shop.example.com/u>",
159
- "",
160
- "body",
161
- ]);
162
- assert.equal(classifyByHeaders(parsed), MessageCategory.marketing);
163
- });
164
-
165
- it("returns automated when DKIM d= mismatches the From domain", async () => {
166
- const parsed = await parse([
167
- "From: alice@personal.example.com",
168
- "To: bob@example.com",
169
- "Subject: forwarded",
170
- "DKIM-Signature: v=1; a=rsa-sha256; d=relay.example.net; s=sel; b=xxx",
171
- "",
172
- "body",
173
- ]);
174
- assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
175
- });
176
-
177
- it("does NOT flag DKIM d= as mismatched when domains share a common root", async () => {
178
- const parsed = await parse([
179
- "From: alice@mail.example.com",
180
- "To: bob@example.com",
181
- "Subject: ok",
182
- "DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=sel; b=xxx",
183
- "",
184
- "body",
185
- ]);
186
- assert.equal(classifyByHeaders(parsed), MessageCategory.personal);
187
- });
188
-
189
- it("returns social for a sender on the social allow-list (linkedin.com)", async () => {
190
- const parsed = await parse([
191
- "From: notifications@linkedin.com",
192
- "To: bob@example.com",
193
- "Subject: someone viewed your profile",
194
- "",
195
- "body",
196
- ]);
197
- assert.equal(classifyByHeaders(parsed), MessageCategory.social);
198
- });
199
-
200
- it("returns social for a subdomain of an allow-listed social domain", async () => {
201
- const parsed = await parse([
202
- "From: noreply@email.x.com",
203
- "To: bob@example.com",
204
- "Subject: New follower",
205
- "",
206
- "body",
207
- ]);
208
- assert.equal(classifyByHeaders(parsed), MessageCategory.social);
209
- });
210
-
211
- it("returns personal for plain mail with no special headers", async () => {
212
- const parsed = await parse([
213
- "From: alice@example.com",
214
- "To: bob@example.com",
215
- "Subject: lunch?",
216
- "",
217
- "hi bob, lunch on friday?",
218
- ]);
219
- assert.equal(classifyByHeaders(parsed), MessageCategory.personal);
220
- });
221
-
222
- it("returns personal when From has no parseable address", async () => {
223
- const parsed = await parse([
224
- "From: ",
225
- "To: bob@example.com",
226
- "Subject: weird",
227
- "",
228
- "body",
229
- ]);
230
- assert.equal(classifyByHeaders(parsed), MessageCategory.personal);
231
- });
232
-
233
- it("category stays automated for DKIM mismatch even when extractAuthenticity also reports mismatch", async () => {
234
- const parsed = await parse([
235
- "From: alice@personal.example.com",
236
- "To: bob@example.com",
237
- "Subject: forwarded",
238
- "DKIM-Signature: v=1; a=rsa-sha256; d=relay.example.net; s=sel; b=xxx",
239
- "",
240
- "body",
241
- ]);
242
- // Both must agree — category heuristic and structured field
243
- assert.equal(classifyByHeaders(parsed), MessageCategory.automated);
244
- const auth = extractAuthenticity(parsed);
245
- assert.ok(auth, "authenticity should be present");
246
- assert.equal(auth.dkimMismatch, true);
247
- });
248
-
249
- it("transactional rule wins over List-Unsubscribe", async () => {
250
- // GitHub mail typically has List-Unsubscribe + List-Id (newsletter
251
- // signal), but the EDD says transactional allow-list wins because
252
- // receipts/security alerts are higher priority than newsletter framing.
253
- const parsed = await parse([
254
- "From: noreply@github.com",
255
- "To: bob@example.com",
256
- "Subject: Security alert",
257
- "List-Id: <alerts.github.com>",
258
- "List-Unsubscribe: <https://github.com/u>",
259
- "",
260
- "body",
261
- ]);
262
- assert.equal(classifyByHeaders(parsed), MessageCategory.transactional);
263
- });
264
-
265
- it("List-Unsubscribe wins over Auto-Submitted", async () => {
266
- const parsed = await parse([
267
- "From: alice@example.com",
268
- "To: bob@example.com",
269
- "Subject: list bounce",
270
- "Auto-Submitted: auto-generated",
271
- "List-Unsubscribe: <https://example.com/u>",
272
- "",
273
- "body",
274
- ]);
275
- assert.equal(classifyByHeaders(parsed), MessageCategory.marketing);
276
- });
277
- });
278
-
279
- describe("extractAuthenticity", () => {
280
- it("returns null when no DKIM-Signature header is present", async () => {
281
- const parsed = await parse([
282
- "From: alice@example.com",
283
- "To: bob@example.com",
284
- "Subject: no dkim",
285
- "",
286
- "body",
287
- ]);
288
- assert.equal(extractAuthenticity(parsed), null);
289
- });
290
-
291
- it("returns null when From address is missing", async () => {
292
- const parsed = await parse([
293
- "From: ",
294
- "To: bob@example.com",
295
- "Subject: weird",
296
- "DKIM-Signature: v=1; a=rsa-sha256; d=relay.example.net; s=sel; b=xxx",
297
- "",
298
- "body",
299
- ]);
300
- assert.equal(extractAuthenticity(parsed), null);
301
- });
302
-
303
- it("returns dkimMismatch: true when signing domain differs from From domain", async () => {
304
- const parsed = await parse([
305
- "From: alice@personal.example.com",
306
- "To: bob@example.com",
307
- "Subject: forwarded",
308
- "DKIM-Signature: v=1; a=rsa-sha256; d=relay.example.net; s=sel; b=xxx",
309
- "",
310
- "body",
311
- ]);
312
- const auth = extractAuthenticity(parsed);
313
- assert.ok(auth, "expected authenticity object");
314
- assert.equal(auth.fromDomain, "personal.example.com");
315
- assert.equal(auth.dkimDomain, "relay.example.net");
316
- assert.equal(auth.dkimMismatch, true);
317
- });
318
-
319
- it("returns dkimMismatch: false when signing domain equals From domain (aligned)", async () => {
320
- const parsed = await parse([
321
- "From: alice@example.com",
322
- "To: bob@example.com",
323
- "Subject: aligned",
324
- "DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=sel; b=xxx",
325
- "",
326
- "body",
327
- ]);
328
- const auth = extractAuthenticity(parsed);
329
- assert.ok(auth, "expected authenticity object");
330
- assert.equal(auth.fromDomain, "example.com");
331
- assert.equal(auth.dkimDomain, "example.com");
332
- assert.equal(auth.dkimMismatch, false);
333
- });
334
-
335
- it("returns dkimMismatch: false for subdomain-aligned signing (d= is parent of From domain)", async () => {
336
- const parsed = await parse([
337
- "From: alice@mail.example.com",
338
- "To: bob@example.com",
339
- "Subject: subdomain ok",
340
- "DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=sel; b=xxx",
341
- "",
342
- "body",
343
- ]);
344
- const auth = extractAuthenticity(parsed);
345
- assert.ok(auth, "expected authenticity object");
346
- assert.equal(auth.fromDomain, "mail.example.com");
347
- assert.equal(auth.dkimMismatch, false);
348
- });
349
-
350
- it("returns dkimMismatch: false for subdomain-aligned signing (d= is child of From domain)", async () => {
351
- const parsed = await parse([
352
- "From: alice@example.com",
353
- "To: bob@example.com",
354
- "Subject: child ok",
355
- "DKIM-Signature: v=1; a=rsa-sha256; d=mail.example.com; s=sel; b=xxx",
356
- "",
357
- "body",
358
- ]);
359
- const auth = extractAuthenticity(parsed);
360
- assert.ok(auth, "expected authenticity object");
361
- assert.equal(auth.fromDomain, "example.com");
362
- assert.equal(auth.dkimMismatch, false);
363
- });
364
-
365
- it("handles multiple DKIM signatures: aligned signature wins (no mismatch)", async () => {
366
- const parsed = await parse([
367
- "From: alice@example.com",
368
- "To: bob@example.com",
369
- "Subject: multi",
370
- "DKIM-Signature: v=1; a=rsa-sha256; d=relay.example.net; s=s1; b=xxx",
371
- "DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=s2; b=yyy",
372
- "",
373
- "body",
374
- ]);
375
- const auth = extractAuthenticity(parsed);
376
- assert.ok(auth, "expected authenticity object");
377
- assert.equal(auth.dkimMismatch, false);
378
- });
379
-
380
- it("reports first mismatching domain when all signatures mismatch", async () => {
381
- const parsed = await parse([
382
- "From: alice@personal.example.com",
383
- "To: bob@example.com",
384
- "Subject: all mismatch",
385
- "DKIM-Signature: v=1; a=rsa-sha256; d=relay-a.net; s=s1; b=xxx",
386
- "DKIM-Signature: v=1; a=rsa-sha256; d=relay-b.net; s=s2; b=yyy",
387
- "",
388
- "body",
389
- ]);
390
- const auth = extractAuthenticity(parsed);
391
- assert.ok(auth, "expected authenticity object");
392
- assert.equal(auth.dkimMismatch, true);
393
- assert.equal(auth.dkimDomain, "relay-a.net");
394
- });
395
-
396
- it("category and dkimMismatch always agree (mismatch case)", async () => {
397
- const parsed = await parse([
398
- "From: alice@personal.example.com",
399
- "To: bob@example.com",
400
- "Subject: forwarded",
401
- "DKIM-Signature: v=1; a=rsa-sha256; d=relay.example.net; s=sel; b=xxx",
402
- "",
403
- "body",
404
- ]);
405
- const category = classifyByHeaders(parsed);
406
- const auth = extractAuthenticity(parsed);
407
- assert.equal(category, MessageCategory.automated);
408
- assert.ok(auth, "expected authenticity object");
409
- assert.equal(auth.dkimMismatch, true);
410
- });
411
-
412
- it("category and dkimMismatch always agree (aligned case)", async () => {
413
- const parsed = await parse([
414
- "From: alice@example.com",
415
- "To: bob@example.com",
416
- "Subject: aligned",
417
- "DKIM-Signature: v=1; a=rsa-sha256; d=example.com; s=sel; b=xxx",
418
- "",
419
- "body",
420
- ]);
421
- const category = classifyByHeaders(parsed);
422
- const auth = extractAuthenticity(parsed);
423
- assert.equal(category, MessageCategory.personal);
424
- assert.ok(auth, "expected authenticity object");
425
- assert.equal(auth.dkimMismatch, false);
426
- });
427
- });
428
-
429
- describe("extractAuthResult", () => {
430
- it("returns null when Authentication-Results header is absent", async () => {
431
- const parsed = await parse([
432
- "From: alice@example.com",
433
- "To: bob@example.com",
434
- "Subject: no auth-results",
435
- "",
436
- "body",
437
- ]);
438
- assert.equal(extractAuthResult(parsed), null);
439
- });
440
-
441
- it("parses dmarc=pass spf=pass dkim=pass", async () => {
442
- const parsed = await parse([
443
- "From: alice@example.com",
444
- "To: bob@example.com",
445
- "Subject: auth results",
446
- "Authentication-Results: mx.example.com; dmarc=pass; spf=pass; dkim=pass",
447
- "",
448
- "body",
449
- ]);
450
- const result = extractAuthResult(parsed);
451
- assert.ok(result);
452
- assert.equal(result.dmarc, "Pass");
453
- assert.equal(result.spf, "Pass");
454
- assert.equal(result.dkim, "Pass");
455
- });
456
-
457
- it("parses dmarc=fail", async () => {
458
- const parsed = await parse([
459
- "From: alice@example.com",
460
- "To: bob@example.com",
461
- "Subject: auth results",
462
- "Authentication-Results: mx.example.com; dmarc=fail",
463
- "",
464
- "body",
465
- ]);
466
- const result = extractAuthResult(parsed);
467
- assert.ok(result);
468
- assert.equal(result.dmarc, "Fail");
469
- });
470
-
471
- it("returns undefined for mechanism when absent", async () => {
472
- const parsed = await parse([
473
- "From: alice@example.com",
474
- "To: bob@example.com",
475
- "Subject: partial",
476
- "Authentication-Results: mx.example.com; spf=softfail",
477
- "",
478
- "body",
479
- ]);
480
- const result = extractAuthResult(parsed);
481
- assert.ok(result);
482
- assert.equal(result.spf, "Softfail");
483
- assert.equal(result.dmarc, undefined);
484
- assert.equal(result.dkim, undefined);
485
- });
486
- });
487
-
488
- describe("extractProviderSpam", () => {
489
- it("returns null when no spam headers are present", async () => {
490
- const parsed = await parse([
491
- "From: alice@example.com",
492
- "To: bob@example.com",
493
- "Subject: clean",
494
- "",
495
- "body",
496
- ]);
497
- assert.equal(extractProviderSpam(parsed), null);
498
- });
499
-
500
- it("parses X-SpamExperts-Class: ham as classified=false", async () => {
501
- const parsed = await parse([
502
- "From: alice@example.com",
503
- "To: bob@example.com",
504
- "Subject: spamexperts ham",
505
- "X-SpamExperts-Class: ham",
506
- "",
507
- "body",
508
- ]);
509
- const result = extractProviderSpam(parsed);
510
- assert.ok(result);
511
- assert.equal(result.classified, false);
512
- assert.equal(result.source, "x-spamexperts-class");
513
- });
514
-
515
- it("parses X-SpamExperts-Class: spam as classified=true", async () => {
516
- const parsed = await parse([
517
- "From: alice@example.com",
518
- "To: bob@example.com",
519
- "Subject: spamexperts spam",
520
- "X-SpamExperts-Class: spam",
521
- "",
522
- "body",
523
- ]);
524
- const result = extractProviderSpam(parsed);
525
- assert.ok(result);
526
- assert.equal(result.classified, true);
527
- assert.equal(result.source, "x-spamexperts-class");
528
- });
529
-
530
- it("parses X-Spam-Status: Yes with score", async () => {
531
- const parsed = await parse([
532
- "From: alice@example.com",
533
- "To: bob@example.com",
534
- "Subject: spam status",
535
- "X-Spam-Status: Yes, score=8.4 required=5.0",
536
- "",
537
- "body",
538
- ]);
539
- const result = extractProviderSpam(parsed);
540
- assert.ok(result);
541
- assert.equal(result.classified, true);
542
- assert.equal(result.score, "8.4");
543
- assert.equal(result.source, "x-spam-status");
544
- });
545
-
546
- it("parses X-Spam-Status: No as classified=false", async () => {
547
- const parsed = await parse([
548
- "From: alice@example.com",
549
- "To: bob@example.com",
550
- "Subject: not spam",
551
- "X-Spam-Status: No, score=1.2",
552
- "",
553
- "body",
554
- ]);
555
- const result = extractProviderSpam(parsed);
556
- assert.ok(result);
557
- assert.equal(result.classified, false);
558
- });
559
- });
560
-
561
- describe("extractHasListUnsubscribe", () => {
562
- it("returns false when no List-Unsubscribe header", async () => {
563
- const parsed = await parse([
564
- "From: alice@example.com",
565
- "To: bob@example.com",
566
- "Subject: personal",
567
- "",
568
- "body",
569
- ]);
570
- assert.equal(extractHasListUnsubscribe(parsed), false);
571
- });
572
-
573
- it("returns true when List-Unsubscribe header is present", async () => {
574
- const parsed = await parse([
575
- "From: newsletter@example.com",
576
- "To: bob@example.com",
577
- "Subject: our newsletter",
578
- "List-Unsubscribe: <https://example.com/unsubscribe>",
579
- "",
580
- "body",
581
- ]);
582
- assert.equal(extractHasListUnsubscribe(parsed), true);
583
- });
584
- });
585
-
586
- const messageFrom = (parsed: ParsedMail): MessageItem => ({
587
- messageId: "m-1",
588
- mailboxId: "mb-1",
589
- uid: 1,
590
- sequenceNumber: 1,
591
- rfc822Size: 1,
592
- internalDate: 1,
593
- envelopeId: "e-1",
594
- rootBodyPartId: "bp-1",
595
- status: "active",
596
- syncStatus: "synced",
597
- abandonedMutation: "none",
598
- category: "uncategorized",
599
- classificationState: "NotExamined",
600
- authenticityVerdict: "NotEvaluated",
601
- hasListUnsubscribe: false,
602
- movedByRemit: false,
603
- createdAt: 1,
604
- updatedAt: 1,
605
- providerSpam: extractProviderSpam(parsed) ?? undefined,
606
- authResult: extractAuthResult(parsed) ?? undefined,
607
- authenticity: extractAuthenticity(parsed) ?? undefined,
608
- });
609
-
610
- const inboxVerdict = (parsed: ParsedMail) =>
611
- classifyPlacement(
612
- messageFrom(parsed),
613
- "inbox",
614
- SenderTrust.Unknown,
615
- SenderOverride.None,
616
- );
617
-
618
- const withBody = (lines: string[]): string[] => [...lines, "", "body"];
619
-
620
- describe("Authentication-Results adversarial findings (#657)", () => {
621
- it("keeps the aligned dkim result when a comment carries a semicolon", async () => {
622
- const parsed = await parse(
623
- withBody([
624
- "From: Alice <alice@acme.example>",
625
- "Authentication-Results: gw.example; dkim=fail (1024-bit key; unprotected) header.d=acme.example; dkim=pass header.d=relay.example; dmarc=fail (p=REJECT) header.from=acme.example",
626
- "X-Spam-Status: No, score=0.1",
627
- ]),
628
- );
629
- assert.deepEqual(extractAuthResult(parsed), {
630
- dmarc: "Fail",
631
- spf: undefined,
632
- dkim: "Fail",
633
- });
634
- });
635
-
636
- it("checks alignment even when an unrelated dkim=pass is present", async () => {
637
- const header = (extra: string) =>
638
- parse(
639
- withBody([
640
- "From: Alice <alice@acme.example>",
641
- `Authentication-Results: gw.corp.example; ${extra}dkim=fail (body hash did not verify) header.d=acme.example; dmarc=fail (p=REJECT) header.from=acme.example`,
642
- "X-Spam-Status: No, score=0.1",
643
- ]),
644
- );
645
- const without = await header("");
646
- const withEsp = await header("dkim=pass header.d=esp-mailer.example; ");
647
- assert.deepEqual(extractAuthResult(withEsp), extractAuthResult(without));
648
- assert.deepEqual(inboxVerdict(withEsp), inboxVerdict(without));
649
- assert.equal(inboxVerdict(withEsp).action, "leave");
650
- });
651
-
652
- it("never junks list mail whose relay stripped the original signature", async () => {
653
- const parsed = await parse(
654
- withBody([
655
- "From: Alice <alice@acme.example>",
656
- "To: list@googlegroups.example",
657
- "Authentication-Results: mx.google.example; dkim=pass header.d=googlegroups.example; spf=pass smtp.mailfrom=googlegroups.example; dmarc=fail (p=REJECT) header.from=acme.example",
658
- "List-Id: <list.googlegroups.example>",
659
- "X-Spam-Status: No, score=0.2",
660
- ]),
661
- );
662
- assert.equal(extractAuthenticity(parsed), null);
663
- assert.equal(inboxVerdict(parsed).action, "leave");
664
- });
665
-
666
- it("never lets a prepended header override the raw signing domain", async () => {
667
- const parsed = await parse(
668
- withBody([
669
- "Authentication-Results: anything-i-like; dkim=pass header.d=bank.example",
670
- "From: Security <security@bank.example>",
671
- "To: victim@example.com",
672
- "Subject: Verify your account",
673
- "DKIM-Signature: v=1; a=rsa-sha256; d=phish-host.example; s=sel; b=xxx",
674
- "X-Spam-Status: No, score=0.1",
675
- ]),
676
- );
677
- assert.deepEqual(extractAuthenticity(parsed), {
678
- fromDomain: "bank.example",
679
- dkimDomain: "phish-host.example",
680
- dkimMismatch: true,
681
- });
682
- });
683
-
684
- it("never lets a prepended header hide the receiving MTA's dmarc=fail", async () => {
685
- const lines = (order: "forged-first" | "real-first") => {
686
- const forged =
687
- "Authentication-Results: whatever; dkim=pass header.d=evil-mimic.example; dmarc=pass header.from=evil-mimic.example";
688
- const real = "Authentication-Results: mx.example.com; dmarc=fail";
689
- return withBody([
690
- ...(order === "forged-first" ? [forged, real] : [real, forged]),
691
- "From: Support <support@evil-mimic.example>",
692
- "To: me@example.com",
693
- "Subject: Verify your account",
694
- "DKIM-Signature: v=1; a=rsa-sha256; d=relay.example.net; s=sel; b=xxx",
695
- "X-Spam-Status: No, score=0.1",
696
- ]);
697
- };
698
- for (const order of ["forged-first", "real-first"] as const) {
699
- const parsed = await parse(lines(order));
700
- assert.equal(extractAuthResult(parsed)?.dmarc, "Fail", order);
701
- assert.equal(inboxVerdict(parsed).action, "move-to-junk", order);
702
- }
703
- });
704
-
705
- it("never lets a sender-added aligned dkim=pass override the receiver's verdict", async () => {
706
- const receivers = [
707
- "Authentication-Results: mx.me.example; dkim=fail header.d=phish.example; dmarc=fail header.from=bank.example",
708
- "Authentication-Results: mx.me.example; dkim=none; dmarc=fail header.from=bank.example",
709
- ];
710
- const forged = "Authentication-Results: x; dkim=pass header.d=bank.example";
711
- for (const [receiver, expected] of [
712
- [receivers[0], "Fail"],
713
- [receivers[1], "None"],
714
- ] as const) {
715
- for (const headers of [
716
- [receiver, forged],
717
- [forged, receiver],
718
- ]) {
719
- const parsed = await parse(
720
- withBody([...headers, "From: B <s@bank.example>"]),
721
- );
722
- assert.equal(
723
- extractAuthResult(parsed)?.dkim,
724
- expected,
725
- headers.join(" | "),
726
- );
727
- }
728
- }
729
- });
730
-
731
- it("never reads dkim=fail out of another method's comment", async () => {
732
- const parsed = await parse(
733
- withBody([
734
- "From: Alice <alice@acme.example>",
735
- "Authentication-Results: mx.example.net; arc=fail (i=1 spf=fail; dkim=fail",
736
- " header.d=arcsigner.example; dmarc=fail); spf=pass smtp.mailfrom=acme.example;",
737
- " dmarc=pass header.from=acme.example",
738
- ]),
739
- );
740
- assert.deepEqual(extractAuthResult(parsed), {
741
- dmarc: "Pass",
742
- spf: "Pass",
743
- dkim: undefined,
744
- });
745
- });
746
-
747
- it("reads dmarc from its own resinfo, not from an arc comment", async () => {
748
- const parsed = await parse(
749
- withBody([
750
- "From: Alice <alice@acme.example>",
751
- "Authentication-Results: gw.example; arc=pass (i=1 dmarc=fail fromdomain=acme.example); dmarc=pass header.from=acme.example",
752
- ]),
753
- );
754
- assert.equal(extractAuthResult(parsed)?.dmarc, "Pass");
755
- });
756
-
757
- describe("grammar edge cases", () => {
758
- const dkimFor = async (value: string) =>
759
- extractAuthResult(
760
- await parse(
761
- withBody([
762
- "From: Alice <alice@acme.example>",
763
- `Authentication-Results: ${value}`,
764
- ]),
765
- ),
766
- )?.dkim;
767
-
768
- it("aligns a signing domain written with a trailing root dot", async () => {
769
- assert.equal(
770
- await dkimFor(
771
- "gw.example; dkim=fail header.d=esp.example; dkim=pass header.d=acme.example.",
772
- ),
773
- "Pass",
774
- );
775
- });
776
-
777
- it("never aligns a header.i without an @", async () => {
778
- assert.equal(
779
- await dkimFor(
780
- "gw.example; dkim=fail header.d=esp.example; dkim=pass header.i=acme.example",
781
- ),
782
- "Fail",
783
- );
784
- });
785
-
786
- it("never aligns a result whose header.i falls outside header.d", async () => {
787
- assert.equal(
788
- await dkimFor(
789
- "gw.example; dkim=fail header.d=esp.example; dkim=pass header.d=acme.example header.i=@evil.example",
790
- ),
791
- "Fail",
792
- );
793
- });
794
-
795
- it("aligns through header.i when header.d is absent", async () => {
796
- assert.equal(
797
- await dkimFor(
798
- "gw.example; dkim=fail header.d=esp.example; dkim=pass header.i=alice@mail.acme.example",
799
- ),
800
- "Pass",
801
- );
802
- });
803
-
804
- it("reads upper-case tokens", async () => {
805
- const parsed = await parse(
806
- withBody([
807
- "From: Alice <alice@acme.example>",
808
- "Authentication-Results: GW.EXAMPLE; DKIM=FAIL HEADER.D=EVIL.EXAMPLE; DMARC=FAIL",
809
- ]),
810
- );
811
- assert.deepEqual(extractAuthResult(parsed), {
812
- dmarc: "Fail",
813
- spf: undefined,
814
- dkim: "Fail",
815
- });
816
- });
817
-
818
- it("reads a folded header whose continuation starts with dkim=", async () => {
819
- const parsed = await parse(
820
- withBody([
821
- "From: Alice <alice@acme.example>",
822
- "Authentication-Results: mx.example.com;",
823
- "\tdkim=pass header.d=acme.example;",
824
- "\tdmarc=fail header.from=acme.example",
825
- ]),
826
- );
827
- assert.deepEqual(extractAuthResult(parsed), {
828
- dmarc: "Fail",
829
- spf: undefined,
830
- dkim: "Pass",
831
- });
832
- });
833
-
834
- it("prefers the aligned result wherever it is listed", async () => {
835
- assert.equal(
836
- await dkimFor(
837
- "gw.example; dkim=fail header.d=esp-mailer.example; dkim=pass header.d=acme.example; dmarc=fail",
838
- ),
839
- "Pass",
840
- );
841
- });
842
-
843
- it("reads dkim=fail with no domain", async () => {
844
- assert.equal(await dkimFor("gw.example; dkim=fail; dmarc=fail"), "Fail");
845
- });
846
-
847
- it("never reads an unrecognised result as a pass", async () => {
848
- assert.equal(
849
- await dkimFor(
850
- "gw.example; dkim=pass header.d=acme.example; dkim=permerror header.d=acme.example",
851
- ),
852
- undefined,
853
- );
854
- });
855
- });
856
-
857
- describe("fail closed", () => {
858
- it("yields no result for an empty header", async () => {
859
- const parsed = await parse(
860
- withBody([
861
- "From: Alice <alice@acme.example>",
862
- "Authentication-Results: ",
863
- ]),
864
- );
865
- assert.equal(extractAuthResult(parsed), null);
866
- });
867
-
868
- it("yields no result for a header that does not parse", async () => {
869
- const parsed = await parse(
870
- withBody([
871
- "From: Alice <alice@acme.example>",
872
- "Authentication-Results: mx.example; dmarc=pass (unterminated",
873
- ]),
874
- );
875
- assert.equal(extractAuthResult(parsed), null);
876
- });
877
-
878
- it("ignores an unparseable header beside one that parses", async () => {
879
- const parsed = await parse(
880
- withBody([
881
- "Authentication-Results: forged; dmarc=pass )",
882
- "From: Alice <alice@acme.example>",
883
- "Authentication-Results: mx.example.com; dmarc=fail header.from=acme.example",
884
- ]),
885
- );
886
- assert.equal(extractAuthResult(parsed)?.dmarc, "Fail");
887
- });
888
- });
889
- });