@remit/mailbox-service 0.0.112 → 0.0.114
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/dist/account-credentials.d.ts +83 -0
- package/dist/account-credentials.js +104 -0
- package/dist/attribute-mapper.d.ts +36 -0
- package/dist/attribute-mapper.js +119 -0
- package/dist/body-parse.d.ts +28 -0
- package/{src/body-parse.ts → dist/body-parse.js} +18 -26
- package/dist/body-part-mapper.d.ts +92 -0
- package/dist/body-part-mapper.js +301 -0
- package/dist/body-sync-queue.d.ts +59 -0
- package/dist/body-sync-queue.js +56 -0
- package/dist/body-sync-terminal.d.ts +58 -0
- package/dist/body-sync-terminal.js +95 -0
- package/dist/body-sync.d.ts +351 -0
- package/dist/body-sync.js +1204 -0
- package/dist/calendar-parts.d.ts +23 -0
- package/dist/calendar-parts.js +18 -0
- package/dist/classification-backfill.d.ts +91 -0
- package/dist/classification-backfill.js +180 -0
- package/dist/classify-message.d.ts +31 -0
- package/dist/classify-message.js +89 -0
- package/dist/connection-factory.d.ts +54 -0
- package/dist/connection-factory.js +54 -0
- package/dist/connection-test.d.ts +41 -0
- package/dist/connection-test.js +152 -0
- package/dist/external-move.d.ts +94 -0
- package/dist/external-move.js +173 -0
- package/dist/filter-operations.d.ts +42 -0
- package/dist/filter-operations.js +211 -0
- package/dist/filters/filter-config.d.ts +14 -0
- package/dist/filters/filter-config.js +10 -0
- package/{src/filters/list-id.ts → dist/filters/list-id.d.ts} +2 -16
- package/dist/filters/list-id.js +26 -0
- package/dist/filters/match.d.ts +69 -0
- package/dist/filters/match.js +165 -0
- package/dist/filters/pipeline.d.ts +64 -0
- package/dist/filters/pipeline.js +100 -0
- package/{src/flag-push-terminal.ts → dist/flag-push-terminal.d.ts} +16 -72
- package/dist/flag-push-terminal.js +63 -0
- package/dist/flag-push.d.ts +69 -0
- package/dist/flag-push.js +111 -0
- package/dist/flag-queue.d.ts +175 -0
- package/dist/flag-queue.js +291 -0
- package/dist/folder-operations.d.ts +39 -0
- package/dist/folder-operations.js +112 -0
- package/dist/folder-placement.d.ts +13 -0
- package/dist/folder-placement.js +23 -0
- package/dist/heuristics/authenticationResults.d.ts +10 -0
- package/dist/heuristics/authenticationResults.js +187 -0
- package/dist/heuristics/classifyByHeaders.d.ts +82 -0
- package/dist/heuristics/classifyByHeaders.js +365 -0
- package/dist/heuristics/classifyPlacement.d.ts +67 -0
- package/dist/heuristics/classifyPlacement.js +153 -0
- package/dist/heuristics/machineSenders.d.ts +21 -0
- package/{src/heuristics/machineSenders.ts → dist/heuristics/machineSenders.js} +34 -40
- package/dist/heuristics/senderMismatch.d.ts +81 -0
- package/dist/heuristics/senderMismatch.js +192 -0
- package/dist/heuristics/socialDomains.d.ts +14 -0
- package/{src/heuristics/socialDomains.ts → dist/heuristics/socialDomains.js} +9 -9
- package/dist/heuristics/transactionalDomains.d.ts +18 -0
- package/{src/heuristics/transactionalDomains.ts → dist/heuristics/transactionalDomains.js} +8 -8
- package/dist/imapflow-connection.d.ts +377 -0
- package/dist/imapflow-connection.js +1147 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.js +65 -0
- package/dist/list-id-backfill.d.ts +83 -0
- package/dist/list-id-backfill.js +132 -0
- package/dist/mailbox-cursor-rebuild.d.ts +74 -0
- package/dist/mailbox-cursor-rebuild.js +71 -0
- package/{src/mailbox-cursor.ts → dist/mailbox-cursor.d.ts} +20 -98
- package/dist/mailbox-cursor.js +102 -0
- package/dist/mailbox-intent.d.ts +41 -0
- package/{src/mailbox-intent.ts → dist/mailbox-intent.js} +38 -64
- package/dist/mailbox-management.d.ts +240 -0
- package/dist/mailbox-management.js +576 -0
- package/dist/mailbox-presence.d.ts +52 -0
- package/{src/mailbox-presence.ts → dist/mailbox-presence.js} +19 -30
- package/dist/mailbox-queue.d.ts +129 -0
- package/dist/mailbox-queue.js +216 -0
- package/dist/mailbox-sync.d.ts +93 -0
- package/dist/mailbox-sync.js +490 -0
- package/dist/mailbox-upstream.d.ts +38 -0
- package/{src/mailbox-upstream.ts → dist/mailbox-upstream.js} +37 -47
- package/dist/message-category.d.ts +51 -0
- package/{src/message-category.ts → dist/message-category.js} +28 -71
- package/dist/message-move.d.ts +285 -0
- package/dist/message-move.js +858 -0
- package/dist/message-parser.d.ts +14 -0
- package/dist/message-parser.js +17 -0
- package/{src/message-presence.ts → dist/message-presence.d.ts} +1 -13
- package/dist/message-presence.js +29 -0
- package/dist/message-read.d.ts +51 -0
- package/dist/message-read.js +31 -0
- package/dist/message-sync.d.ts +345 -0
- package/dist/message-sync.js +1358 -0
- package/dist/mime-walker.d.ts +102 -0
- package/dist/mime-walker.js +198 -0
- package/dist/organize-job.d.ts +20 -0
- package/dist/organize-job.js +37 -0
- package/dist/outbox-attachment-content.d.ts +37 -0
- package/dist/outbox-attachment-content.js +50 -0
- package/dist/outbox-attachment-filename.d.ts +19 -0
- package/dist/outbox-attachment-filename.js +64 -0
- package/dist/outbox-attachment.d.ts +148 -0
- package/dist/outbox-attachment.js +273 -0
- package/dist/outbox-queue.d.ts +73 -0
- package/dist/outbox-queue.js +280 -0
- package/{src/outbox-status.ts → dist/outbox-status.d.ts} +1 -6
- package/dist/outbox-status.js +18 -0
- package/dist/ownership.d.ts +24 -0
- package/dist/ownership.js +83 -0
- package/dist/placement-move-terminal.d.ts +67 -0
- package/dist/placement-move-terminal.js +88 -0
- package/dist/placement-move.d.ts +83 -0
- package/dist/placement-move.js +216 -0
- package/{src/placement-settled.ts → dist/placement-settled.d.ts} +13 -68
- package/dist/placement-settled.js +75 -0
- package/dist/quarantine.d.ts +95 -0
- package/dist/quarantine.js +131 -0
- package/dist/refusal.d.ts +31 -0
- package/dist/refusal.js +3 -0
- package/{src/restore-source-placement.ts → dist/restore-source-placement.d.ts} +29 -97
- package/dist/restore-source-placement.js +79 -0
- package/dist/search-text-backfill.d.ts +15 -0
- package/dist/search-text-backfill.js +33 -0
- package/dist/snippet.d.ts +61 -0
- package/dist/snippet.js +129 -0
- package/dist/spam-report.d.ts +86 -0
- package/dist/spam-report.js +241 -0
- package/dist/sync-warning.d.ts +21 -0
- package/dist/sync-warning.js +26 -0
- package/dist/sync-watermarks.d.ts +138 -0
- package/dist/sync-watermarks.js +205 -0
- package/dist/test-helpers/fake-imap.d.ts +151 -0
- package/dist/test-helpers/fake-imap.js +508 -0
- package/dist/test-helpers/folder-roles.d.ts +34 -0
- package/dist/test-helpers/folder-roles.js +47 -0
- package/dist/test-helpers/mime-tree-generator.d.ts +82 -0
- package/dist/test-helpers/mime-tree-generator.js +329 -0
- package/dist/test-helpers/test-store.d.ts +69 -0
- package/dist/test-helpers/test-store.js +193 -0
- package/dist/text/normalizer.d.ts +20 -0
- package/dist/text/normalizer.js +102 -0
- package/dist/thread-message-rows.d.ts +69 -0
- package/dist/thread-message-rows.js +40 -0
- package/dist/thread-root.d.ts +7 -0
- package/dist/thread-root.js +25 -0
- package/dist/types.d.ts +343 -0
- package/dist/types.js +19 -0
- package/package.json +86 -88
- package/src/account-credentials.ts +0 -210
- package/src/attribute-mapper.test.ts +0 -104
- package/src/attribute-mapper.ts +0 -148
- package/src/body-parse.test.ts +0 -55
- package/src/body-part-mapper.property.test.ts +0 -354
- package/src/body-part-mapper.ts +0 -410
- package/src/body-sync-queue.test.ts +0 -64
- package/src/body-sync-queue.ts +0 -131
- package/src/body-sync-terminal.test.ts +0 -148
- package/src/body-sync-terminal.ts +0 -174
- package/src/body-sync.test.ts +0 -925
- package/src/body-sync.ts +0 -1891
- package/src/calendar-parts.ts +0 -41
- package/src/classification-backfill.test.ts +0 -285
- package/src/classification-backfill.ts +0 -363
- package/src/classify-message.test.ts +0 -142
- package/src/classify-message.ts +0 -116
- package/src/connection-factory.ts +0 -102
- package/src/connection-test.ts +0 -208
- package/src/credentials.test.ts +0 -181
- package/src/external-move.test.ts +0 -262
- package/src/external-move.ts +0 -285
- package/src/filter-operations.test.ts +0 -319
- package/src/filter-operations.ts +0 -377
- package/src/filter-update.test.ts +0 -313
- package/src/filters/filter-config.test.ts +0 -87
- package/src/filters/filter-config.ts +0 -34
- package/src/filters/list-id.test.ts +0 -50
- package/src/filters/match.test.ts +0 -392
- package/src/filters/match.ts +0 -225
- package/src/filters/pipeline.test.ts +0 -199
- package/src/filters/pipeline.ts +0 -170
- package/src/flag-push.test.ts +0 -112
- package/src/flag-push.ts +0 -198
- package/src/flag-queue.test.ts +0 -197
- package/src/flag-queue.ts +0 -509
- package/src/folder-operations.test.ts +0 -273
- package/src/folder-operations.ts +0 -217
- package/src/folder-placement.ts +0 -56
- package/src/forwarded-message.test.ts +0 -122
- package/src/heuristics/authenticationResults.test.ts +0 -189
- package/src/heuristics/authenticationResults.ts +0 -212
- package/src/heuristics/classifyByHeaders.test.ts +0 -889
- package/src/heuristics/classifyByHeaders.ts +0 -467
- package/src/heuristics/classifyPlacement.test.ts +0 -366
- package/src/heuristics/classifyPlacement.ts +0 -211
- package/src/heuristics/classifyRealMail.test.ts +0 -242
- package/src/heuristics/machineSenders.test.ts +0 -101
- package/src/heuristics/senderMismatch.test.ts +0 -346
- package/src/heuristics/senderMismatch.ts +0 -259
- package/src/imapflow-connect-retry.test.ts +0 -97
- package/src/imapflow-connection.test.ts +0 -503
- package/src/imapflow-connection.ts +0 -1476
- package/src/index.ts +0 -397
- package/src/list-id-backfill.test.ts +0 -207
- package/src/list-id-backfill.ts +0 -274
- package/src/mailbox-cursor-rebuild.test.ts +0 -171
- package/src/mailbox-cursor-rebuild.ts +0 -146
- package/src/mailbox-cursor.test.ts +0 -141
- package/src/mailbox-delete-intent.test.ts +0 -119
- package/src/mailbox-management.test.ts +0 -243
- package/src/mailbox-management.ts +0 -838
- package/src/mailbox-presence.test.ts +0 -74
- package/src/mailbox-queue.ts +0 -397
- package/src/mailbox-rename-intent.test.ts +0 -220
- package/src/mailbox-rename-subtree.test.ts +0 -287
- package/src/mailbox-sync.test.ts +0 -286
- package/src/mailbox-sync.ts +0 -724
- package/src/message-move.test.ts +0 -726
- package/src/message-move.ts +0 -1518
- package/src/message-parser.ts +0 -26
- package/src/message-presence.test.ts +0 -50
- package/src/message-read.ts +0 -87
- package/src/message-save-retry.test.ts +0 -90
- package/src/message-sync-flags.test.ts +0 -167
- package/src/message-sync.test.ts +0 -762
- package/src/message-sync.ts +0 -2133
- package/src/mime-walker.test.ts +0 -311
- package/src/mime-walker.ts +0 -306
- package/src/organize-job.ts +0 -93
- package/src/outbox-attachment-content.test.ts +0 -153
- package/src/outbox-attachment-content.ts +0 -104
- package/src/outbox-attachment-filename.test.ts +0 -132
- package/src/outbox-attachment-filename.ts +0 -76
- package/src/outbox-attachment.test.ts +0 -626
- package/src/outbox-attachment.ts +0 -536
- package/src/outbox-failed-recovery.test.ts +0 -299
- package/src/outbox-queue.test.ts +0 -315
- package/src/outbox-queue.ts +0 -525
- package/src/ownership.ts +0 -140
- package/src/placement-move-terminal.ts +0 -145
- package/src/placement-move.ts +0 -363
- package/src/placement-settled.test.ts +0 -147
- package/src/placement.test.ts +0 -404
- package/src/quarantine.test.ts +0 -133
- package/src/quarantine.ts +0 -229
- package/src/refusal.ts +0 -29
- package/src/snippet.test.ts +0 -213
- package/src/snippet.ts +0 -152
- package/src/spam-report.test.ts +0 -329
- package/src/spam-report.ts +0 -357
- package/src/sync-warning.test.ts +0 -213
- package/src/sync-warning.ts +0 -56
- package/src/sync-watermarks.test.ts +0 -409
- package/src/sync-watermarks.ts +0 -319
- package/src/system-flag-wire-format.test.ts +0 -77
- package/src/test-helpers/fake-imap.ts +0 -690
- package/src/test-helpers/folder-roles.ts +0 -66
- package/src/test-helpers/mime-tree-generator.ts +0 -461
- package/src/test-helpers/test-queue.ts +0 -56
- package/src/test-helpers/test-store.ts +0 -299
- package/src/text/normalizer.test.ts +0 -223
- package/src/text/normalizer.ts +0 -140
- package/src/thread-message-rows.ts +0 -77
- package/src/thread-root.test.ts +0 -29
- package/src/thread-root.ts +0 -51
- package/src/types/mailparser-augment.d.ts +0 -23
- package/src/types/natural-porter-stemmer.d.ts +0 -50
- package/src/types/stopword.d.ts +0 -69
- package/src/types.ts +0 -371
- 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,97 +0,0 @@
|
|
|
1
|
-
import assert from "node:assert/strict";
|
|
2
|
-
import { createServer, type Server, type Socket } from "node:net";
|
|
3
|
-
import { after, describe, it } from "node:test";
|
|
4
|
-
import { ImapFlowConnection } from "./imapflow-connection.js";
|
|
5
|
-
import { MailConnectionError } from "./types.js";
|
|
6
|
-
|
|
7
|
-
const servers: Server[] = [];
|
|
8
|
-
const sockets: Socket[] = [];
|
|
9
|
-
|
|
10
|
-
after(() => {
|
|
11
|
-
for (const socket of sockets) socket.destroy();
|
|
12
|
-
for (const server of servers) server.close();
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
const listen = (server: Server): Promise<number> =>
|
|
16
|
-
new Promise((resolve) => {
|
|
17
|
-
servers.push(server);
|
|
18
|
-
server.listen(0, "127.0.0.1", () => {
|
|
19
|
-
const address = server.address();
|
|
20
|
-
resolve(typeof address === "object" && address ? address.port : 0);
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
const refusingLogin = async (refusal: string) => {
|
|
25
|
-
const state = { connections: 0 };
|
|
26
|
-
const server = createServer((socket) => {
|
|
27
|
-
state.connections++;
|
|
28
|
-
sockets.push(socket);
|
|
29
|
-
socket.write("* OK [CAPABILITY IMAP4rev1] ready\r\n");
|
|
30
|
-
let buffered = "";
|
|
31
|
-
socket.on("data", (chunk) => {
|
|
32
|
-
buffered += chunk.toString();
|
|
33
|
-
let end = buffered.indexOf("\r\n");
|
|
34
|
-
while (end !== -1) {
|
|
35
|
-
const line = buffered.slice(0, end);
|
|
36
|
-
buffered = buffered.slice(end + 2);
|
|
37
|
-
const [tag, command] = line.split(" ");
|
|
38
|
-
const verb = command?.toUpperCase();
|
|
39
|
-
if (verb === "CAPABILITY") {
|
|
40
|
-
socket.write(`* CAPABILITY IMAP4rev1\r\n${tag} OK done\r\n`);
|
|
41
|
-
} else if (verb === "LOGIN") {
|
|
42
|
-
socket.write(`${tag} NO [AUTHENTICATIONFAILED] ${refusal}\r\n`);
|
|
43
|
-
} else if (verb === "LOGOUT") {
|
|
44
|
-
socket.end(`* BYE\r\n${tag} OK bye\r\n`);
|
|
45
|
-
} else {
|
|
46
|
-
socket.write(`${tag} BAD unknown\r\n`);
|
|
47
|
-
}
|
|
48
|
-
end = buffered.indexOf("\r\n");
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
socket.on("error", () => {});
|
|
52
|
-
});
|
|
53
|
-
return { port: await listen(server), state };
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
const closedPort = async (): Promise<number> => {
|
|
57
|
-
const server = createServer();
|
|
58
|
-
const port = await listen(server);
|
|
59
|
-
await new Promise((resolve) => server.close(resolve));
|
|
60
|
-
return port;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
const connectTo = (port: number) =>
|
|
64
|
-
new ImapFlowConnection({
|
|
65
|
-
host: "127.0.0.1",
|
|
66
|
-
port,
|
|
67
|
-
user: "alice@example.com",
|
|
68
|
-
credentials: { kind: "password", password: "s3cr3t" },
|
|
69
|
-
tls: false,
|
|
70
|
-
}).connect();
|
|
71
|
-
|
|
72
|
-
describe("ImapFlowConnection.connect() retry policy", () => {
|
|
73
|
-
it("retries a refused connection, then names the endpoint in a network error", async () => {
|
|
74
|
-
const port = await closedPort();
|
|
75
|
-
|
|
76
|
-
await assert.rejects(connectTo(port), (error: unknown) => {
|
|
77
|
-
assert.ok(error instanceof MailConnectionError);
|
|
78
|
-
assert.equal(error.kind, "network");
|
|
79
|
-
assert.match(error.message, new RegExp(`127\\.0\\.0\\.1:${port}`));
|
|
80
|
-
return true;
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it("does not retry a refused login and carries the server's own words", async () => {
|
|
85
|
-
const { port, state } = await refusingLogin(
|
|
86
|
-
"LOGIN failed. SmtpClientAuthentication is disabled for the Tenant.",
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
await assert.rejects(connectTo(port), (error: unknown) => {
|
|
90
|
-
assert.ok(error instanceof MailConnectionError);
|
|
91
|
-
assert.equal(error.kind, "auth");
|
|
92
|
-
assert.match(error.message, /SmtpClientAuthentication is disabled/);
|
|
93
|
-
return true;
|
|
94
|
-
});
|
|
95
|
-
assert.equal(state.connections, 1);
|
|
96
|
-
});
|
|
97
|
-
});
|