Skip to content

Email for professionals and teams

fix(security): stop inbound mail renaming contacts, forging read state, notifying from trash (#223)

Hieu Nguyen (Jack)0001b7be6b61
diff --git a/src/lib/contacts/service.ts b/src/lib/contacts/service.ts
index d4fa7fb..35d5a20 100644
--- a/src/lib/contacts/service.ts
+++ b/src/lib/contacts/service.ts
@@ -20,7 +20,7 @@ export async function upsertContactFromAddress(env: CloudflareEnv, input: Contac
const now = new Date();
if (existing) {
- const nextDisplayName = getNextDisplayName(existing.displayName, existing.source, displayName);
+ const nextDisplayName = getNextDisplayName(existing.displayName, existing.source, displayName, input.source);
const nextSource = existing.source === "manual" || (existing.source === "outbound" && input.source === "inbound")
? existing.source
: input.source;
@@ -160,8 +160,10 @@ export async function blockContact(env: CloudflareEnv, input: BlockContactInput)
return { email, blocked: true };
}
-function getNextDisplayName(existingName: string | null, source: string, nextName: string | null): string | null {
+function getNextDisplayName(existingName: string | null, source: string, nextName: string | null, incomingSource: string): string | null {
if (source === "manual") return existingName;
+ // A sender chooses its own From name, so inbound mail may name a contact but never rename one.
+ if (incomingSource === "inbound" && existingName) return existingName;
if (nextName) return nextName;
return existingName;
}
diff --git a/src/lib/email/inbound.ts b/src/lib/email/inbound.ts
index cfe02ac..2046595 100644
--- a/src/lib/email/inbound.ts
+++ b/src/lib/email/inbound.ts
@@ -1,6 +1,6 @@
import { and, eq, isNotNull } from "drizzle-orm";
import { getDb } from "@/db";
-import { domains, mailboxAliases, mailboxes, messages, outboundJobs, users } from "@/db/schema";
+import { messages, outboundJobs, users } from "@/db/schema";
import { newId } from "@/lib/ids";
import { buildSnippet, parseRawMime } from "@/lib/email/parse";
import { boundParsedEmail } from "@/lib/email/inbound-limits";
@@ -40,24 +40,6 @@ export type InboundQueueMessage = {
};
/** True when the address is a mailbox or alias owned by the user, so their own mail never counts as unread. */
-async function isOwnAddress(db: ReturnType<typeof getDb>, userId: string, address: string) {
- const at = address.lastIndexOf("@");
- if (at < 1) return false;
- const localPart = address.slice(0, at).toLowerCase();
- const hostname = address.slice(at + 1).toLowerCase();
- const [mailbox] = await db.select({ id: mailboxes.id }).from(mailboxes)
- .innerJoin(domains, eq(mailboxes.domainId, domains.id))
- .where(and(eq(mailboxes.userId, userId), eq(mailboxes.localPart, localPart), eq(domains.hostname, hostname)))
- .limit(1);
- if (mailbox) return true;
- const [alias] = await db.select({ id: mailboxAliases.id }).from(mailboxAliases)
- .innerJoin(mailboxes, eq(mailboxAliases.mailboxId, mailboxes.id))
- .innerJoin(domains, eq(mailboxAliases.domainId, domains.id))
- .where(and(eq(mailboxes.userId, userId), eq(mailboxAliases.localPart, localPart), eq(domains.hostname, hostname)))
- .limit(1);
- return Boolean(alias);
-}
-
export async function processInboundMessage(
env: CloudflareEnv,
payload: InboundQueueMessage,
@@ -187,7 +169,6 @@ export async function processInboundMessage(
console.error(`Contact upsert failed for ${messageId}`, error);
return null;
});
- const sentByOwner = await isOwnAddress(db, decision.mailbox.userId, getEmailAddress(fromAddr) || fromAddr).catch(() => false);
let threadId: string;
try {
threadId = await resolveThreadId(db, {
@@ -224,7 +205,8 @@ export async function processInboundMessage(
htmlBody: parsed.html,
rawR2Key: payload.rawR2Key,
status,
- read: sentByOwner,
+ // The From header is forgeable, so mail "from yourself" is not treated as already read.
+ read: false,
threadId,
inReplyTo: parsed.inReplyTo,
references: parsed.references.length ? parsed.references.join(" ") : null,
@@ -270,8 +252,8 @@ export async function processInboundMessage(
}
}
- if (status === "spam") {
- console.info(`No notification for ${messageId}: filed as spam`);
+ if (status === "spam" || status === "trash") {
+ console.info(`No notification for ${messageId}: filed as ${status}`);
} else {
const notificationUserIds = await getMailboxNotificationUserIds(
env,
diff --git a/src/lib/email/thread-view.ts b/src/lib/email/thread-view.ts
index 12e94b3..f057abe 100644
--- a/src/lib/email/thread-view.ts
+++ b/src/lib/email/thread-view.ts
@@ -1,4 +1,4 @@
-import { and, asc, eq, inArray, isNull, notInArray } from "drizzle-orm";
+import { and, asc, eq, inArray, isNull, ne, notInArray, or } from "drizzle-orm";
import { getDb } from "@/db";
import { messageAttachments, messages } from "@/db/schema";
import { getContactAvatarMap, getContactDisplayNameMap } from "@/lib/contacts/service";
@@ -39,6 +39,8 @@ export async function getMessageThreadForUser(env: CloudflareEnv, user: SessionU
eq(messages.mailboxId, message.mailboxId),
eq(messages.threadId, message.threadId),
notInArray(messages.status, ["draft", "trash"]),
+ // Spam stays out of a conversation unless it is the message being opened.
+ or(ne(messages.status, "spam"), eq(messages.id, messageId)),
),
)
.orderBy(asc(messages.createdAt))