Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changes/perf-disposable-domain-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### Fixed

- **Scoring mail senders no longer rebuilds the throwaway-domain list for each sender.** The spam score
(`scoreGroup`) and the organizations from mail domains now share one set built on first use
(`isDisposableDomain`). Scoring 20,000 senders took about 7 s and now takes about 0.2 s.
7 changes: 6 additions & 1 deletion src/mail/score.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest"
import { headersOf, mailHeadersOf } from "./headers.js"
import { domainsToAge, probeMail } from "./probe.js"
import { FREE_MAIL, scoreGroup, spoofedBrand, youngAt } from "./score.js"
import { FREE_MAIL, isDisposableDomain, scoreGroup, spoofedBrand, youngAt } from "./score.js"
import type { Listed } from "./source.js"

const head = (lines: string[]) => `${lines.join("\r\n")}\r\n`
Expand Down Expand Up @@ -137,6 +137,11 @@ describe("scoreGroup", () => {
expect(scoreGroup(throwaway, { replied: false, bulk: 0, ages }).signals).toEqual(["disposable_domain"])
})

it("knows a throwaway domain from the shipped list and leaves mailbox providers and companies alone", () => {
expect(["mailinator.com", "guerrillamail.com"].map(isDisposableDomain)).toEqual([true, true])
expect(["gmail.com", "shop.example"].map(isDisposableDomain)).toEqual([false, false])
})

it("adds mail never opened and a DKIM check most of the mail did not pass, and ignores unknowns", () => {
const robot = [1, 2].map((n) =>
email(n, ["From: deals@shop.example", "Authentication-Results: mx.google.com; spf=pass"], { seen: false }),
Expand Down
12 changes: 10 additions & 2 deletions src/mail/score.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDisposableEmailDomain } from "disposable-email-domains-js"
import { disposableEmailBlocklistSet } from "disposable-email-domains-js"
import { getDomain } from "tldts"
import type { Listed } from "./source.js"

Expand Down Expand Up @@ -85,6 +85,14 @@ export const FREE_MAIL = new Set([
"zoho.com",
])

let disposable: Set<string> | undefined
// `isDisposableEmailDomain` builds its set of about 9,000 domains on every call.
/** A throwaway mail domain (CC0 list); `domain` is lower case, as `registrableOf` gives it. */
export const isDisposableDomain = (domain: string): boolean => {
disposable ??= disposableEmailBlocklistSet()
return disposable.has(domain)
}

/** A brand and the first labels of the domains it really sends from. */
const BRANDS: Record<string, string[]> = {
paypal: ["paypal"],
Expand Down Expand Up @@ -192,7 +200,7 @@ export const scoreGroup = (
// Gmail's stamp without a dkim= verdict means the mail carried no signature: that counts as not passed.
const stamped = items.filter((item) => item.headers?.auth !== undefined)
if (stamped.length && most(stamped, (item) => item.headers?.auth?.dkim !== "pass")) signals.push("dkim_not_passed")
if (domain !== undefined && isDisposableEmailDomain(domain)) signals.push("disposable_domain")
if (domain !== undefined && isDisposableDomain(domain)) signals.push("disposable_domain")
if (context.bulk >= 0.8 && !context.replied) signals.push("bulk_never_replied")
if (context.replied) signals.push("replied")
const kept =
Expand Down
12 changes: 2 additions & 10 deletions src/store/sqlite/domain-organizations.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { disposableEmailBlocklistSet } from "disposable-email-domains-js"
import { getDomainWithoutSuffix } from "tldts"
import { isReadable } from "../../domain/display-names.js"
import { isCompanyName } from "../../domain/person-kinds.js"
import { FREE_MAIL, registrableOf } from "../../mail/score.js"
import { FREE_MAIL, isDisposableDomain, registrableOf } from "../../mail/score.js"
import type { CacheDatabase } from "../driver.js"
import { fold } from "../normalize.js"
import { actorOfOrigin } from "./actors.js"
Expand Down Expand Up @@ -42,18 +41,11 @@ export const BY_DOMAIN = "domain"
/** A rejected `proposed_actions` row of this kind keeps a rule from making an organization for its domain again. */
export const REFUSED_DOMAIN = "organization-domain"

let disposable: Set<string> | undefined
// `isDisposableEmailDomain` builds the whole set on every call: about 0.5 ms an address.
const isDisposable = (domain: string): boolean => {
disposable ??= disposableEmailBlocklistSet()
return disposable.has(domain)
}

/** The registrable domain whose organization an address joins; none for free, throwaway and platform mail. */
export const companyDomainOf = (address: string): string | undefined => {
if (!address.includes("@")) return undefined
const domain = registrableOf(address)
return domain.includes(".") && !FREE_MAIL.has(domain) && !SENDING_PLATFORMS.has(domain) && !isDisposable(domain)
return domain.includes(".") && !FREE_MAIL.has(domain) && !SENDING_PLATFORMS.has(domain) && !isDisposableDomain(domain)
? domain
: undefined
}
Expand Down
Loading