@remit/mailbox-service 0.0.111 → 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 (298) 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} +19 -77
  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} +39 -63
  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} +19 -50
  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 -101
  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 -73
  150. package/src/account-credentials.ts +0 -210
  151. package/src/adversarial-mime.e2e.test.ts +0 -307
  152. package/src/attribute-mapper.test.ts +0 -104
  153. package/src/attribute-mapper.ts +0 -148
  154. package/src/body-parse.test.ts +0 -55
  155. package/src/body-part-mapper.property.test.ts +0 -354
  156. package/src/body-part-mapper.ts +0 -360
  157. package/src/body-sync-auto-read-writeonce.test.ts +0 -219
  158. package/src/body-sync-calendar-suggestion.test.ts +0 -433
  159. package/src/body-sync-category-denormalize.test.ts +0 -352
  160. package/src/body-sync-category-override.test.ts +0 -501
  161. package/src/body-sync-category-writeonce.test.ts +0 -234
  162. package/src/body-sync-filter.test.ts +0 -237
  163. package/src/body-sync-message-gone.test.ts +0 -139
  164. package/src/body-sync-placement-flags.test.ts +0 -347
  165. package/src/body-sync-placement-writeonce.test.ts +0 -337
  166. package/src/body-sync-quarantine.test.ts +0 -276
  167. package/src/body-sync-queue.test.ts +0 -124
  168. package/src/body-sync-queue.ts +0 -131
  169. package/src/body-sync-resync.test.ts +0 -605
  170. package/src/body-sync-terminal.test.ts +0 -310
  171. package/src/body-sync-terminal.ts +0 -176
  172. package/src/body-sync-unclassifiable-once.test.ts +0 -260
  173. package/src/body-sync-unsubscribed-auto-read.test.ts +0 -176
  174. package/src/body-sync.ts +0 -1970
  175. package/src/calendar-parts.ts +0 -41
  176. package/src/classification-backfill.test.ts +0 -615
  177. package/src/classification-backfill.ts +0 -363
  178. package/src/classify-message.test.ts +0 -205
  179. package/src/classify-message.ts +0 -116
  180. package/src/connection-factory.ts +0 -102
  181. package/src/connection-test.ts +0 -208
  182. package/src/credentials.test.ts +0 -181
  183. package/src/external-move.test.ts +0 -369
  184. package/src/external-move.ts +0 -272
  185. package/src/filters/anchor-drift.ts +0 -55
  186. package/src/filters/filter-config.test.ts +0 -229
  187. package/src/filters/filter-config.ts +0 -69
  188. package/src/filters/list-id.test.ts +0 -50
  189. package/src/filters/match.test.ts +0 -429
  190. package/src/filters/match.ts +0 -274
  191. package/src/filters/message-embedder.test.ts +0 -39
  192. package/src/filters/message-embedder.ts +0 -33
  193. package/src/filters/pipeline.test.ts +0 -330
  194. package/src/filters/pipeline.ts +0 -284
  195. package/src/flag-push-terminal.test.ts +0 -209
  196. package/src/flag-push.test.ts +0 -408
  197. package/src/flag-push.ts +0 -198
  198. package/src/flag-queue-stars.test.ts +0 -171
  199. package/src/flag-queue.ts +0 -509
  200. package/src/folder-placement.ts +0 -56
  201. package/src/heuristics/authenticationResults.test.ts +0 -189
  202. package/src/heuristics/authenticationResults.ts +0 -212
  203. package/src/heuristics/classifyByHeaders.test.ts +0 -889
  204. package/src/heuristics/classifyByHeaders.ts +0 -467
  205. package/src/heuristics/classifyPlacement.test.ts +0 -366
  206. package/src/heuristics/classifyPlacement.ts +0 -211
  207. package/src/heuristics/classifyRealMail.test.ts +0 -242
  208. package/src/heuristics/machineSenders.test.ts +0 -101
  209. package/src/heuristics/senderMismatch.test.ts +0 -346
  210. package/src/heuristics/senderMismatch.ts +0 -259
  211. package/src/imapflow-connect-retry.test.ts +0 -134
  212. package/src/imapflow-connection.e2e.test.ts +0 -335
  213. package/src/imapflow-connection.integ.test.ts +0 -316
  214. package/src/imapflow-connection.test.ts +0 -469
  215. package/src/imapflow-connection.ts +0 -1475
  216. package/src/index.ts +0 -350
  217. package/src/list-id-backfill.test.ts +0 -573
  218. package/src/list-id-backfill.ts +0 -274
  219. package/src/mailbox-cursor-rebuild.test.ts +0 -171
  220. package/src/mailbox-cursor-rebuild.ts +0 -146
  221. package/src/mailbox-cursor.test.ts +0 -249
  222. package/src/mailbox-delete-intent.test.ts +0 -246
  223. package/src/mailbox-management.test.ts +0 -356
  224. package/src/mailbox-management.ts +0 -839
  225. package/src/mailbox-presence.test.ts +0 -72
  226. package/src/mailbox-queue.ts +0 -415
  227. package/src/mailbox-rename-intent.test.ts +0 -389
  228. package/src/mailbox-rename-subtree.test.ts +0 -664
  229. package/src/mailbox-sync.test.ts +0 -848
  230. package/src/mailbox-sync.ts +0 -724
  231. package/src/message-delete-junk-reconcile.test.ts +0 -117
  232. package/src/message-delete-no-trash.test.ts +0 -245
  233. package/src/message-delete-unsettled-placement.test.ts +0 -396
  234. package/src/message-move-copy.test.ts +0 -562
  235. package/src/message-move-empty-trash.test.ts +0 -326
  236. package/src/message-move-same-mailbox.test.ts +0 -139
  237. package/src/message-move-unsettled-placement.test.ts +0 -378
  238. package/src/message-move.ts +0 -1348
  239. package/src/message-parser.ts +0 -26
  240. package/src/message-presence.test.ts +0 -50
  241. package/src/message-sync-address-spoof.test.ts +0 -293
  242. package/src/message-sync-category.test.ts +0 -142
  243. package/src/message-sync-changedsince.test.ts +0 -840
  244. package/src/message-sync-flags.test.ts +0 -158
  245. package/src/message-sync-junk-harvest.test.ts +0 -244
  246. package/src/message-sync-quarantine.test.ts +0 -416
  247. package/src/message-sync-repoint.test.ts +0 -419
  248. package/src/message-sync.ts +0 -1963
  249. package/src/mime-walker.test.ts +0 -311
  250. package/src/mime-walker.ts +0 -305
  251. package/src/outbox-attachment-content.test.ts +0 -153
  252. package/src/outbox-attachment-content.ts +0 -104
  253. package/src/outbox-attachment-filename.test.ts +0 -132
  254. package/src/outbox-attachment-filename.ts +0 -76
  255. package/src/outbox-attachment.test.ts +0 -738
  256. package/src/outbox-attachment.ts +0 -536
  257. package/src/outbox-failed-recovery.test.ts +0 -370
  258. package/src/outbox-queue-settled-row.test.ts +0 -140
  259. package/src/outbox-queue.test.ts +0 -291
  260. package/src/outbox-queue.ts +0 -508
  261. package/src/pass-through-unit-of-work.ts +0 -17
  262. package/src/placement-move-terminal.test.ts +0 -300
  263. package/src/placement-move-terminal.ts +0 -148
  264. package/src/placement-move-unsettled.test.ts +0 -275
  265. package/src/placement-move.ts +0 -336
  266. package/src/placement-settled.test.ts +0 -147
  267. package/src/placement-transition-race.test.ts +0 -482
  268. package/src/quarantine.test.ts +0 -191
  269. package/src/quarantine.ts +0 -228
  270. package/src/snippet.test.ts +0 -213
  271. package/src/snippet.ts +0 -152
  272. package/src/spam-report.test.ts +0 -801
  273. package/src/spam-report.ts +0 -357
  274. package/src/stale-message-reconcile.test.ts +0 -82
  275. package/src/stale-message-reconcile.ts +0 -55
  276. package/src/sync-watermarks.test.ts +0 -409
  277. package/src/sync-watermarks.ts +0 -319
  278. package/src/system-flag-wire-format.test.ts +0 -77
  279. package/src/test-helpers/folder-roles.ts +0 -66
  280. package/src/test-helpers/isolated-mailbox.ts +0 -44
  281. package/src/test-helpers/mailfuzz-connection.ts +0 -27
  282. package/src/test-helpers/mime-tree-generator.ts +0 -461
  283. package/src/text/normalizer.test.ts +0 -223
  284. package/src/text/normalizer.ts +0 -140
  285. package/src/thread-message-rows.ts +0 -77
  286. package/src/types/mailparser-augment.d.ts +0 -15
  287. package/src/types/natural-porter-stemmer.d.ts +0 -50
  288. package/src/types/stopword.d.ts +0 -69
  289. package/src/types.ts +0 -371
  290. package/test/fixtures/mime-adversarial-e2e/01-octet-stream-pdf.eml +0 -21
  291. package/test/fixtures/mime-adversarial-e2e/02-nested-related-cid-image.eml +0 -31
  292. package/test/fixtures/mime-adversarial-e2e/03-calendar-invite.eml +0 -33
  293. package/test/fixtures/mime-adversarial-e2e/04-forwarded-rfc822-with-attachments.eml +0 -42
  294. package/test/fixtures/mime-adversarial-e2e/05-tnef-winmail.eml +0 -25
  295. package/test/fixtures/mime-adversarial-e2e/06-8bit-non-ascii.eml +0 -15
  296. package/test/fixtures/mime-adversarial-e2e/07-quoted-printable-soft-breaks.eml +0 -17
  297. package/test/fixtures/mime-adversarial-e2e/08-alternative-empty-text.eml +0 -18
  298. package/tsconfig.json +0 -8
