@remit/mailbox-service 0.0.47 → 0.0.48

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remit/mailbox-service",
3
- "version": "0.0.47",
3
+ "version": "0.0.48",
4
4
  "type": "module",
5
5
  "main": "src/index.ts",
6
6
  "types": "src/index.ts",
@@ -95,7 +95,11 @@ export const classifyByHeaders = (parsed: ParsedMail): Category => {
95
95
  if (matchesPrecedence(headers)) return MessageCategory.automated;
96
96
  if (isMachineSender(parsed, lines)) return MessageCategory.automated;
97
97
 
98
- if (fromDomain && dkimMismatchResult(headers, lines, fromDomain).mismatch) {
98
+ if (
99
+ fromDomain &&
100
+ pickAlignedOrFirstMismatch(extractDkimDomains(headers, lines), fromDomain)
101
+ .mismatch
102
+ ) {
99
103
  return MessageCategory.automated;
100
104
  }
101
105
 
@@ -126,15 +130,12 @@ export const extractAuthenticity = (
126
130
  const dkimDomains = extractDkimDomains(headers, lines);
127
131
  if (dkimDomains.length === 0) return null;
128
132
 
129
- const result = dkimMismatchResult(headers, lines, fromDomain);
130
-
131
- // Pick the reported domain: first mismatching one on mismatch, first domain otherwise.
132
- const reportedDomain = result.mismatchingDomain ?? dkimDomains[0];
133
+ const picked = pickAlignedOrFirstMismatch(dkimDomains, fromDomain);
133
134
 
134
135
  return {
135
136
  fromDomain,
136
- dkimDomain: reportedDomain,
137
- dkimMismatch: result.mismatch,
137
+ dkimDomain: picked.domain ?? undefined,
138
+ dkimMismatch: picked.mismatch,
138
139
  };
139
140
  };
140
141
 
@@ -296,38 +297,34 @@ const domainMatches = (
296
297
  };
297
298
 
298
299
  /**
299
- * Check whether DKIM signing domain(s) align with the From domain and
300
- * return a structured result so both the category heuristic and the
301
- * authenticity extractor share the exact same alignment logic.
302
- *
303
- * Alignment: signing domain equals From domain, or one is a subdomain of
304
- * the other (parent/child). Any single aligned domain is enough to consider
305
- * the message non-mismatching — a legitimate re-mailer signing under a
306
- * subdomain is not suspicious.
307
- *
308
- * On mismatch the first non-aligned domain is reported so the UI can show
309
- * "signed by relay.example.net, claims example.com".
300
+ * Whether a signing domain aligns with the From domain: equal, or one is a
301
+ * subdomain of the other (parent/child). A legitimate re-mailer signing under
302
+ * a subdomain is not suspicious, so either direction counts as aligned.
310
303
  */
311
- const dkimMismatchResult = (
312
- headers: Headers,
313
- lines: HeaderLines,
304
+ const domainsAligned = (signingDomain: string, fromDomain: string): boolean =>
305
+ signingDomain === fromDomain ||
306
+ fromDomain.endsWith(`.${signingDomain}`) ||
307
+ signingDomain.endsWith(`.${fromDomain}`);
308
+
309
+ /**
310
+ * Pick the domain to report out of a list of candidate signing domains: the
311
+ * first one aligned with the From domain, so a legitimate signature is never
312
+ * shadowed by an earlier unrelated one. When none align, the first is
313
+ * reported as the mismatching evidence — the UI can then show "signed by
314
+ * relay.example.net, claims example.com". Shared by the category heuristic
315
+ * (rule 9, which reads only `.mismatch`) and the authenticity extractor
316
+ * (which also reads `.domain`), so the two can never disagree.
317
+ */
318
+ const pickAlignedOrFirstMismatch = (
319
+ domains: string[],
314
320
  fromDomain: string,
315
- ): { mismatch: boolean; mismatchingDomain: string | null } => {
316
- const dkimDomains = extractDkimDomains(headers, lines);
317
- if (dkimDomains.length === 0)
318
- return { mismatch: false, mismatchingDomain: null };
321
+ ): { mismatch: boolean; domain: string | null } => {
319
322
  let firstMismatching: string | null = null;
320
- for (const d of dkimDomains) {
321
- if (
322
- d === fromDomain ||
323
- fromDomain.endsWith(`.${d}`) ||
324
- d.endsWith(`.${fromDomain}`)
325
- ) {
326
- return { mismatch: false, mismatchingDomain: null };
327
- }
323
+ for (const d of domains) {
324
+ if (domainsAligned(d, fromDomain)) return { mismatch: false, domain: d };
328
325
  if (!firstMismatching) firstMismatching = d;
329
326
  }
330
- return { mismatch: true, mismatchingDomain: firstMismatching };
327
+ return { mismatch: firstMismatching !== null, domain: firstMismatching };
331
328
  };
332
329
 
333
330
  const extractDkimDomains = (headers: Headers, lines: HeaderLines): string[] => {
@@ -87,6 +87,28 @@ describe("classifyDisplayNameCorrespondence", () => {
87
87
  DisplayNameCorrespondence.Unrelated,
88
88
  );
89
89
  });
90
+
91
+ // Live phishing shape: a short, valuable brand name embedded as a
92
+ // coincidental substring of a longer, attacker-chosen domain. "ing" sits
93
+ // inside "secureingverify" the same way "irs"/"dhl"/"ups"/"kpn" sit inside
94
+ // countless lookalike domains — none of that is the domain naming the
95
+ // brand.
96
+ it("does not match a short brand name that is merely embedded in a longer domain label (ING)", () => {
97
+ assert.equal(
98
+ classifyDisplayNameCorrespondence(
99
+ "ING Fraudedesk",
100
+ "secure-ing-verify.tk",
101
+ ),
102
+ DisplayNameCorrespondence.Unrelated,
103
+ );
104
+ });
105
+
106
+ it("still matches a short brand name against its own real domain", () => {
107
+ assert.equal(
108
+ classifyDisplayNameCorrespondence("ING", "ing.nl"),
109
+ DisplayNameCorrespondence.Corresponds,
110
+ );
111
+ });
90
112
  });
91
113
 
92
114
  describe("extractOffDomainLinkDomains", () => {
@@ -90,11 +90,22 @@ const lookalikeThreshold = (length: number): number => {
90
90
  /**
91
91
  * Whether the From display name corresponds to the From domain.
92
92
  *
93
- * Containment decides: the normalised name, or any word of it, appearing inside
94
- * the registrable domain or one of the domain's labels. `GitHub` sits inside
95
- * `notifications.github.com`; `InfoMedics` sits nowhere inside
93
+ * Containment decides: the normalised name, or any word of it, containing an
94
+ * entire domain candidate. `GitHub` contains the label `github` from
95
+ * `notifications.github.com`; `InfoMedics` contains no label of
96
96
  * `serviceupdatebank.atlassian.net`.
97
97
  *
98
+ * Only that direction counts — a domain candidate containing the (shorter)
99
+ * name does not. `ING Fraudedesk` is not a match for `secure-ing-verify.tk`
100
+ * just because the three-letter word "ing" sits inside "secureingverify":
101
+ * that is a coincidental substring of a longer label the domain owner chose,
102
+ * not a domain that names the brand. The direction this drops is exactly the
103
+ * one a short, valuable brand name is deliberately embedded into a longer,
104
+ * unrelated-looking domain to exploit — the live Dutch-bank shape this was
105
+ * fixed against (`ING`). A real short brand over its own domain (`ING` /
106
+ * `ing.nl`) still matches: name and label are then equal, and equality
107
+ * satisfies containment in either direction.
108
+ *
98
109
  * A bounded edit distance is the secondary test, and only reaches names that
99
110
  * nearly match a label — `InfoMedics` against `1nfomedics.nl`. It cannot promote
100
111
  * an unrelated name on its own.
@@ -117,7 +128,7 @@ export const classifyDisplayNameCorrespondence = (
117
128
  const terms = [name, ...words(raw)];
118
129
  for (const term of terms) {
119
130
  for (const candidate of candidates) {
120
- if (candidate.includes(term) || term.includes(candidate)) {
131
+ if (term.includes(candidate)) {
121
132
  return DisplayNameCorrespondence.Corresponds;
122
133
  }
123
134
  }