Skip to content

Email for professionals and teams

Added Spam filter

Hieu Nguyen (Jack)c5cfa1be6da1
diff --git a/docs/spam-protection.md b/docs/spam-protection.md
new file mode 100644
index 0000000..5438dd5
--- /dev/null
+++ b/docs/spam-protection.md
@@ -0,0 +1,11 @@
+# Spam protection
+
+Mailflare analyzes stored incoming messages locally. The filter combines trusted authentication results, sender relationships, mailbox-scoped reputation, bounded Bayesian token learning, URL checks, message structure, and attachment metadata. No message content or spam intelligence is sent to an external service.
+
+Scores range from 0 to 100. Messages scoring 70 or higher move to Spam. Scores from 40 through 69 are recorded as suspicious but remain in their normal Inbox or custom folder. Analysis failures also deliver normally. Automatic replies and new-message notifications are suppressed for messages placed in Spam.
+
+The Spam Filter switch is under **Settings → Inbox → Spam protection** and is enabled by default. Disabling it affects future incoming messages; it does not move or rescan existing mail.
+
+Use **Report spam** to train a message as spam. Use **Not spam** from the Spam folder to train it as legitimate and return it to Inbox. Learning is scoped to the message's mailbox, only explicit user feedback trains the classifier, and changing a decision reverses the previous training first.
+
+Mailflare accepts mail before this analysis. Forward-only routing occurs before stored-message filtering and is outside the filter's v1 scope.
diff --git a/drizzle/migrations/0029_add_spam_protection.sql b/drizzle/migrations/0029_add_spam_protection.sql
new file mode 100644
index 0000000..262c60b
--- /dev/null
+++ b/drizzle/migrations/0029_add_spam_protection.sql
@@ -0,0 +1,43 @@
+ALTER TABLE `users` ADD `spam_protection_enabled` integer DEFAULT true NOT NULL;
+ALTER TABLE `messages` ADD `spam_score` integer;
+ALTER TABLE `messages` ADD `spam_verdict` text;
+ALTER TABLE `messages` ADD `spam_signals` text;
+ALTER TABLE `messages` ADD `spam_analyzed_at` integer;
+ALTER TABLE `messages` ADD `spam_analysis_error` text;
+CREATE INDEX `messages_raw_r2_key_idx` ON `messages` (`raw_r2_key`);
+
+CREATE TABLE `spam_token_stats` (
+ `mailbox_id` text NOT NULL REFERENCES `mailboxes`(`id`) ON DELETE cascade,
+ `token` text NOT NULL,
+ `spam_count` integer DEFAULT 0 NOT NULL,
+ `ham_count` integer DEFAULT 0 NOT NULL,
+ `updated_at` integer NOT NULL
+);
+CREATE UNIQUE INDEX `spam_token_stats_mailbox_token_idx` ON `spam_token_stats` (`mailbox_id`, `token`);
+CREATE INDEX `spam_token_stats_mailbox_idx` ON `spam_token_stats` (`mailbox_id`);
+
+CREATE TABLE `spam_reputation` (
+ `mailbox_id` text NOT NULL REFERENCES `mailboxes`(`id`) ON DELETE cascade,
+ `type` text NOT NULL,
+ `key` text NOT NULL,
+ `messages_seen` integer DEFAULT 0 NOT NULL,
+ `spam_count` integer DEFAULT 0 NOT NULL,
+ `ham_count` integer DEFAULT 0 NOT NULL,
+ `first_seen_at` integer NOT NULL,
+ `last_seen_at` integer NOT NULL
+);
+CREATE UNIQUE INDEX `spam_reputation_mailbox_type_key_idx` ON `spam_reputation` (`mailbox_id`, `type`, `key`);
+CREATE INDEX `spam_reputation_mailbox_idx` ON `spam_reputation` (`mailbox_id`);
+
+CREATE TABLE `spam_feedback` (
+ `message_id` text PRIMARY KEY NOT NULL REFERENCES `messages`(`id`) ON DELETE cascade,
+ `mailbox_id` text NOT NULL REFERENCES `mailboxes`(`id`) ON DELETE cascade,
+ `actor_user_id` text REFERENCES `users`(`id`) ON DELETE set null,
+ `classification` text NOT NULL,
+ `training_tokens` text NOT NULL,
+ `reputation_keys` text NOT NULL,
+ `tokenizer_version` integer DEFAULT 1 NOT NULL,
+ `created_at` integer NOT NULL,
+ `updated_at` integer NOT NULL
+);
+CREATE INDEX `spam_feedback_mailbox_idx` ON `spam_feedback` (`mailbox_id`);
diff --git a/src/app/(dashboard)/inbox/[messageId]/page.tsx b/src/app/(dashboard)/inbox/[messageId]/page.tsx
index 67c7099..cd404ee 100644
--- a/src/app/(dashboard)/inbox/[messageId]/page.tsx
+++ b/src/app/(dashboard)/inbox/[messageId]/page.tsx
@@ -17,6 +17,7 @@ import { usePageLoading } from "@/components/page-loading";
import { PreviousMessage } from "@/components/previous-message";
import { ConversationThread } from "@/components/messages/conversation-thread";
import { ThreadMessageActions } from "@/components/messages/thread-message-actions";
+import { SpamScoreDetails } from "@/components/messages/spam-score-details";
import { useMessageThread } from "@/components/messages/use-message-thread";
import { useLatestMessagesFirst } from "@/components/messages/use-latest-messages-first";
import { getMessageBackHref } from "@/components/message-actions/utils";
@@ -168,6 +169,12 @@ export default function MessageDetailPage() {
{message.subject ?? "(no subject)"}
</h1>
</div>
+ <SpamScoreDetails
+ score={message.spamScore}
+ verdict={message.spamVerdict}
+ signals={message.spamSignals}
+ analysisError={message.spamAnalysisError}
+ />
<ConversationThread
currentMessageId={message.id}
position={latestMessagesFirst ? "after" : "before"}
diff --git a/src/app/(settings)/settings/inbox/page.tsx b/src/app/(settings)/settings/inbox/page.tsx
index 67cb62f..097200f 100644
--- a/src/app/(settings)/settings/inbox/page.tsx
+++ b/src/app/(settings)/settings/inbox/page.tsx
@@ -1,10 +1,20 @@
import { InboxThreadingSettings } from "@/components/settings/inbox-threading-settings";
import { InboxShortcutsSettings } from "@/components/settings/inbox-shortcuts-settings";
import { MailboxAutoReplyForm } from "@/components/settings/mailbox-auto-reply-form";
+import { SpamFilterSettings } from "@/components/settings/spam-filter-settings";
export default function SettingsInboxPage() {
return (
<div className="space-y-8 py-4">
+ <section className="space-y-4">
+ <div>
+ <h2 className="text-xl font-semibold text-neutral-900">Spam protection</h2>
+ <p className="mt-1 text-sm text-neutral-500">Control local spam analysis for incoming messages.</p>
+ </div>
+ <div className="rounded-3xl bg-white p-6">
+ <SpamFilterSettings />
+ </div>
+ </section>
<section className="space-y-4">
<div>
<h2 className="text-xl font-semibold text-neutral-900">Threading</h2>
diff --git a/src/app/api/auth/me/route.ts b/src/app/api/auth/me/route.ts
index 66fd685..6e68ac8 100644
--- a/src/app/api/auth/me/route.ts
+++ b/src/app/api/auth/me/route.ts
@@ -33,6 +33,7 @@ export async function GET(request: Request) {
role: user.role,
canManageMailboxes: user.canManageMailboxes,
keyboardShortcutsEnabled: user.keyboardShortcutsEnabled,
+ spamProtectionEnabled: user.spamProtectionEnabled,
hasAvatar: !!user.avatarKey,
},
hasMailboxes,
diff --git a/src/app/api/messages/bulk/route.ts b/src/app/api/messages/bulk/route.ts
index 5a9bec1..aea7b34 100644
--- a/src/app/api/messages/bulk/route.ts
+++ b/src/app/api/messages/bulk/route.ts
@@ -6,6 +6,7 @@ import { getCurrentUser } from "@/lib/auth/cookies";
import { getEnv } from "@/lib/cloudflare";
import { getMailboxAccessLevel } from "@/lib/mailboxes/access";
import { createAuditLog } from "@/lib/mailboxes/audit";
+import { applySpamFeedback } from "@/lib/spam/feedback";
import type { BulkMessagePayload } from "./types";
import {
getReadValueForBulkAction,
@@ -76,6 +77,17 @@ export async function POST(request: Request) {
if (allowedMessageIds.length === 0) {
return NextResponse.json({ error: "No accessible messages" }, { status: 404 });
}
+ if (payload.action === "spam") {
+ for (const messageId of allowedMessageIds) await applySpamFeedback(env, user, messageId, "spam");
+ return NextResponse.json({ ok: true });
+ }
+ if (payload.action === "inbox") {
+ const spamMessageIds = selectedMessages.filter((message) => message.status === "spam" && allowedMessageIds.includes(message.id)).map((message) => message.id);
+ const normalMessageIds = allowedMessageIds.filter((messageId) => !spamMessageIds.includes(messageId));
+ for (const messageId of spamMessageIds) await applySpamFeedback(env, user, messageId, "ham");
+ if (normalMessageIds.length) await db.update(messages).set(values).where(inArray(messages.id, normalMessageIds));
+ return NextResponse.json({ ok: true });
+ }
await db.update(messages).set(values).where(inArray(messages.id, allowedMessageIds));
await Promise.all(
diff --git a/src/app/api/settings/spam/route.ts b/src/app/api/settings/spam/route.ts
new file mode 100644
index 0000000..2823969
--- /dev/null
+++ b/src/app/api/settings/spam/route.ts
@@ -0,0 +1,30 @@
+import { eq } from "drizzle-orm";
+import { NextResponse } from "next/server";
+import { ZodError } from "zod";
+import { getDb } from "@/db";
+import { users } from "@/db/schema";
+import { requireSessionUser } from "@/lib/api/auth";
+import { getEnv } from "@/lib/cloudflare";
+import type { UpdateSpamSettingsInput } from "./types";
+import { parseUpdateSpamSettingsRequest } from "./utils";
+
+export async function GET(request: Request) {
+ const env = getEnv();
+ const auth = await requireSessionUser(env, request);
+ if (auth.error) return auth.error;
+ return NextResponse.json({ enabled: auth.user.spamProtectionEnabled });
+}
+
+export async function PATCH(request: Request) {
+ const env = getEnv();
+ const auth = await requireSessionUser(env, request);
+ if (auth.error) return auth.error;
+ let input: UpdateSpamSettingsInput;
+ try {
+ input = await parseUpdateSpamSettingsRequest(request);
+ } catch (error) {
+ return NextResponse.json({ error: error instanceof ZodError ? error.flatten() : "Invalid request" }, { status: 400 });
+ }
+ await getDb(env).update(users).set({ spamProtectionEnabled: input.enabled }).where(eq(users.id, auth.user.id));
+ return NextResponse.json({ enabled: input.enabled });
+}
diff --git a/src/app/api/settings/spam/types.d.ts b/src/app/api/settings/spam/types.d.ts
new file mode 100644
index 0000000..b91803d
--- /dev/null
+++ b/src/app/api/settings/spam/types.d.ts
@@ -0,0 +1,4 @@
+import type { updateSpamSettingsSchema } from "@/lib/validators";
+import type { z } from "zod";
+
+export type UpdateSpamSettingsInput = z.infer<typeof updateSpamSettingsSchema>;
diff --git a/src/app/api/settings/spam/utils.ts b/src/app/api/settings/spam/utils.ts
new file mode 100644
index 0000000..72dc212
--- /dev/null
+++ b/src/app/api/settings/spam/utils.ts
@@ -0,0 +1,6 @@
+import { updateSpamSettingsSchema } from "@/lib/validators";
+import type { UpdateSpamSettingsInput } from "./types";
+
+export async function parseUpdateSpamSettingsRequest(request: Request): Promise<UpdateSpamSettingsInput> {
+ return updateSpamSettingsSchema.parse(await request.json());
+}
diff --git a/src/components/message-actions/utils.ts b/src/components/message-actions/utils.ts
index f8d86d5..9515391 100644
--- a/src/components/message-actions/utils.ts
+++ b/src/components/message-actions/utils.ts
@@ -105,8 +105,8 @@ export function getMoveMessageActions(
direction: "inbound" | "outbound",
): MoveMessageActionItem[] {
const actions: MoveMessageActionItem[] = [];
- if (status === "archived" && direction === "inbound") {
- actions.push({ action: "inbox", label: "Inbox", icon: InboxIcon });
+ if ((status === "archived" || status === "spam") && direction === "inbound") {
+ actions.push({ action: "inbox", label: status === "spam" ? "Not spam" : "Inbox", icon: InboxIcon });
}
if (status !== "archived")
actions.push({ action: "archive", label: "Archived", icon: ArchiveIcon });
diff --git a/src/components/messages/spam-score-details-types.d.ts b/src/components/messages/spam-score-details-types.d.ts
new file mode 100644
index 0000000..291f2f0
--- /dev/null
+++ b/src/components/messages/spam-score-details-types.d.ts
@@ -0,0 +1,10 @@
+import type { SpamSignal } from "@/lib/spam/types";
+
+export type SpamScoreDetailsProps = {
+ score?: number | null;
+ verdict?: "inbox" | "suspicious" | "spam" | null;
+ signals?: string | null;
+ analysisError?: string | null;
+};
+
+export type ParsedSpamDetails = { signals: SpamSignal[] };
diff --git a/src/components/messages/spam-score-details-utils.ts b/src/components/messages/spam-score-details-utils.ts
new file mode 100644
index 0000000..f583615
--- /dev/null
+++ b/src/components/messages/spam-score-details-utils.ts
@@ -0,0 +1,13 @@
+import type { SpamSignal } from "@/lib/spam/types";
+
+export function parseSpamSignals(value: string | null | undefined): SpamSignal[] {
+ if (!value) return [];
+ try {
+ const result = JSON.parse(value) as unknown;
+ return Array.isArray(result)
+ ? result.filter((item): item is SpamSignal => !!item && typeof item === "object" && typeof item.id === "string" && typeof item.score === "number" && typeof item.reason === "string")
+ : [];
+ } catch {
+ return [];
+ }
+}
diff --git a/src/components/messages/spam-score-details.tsx b/src/components/messages/spam-score-details.tsx
new file mode 100644
index 0000000..29ba73d
--- /dev/null
+++ b/src/components/messages/spam-score-details.tsx
@@ -0,0 +1,26 @@
+import type { SpamScoreDetailsProps } from "./spam-score-details-types";
+import { parseSpamSignals } from "./spam-score-details-utils";
+
+export function SpamScoreDetails({ score, verdict, signals, analysisError }: SpamScoreDetailsProps) {
+ if (score == null && !analysisError) return null;
+ const parsedSignals = parseSpamSignals(signals);
+ return (
+ <details className="mx-6 mb-2 rounded-xl border border-neutral-200 bg-neutral-50 px-4 py-3">
+ <summary className="cursor-pointer text-sm font-medium text-neutral-800">
+ {score == null ? "Spam analysis unavailable" : `Spam score: ${score} · ${verdict ?? "inbox"}`}
+ </summary>
+ {analysisError ? (
+ <p className="mt-2 text-sm text-neutral-600">The filter could not analyze this message. It was delivered normally.</p>
+ ) : parsedSignals.length > 0 ? (
+ <div className="mt-3 space-y-1.5 text-sm text-neutral-600">
+ <p className="font-medium text-neutral-800">Why Mailflare gave this score</p>
+ {parsedSignals.map((signal) => (
+ <p key={signal.id}><span className={signal.score > 0 ? "text-red-600" : "text-green-700"}>{signal.score > 0 ? "+" : ""}{signal.score}</span>{" "}{signal.reason}</p>
+ ))}
+ </div>
+ ) : (
+ <p className="mt-2 text-sm text-neutral-600">No significant spam signals were found.</p>
+ )}
+ </details>
+ );
+}
diff --git a/src/components/settings/spam-filter-settings.tsx b/src/components/settings/spam-filter-settings.tsx
new file mode 100644
index 0000000..6bae64d
--- /dev/null
+++ b/src/components/settings/spam-filter-settings.tsx
@@ -0,0 +1,57 @@
+"use client";
+
+import { useEffect, useState } from "react";
+import { authFetch } from "@/lib/auth/client";
+import { Switch } from "@/components/ui/switch";
+
+export function SpamFilterSettings() {
+ const [enabled, setEnabled] = useState(true);
+ const [loading, setLoading] = useState(true);
+ const [error, setError] = useState<string | null>(null);
+
+ useEffect(() => {
+ void authFetch("/api/settings/spam")
+ .then(async (response) => {
+ const data = await response.json() as { enabled?: boolean; error?: string };
+ if (!response.ok) throw new Error(data.error ?? "Failed to load spam filter settings");
+ setEnabled(data.enabled !== false);
+ })
+ .catch((nextError) => setError(nextError instanceof Error ? nextError.message : "Failed to load spam filter settings"))
+ .finally(() => setLoading(false));
+ }, []);
+
+ async function updateEnabled(nextEnabled: boolean) {
+ const previous = enabled;
+ setEnabled(nextEnabled);
+ setLoading(true);
+ setError(null);
+ try {
+ const response = await authFetch("/api/settings/spam", {
+ method: "PATCH",
+ headers: { "Content-Type": "application/json" },
+ body: JSON.stringify({ enabled: nextEnabled }),
+ });
+ const data = await response.json() as { enabled?: boolean; error?: string };
+ if (!response.ok) throw new Error(data.error ?? "Failed to update spam filter settings");
+ setEnabled(data.enabled !== false);
+ } catch (nextError) {
+ setEnabled(previous);
+ setError(nextError instanceof Error ? nextError.message : "Failed to update spam filter settings");
+ } finally {
+ setLoading(false);
+ }
+ }
+
+ return (
+ <div>
+ <label className="flex items-start gap-3 rounded-xl bg-neutral-50 p-4">
+ <span className="flex-1">
+ <span className="block text-sm font-medium text-neutral-900">Spam Filter</span>
+ <span className="mt-1 block text-sm text-neutral-500">Analyze incoming messages locally and detect high-confidence spam</span>
+ </span>
+ <Switch checked={enabled} disabled={loading} onCheckedChange={(value) => void updateEnabled(value)} aria-label="Enable spam filter" />
+ </label>
+ {error && <p className="mt-2 px-4 text-sm text-red-600">{error}</p>}
+ </div>
+ );
+}
diff --git a/src/db/schema/index.ts b/src/db/schema/index.ts
index 1cb24a8..83c45d3 100644
--- a/src/db/schema/index.ts
+++ b/src/db/schema/index.ts
@@ -13,6 +13,7 @@ export const users = sqliteTable("users", {
disabled: integer("disabled", { mode: "boolean" }).notNull().default(false),
canManageMailboxes: integer("can_manage_mailboxes", { mode: "boolean" }).notNull().default(false),
keyboardShortcutsEnabled: integer("keyboard_shortcuts_enabled", { mode: "boolean" }).notNull().default(true),
+ spamProtectionEnabled: integer("spam_protection_enabled", { mode: "boolean" }).notNull().default(true),
createdByUserId: text("created_by_user_id").references((): AnySQLiteColumn => users.id, { onDelete: "set null" }),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
@@ -227,6 +228,11 @@ export const messages = sqliteTable(
// and so outgoing replies can carry them on to the recipient's client.
inReplyTo: text("in_reply_to"),
references: text("references_header"),
+ spamScore: integer("spam_score"),
+ spamVerdict: text("spam_verdict", { enum: ["inbox", "suspicious", "spam"] }),
+ spamSignals: text("spam_signals"),
+ spamAnalyzedAt: integer("spam_analyzed_at", { mode: "timestamp" }),
+ spamAnalysisError: text("spam_analysis_error"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
@@ -237,9 +243,59 @@ export const messages = sqliteTable(
index("messages_folder_idx").on(t.folderId),
index("messages_thread_idx").on(t.mailboxId, t.threadId),
index("messages_provider_message_idx").on(t.mailboxId, t.providerMessageId),
+ index("messages_raw_r2_key_idx").on(t.rawR2Key),
],
);
+export const spamTokenStats = sqliteTable(
+ "spam_token_stats",
+ {
+ mailboxId: text("mailbox_id").notNull().references(() => mailboxes.id, { onDelete: "cascade" }),
+ token: text("token").notNull(),
+ spamCount: integer("spam_count").notNull().default(0),
+ hamCount: integer("ham_count").notNull().default(0),
+ updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().$defaultFn(() => new Date()),
+ },
+ (t) => [
+ uniqueIndex("spam_token_stats_mailbox_token_idx").on(t.mailboxId, t.token),
+ index("spam_token_stats_mailbox_idx").on(t.mailboxId),
+ ],
+);
+
+export const spamReputation = sqliteTable(
+ "spam_reputation",
+ {
+ mailboxId: text("mailbox_id").notNull().references(() => mailboxes.id, { onDelete: "cascade" }),
+ type: text("type", { enum: ["email", "domain", "fingerprint"] }).notNull(),
+ key: text("key").notNull(),
+ messagesSeen: integer("messages_seen").notNull().default(0),
+ spamCount: integer("spam_count").notNull().default(0),
+ hamCount: integer("ham_count").notNull().default(0),
+ firstSeenAt: integer("first_seen_at", { mode: "timestamp" }).notNull().$defaultFn(() => new Date()),
+ lastSeenAt: integer("last_seen_at", { mode: "timestamp" }).notNull().$defaultFn(() => new Date()),
+ },
+ (t) => [
+ uniqueIndex("spam_reputation_mailbox_type_key_idx").on(t.mailboxId, t.type, t.key),
+ index("spam_reputation_mailbox_idx").on(t.mailboxId),
+ ],
+);
+
+export const spamFeedback = sqliteTable(
+ "spam_feedback",
+ {
+ messageId: text("message_id").primaryKey().references(() => messages.id, { onDelete: "cascade" }),
+ mailboxId: text("mailbox_id").notNull().references(() => mailboxes.id, { onDelete: "cascade" }),
+ actorUserId: text("actor_user_id").references(() => users.id, { onDelete: "set null" }),
+ classification: text("classification", { enum: ["spam", "ham"] }).notNull(),
+ trainingTokens: text("training_tokens").notNull(),
+ reputationKeys: text("reputation_keys").notNull(),
+ tokenizerVersion: integer("tokenizer_version").notNull().default(1),
+ createdAt: integer("created_at", { mode: "timestamp" }).notNull().$defaultFn(() => new Date()),
+ updatedAt: integer("updated_at", { mode: "timestamp" }).notNull().$defaultFn(() => new Date()),
+ },
+ (t) => [index("spam_feedback_mailbox_idx").on(t.mailboxId)],
+);
+
export const messageAttachments = sqliteTable(
"message_attachments",
{
@@ -512,6 +568,9 @@ export const schema = {
folders,
apiKeys,
messages,
+ spamTokenStats,
+ spamReputation,
+ spamFeedback,
messageAttachments,
outboundJobs,
emailTemplates,
diff --git a/src/hooks/types.d.ts b/src/hooks/types.d.ts
index bed1531..854f5af 100644
--- a/src/hooks/types.d.ts
+++ b/src/hooks/types.d.ts
@@ -30,6 +30,11 @@ export type Message = {
threadId: string | null;
inReplyTo?: string | null;
references?: string | null;
+ spamScore?: number | null;
+ spamVerdict?: "inbox" | "suspicious" | "spam" | null;
+ spamSignals?: string | null;
+ spamAnalyzedAt?: string | null;
+ spamAnalysisError?: string | null;
/** Messages in the same conversation (excluding drafts and trash), when the list API computed it. */
threadCount?: number;
/** Unread messages in the conversation; zero means the grouped thread is read. */
diff --git a/src/lib/auth/types.d.ts b/src/lib/auth/types.d.ts
index 08842e7..98ddbd3 100644
--- a/src/lib/auth/types.d.ts
+++ b/src/lib/auth/types.d.ts
@@ -11,6 +11,7 @@ export type SessionUser = {
disabled: boolean;
canManageMailboxes: boolean;
keyboardShortcutsEnabled: boolean;
+ spamProtectionEnabled: boolean;
createdByUserId: string | null;
createdAt: Date;
};
diff --git a/src/lib/backups/export.ts b/src/lib/backups/export.ts
index d37c02d..e97ebff 100644
--- a/src/lib/backups/export.ts
+++ b/src/lib/backups/export.ts
@@ -1,7 +1,7 @@
import type { DatabaseBackupDocument, DatabaseBackupTable, DatabaseRecord } from "./types";
import { mergeLegacyMessageBodies } from "./utils";
-const BACKUP_TABLES: DatabaseBackupTable[] = ["users", "domains", "mailboxes", "mailbox_access", "contacts", "folders", "api_keys", "messages", "message_attachments", "outbound_jobs", "routing_rules", "webhooks", "webhook_deliveries", "sessions", "audit_logs", "backup_settings", "backups", "app_settings", "license_settings", "email_templates", "calendar_events", "auto_reply_deliveries"];
+const BACKUP_TABLES: DatabaseBackupTable[] = ["users", "domains", "mailboxes", "mailbox_access", "contacts", "folders", "api_keys", "messages", "message_attachments", "outbound_jobs", "routing_rules", "webhooks", "webhook_deliveries", "sessions", "audit_logs", "backup_settings", "backups", "app_settings", "license_settings", "email_templates", "calendar_events", "auto_reply_deliveries", "spam_token_stats", "spam_reputation", "spam_feedback"];
/**
* Tables every backup document must contain. Tables added to BACKUP_TABLES
* after the format shipped are absent from older documents, so they stay
diff --git a/src/lib/backups/types.d.ts b/src/lib/backups/types.d.ts
index 775a1ba..ac6d5bf 100644
--- a/src/lib/backups/types.d.ts
+++ b/src/lib/backups/types.d.ts
@@ -1,5 +1,5 @@
export type BackupScheduleType = "daily" | "weekly" | "monthly";
-export type DatabaseBackupTable = "users" | "domains" | "mailboxes" | "mailbox_access" | "contacts" | "folders" | "api_keys" | "messages" | "message_attachments" | "outbound_jobs" | "routing_rules" | "webhooks" | "webhook_deliveries" | "sessions" | "audit_logs" | "backup_settings" | "backups" | "app_settings" | "license_settings" | "email_templates" | "calendar_events" | "auto_reply_deliveries";
+export type DatabaseBackupTable = "users" | "domains" | "mailboxes" | "mailbox_access" | "contacts" | "folders" | "api_keys" | "messages" | "message_attachments" | "outbound_jobs" | "routing_rules" | "webhooks" | "webhook_deliveries" | "sessions" | "audit_logs" | "backup_settings" | "backups" | "app_settings" | "license_settings" | "email_templates" | "calendar_events" | "auto_reply_deliveries" | "spam_token_stats" | "spam_reputation" | "spam_feedback";
export type DatabaseRecord = Record<string, string | number | null>;
export type DatabaseBackupDocument = { format: "mailflare-database-backup"; version: 1; createdAt: string; tables: Record<DatabaseBackupTable, DatabaseRecord[]>; };
diff --git a/src/lib/contacts/service.ts b/src/lib/contacts/service.ts
index e73c795..f4aaa4a 100644
--- a/src/lib/contacts/service.ts
+++ b/src/lib/contacts/service.ts
@@ -21,7 +21,9 @@ export async function upsertContactFromAddress(env: CloudflareEnv, input: Contac
if (existing) {
const nextDisplayName = getNextDisplayName(existing.displayName, existing.source, displayName);
- const nextSource = existing.source === "manual" ? "manual" : input.source;
+ const nextSource = existing.source === "manual" || (existing.source === "outbound" && input.source === "inbound")
+ ? existing.source
+ : input.source;
await db
.update(contacts)
diff --git a/src/lib/email/inbound.ts b/src/lib/email/inbound.ts
index 33435af..6cdb964 100644
--- a/src/lib/email/inbound.ts
+++ b/src/lib/email/inbound.ts
@@ -1,6 +1,6 @@
-import { eq } from "drizzle-orm";
+import { and, eq } from "drizzle-orm";
import { getDb } from "@/db";
-import { messages } from "@/db/schema";
+import { messages, users } from "@/db/schema";
import { newId } from "@/lib/ids";
import { buildSnippet, parseRawMime } from "@/lib/email/parse";
import { resolveInboundAddress, resolveInboxRuleDestination } from "@/lib/email/routing";
@@ -13,6 +13,9 @@ import { listMessageAttachments, storeMessageAttachments } from "@/lib/email/att
import { getUnsubscribeUrlFromRawR2Key } from "@/lib/email/unsubscribe";
import { resolveThreadId } from "@/lib/email/threading";
import type { SessionUser } from "@/lib/auth/types";
+import { analyzeSpam } from "@/lib/spam/engine";
+import { getReputationKeys } from "@/lib/spam/analyzers/reputation";
+import { recordReputationObservation } from "@/lib/spam/repository";
import {
getMailboxNotificationUserIds,
notifyUsersOfNewMessage,
@@ -52,6 +55,11 @@ export async function processInboundMessage(
}
if (!decision.mailbox) return;
+ const [stored] = await db.select({ id: messages.id }).from(messages).where(and(
+ eq(messages.mailboxId, decision.mailbox.mailboxId),
+ eq(messages.rawR2Key, payload.rawR2Key),
+ )).limit(1);
+ if (stored) return;
const raw = await env.BUCKET.get(payload.rawR2Key);
if (!raw) {
@@ -75,6 +83,35 @@ export async function processInboundMessage(
subject: parsed.subject,
content: [parsed.text, parsed.html, snippet].filter(Boolean).join(" "),
});
+ let spamAnalysis: Awaited<ReturnType<typeof analyzeSpam>> | null = null;
+ let spamAnalysisError: string | null = null;
+ const [owner] = await db.select({ enabled: users.spamProtectionEnabled }).from(users).where(eq(users.id, decision.mailbox.userId)).limit(1);
+ if (owner?.enabled !== false) {
+ try {
+ spamAnalysis = await analyzeSpam(db, {
+ mailboxId: decision.mailbox.mailboxId,
+ userId: decision.mailbox.userId,
+ envelopeFrom: payload.from,
+ headers: payload.headers,
+ message: parsed,
+ });
+ } catch (error) {
+ spamAnalysisError = error instanceof Error ? error.message.slice(0, 300) : "Spam analysis failed";
+ console.error(`Spam analysis failed for ${messageId}`, error);
+ }
+ }
+ if (destination.status === "spam" && spamAnalysis) {
+ spamAnalysis = {
+ score: 100,
+ verdict: "spam",
+ signals: [{ id: "mailbox_rule_spam", score: 100, reason: "A mailbox rule marked this message as spam" }],
+ fingerprint: spamAnalysis?.fingerprint ?? "",
+ };
+ }
+ const status = destination.status === "received" && spamAnalysis?.verdict === "spam"
+ ? "spam"
+ : destination.status;
+ const folderId = status === "spam" ? null : destination.folderId;
const contact = await upsertContactFromAddress(env, {
userId: decision.mailbox.userId,
address: fromAddr,
@@ -92,7 +129,7 @@ export async function processInboundMessage(
id: messageId,
userId: decision.mailbox.userId,
mailboxId: decision.mailbox.mailboxId,
- folderId: destination.folderId,
+ folderId,
direction: "inbound",
providerMessageId: parsed.messageId,
fromAddr,
@@ -103,19 +140,37 @@ export async function processInboundMessage(
textBody: parsed.text,
htmlBody: parsed.html,
rawR2Key: payload.rawR2Key,
- status: destination.status,
+ status,
threadId,
inReplyTo: parsed.inReplyTo,
references: parsed.references.length ? parsed.references.join(" ") : null,
+ spamScore: spamAnalysis?.score ?? null,
+ spamVerdict: spamAnalysis?.verdict ?? null,
+ spamSignals: spamAnalysis ? JSON.stringify(spamAnalysis.signals) : null,
+ spamAnalyzedAt: spamAnalysis ? new Date() : null,
+ spamAnalysisError,
});
await storeMessageAttachments(env, messageId, parsed.attachments, { validate: false });
+ if (spamAnalysis) {
+ try {
+ await recordReputationObservation(env, decision.mailbox.mailboxId, getReputationKeys(parsed, spamAnalysis.fingerprint));
+ } catch (error) {
+ console.error(`Spam reputation observation failed for ${messageId}`, error);
+ }
+ console.info(JSON.stringify({
+ messageId,
+ spamScore: spamAnalysis.score,
+ verdict: spamAnalysis.verdict,
+ signals: spamAnalysis.signals.map((signal) => signal.id),
+ }));
+ }
} catch (error) {
await db.delete(messages).where(eq(messages.id, messageId));
throw error;
}
- if (destination.status === "received") {
+ if (status === "received") {
try {
await sendMailboxAutoReply(env, {
mailboxId: decision.mailbox.mailboxId,
@@ -130,19 +185,21 @@ export async function processInboundMessage(
}
}
- const notificationUserIds = await getMailboxNotificationUserIds(
- env,
- decision.mailbox.mailboxId,
- decision.mailbox.userId,
- );
- await notifyUsersOfNewMessage(env, notificationUserIds, {
- type: "new_message",
- messageId,
- mailboxId: decision.mailbox.mailboxId,
- from: fromAddr,
- fromName: contact?.displayName ?? null,
- subject: parsed.subject,
- });
+ if (status !== "spam") {
+ const notificationUserIds = await getMailboxNotificationUserIds(
+ env,
+ decision.mailbox.mailboxId,
+ decision.mailbox.userId,
+ );
+ await notifyUsersOfNewMessage(env, notificationUserIds, {
+ type: "new_message",
+ messageId,
+ mailboxId: decision.mailbox.mailboxId,
+ from: fromAddr,
+ fromName: contact?.displayName ?? null,
+ subject: parsed.subject,
+ });
+ }
await dispatchWebhooks(env, decision.mailbox.userId, "message.inbound", {
messageId,
from: fromAddr,
@@ -150,6 +207,8 @@ export async function processInboundMessage(
cc: parsed.ccAddr ?? undefined,
subject: parsed.subject,
threadId,
+ spamScore: spamAnalysis?.score,
+ spamVerdict: spamAnalysis?.verdict,
});
}
diff --git a/src/lib/setup/migration.ts b/src/lib/setup/migration.ts
index c1e8f3a..d91ec72 100644
--- a/src/lib/setup/migration.ts
+++ b/src/lib/setup/migration.ts
@@ -28,10 +28,11 @@ const MIGRATION_NAMES = [
"0026_add_contact_avatars.sql",
"0027_add_domain_sending_intent.sql",
"0028_add_keyboard_shortcuts_setting.sql",
+ "0029_add_spam_protection.sql",
];
const INITIAL_SCHEMA_SQL = `
-CREATE TABLE IF NOT EXISTS users (id text PRIMARY KEY NOT NULL, email text NOT NULL UNIQUE, reset_email text, forwarding_email text, password_hash text NOT NULL, name text NOT NULL, avatar_key text, role text DEFAULT 'user' NOT NULL, disabled integer DEFAULT false NOT NULL, can_manage_mailboxes integer DEFAULT false NOT NULL, keyboard_shortcuts_enabled integer DEFAULT true NOT NULL, created_by_user_id text REFERENCES users(id) ON DELETE set null, created_at integer NOT NULL);
+CREATE TABLE IF NOT EXISTS users (id text PRIMARY KEY NOT NULL, email text NOT NULL UNIQUE, reset_email text, forwarding_email text, password_hash text NOT NULL, name text NOT NULL, avatar_key text, role text DEFAULT 'user' NOT NULL, disabled integer DEFAULT false NOT NULL, can_manage_mailboxes integer DEFAULT false NOT NULL, keyboard_shortcuts_enabled integer DEFAULT true NOT NULL, spam_protection_enabled integer DEFAULT true NOT NULL, created_by_user_id text REFERENCES users(id) ON DELETE set null, created_at integer NOT NULL);
CREATE INDEX IF NOT EXISTS users_created_by_idx ON users(created_by_user_id);
CREATE TABLE IF NOT EXISTS domains (id text PRIMARY KEY NOT NULL, user_id text NOT NULL REFERENCES users(id) ON DELETE cascade, hostname text NOT NULL, zone_id text NOT NULL, status text DEFAULT 'pending' NOT NULL, routing_status text, sending_subdomain_tag text, sending_requested integer DEFAULT false NOT NULL, sending_enabled integer DEFAULT false NOT NULL, routing_enabled integer DEFAULT false NOT NULL, created_at integer NOT NULL);
CREATE UNIQUE INDEX IF NOT EXISTS domains_hostname_idx ON domains(hostname);
@@ -56,12 +57,21 @@ CREATE UNIQUE INDEX IF NOT EXISTS folders_mailbox_name_idx ON folders(mailbox_id
CREATE INDEX IF NOT EXISTS folders_user_idx ON folders(user_id);
CREATE INDEX IF NOT EXISTS folders_mailbox_idx ON folders(mailbox_id);
CREATE TABLE IF NOT EXISTS api_keys (id text PRIMARY KEY NOT NULL, user_id text NOT NULL REFERENCES users(id) ON DELETE cascade, name text NOT NULL, prefix text NOT NULL, key_hash text NOT NULL, scopes text NOT NULL, created_at integer NOT NULL, last_used_at integer);
-CREATE TABLE IF NOT EXISTS messages (id text PRIMARY KEY NOT NULL, user_id text NOT NULL REFERENCES users(id) ON DELETE cascade, mailbox_id text REFERENCES mailboxes(id) ON DELETE set null, direction text NOT NULL, provider_message_id text, folder_id text REFERENCES folders(id) ON DELETE set null, from_addr text NOT NULL, to_addr text NOT NULL, cc_addr text, bcc_addr text, subject text, snippet text, text_body text, html_body text, raw_r2_key text, status text DEFAULT 'received' NOT NULL, read integer DEFAULT false NOT NULL, starred integer DEFAULT false NOT NULL, snoozed_until integer, thread_id text, in_reply_to text, references_header text, created_at integer NOT NULL);
+CREATE TABLE IF NOT EXISTS messages (id text PRIMARY KEY NOT NULL, user_id text NOT NULL REFERENCES users(id) ON DELETE cascade, mailbox_id text REFERENCES mailboxes(id) ON DELETE set null, direction text NOT NULL, provider_message_id text, folder_id text REFERENCES folders(id) ON DELETE set null, from_addr text NOT NULL, to_addr text NOT NULL, cc_addr text, bcc_addr text, subject text, snippet text, text_body text, html_body text, raw_r2_key text, status text DEFAULT 'received' NOT NULL, read integer DEFAULT false NOT NULL, starred integer DEFAULT false NOT NULL, snoozed_until integer, thread_id text, in_reply_to text, references_header text, spam_score integer, spam_verdict text, spam_signals text, spam_analyzed_at integer, spam_analysis_error text, created_at integer NOT NULL);
CREATE INDEX IF NOT EXISTS messages_user_created_idx ON messages(user_id, created_at);
CREATE INDEX IF NOT EXISTS messages_mailbox_idx ON messages(mailbox_id);
CREATE INDEX IF NOT EXISTS messages_folder_idx ON messages(folder_id);
CREATE INDEX IF NOT EXISTS messages_thread_idx ON messages(mailbox_id, thread_id);
CREATE INDEX IF NOT EXISTS messages_provider_message_idx ON messages(mailbox_id, provider_message_id);
+CREATE INDEX IF NOT EXISTS messages_raw_r2_key_idx ON messages(raw_r2_key);
+CREATE TABLE IF NOT EXISTS spam_token_stats (mailbox_id text NOT NULL REFERENCES mailboxes(id) ON DELETE cascade, token text NOT NULL, spam_count integer DEFAULT 0 NOT NULL, ham_count integer DEFAULT 0 NOT NULL, updated_at integer NOT NULL);
+CREATE UNIQUE INDEX IF NOT EXISTS spam_token_stats_mailbox_token_idx ON spam_token_stats(mailbox_id, token);
+CREATE INDEX IF NOT EXISTS spam_token_stats_mailbox_idx ON spam_token_stats(mailbox_id);
+CREATE TABLE IF NOT EXISTS spam_reputation (mailbox_id text NOT NULL REFERENCES mailboxes(id) ON DELETE cascade, type text NOT NULL, key text NOT NULL, messages_seen integer DEFAULT 0 NOT NULL, spam_count integer DEFAULT 0 NOT NULL, ham_count integer DEFAULT 0 NOT NULL, first_seen_at integer NOT NULL, last_seen_at integer NOT NULL);
+CREATE UNIQUE INDEX IF NOT EXISTS spam_reputation_mailbox_type_key_idx ON spam_reputation(mailbox_id, type, key);
+CREATE INDEX IF NOT EXISTS spam_reputation_mailbox_idx ON spam_reputation(mailbox_id);
+CREATE TABLE IF NOT EXISTS spam_feedback (message_id text PRIMARY KEY NOT NULL REFERENCES messages(id) ON DELETE cascade, mailbox_id text NOT NULL REFERENCES mailboxes(id) ON DELETE cascade, actor_user_id text REFERENCES users(id) ON DELETE set null, classification text NOT NULL, training_tokens text NOT NULL, reputation_keys text NOT NULL, tokenizer_version integer DEFAULT 1 NOT NULL, created_at integer NOT NULL, updated_at integer NOT NULL);
+CREATE INDEX IF NOT EXISTS spam_feedback_mailbox_idx ON spam_feedback(mailbox_id);
CREATE TABLE IF NOT EXISTS message_attachments (id text PRIMARY KEY NOT NULL, message_id text NOT NULL REFERENCES messages(id) ON DELETE cascade, filename text NOT NULL, content_type text NOT NULL, size integer NOT NULL, disposition text DEFAULT 'attachment' NOT NULL, content_id text, r2_key text NOT NULL UNIQUE, created_at integer NOT NULL);
CREATE INDEX IF NOT EXISTS message_attachments_message_idx ON message_attachments(message_id);
CREATE TABLE IF NOT EXISTS outbound_jobs (id text PRIMARY KEY NOT NULL, user_id text NOT NULL REFERENCES users(id) ON DELETE cascade, message_id text REFERENCES messages(id) ON DELETE set null, status text DEFAULT 'queued' NOT NULL, payload text NOT NULL, error text, scheduled_at integer, created_at integer NOT NULL, updated_at integer NOT NULL);
diff --git a/src/lib/spam/analyzers/authentication.ts b/src/lib/spam/analyzers/authentication.ts
new file mode 100644
index 0000000..f95050a
--- /dev/null
+++ b/src/lib/spam/analyzers/authentication.ts
@@ -0,0 +1,17 @@
+import type { SpamSignal } from "../types";
+import { SPAM_WEIGHTS } from "../weights";
+import { findHeader } from "./utils";
+
+export function analyzeAuthentication(headers?: Record<string, string>): SpamSignal[] {
+ const value = findHeader(headers, "authentication-results");
+ if (!value) return [];
+ const authservId = value.split(";", 1)[0]?.trim().toLowerCase() ?? "";
+ if (!/(^|\.)cloudflare\.(com|net)$/.test(authservId)) return [];
+ const signals: SpamSignal[] = [];
+ for (const method of ["dmarc", "dkim", "spf"] as const) {
+ const result = value.match(new RegExp(`(?:^|[;\\s])${method}=([a-z]+)`, "i"))?.[1]?.toLowerCase();
+ if (result === "pass") signals.push({ id: `${method}_passed`, score: SPAM_WEIGHTS.authentication[`${method}Pass`], reason: `${method.toUpperCase()} authentication passed` });
+ if (result === "fail") signals.push({ id: `${method}_failed`, score: SPAM_WEIGHTS.authentication[`${method}Fail`], reason: `${method.toUpperCase()} authentication failed` });
+ }
+ return signals;
+}
diff --git a/src/lib/spam/analyzers/reputation.ts b/src/lib/spam/analyzers/reputation.ts
new file mode 100644
index 0000000..85a96f2
--- /dev/null
+++ b/src/lib/spam/analyzers/reputation.ts
@@ -0,0 +1,13 @@
+import { getEmailAddress } from "@/lib/email/address";
+import type { ParsedEmail } from "@/lib/email/parse";
+import { getDomain } from "../tokenizer";
+
+export function getReputationKeys(message: ParsedEmail, fingerprint: string) {
+ const email = getEmailAddress(message.fromAddr ?? "").toLowerCase();
+ const domain = getDomain(email);
+ return [
+ ...(email ? [{ type: "email" as const, key: email }] : []),
+ ...(domain ? [{ type: "domain" as const, key: domain }] : []),
+ { type: "fingerprint" as const, key: fingerprint },
+ ];
+}
diff --git a/src/lib/spam/analyzers/structure.ts b/src/lib/spam/analyzers/structure.ts
new file mode 100644
index 0000000..82aac98
--- /dev/null
+++ b/src/lib/spam/analyzers/structure.ts
@@ -0,0 +1,28 @@
+import type { ParsedEmail } from "@/lib/email/parse";
+import type { PreparedSpamContent, SpamSignal } from "../types";
+import { getDomain } from "../tokenizer";
+import { findHeader } from "./utils";
+
+export function analyzeStructure(message: ParsedEmail, headers: Record<string, string> | undefined, prepared: PreparedSpamContent): SpamSignal[] {
+ const signals: SpamSignal[] = [];
+ const visible = prepared.visibleText;
+ const fromDomain = getDomain(message.fromAddr);
+ const replyDomain = getDomain(findHeader(headers, "reply-to"));
+ const upperLetters = (visible.match(/[A-Z]/g) ?? []).length;
+ const letters = (visible.match(/[A-Za-z]/g) ?? []).length;
+ if (!message.subject?.trim()) signals.push({ id: "empty_subject", score: 3, reason: "Message has no subject" });
+ if (message.html && !message.text?.trim() && visible.trim().length < 80) signals.push({ id: "thin_html_only", score: 6, reason: "HTML-only message has very little visible text" });
+ if (/[\u200B-\u200D\u2060\uFEFF]/u.test(visible)) signals.push({ id: "zero_width_text", score: 7, reason: "Message contains hidden zero-width characters" });
+ if (letters > 40 && upperLetters / letters > 0.65) signals.push({ id: "excessive_capitals", score: 4, reason: "Message uses excessive capitalization" });
+ if (replyDomain && fromDomain && replyDomain !== fromDomain) signals.push({ id: "reply_to_mismatch", score: 5, reason: "Reply-To domain differs from the sender domain" });
+ const credentialLure = /\b(verify|confirm|unlock|restore|validate|sign[ -]?in|log[ -]?in)\b.{0,60}\b(account|password|mailbox|identity|access)\b/i.test(visible)
+ || /\b(account|password|mailbox|identity|access)\b.{0,60}\b(verify|confirm|unlock|restore|validate|sign[ -]?in|log[ -]?in)\b/i.test(visible);
+ const urgent = /\b(urgent|immediately|within 24 hours|suspended|expires? today|final warning)\b/i.test(visible);
+ if (credentialLure && urgent && prepared.urlDomains.length > 0) signals.push({ id: "credential_phishing_pattern", score: 24, reason: "Message combines an urgent account request with an external link" });
+ const paymentLure = /\b(gift card|wire transfer|cryptocurrency|bitcoin|wallet|bank transfer)\b/i.test(visible);
+ const paymentRequest = /\b(pay|payment|send|transfer|purchase|deposit)\b/i.test(visible);
+ if (paymentLure && paymentRequest && (urgent || (!!replyDomain && !!fromDomain && replyDomain !== fromDomain))) signals.push({ id: "payment_scam_pattern", score: 16, reason: "Message contains a high-risk urgent payment pattern" });
+ const risky = message.attachments.filter((attachment) => /\.(exe|scr|js|vbs|bat|cmd|iso|img)$/i.test(attachment.filename));
+ if (risky.length) signals.push({ id: "risky_attachment", score: 10, reason: "Message contains a potentially risky attachment", metadata: { count: risky.length } });
+ return signals;
+}
diff --git a/src/lib/spam/analyzers/urls.ts b/src/lib/spam/analyzers/urls.ts
new file mode 100644
index 0000000..c658c7d
--- /dev/null
+++ b/src/lib/spam/analyzers/urls.ts
@@ -0,0 +1,21 @@
+import type { ParsedEmail } from "@/lib/email/parse";
+import type { PreparedSpamContent, SpamSignal } from "../types";
+
+export function analyzeUrls(message: ParsedEmail, prepared: PreparedSpamContent): SpamSignal[] {
+ const domains = prepared.urlDomains;
+ const signals: SpamSignal[] = [];
+ if (domains.some((domain) => /^\d{1,3}(?:\.\d{1,3}){3}$/.test(domain))) signals.push({ id: "ip_address_url", score: 8, reason: "A link uses an IP address instead of a domain" });
+ if (domains.some((domain) => domain.includes("xn--"))) signals.push({ id: "punycode_url", score: 5, reason: "A link uses an internationalized domain encoding" });
+ if (domains.length > 10) signals.push({ id: "many_link_domains", score: Math.min(8, domains.length - 8), reason: "Message links to an unusually large number of domains", metadata: { count: domains.length } });
+ for (const match of (message.html ?? "").slice(0, 250_000).matchAll(/<a\b[^>]*\bhref=["'](https?:\/\/[^"']+)["'][^>]*>\s*(https?:\/\/[^<\s]+)\s*<\/a>/gi)) {
+ try {
+ const target = new URL(match[1]).hostname.toLowerCase().replace(/^www\./, "");
+ const displayed = new URL(match[2]).hostname.toLowerCase().replace(/^www\./, "");
+ if (target !== displayed) {
+ signals.push({ id: "displayed_link_mismatch", score: 10, reason: "A link's displayed address differs from its destination" });
+ break;
+ }
+ } catch {}
+ }
+ return signals;
+}
diff --git a/src/lib/spam/analyzers/utils.ts b/src/lib/spam/analyzers/utils.ts
new file mode 100644
index 0000000..5e8db8e
--- /dev/null
+++ b/src/lib/spam/analyzers/utils.ts
@@ -0,0 +1,4 @@
+export function findHeader(headers: Record<string, string> | undefined, name: string): string | undefined {
+ const key = Object.keys(headers ?? {}).find((item) => item.toLowerCase() === name);
+ return key ? headers?.[key] : undefined;
+}
diff --git a/src/lib/spam/classifier.ts b/src/lib/spam/classifier.ts
new file mode 100644
index 0000000..4ca6f3f
--- /dev/null
+++ b/src/lib/spam/classifier.ts
@@ -0,0 +1,49 @@
+import { and, count, eq, inArray } from "drizzle-orm";
+import type { AppDatabase } from "@/db";
+import { spamFeedback, spamTokenStats } from "@/db/schema";
+import type { BayesianResult } from "./types";
+import { MAX_BAYESIAN_TOKENS, SPAM_WEIGHTS } from "./weights";
+
+export async function classifyTokens(db: AppDatabase, mailboxId: string, tokens: string[]): Promise<BayesianResult> {
+ const [spamTotalRow, hamTotalRow] = await Promise.all([
+ db.select({ value: count() }).from(spamFeedback).where(and(eq(spamFeedback.mailboxId, mailboxId), eq(spamFeedback.classification, "spam"))),
+ db.select({ value: count() }).from(spamFeedback).where(and(eq(spamFeedback.mailboxId, mailboxId), eq(spamFeedback.classification, "ham"))),
+ ]);
+ const spamTotal = spamTotalRow[0]?.value ?? 0;
+ const hamTotal = hamTotalRow[0]?.value ?? 0;
+ const trainedMessages = spamTotal + hamTotal;
+ if (spamTotal === 0 || hamTotal === 0 || tokens.length === 0) {
+ return { probability: 0.5, contribution: 0, trainedMessages, strongestTokens: [] };
+ }
+
+ const rows: Array<typeof spamTokenStats.$inferSelect> = [];
+ for (let index = 0; index < tokens.length; index += 90) {
+ rows.push(...await db.select().from(spamTokenStats).where(and(
+ eq(spamTokenStats.mailboxId, mailboxId),
+ inArray(spamTokenStats.token, tokens.slice(index, index + 90)),
+ )));
+ }
+ const strongestTokens = rows.map((row) => {
+ const spamRate = (row.spamCount + 1) / (spamTotal + 2);
+ const hamRate = (row.hamCount + 1) / (hamTotal + 2);
+ const probability = Math.max(0.1, Math.min(0.9, spamRate / (spamRate + hamRate)));
+ return { token: row.token, probability };
+ }).sort((a, b) => Math.abs(b.probability - 0.5) - Math.abs(a.probability - 0.5)).slice(0, MAX_BAYESIAN_TOKENS);
+ if (strongestTokens.length === 0) return { probability: 0.5, contribution: 0, trainedMessages, strongestTokens };
+
+ let spamProduct = 1;
+ let hamProduct = 1;
+ for (const token of strongestTokens) {
+ spamProduct *= token.probability;
+ hamProduct *= 1 - token.probability;
+ }
+ const probability = spamProduct / (spamProduct + hamProduct);
+ const fullContribution = (probability - 0.5) * SPAM_WEIGHTS.content.maximum * 2;
+ const reliability = Math.min(1, Math.min(spamTotal, hamTotal) / 20);
+ return {
+ probability,
+ contribution: Math.round(fullContribution * reliability),
+ trainedMessages,
+ strongestTokens,
+ };
+}
diff --git a/src/lib/spam/engine.ts b/src/lib/spam/engine.ts
new file mode 100644
index 0000000..f889d58
--- /dev/null
+++ b/src/lib/spam/engine.ts
@@ -0,0 +1,74 @@
+import { and, eq } from "drizzle-orm";
+import type { AppDatabase } from "@/db";
+import { contacts, spamReputation } from "@/db/schema";
+import { getEmailAddress } from "@/lib/email/address";
+import { analyzeAuthentication } from "./analyzers/authentication";
+import { getReputationKeys } from "./analyzers/reputation";
+import { analyzeStructure } from "./analyzers/structure";
+import { analyzeUrls } from "./analyzers/urls";
+import { classifyTokens } from "./classifier";
+import { buildFingerprint, getDomain, prepareSpamContent, tokenizeMessage } from "./tokenizer";
+import type { SpamAnalysisInput, SpamAnalysisResult, SpamSignal } from "./types";
+import { SPAM_THRESHOLDS, SPAM_WEIGHTS } from "./weights";
+import { LocalSpamIntelligenceProvider } from "./providers/local";
+
+export async function analyzeSpam(db: AppDatabase, input: SpamAnalysisInput): Promise<SpamAnalysisResult> {
+ const prepared = prepareSpamContent(input.message);
+ const fingerprint = buildFingerprint(input.message, prepared);
+ const sender = getEmailAddress(input.message.fromAddr ?? input.envelopeFrom).toLowerCase();
+ const signals: SpamSignal[] = [
+ ...analyzeAuthentication(input.headers),
+ ...analyzeUrls(input.message, prepared),
+ ...analyzeStructure(input.message, input.headers, prepared),
+ ];
+ const intelligenceProvider = input.intelligenceProvider ?? new LocalSpamIntelligenceProvider();
+ signals.push(...await intelligenceProvider.check({
+ senderDomain: getDomain(sender),
+ urlDomains: prepared.urlDomains,
+ fingerprint,
+ }));
+
+ const [contact] = sender ? await db.select().from(contacts).where(and(eq(contacts.userId, input.userId), eq(contacts.email, sender))).limit(1) : [];
+ if (contact?.blocked) {
+ return { score: 100, verdict: "spam", signals: [{ id: "blocked_sender", score: 100, reason: "Sender is blocked" }], fingerprint };
+ }
+ if (contact?.source === "manual") signals.push({ id: "manual_contact", score: SPAM_WEIGHTS.relationships.manuallySavedContact, reason: "Sender is in your manually saved contacts" });
+ if (contact?.source === "outbound") signals.push({ id: "previously_sent", score: SPAM_WEIGHTS.relationships.previouslySentTo, reason: "You have previously sent email to this address" });
+
+ const identities = getReputationKeys(input.message, fingerprint);
+ const reputationRecords = await Promise.all(identities.map(async (identity) => {
+ const [record] = await db.select().from(spamReputation).where(and(
+ eq(spamReputation.mailboxId, input.mailboxId),
+ eq(spamReputation.type, identity.type),
+ eq(spamReputation.key, identity.key),
+ )).limit(1);
+ return { identity, record };
+ }));
+ const reputationSignals: SpamSignal[] = [];
+ for (const { identity, record } of reputationRecords) {
+ if (!record || record.spamCount + record.hamCount < 3) continue;
+ const spamRatio = (record.spamCount + 1) / (record.spamCount + record.hamCount + 2);
+ if (spamRatio >= 0.8) reputationSignals.push({ id: `poor_${identity.type}_reputation`, score: SPAM_WEIGHTS.reputation.establishedSpam, reason: `${identity.type === "fingerprint" ? "Similar messages have" : "This sender has"} repeatedly been marked as spam` });
+ if (spamRatio <= 0.2) reputationSignals.push({ id: `good_${identity.type}_reputation`, score: SPAM_WEIGHTS.reputation.establishedHam, reason: `${identity.type === "fingerprint" ? "Similar messages have" : "This sender has"} repeatedly been marked as legitimate` });
+ }
+ const strongestReputation = reputationSignals.sort((a, b) => Math.abs(b.score) - Math.abs(a.score))[0];
+ if (strongestReputation) signals.push(strongestReputation);
+
+ const bayesian = await classifyTokens(db, input.mailboxId, tokenizeMessage(input.message, prepared));
+ if (bayesian.contribution !== 0) signals.push({
+ id: bayesian.contribution > 0 ? "bayesian_spam" : "bayesian_ham",
+ score: bayesian.contribution,
+ reason: bayesian.contribution > 0 ? "Content resembles messages previously marked as spam" : "Content resembles messages previously marked as legitimate",
+ metadata: { probability: Number(bayesian.probability.toFixed(3)), trainedMessages: bayesian.trainedMessages },
+ });
+
+ const rawScore = signals.reduce((total, signal) => total + signal.score, 0);
+ const score = Math.max(0, Math.min(100, rawScore));
+ if (rawScore !== score) signals.push({
+ id: "score_boundary",
+ score: score - rawScore,
+ reason: rawScore > 100 ? "Score is capped at 100" : "Score cannot be lower than 0",
+ });
+ const verdict = score >= SPAM_THRESHOLDS.spam ? "spam" : score >= SPAM_THRESHOLDS.suspicious ? "suspicious" : "inbox";
+ return { score, verdict, signals: signals.filter((signal) => Math.abs(signal.score) >= 2 || signal.id === "score_boundary"), contentProbability: bayesian.probability, fingerprint };
+}
diff --git a/src/lib/spam/feedback.ts b/src/lib/spam/feedback.ts
new file mode 100644
index 0000000..29b660e
--- /dev/null
+++ b/src/lib/spam/feedback.ts
@@ -0,0 +1,54 @@
+import { eq } from "drizzle-orm";
+import { getDb } from "@/db";
+import { messages, spamFeedback } from "@/db/schema";
+import { getMailboxAccessLevel } from "@/lib/mailboxes/access";
+import type { SessionUser } from "@/lib/auth/types";
+import { createAuditLog } from "@/lib/mailboxes/audit";
+import { buildFingerprint, prepareSpamContent, tokenizeMessage } from "./tokenizer";
+import { getReputationKeys } from "./analyzers/reputation";
+import type { ReputationKey, SpamClassification } from "./types";
+import { TOKENIZER_VERSION } from "./weights";
+
+export async function applySpamFeedback(env: CloudflareEnv, user: SessionUser, messageId: string, classification: SpamClassification): Promise<boolean> {
+ const db = getDb(env);
+ const [message] = await db.select().from(messages).where(eq(messages.id, messageId)).limit(1);
+ if (!message?.mailboxId || message.direction !== "inbound") return false;
+ const access = await getMailboxAccessLevel(db, user, message.mailboxId);
+ if (!access?.canManage) return false;
+ const [previous] = await db.select().from(spamFeedback).where(eq(spamFeedback.messageId, messageId)).limit(1);
+ const status = classification === "spam" ? "spam" : "received";
+ if (previous?.classification === classification) {
+ await db.update(messages).set({ status, folderId: null }).where(eq(messages.id, messageId));
+ return true;
+ }
+
+ const parsed = {
+ subject: message.subject, text: message.textBody, html: message.htmlBody,
+ messageId: message.providerMessageId, fromAddr: message.fromAddr, toAddr: message.toAddr,
+ ccAddr: message.ccAddr, inReplyTo: message.inReplyTo,
+ references: message.references?.split(/\s+/).filter(Boolean) ?? [], date: message.createdAt,
+ attachments: [],
+ };
+ const prepared = prepareSpamContent(parsed);
+ const tokens = previous ? JSON.parse(previous.trainingTokens) as string[] : tokenizeMessage(parsed, prepared);
+ const keys = previous ? JSON.parse(previous.reputationKeys) as ReputationKey[] : getReputationKeys(parsed, buildFingerprint(parsed, prepared));
+ const now = Math.floor(Date.now() / 1000);
+ const statements: D1PreparedStatement[] = [];
+ const addCounts = (target: SpamClassification, amount: 1 | -1) => {
+ for (const token of tokens) statements.push(env.DB.prepare(`INSERT INTO spam_token_stats (mailbox_id, token, spam_count, ham_count, updated_at) VALUES (?, ?, ?, ?, ?) ON CONFLICT(mailbox_id, token) DO UPDATE SET spam_count = MAX(0, spam_count + excluded.spam_count), ham_count = MAX(0, ham_count + excluded.ham_count), updated_at = excluded.updated_at`).bind(message.mailboxId, token, target === "spam" ? amount : 0, target === "ham" ? amount : 0, now));
+ for (const item of keys) statements.push(env.DB.prepare(`INSERT INTO spam_reputation (mailbox_id, type, key, messages_seen, spam_count, ham_count, first_seen_at, last_seen_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(mailbox_id, type, key) DO UPDATE SET messages_seen = MAX(messages_seen, excluded.messages_seen), spam_count = MAX(0, spam_count + excluded.spam_count), ham_count = MAX(0, ham_count + excluded.ham_count), last_seen_at = excluded.last_seen_at`).bind(message.mailboxId, item.type, item.key, amount > 0 ? 1 : 0, target === "spam" ? amount : 0, target === "ham" ? amount : 0, now, now));
+ };
+ if (previous) addCounts(previous.classification, -1);
+ addCounts(classification, 1);
+ statements.push(env.DB.prepare(`INSERT INTO spam_feedback (message_id, mailbox_id, actor_user_id, classification, training_tokens, reputation_keys, tokenizer_version, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ON CONFLICT(message_id) DO UPDATE SET actor_user_id = excluded.actor_user_id, classification = excluded.classification, updated_at = excluded.updated_at`).bind(messageId, message.mailboxId, user.id, classification, JSON.stringify(tokens), JSON.stringify(keys), TOKENIZER_VERSION, now, now));
+ statements.push(env.DB.prepare("UPDATE messages SET status = ?, folder_id = NULL WHERE id = ?").bind(status, messageId));
+ await env.DB.batch(statements);
+ await createAuditLog(env, {
+ actorUserId: user.id,
+ mailboxId: message.mailboxId,
+ messageId,
+ action: "email.spam_feedback",
+ metadata: { classification },
+ });
+ return true;
+}
diff --git a/src/lib/spam/providers/local.ts b/src/lib/spam/providers/local.ts
new file mode 100644
index 0000000..ec32b36
--- /dev/null
+++ b/src/lib/spam/providers/local.ts
@@ -0,0 +1,7 @@
+import type { SpamIntelligenceProvider, SpamSignal } from "../types";
+
+export class LocalSpamIntelligenceProvider implements SpamIntelligenceProvider {
+ check(): Promise<SpamSignal[]> {
+ return Promise.resolve([]);
+ }
+}
diff --git a/src/lib/spam/repository.ts b/src/lib/spam/repository.ts
new file mode 100644
index 0000000..06ac0e7
--- /dev/null
+++ b/src/lib/spam/repository.ts
@@ -0,0 +1,8 @@
+import type { ReputationKey } from "./types";
+
+export async function recordReputationObservation(env: CloudflareEnv, mailboxId: string, keys: ReputationKey[]): Promise<void> {
+ const now = Math.floor(Date.now() / 1000);
+ await env.DB.batch(keys.map((item) => env.DB.prepare(
+ `INSERT INTO spam_reputation (mailbox_id, type, key, messages_seen, spam_count, ham_count, first_seen_at, last_seen_at) VALUES (?, ?, ?, 1, 0, 0, ?, ?) ON CONFLICT(mailbox_id, type, key) DO UPDATE SET messages_seen = messages_seen + 1, last_seen_at = excluded.last_seen_at`,
+ ).bind(mailboxId, item.type, item.key, now, now)));
+}
diff --git a/src/lib/spam/tokenizer.ts b/src/lib/spam/tokenizer.ts
new file mode 100644
index 0000000..03670b7
--- /dev/null
+++ b/src/lib/spam/tokenizer.ts
@@ -0,0 +1,67 @@
+import { getEmailAddress } from "@/lib/email/address";
+import { htmlToReadableText } from "@/lib/email/reply-content-utils";
+import type { ParsedEmail } from "@/lib/email/parse";
+import type { PreparedSpamContent } from "./types";
+import { MAX_CANDIDATE_TOKENS } from "./weights";
+
+const URL_RE = /https?:\/\/[^\s<>"']+/gi;
+
+export function getDomain(value: string | null | undefined): string {
+ const address = getEmailAddress(value ?? "").toLowerCase();
+ return address.includes("@") ? address.slice(address.lastIndexOf("@") + 1) : "";
+}
+
+export function extractUrlDomains(message: ParsedEmail): string[] {
+ const source = `${message.text ?? ""}\n${message.html ?? ""}`.slice(0, 250_000);
+ const result = new Set<string>();
+ for (const match of source.matchAll(URL_RE)) {
+ try {
+ const domain = new URL(match[0]).hostname.toLowerCase().replace(/^www\./, "");
+ if (domain) result.add(domain);
+ } catch {}
+ if (result.size >= 50) break;
+ }
+ return [...result];
+}
+
+export function prepareSpamContent(message: ParsedEmail): PreparedSpamContent {
+ return {
+ visibleText: `${message.subject ?? ""}\n${message.text ?? ""}\n${htmlToReadableText(message.html)}`.slice(0, 250_000),
+ urlDomains: extractUrlDomains(message),
+ };
+}
+
+export function tokenizeMessage(message: ParsedEmail, prepared = prepareSpamContent(message)): string[] {
+ const sender = getEmailAddress(message.fromAddr ?? "").toLowerCase();
+ const senderDomain = getDomain(sender);
+ const tokens = new Set<string>();
+ for (const raw of prepared.visibleText.toLowerCase().match(/[\p{L}\p{N}][\p{L}\p{N}_$%.-]{1,48}/gu) ?? []) {
+ const token = raw.replace(/^[._-]+|[._-]+$/g, "");
+ if (token.length >= 2 && token.length <= 50) tokens.add(token);
+ if (tokens.size >= MAX_CANDIDATE_TOKENS) break;
+ }
+ if (message.subject) {
+ for (const raw of message.subject.toLowerCase().match(/[\p{L}\p{N}]{2,40}/gu) ?? []) tokens.add(`SUBJECT:${raw}`);
+ }
+ if (sender) tokens.add(`FROM:${sender}`);
+ if (senderDomain) tokens.add(`FROM_DOMAIN:${senderDomain}`);
+ for (const domain of prepared.urlDomains) tokens.add(`URL_DOMAIN:${domain}`);
+ if (message.html && !message.text?.trim()) tokens.add("HTML_ONLY");
+ if (message.attachments.length) tokens.add("HAS_ATTACHMENT");
+ return [...tokens].slice(0, MAX_CANDIDATE_TOKENS);
+}
+
+export function buildFingerprint(message: ParsedEmail, prepared = prepareSpamContent(message)): string {
+ const normalized = [
+ (message.subject ?? "").toLowerCase().replace(/\s+/g, " ").trim().slice(0, 160),
+ getDomain(message.fromAddr),
+ [...prepared.urlDomains].sort().join(","),
+ prepared.visibleText.toLowerCase().replace(/\d+/g, "#").replace(/\s+/g, " ").trim().slice(0, 500),
+ ].join("|");
+ let hash = 2166136261;
+ for (let index = 0; index < normalized.length; index += 1) {
+ hash ^= normalized.charCodeAt(index);
+ hash = Math.imul(hash, 16777619);
+ }
+ return (hash >>> 0).toString(16).padStart(8, "0");
+}
diff --git a/src/lib/spam/types.d.ts b/src/lib/spam/types.d.ts
new file mode 100644
index 0000000..ddc31b6
--- /dev/null
+++ b/src/lib/spam/types.d.ts
@@ -0,0 +1,46 @@
+import type { ParsedEmail } from "@/lib/email/parse";
+
+export type SpamVerdict = "inbox" | "suspicious" | "spam";
+export type SpamClassification = "spam" | "ham";
+
+export type SpamSignal = {
+ id: string;
+ score: number;
+ reason: string;
+ metadata?: Record<string, unknown>;
+};
+
+export type SpamAnalysisInput = {
+ mailboxId: string;
+ userId: string;
+ envelopeFrom: string;
+ headers?: Record<string, string>;
+ message: ParsedEmail;
+ intelligenceProvider?: SpamIntelligenceProvider;
+};
+
+export type SpamAnalysisResult = {
+ score: number;
+ verdict: SpamVerdict;
+ signals: SpamSignal[];
+ contentProbability?: number;
+ fingerprint: string;
+};
+
+export type ReputationKey = { type: "email" | "domain" | "fingerprint"; key: string };
+
+export type PreparedSpamContent = {
+ visibleText: string;
+ urlDomains: string[];
+};
+
+export interface SpamIntelligenceProvider {
+ check(input: { senderDomain: string; urlDomains: string[]; fingerprint: string }): Promise<SpamSignal[]>;
+}
+
+export type BayesianResult = {
+ probability: number;
+ contribution: number;
+ trainedMessages: number;
+ strongestTokens: Array<{ token: string; probability: number }>;
+};
diff --git a/src/lib/spam/weights.ts b/src/lib/spam/weights.ts
new file mode 100644
index 0000000..728c7b7
--- /dev/null
+++ b/src/lib/spam/weights.ts
@@ -0,0 +1,12 @@
+export const SPAM_THRESHOLDS = { suspicious: 40, spam: 70 } as const;
+
+export const SPAM_WEIGHTS = {
+ authentication: { dmarcFail: 18, dmarcPass: -8, dkimFail: 8, dkimPass: -4, spfFail: 6, spfPass: -2 },
+ relationships: { manuallySavedContact: -15, previouslySentTo: -25 },
+ reputation: { establishedSpam: 25, establishedHam: -15 },
+ content: { maximum: 30 },
+} as const;
+
+export const MAX_CANDIDATE_TOKENS = 500;
+export const MAX_BAYESIAN_TOKENS = 20;
+export const TOKENIZER_VERSION = 1;
diff --git a/src/lib/user.ts b/src/lib/user.ts
index a8a9f2d..ffea32e 100644
--- a/src/lib/user.ts
+++ b/src/lib/user.ts
@@ -4,6 +4,7 @@ import { messages, domains, mailboxes } from "@/db/schema";
import { getMailboxAccessLevel, listAccessibleMailboxIds } from "@/lib/mailboxes/access";
import { createAuditLog } from "@/lib/mailboxes/audit";
import type { SessionUser } from "@/lib/auth/types";
+import { applySpamFeedback } from "@/lib/spam/feedback";
export async function listMessagesForUser(
env: CloudflareEnv,
@@ -105,6 +106,8 @@ export async function updateMessageStatusForUser(
if (!message?.mailboxId) return false;
const access = await getMailboxAccessLevel(db, user, message.mailboxId);
if (!access?.canManage) return false;
+ if (status === "spam") return applySpamFeedback(env, user, messageId, "spam");
+ if (status === "received" && message.status === "spam") return applySpamFeedback(env, user, messageId, "ham");
await db.update(messages).set({ status }).where(eq(messages.id, messageId));
await createAuditLog(env, {
actorUserId: user.id,
diff --git a/src/lib/validators.ts b/src/lib/validators.ts
index 9ebf1e3..a507d32 100644
--- a/src/lib/validators.ts
+++ b/src/lib/validators.ts
@@ -193,6 +193,10 @@ export const updateShortcutsSettingsSchema = z.object({
enabled: z.boolean(),
});
+export const updateSpamSettingsSchema = z.object({
+ enabled: z.boolean(),
+});
+
export const changePasswordSchema = z.object({
currentPassword: z.string().min(1),
newPassword: z.string().min(8).max(128),
diff --git a/tests/setup-bootstrap-schema.test.mjs b/tests/setup-bootstrap-schema.test.mjs
index cc6aad3..d4adbea 100644
--- a/tests/setup-bootstrap-schema.test.mjs
+++ b/tests/setup-bootstrap-schema.test.mjs
@@ -27,6 +27,7 @@ test("bootstrap records migrations represented in the current schema so later de
"0021_add_mailbox_signature.sql",
"0022_add_mailbox_auto_reply.sql",
"0027_add_domain_sending_intent.sql",
+ "0029_add_spam_protection.sql",
]) {
assert.ok(names.includes(name), `MIGRATION_NAMES is missing ${name}`);
}
@@ -42,6 +43,10 @@ test("fresh bootstrap schema accepts the current Drizzle mailbox and license ins
assert.match(mailboxCreate[1], /\bauto_reply_body\b/);
assert.match(sql, /CREATE TABLE IF NOT EXISTS license_settings \(/);
assert.match(sql, /CREATE TABLE IF NOT EXISTS auto_reply_deliveries \(/);
+ assert.match(sql, /\bspam_protection_enabled\b/);
+ assert.match(sql, /CREATE TABLE IF NOT EXISTS spam_token_stats \(/);
+ assert.match(sql, /CREATE TABLE IF NOT EXISTS spam_reputation \(/);
+ assert.match(sql, /CREATE TABLE IF NOT EXISTS spam_feedback \(/);
const domainCreate = sql.match(/CREATE TABLE IF NOT EXISTS domains \(([\s\S]*?)\);/);
assert.ok(domainCreate, "domains CREATE TABLE not found");
assert.match(domainCreate[1], /\bsending_requested\b/);