@@ -1,259 +0,0 @@
1
- import { DisplayNameCorrespondence } from "@remit/domain-enums";
2
- import type { ParsedMail } from "mailparser";
3
- import { getDomain, parse as parseHost } from "tldts";
4
- import type { FolderPlacement } from "./classifyPlacement.js";
5
-
6
- type CorrespondenceValue =
7
- (typeof DisplayNameCorrespondence)[keyof typeof DisplayNameCorrespondence];
8
-
9
- /**
10
- * The two signals that survive a passing SPF/DKIM/DMARC check: a display name
11
- * that belongs to nobody at the sending domain, and body links that leave it.
12
- *
13
- * Both are deliberately aggressive, and both are computed only for mail the
14
- * provider's own filter already called spam or that sits in the Junk folder
15
- * (see {@link extractSenderMismatch}).
16
- * On a shared-tenant host — a free Atlassian, Salesforce or Zendesk instance —
17
- * the verified subdomain is chosen by whoever signed up, so a passing signature
18
- * proves the domain and nothing about the identity the message claims.
19
- */
20
- export interface SenderMismatchSignals {
21
- displayNameCorrespondence?: CorrespondenceValue;
22
- offDomainLinkDomains?: string[];
23
- }
24
-
25
- export interface SenderMismatchContext {
26
- fromDomain: string;
27
- providerSpamClassified: boolean;
28
- placement: FolderPlacement;
29
- /**
30
- * List-Unsubscribe present. Bulk mail routinely shows a brand name over an
31
- * ESP's sending domain, so the display-name comparison says nothing there and
32
- * is not made.
33
- */
34
- bulkSender: boolean;
35
- }
36
-
37
- const MAX_LINK_DOMAINS = 10;
38
-
39
- const normalize = (value: string): string =>
40
- value.toLowerCase().replace(/[^a-z0-9]/g, "");
41
-
42
- /**
43
- * The parts of a host a display name could honestly correspond to: the
44
- * registrable name without its public suffix, every subdomain label, and the
45
- * registrable domain run together. The suffix itself is dropped — `net` and
46
- * `com` sit inside half the brand names in existence.
47
- */
48
- const correspondenceCandidates = (host: string): string[] => {
49
- const parsed = parseHost(host);
50
- const labels = (parsed.subdomain ?? "").split(".");
51
- if (parsed.domainWithoutSuffix !== null) {
52
- labels.push(parsed.domainWithoutSuffix);
53
- } else {
54
- labels.push(...host.split(".").slice(0, -1));
55
- }
56
- if (parsed.domain !== null) labels.push(parsed.domain);
57
- return labels.map(normalize).filter((label) => label.length >= 3);
58
- };
59
-
60
- const words = (displayName: string): string[] =>
61
- displayName
62
- .toLowerCase()
63
- .split(/[^a-z0-9]+/)
64
- .filter((word) => word.length >= 3);
65
-
66
- const editDistance = (a: string, b: string): number => {
67
- let previous = Array.from({ length: b.length + 1 }, (_, i) => i);
68
- for (let i = 1; i <= a.length; i++) {
69
- const current = [i];
70
- for (let j = 1; j <= b.length; j++) {
71
- current[j] = Math.min(
72
- previous[j] + 1,
73
- current[j - 1] + 1,
74
- previous[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1),
75
- );
76
- }
77
- previous = current;
78
- }
79
- return previous[b.length];
80
- };
81
-
82
- const ADDRESS_SHAPE = /[^\s<>@,;:"]+@([a-z0-9-]+(?:\.[a-z0-9-]+)+)/i;
83
-
84
- const registrableDomain = (host: string): string =>
85
- getDomain(host) ?? host.toLowerCase();
86
-
87
- /**
88
- * The registrable domain of the address a display name spells out, or
89
- * `undefined` when it holds an `@` without an address behind it — `Bob @ Acme`
90
- * asserts no address and is left to the ordinary comparison.
91
- */
92
- const claimedAddressDomain = (displayName: string): string | undefined => {
93
- const host = ADDRESS_SHAPE.exec(displayName)?.[1];
94
- if (host === undefined) return undefined;
95
- return registrableDomain(host);
96
- };
97
-
98
- /**
99
- * The distance a name of this length may be from a domain label and still be
100
- * read as an imitation of it. Tight on purpose: an unrelated brand name and an
101
- * ESP's domain are always far apart, so distance alone must never decide.
102
- */
103
- const lookalikeThreshold = (length: number): number => {
104
- if (length < 5) return 0;
105
- if (length < 9) return 1;
106
- return 2;
107
- };
108
-
109
- /**
110
- * Whether the From display name corresponds to the From domain.
111
- *
112
- * Containment decides: the normalised name, or any word of it, containing an
113
- * entire domain candidate. `GitHub` contains the label `github` from
114
- * `notifications.github.com`; `InfoMedics` contains no label of
115
- * `serviceupdatebank.atlassian.net`.
116
- *
117
- * Only that direction counts — a domain candidate containing the (shorter)
118
- * name does not. `ING Fraudedesk` is not a match for `secure-ing-verify.tk`
119
- * just because the three-letter word "ing" sits inside "secureingverify":
120
- * that is a coincidental substring of a longer label the domain owner chose,
121
- * not a domain that names the brand. The direction this drops is exactly the
122
- * one a short, valuable brand name is deliberately embedded into a longer,
123
- * unrelated-looking domain to exploit — the live Dutch-bank shape this was
124
- * fixed against (`ING`). A real short brand over its own domain (`ING` /
125
- * `ing.nl`) still matches: name and label are then equal, and equality
126
- * satisfies containment in either direction.
127
- *
128
- * A bounded edit distance is the secondary test, and only reaches names that
129
- * nearly match a label — `InfoMedics` against `1nfomedics.nl`. It cannot promote
130
- * an unrelated name on its own.
131
- *
132
- * A name that spells out an address is decided before any of that. Over the
133
- * sending domain it claims nothing — `billing@` shown over
134
- * `serviceupdatebank.atlassian.net` names the same party the envelope does.
135
- * Over any other registrable domain it is the strongest non-correspondence
136
- * signal there is: the message asserts, in the one field the recipient reads,
137
- * that it comes from an address that cannot have sent it.
138
- */
139
- export const classifyDisplayNameCorrespondence = (
140
- displayName: string | undefined,
141
- fromDomain: string,
142
- ): CorrespondenceValue => {
143
- const raw = (displayName ?? "").trim();
144
- if (raw === "") return DisplayNameCorrespondence.NoClaim;
145
-
146
- const claimed = claimedAddressDomain(raw);
147
- if (claimed !== undefined) {
148
- if (claimed === registrableDomain(fromDomain)) {
149
- return DisplayNameCorrespondence.NoClaim;
150
- }
151
- return DisplayNameCorrespondence.ForeignAddress;
152
- }
153
-
154
- const name = normalize(raw);
155
- if (name.length < 3) return DisplayNameCorrespondence.NoClaim;
156
-
157
- const candidates = correspondenceCandidates(fromDomain);
158
- if (candidates.length === 0) return DisplayNameCorrespondence.NoClaim;
159
-
160
- const terms = [name, ...words(raw)];
161
- for (const term of terms) {
162
- for (const candidate of candidates) {
163
- if (term.includes(candidate)) {
164
- return DisplayNameCorrespondence.Corresponds;
165
- }
166
- }
167
- }
168
-
169
- for (const candidate of candidates) {
170
- const threshold = lookalikeThreshold(
171
- Math.min(name.length, candidate.length),
172
- );
173
- if (threshold === 0) continue;
174
- if (Math.abs(name.length - candidate.length) > threshold) continue;
175
- if (editDistance(name, candidate) <= threshold) {
176
- return DisplayNameCorrespondence.Lookalike;
177
- }
178
- }
179
-
180
- return DisplayNameCorrespondence.Unrelated;
181
- };
182
-
183
- const hrefsFromHtml = (html: string): string[] => {
184
- const out: string[] = [];
185
- const pattern = /href\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/gi;
186
- let match = pattern.exec(html);
187
- while (match !== null) {
188
- out.push(match[1] ?? match[2] ?? match[3] ?? "");
189
- match = pattern.exec(html);
190
- }
191
- return out;
192
- };
193
-
194
- const urlsFromText = (text: string): string[] =>
195
- text.match(/https?:\/\/[^\s<>"')\]]+/gi) ?? [];
196
-
197
- /**
198
- * Registrable domains the body's links point at, minus the sender's own.
199
- *
200
- * `mailto:`, `tel:`, in-page anchors and relative hrefs carry no destination
201
- * domain and are skipped; anything else is resolved public-suffix-aware through
202
- * tldts, so `co.uk` and `atlassian.net` both survive the comparison a naive dot
203
- * split gets wrong.
204
- */
205
- export const extractOffDomainLinkDomains = (
206
- parsed: ParsedMail,
207
- fromDomain: string,
208
- ): string[] => {
209
- const senderDomain = getDomain(fromDomain);
210
- const html = typeof parsed.html === "string" ? parsed.html : "";
211
- const text = typeof parsed.text === "string" ? parsed.text : "";
212
-
213
- const seen = new Set<string>();
214
- const out: string[] = [];
215
- for (const href of [...hrefsFromHtml(html), ...urlsFromText(text)]) {
216
- const value = href.trim();
217
- if (value === "" || !/^https?:\/\//i.test(value)) continue;
218
- const domain = getDomain(value);
219
- if (domain === null || domain === senderDomain) continue;
220
- if (seen.has(domain)) continue;
221
- seen.add(domain);
222
- out.push(domain);
223
- if (out.length === MAX_LINK_DOMAINS) break;
224
- }
225
- return out;
226
- };
227
-
228
- /**
229
- * Both signals, gated on a spam verdict: the provider's own, or the message
230
- * sitting in the Junk folder.
231
- *
232
- * The gate is what lets the checks be this aggressive: ordinary mail never
233
- * reaches them, so a brand name over an ESP domain or a newsletter full of
234
- * tracking links can never be flagged. Returns an empty object when the gate
235
- * does not open — the fields stay absent, meaning "not compared".
236
- */
237
- export const extractSenderMismatch = (
238
- parsed: ParsedMail,
239
- context: SenderMismatchContext,
240
- ): SenderMismatchSignals => {
241
- if (!context.providerSpamClassified && context.placement !== "junk") {
242
- return {};
243
- }
244
-
245
- const offDomainLinkDomains = extractOffDomainLinkDomains(
246
- parsed,
247
- context.fromDomain,
248
- );
249
-
250
- if (context.bulkSender) return { offDomainLinkDomains };
251
-
252
- return {
253
- displayNameCorrespondence: classifyDisplayNameCorrespondence(
254
- parsed.from?.value?.[0]?.name,
255
- context.fromDomain,
256
- ),
257
- offDomainLinkDomains,
258
- };
259
- };
@@ -1,134 +0,0 @@
1
- /**
2
- * Unit tests for ImapFlowConnection.connect() retry semantics.
3
- *
4
- * Verifies the retry policy without touching a real IMAP server:
5
- * - Network errors (e.g. ECONNREFUSED) are retried with backoff before the
6
- * classified MailConnectionError("network") is finally thrown.
7
- * - Auth errors are thrown immediately and NEVER retried.
8
- *
9
- * The private `attemptConnect` and `sleep` instance methods are stubbed so the
10
- * test exercises the loop logic directly and runs instantly.
11
- */
12
-
13
- import assert from "node:assert/strict";
14
- import { describe, it, mock } from "node:test";
15
- import { ImapFlowConnection } from "./imapflow-connection.js";
16
- import { MailConnectionError } from "./types.js";
17
-
18
- const baseConfig = {
19
- host: "localhost",
20
- port: 143,
21
- user: "alice@example.com",
22
- credentials: { kind: "password" as const, password: "s3cr3t" },
23
- tls: false,
24
- };
25
-
26
- type WithPrivates = {
27
- attemptConnect: () => Promise<void>;
28
- sleep: (ms: number) => Promise<void>;
29
- };
30
-
31
- const makeErrnoError = (code: string): Error => {
32
- const err = new Error(`connect ${code}`);
33
- return Object.assign(err, { code });
34
- };
35
-
36
- describe("ImapFlowConnection.connect() retry policy", () => {
37
- it("retries network errors (ECONNREFUSED) 3x with backoff, then throws classified network error", async () => {
38
- const connection = new ImapFlowConnection(baseConfig);
39
- const priv = connection as unknown as WithPrivates;
40
-
41
- // Always fail with a network-level error.
42
- const attempt = mock.method(priv, "attemptConnect", async () => {
43
- throw makeErrnoError("ECONNREFUSED");
44
- });
45
- // Stub sleep so the backoff does not actually delay the test.
46
- mock.method(priv, "sleep", async () => {});
47
-
48
- await assert.rejects(
49
- () => connection.connect(),
50
- (error: unknown) => {
51
- assert.ok(
52
- error instanceof MailConnectionError,
53
- "final error should be a MailConnectionError",
54
- );
55
- assert.equal(error.kind, "network");
56
- // The host:port endpoint must appear in the message so a DNS /
57
- // connect failure is self-describing in the logs (issue #835).
58
- assert.match(error.message, /localhost:143/);
59
- return true;
60
- },
61
- );
62
-
63
- // 3 attempts total (the retry loop runs maxRetries times).
64
- assert.equal(attempt.mock.callCount(), 3);
65
- });
66
-
67
- it("succeeds on a later attempt when a transient network error clears", async () => {
68
- const connection = new ImapFlowConnection(baseConfig);
69
- const priv = connection as unknown as WithPrivates;
70
-
71
- let calls = 0;
72
- const attempt = mock.method(priv, "attemptConnect", async () => {
73
- calls += 1;
74
- if (calls < 2) {
75
- throw makeErrnoError("ECONNRESET");
76
- }
77
- // Second attempt succeeds.
78
- });
79
- mock.method(priv, "sleep", async () => {});
80
-
81
- await connection.connect();
82
-
83
- assert.equal(attempt.mock.callCount(), 2);
84
- });
85
-
86
- it("does NOT retry auth errors — throws immediately on attempt 1", async () => {
87
- const connection = new ImapFlowConnection(baseConfig);
88
- const priv = connection as unknown as WithPrivates;
89
-
90
- const attempt = mock.method(priv, "attemptConnect", async () => {
91
- throw new Error("Invalid credentials");
92
- });
93
- const sleep = mock.method(priv, "sleep", async () => {});
94
-
95
- await assert.rejects(
96
- () => connection.connect(),
97
- (error: unknown) => {
98
- assert.ok(error instanceof MailConnectionError);
99
- assert.equal(error.kind, "auth");
100
- return true;
101
- },
102
- );
103
-
104
- // Only one attempt, no backoff sleep.
105
- assert.equal(attempt.mock.callCount(), 1);
106
- assert.equal(sleep.mock.callCount(), 0);
107
- });
108
-
109
- it("carries the server's own refusal, which is not always a re-auth", async () => {
110
- const connection = new ImapFlowConnection(baseConfig);
111
- const priv = connection as unknown as WithPrivates;
112
-
113
- // imapflow's shape for a tagged NO: the message is its own, the server's
114
- // words are on `responseText`. Signing in again fixes nothing here — the
115
- // tenant has IMAP switched off — so the text has to reach the user.
116
- mock.method(priv, "attemptConnect", async () => {
117
- throw Object.assign(new Error("Command failed"), {
118
- authenticationFailed: true,
119
- responseText:
120
- "LOGIN failed. User is authenticated but not connected. SmtpClientAuthentication is disabled for the Tenant.",
121
- });
122
- });
123
-
124
- await assert.rejects(
125
- () => connection.connect(),
126
- (error: unknown) => {
127
- assert.ok(error instanceof MailConnectionError);
128
- assert.equal(error.kind, "auth");
129
- assert.match(error.message, /SmtpClientAuthentication is disabled/);
130
- return true;
131
- },
132
- );
133
- });
134
- });