From 57215e34af9ab4b8073e2a4e930af08bf98548e4 Mon Sep 17 00:00:00 2001 From: Madraka <8898640+Madraka@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:25:43 +0300 Subject: [PATCH 1/3] feat(rules): blocked senders, filters and an automatic reply in the mailbox ADR-0008, in the mailbox and its routes. A mailbox's filters and blocked senders run once, where an arrival is filed, by one pure function over a closed vocabulary; the automatic reply answers a person once in seven days by RFC 3834 and leaves by the one function that lets mail leave. With the corrections of the audit of the draft: the envelope sender is asked of the block list; only mail filed in the Inbox wakes the assistant; a waiting reply is asked again whether it is on, and needs its saver to still hold mailbox.manage; a reply that does not leave for a reason of the mailbox answered nobody; a waiting reply is nobody's to take back or give a time; text and HTML are taken together only when they agree; a block can say it means one whole address; the rules routes stop reading at 256 KiB; turning the reply off keeps no stranger's address. Co-Authored-By: Claude Fable 5.2 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com> --- shared/rules-schema.ts | 204 +++++++ shared/rules.ts | 615 +++++++++++++++++++++ test/unit/agent-no-send.test.ts | 31 ++ test/unit/auto-reply.test.ts | 266 +++++++++ test/unit/body.test.ts | 54 ++ test/unit/fresh.test.ts | 2 + test/unit/rules.test.ts | 624 +++++++++++++++++++++ test/workerd-agent/rules.test.ts | 238 ++++++++ test/workerd/auto-reply-helpers.ts | 91 +++ test/workerd/auto-reply-standing.test.ts | 191 +++++++ test/workerd/auto-reply.test.ts | 594 ++++++++++++++++++++ test/workerd/live-methods.ts | 7 + test/workerd/live-signal.test.ts | 30 +- test/workerd/rules-helpers.ts | 141 +++++ test/workerd/rules.test.ts | 672 +++++++++++++++++++++++ test/workerd/second-step.test.ts | 8 +- test/workerd/sending-routes.test.ts | 6 +- workers/agent/tools.ts | 26 +- workers/arrival.ts | 10 +- workers/auth/route-table.ts | 10 + workers/auth/standing.ts | 18 +- workers/durableObject/auto-reply.ts | 261 +++++++++ workers/durableObject/index.ts | 27 +- workers/durableObject/leaving.ts | 74 ++- workers/durableObject/live.ts | 6 + workers/durableObject/migrations.ts | 31 ++ workers/durableObject/originals.ts | 41 +- workers/durableObject/rules.ts | 240 ++++++++ workers/durableObject/submissions.ts | 39 +- workers/index.ts | 2 + workers/lib/body.ts | 33 ++ workers/lib/outgoing-message.ts | 26 +- workers/lib/rule-facts.ts | 58 ++ workers/routes/choices.ts | 27 +- workers/routes/mailboxes.ts | 10 +- workers/routes/rules.ts | 166 ++++++ workers/routes/sending.ts | 2 + 37 files changed, 4802 insertions(+), 79 deletions(-) create mode 100644 shared/rules-schema.ts create mode 100644 shared/rules.ts create mode 100644 test/unit/auto-reply.test.ts create mode 100644 test/unit/body.test.ts create mode 100644 test/unit/rules.test.ts create mode 100644 test/workerd-agent/rules.test.ts create mode 100644 test/workerd/auto-reply-helpers.ts create mode 100644 test/workerd/auto-reply-standing.test.ts create mode 100644 test/workerd/auto-reply.test.ts create mode 100644 test/workerd/rules-helpers.ts create mode 100644 test/workerd/rules.test.ts create mode 100644 workers/durableObject/auto-reply.ts create mode 100644 workers/durableObject/rules.ts create mode 100644 workers/lib/body.ts create mode 100644 workers/lib/rule-facts.ts create mode 100644 workers/routes/rules.ts diff --git a/shared/rules-schema.ts b/shared/rules-schema.ts new file mode 100644 index 0000000..ad9d89c --- /dev/null +++ b/shared/rules-schema.ts @@ -0,0 +1,204 @@ +// Copyright 2026 Cloudhal and the Podbay contributors +// Licensed under the Apache 2.0 license found in the LICENSE file. + +/** + * The strict half of the rules of a mailbox (ADR-0008): what `PUT …/rules`, `POST` and + * `DELETE …/blocked-senders` and `PUT …/auto-reply` take (rule D2). A key the vocabulary does + * not have refuses the whole request: nothing is stored that the one pure function of + * `shared/rules.ts` does not know. + * + * Only the Worker loads this module: the app takes its types from `shared/rules.ts`, so the + * schema library stays out of its bundle. + */ +import { z } from "zod"; +import { plainTextOf } from "./html-text"; +import { isLabelId } from "./labels"; +import { + AUTO_REPLY, + type AutoReply, + blockedSenderOf, + cleanText, + FACT_LIMITS, + isRuleId, + newRuleId, + RULE_LIMITS, + RULE_MATCHES, + RULE_PLACES, + type Rule, + type RuleActions, + type RuleConditions, +} from "./rules"; + +/** Longer than a condition may be once it is cleaned, so that spaces around it are not refused. */ +const text = z.string().max(RULE_LIMITS.condition * 2); +const address = z.strictObject({ match: z.enum(RULE_MATCHES), value: text }); + +const ConditionsSchema = z.strictObject({ + from: address.optional(), + to: address.optional(), + subject: text.optional(), + words: text.optional(), + hasAttachment: z.literal(true).optional(), +}); + +const ActionsSchema = z.strictObject({ + place: z.enum(RULE_PLACES).optional(), + read: z.literal(true).optional(), + star: z.literal(true).optional(), + labels: z.array(z.string().max(64)).max(RULE_LIMITS.labels).optional(), +}); + +const RuleSchema = z.strictObject({ + id: z.string().max(64).optional(), + conditions: ConditionsSchema, + actions: ActionsSchema, +}); + +/** One more than a mailbox may hold, so that "too many" is told apart from "not a list". */ +const RulesBodySchema = z.strictObject({ rules: z.array(RuleSchema).max(RULE_LIMITS.rules + 1) }); + +function conditionsOf(given: z.infer): RuleConditions | null { + const conditions: RuleConditions = {}; + for (const field of ["from", "to"] as const) { + const one = given[field]; + if (one === undefined) continue; + const value = cleanText(one.value); + if (value === null) return null; + conditions[field] = { match: one.match, value }; + } + for (const field of ["subject", "words"] as const) { + const one = given[field]; + if (one === undefined) continue; + const value = cleanText(one); + if (value === null) return null; + conditions[field] = value; + } + if (given.hasAttachment === true) conditions.hasAttachment = true; + return Object.keys(conditions).length === 0 ? null : conditions; +} + +function actionsOf(given: z.infer): RuleActions | null { + const labels = [...new Set(given.labels ?? [])]; + if (!labels.every(isLabelId)) return null; + const actions: RuleActions = { + ...(given.place === undefined ? {} : { place: given.place }), + ...(given.read === true ? { read: true as const } : {}), + ...(given.star === true ? { star: true as const } : {}), + ...(labels.length > 0 ? { labels } : {}), + }; + return Object.keys(actions).length === 0 ? null : actions; +} + +export type ParsedRules = { rules: Rule[] } | { refused: "invalid" | "too_many" }; + +/** + * The rule list a request body is. A rule needs at least one condition and one action; a rule + * that names an id keeps it, any other gets one. Two rules never share an id. + */ +export function parseRules(body: unknown): ParsedRules { + const parsed = RulesBodySchema.safeParse(body); + if (!parsed.success) return { refused: "invalid" }; + if (parsed.data.rules.length > RULE_LIMITS.rules) return { refused: "too_many" }; + const rules: Rule[] = []; + const ids = new Set(); + for (const given of parsed.data.rules) { + const conditions = conditionsOf(given.conditions); + const actions = actionsOf(given.actions); + if (conditions === null || actions === null) return { refused: "invalid" }; + if (given.id !== undefined && (!isRuleId(given.id) || ids.has(given.id))) { + return { refused: "invalid" }; + } + const id = given.id ?? newRuleId(); + ids.add(id); + rules.push({ id, conditions, actions }); + } + return { rules }; +} + +const SenderBodySchema = z.strictObject({ + sender: z.string().max(FACT_LIMITS.address), + kind: z.enum(["address", "domain"]).optional(), +}); + +/** + * The sender a request names, as it is stored, or `null` when it names none. A request may + * say which kind it means: "address" takes one whole address only, and "domain" a domain + * only, so a block offered for one address can never become a block of a whole domain. + */ +export function parseSender(body: unknown): string | null { + const parsed = SenderBodySchema.safeParse(body); + if (!parsed.success) return null; + const sender = blockedSenderOf(parsed.data.sender); + if (sender === null) return null; + const domain = sender.startsWith("@"); + if (parsed.data.kind === "address" && domain) return null; + if (parsed.data.kind === "domain" && !domain) return null; + return sender; +} + +const instant = z.union([z.null(), z.string().max(40)]); + +const AutoReplySchema = z.strictObject({ + isEnabled: z.boolean(), + fromDate: instant, + toDate: instant, + subject: z.union([z.null(), z.string().max(AUTO_REPLY.subject * 2)]), + textBody: z.union([z.null(), z.string().max(AUTO_REPLY.text)]), + htmlBody: z.union([z.null(), z.string().max(AUTO_REPLY.html)]), +}); + +/** An instant as it is stored: UTC, in one form. `undefined` when the text is not a date. */ +function instantOf(value: string | null): string | null | undefined { + if (value === null) return null; + const time = Date.parse(value); + return Number.isNaN(time) ? undefined : new Date(time).toISOString(); +} + +const lines = (value: string | null): string | null => + value === null || value.trim() === "" ? null : value; + +/** Whether two texts say the same, whatever their spaces and line breaks. */ +const sameText = (one: string, other: string): boolean => + one.replace(/\s+/g, " ").trim() === other.replace(/\s+/g, " ").trim(); + +export type ParsedAutoReply = { reply: AutoReply } | { refused: "invalid" | "empty" }; + +/** + * The automatic reply a request body is. A subject is one line; an end that is not after its + * beginning is refused; one that is turned on must have something to say. + */ +export function parseAutoReply(body: unknown): ParsedAutoReply { + const parsed = AutoReplySchema.safeParse(body); + if (!parsed.success) return { refused: "invalid" }; + const fromDate = instantOf(parsed.data.fromDate); + const toDate = instantOf(parsed.data.toDate); + if (fromDate === undefined || toDate === undefined) return { refused: "invalid" }; + if (fromDate !== null && toDate !== null && toDate <= fromDate) return { refused: "invalid" }; + const subject = + parsed.data.subject === null + ? null + : // biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are what it removes + parsed.data.subject.replace(/[\u0000-\u001f\u007f]+/g, " ").trim(); + if (subject !== null && [...subject].length > AUTO_REPLY.subject) return { refused: "invalid" }; + const reply: AutoReply = { + isEnabled: parsed.data.isEnabled, + fromDate, + toDate, + subject: subject === "" ? null : subject, + textBody: lines(parsed.data.textBody), + htmlBody: lines(parsed.data.htmlBody), + }; + if (reply.isEnabled && reply.textBody === null && reply.htmlBody === null) { + return { refused: "empty" }; + } + // The HTML is what is sent and the text is what a person sees on the settings screen: both + // together only when the text is the text of the HTML, so the two cannot say different things. + if ( + reply.textBody !== null && + reply.htmlBody !== null && + sameText(plainTextOf(reply.htmlBody), reply.textBody) === false + ) { + return { refused: "invalid" }; + } + return { reply }; +} diff --git a/shared/rules.ts b/shared/rules.ts new file mode 100644 index 0000000..fd7b925 --- /dev/null +++ b/shared/rules.ts @@ -0,0 +1,615 @@ +// Copyright 2026 Cloudhal and the Podbay contributors +// Licensed under the Apache 2.0 license found in the LICENSE file. + +/** + * The rules of a mailbox for its incoming mail (ADR-0008): blocked senders, filters and the + * automatic reply. What the Worker and the app both need to know about them is here: the + * closed vocabulary, the limits, and the two pure functions that decide. + * + * - `evaluate` goes from the blocked senders, the rule list and the facts of a message to an + * outcome: the place, the marks and the labels. Nothing in it is interpreted: a condition is + * plain text compared without regard to case, on fields cut to a fixed length. There is no + * pattern and no expression, and it never throws: a rule that cannot be evaluated is skipped + * and counted. + * - `replyOwed` says whether the automatic reply is owed for a message, and to whom. Each of + * its refusals is a line of RFC 3834 as the record lists them. + * + * Nothing here needs the schema library, so the app imports this module as it is. The strict + * schemas of the routes are in `shared/rules-schema.ts`, which only the Worker loads. + */ + +/** Decision 11: chosen, and to be revised from use. */ +export const RULE_LIMITS = { + /** Filters in one mailbox. */ + rules: 100, + /** Blocked senders in one mailbox. */ + blocked: 500, + /** Characters in the text of a condition. */ + condition: 200, + /** Labels one rule gives. */ + labels: 10, +} as const; + +/** + * How much of a message a rule is matched against. A message is a stranger's text, so every + * field has a length it is cut to before anything is compared. + */ +export const FACT_LIMITS = { + /** Characters of an address. */ + address: 320, + /** Characters of the sender's name. */ + name: 200, + /** Addresses in To and Cc together. */ + recipients: 100, + /** Characters of the subject. */ + subject: 1000, + /** Characters of the text, from its start. */ + text: 20_000, + /** Labels the rules give one message, all together. */ + labels: 50, +} as const; + +/** Where a rule files a message. Without one it stays in the Inbox. */ +export const RULE_PLACES = ["archive", "spam", "trash"] as const; +export type RulePlace = (typeof RULE_PLACES)[number]; + +export const RULE_MATCHES = ["is", "contains"] as const; +export type RuleMatch = (typeof RULE_MATCHES)[number]; + +/** An address field of a condition: the whole address, or a part of it. */ +export interface AddressCondition { + match: RuleMatch; + value: string; +} + +/** The conditions of a rule: a closed set, each at most once, all of which must hold. */ +export interface RuleConditions { + /** The sender is, or contains. */ + from?: AddressCondition; + /** A recipient (To or Cc) is, or contains. */ + to?: AddressCondition; + /** The subject contains. */ + subject?: string; + /** The message (its subject and its text) contains each of these words. */ + words?: string; + /** It has an attachment. */ + hasAttachment?: true; +} + +/** The actions of a rule: a closed set. No rule forwards and no rule rejects. */ +export interface RuleActions { + /** Skip the inbox (`archive`), move to Trash, or move to Spam. */ + place?: RulePlace; + /** Mark as read. */ + read?: true; + /** Star. */ + star?: true; + /** Give labels, by their ids. */ + labels?: string[]; +} + +export interface Rule { + id: string; + conditions: RuleConditions; + actions: RuleActions; +} + +/** A blocked sender: an address, or `@domain` for everything at a domain. */ +export interface BlockedSender { + sender: string; + created_at: string; +} + +/** What `GET …/rules` answers. */ +export interface RulesAnswer { + rules: Rule[]; + blocked: BlockedSender[]; +} + +/** The codes the routes of this record answer with. */ +export const RULE_CODES = { + invalid: "INVALID", + tooMany: "RULE_LIMIT", + blockedLimit: "BLOCKED_LIMIT", + notBlocked: "NOT_BLOCKED", + noBody: "AUTO_REPLY_EMPTY", +} as const; + +// ── Ids and text ─────────────────────────────────────────────────── + +const RULE_ID = /^rul_[0-9a-f]{24}$/; + +/** Whether a value has the shape of a rule's id. An id is opaque. */ +export function isRuleId(value: unknown): value is string { + return typeof value === "string" && RULE_ID.test(value); +} + +export function newRuleId(): string { + const bytes = crypto.getRandomValues(new Uint8Array(12)); + return `rul_${[...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("")}`; +} + +/** Text as it is compared: one form, lower case. Never a pattern. */ +const fold = (text: string): string => text.normalize("NFC").toLowerCase(); + +/** The text of a condition as it is stored: one form, trimmed, inner spaces single. */ +export function cleanText(raw: unknown): string | null { + if (typeof raw !== "string") return null; + const text = raw.normalize("NFC").replace(/\s+/g, " ").trim(); + if (text === "" || [...text].length > RULE_LIMITS.condition) return null; + return text; +} + +// biome-ignore lint/suspicious/noControlCharactersInRegex: control characters are what it refuses +const NOT_IN_AN_ADDRESS = /[\s\u0000-\u001f\u007f<>,;:"()[\]\\]/; + +/** Whether something is one whole address: a local part, one `@`, a domain with a dot. */ +export function isAddress(value: string): boolean { + if (value.length > 254 || NOT_IN_AN_ADDRESS.test(value)) return false; + const at = value.indexOf("@"); + if (at <= 0 || at !== value.lastIndexOf("@")) return false; + return isDomain(value.slice(at + 1)); +} + +function isDomain(value: string): boolean { + if (value === "" || value.length > 253 || NOT_IN_AN_ADDRESS.test(value)) return false; + if (value.includes("@") || !value.includes(".")) return false; + return value.split(".").every((part) => part !== ""); +} + +/** + * What a person gave as a sender to block, as it is stored: an address in lower case, or + * `@domain` for everything at a domain (given as `example.com` or `@example.com`). `null` + * when it is neither. + */ +export function blockedSenderOf(raw: unknown): string | null { + if (typeof raw !== "string" || raw.length > FACT_LIMITS.address) return null; + const text = fold(raw.trim()); + if (isAddress(text)) return text; + const domain = text.startsWith("@") ? text.slice(1) : text; + return isDomain(domain) ? `@${domain}` : null; +} + +/** Whether a sender is blocked: its address is on the list, or its domain is. */ +export function isBlocked(blocked: readonly string[], from: string): boolean { + const address = fold(from); + if (address === "") return false; + const at = address.lastIndexOf("@"); + const domain = at === -1 ? null : address.slice(at); + return blocked.some((one) => one === address || (domain !== null && one === domain)); +} + +// ── The facts of a message ───────────────────────────────────────── + +/** + * What the rules are told about a message. Built once, where the message was parsed, and cut + * to `FACT_LIMITS`; the rules see nothing else of it. + */ +export interface MessageFacts { + /** The address the message says it is from; the envelope sender when it could not be read. */ + from: string; + /** The name beside that address. */ + fromName: string; + /** The addresses in To and Cc. */ + recipients: string[]; + subject: string; + /** The text of the message, from its start. */ + text: string; + hasAttachment: boolean; + /** The value of `Auto-Submitted`, when the message has the field. */ + autoSubmitted: string | null; + /** Whether the message says it is from a list, or is bulk mail. */ + list: boolean; +} + +const cut = (text: unknown, length: number): string => + typeof text === "string" ? text.slice(0, length) : ""; + +/** The facts of a message, each cut to its limit. Anything that is not text is empty. */ +export function factsOf(raw: { + from?: unknown; + fromName?: unknown; + recipients?: unknown; + subject?: unknown; + text?: unknown; + hasAttachment?: unknown; + autoSubmitted?: unknown; + list?: unknown; +}): MessageFacts { + const recipients = Array.isArray(raw.recipients) ? raw.recipients : []; + return { + from: fold(cut(raw.from, FACT_LIMITS.address)), + fromName: cut(raw.fromName, FACT_LIMITS.name), + recipients: recipients + .filter((one): one is string => typeof one === "string" && one !== "") + .slice(0, FACT_LIMITS.recipients) + .map((one) => fold(cut(one, FACT_LIMITS.address))), + subject: cut(raw.subject, FACT_LIMITS.subject), + text: cut(raw.text, FACT_LIMITS.text), + hasAttachment: raw.hasAttachment === true, + autoSubmitted: + typeof raw.autoSubmitted === "string" ? cut(raw.autoSubmitted, FACT_LIMITS.name) : null, + list: raw.list === true, + }; +} + +// ── The outcome ──────────────────────────────────────────────────── + +/** What the rules decide for a message. */ +export interface Outcome { + place: "inbox" | RulePlace; + read: boolean; + starred: boolean; + /** Label ids, each once. */ + labels: string[]; + /** Whether the sender is blocked. */ + blocked: boolean; + /** Rules that matched. */ + matched: number; + /** Rules that could not be evaluated: skipped, and counted. */ + skipped: number; +} + +/** When rules disagree about the place: Trash wins over Spam, and Spam over Archive. */ +const WEIGHT: Record = { inbox: 0, archive: 1, spam: 2, trash: 3 }; + +const isObject = (value: unknown): value is Record => + typeof value === "object" && value !== null && !Array.isArray(value); + +/** A stored text of a condition, folded; `null` when it is not one. */ +function needle(value: unknown): string | null { + if (typeof value !== "string" || value === "" || value.length > RULE_LIMITS.condition * 4) { + return null; + } + return fold(value); +} + +/** What a message is compared as: every field folded once. */ +interface Folded { + from: string; + fromName: string; + recipients: string[]; + subject: string; + /** The subject and the text, for "contains the words". */ + all: string; + hasAttachment: boolean; +} + +/** + * Whether an address condition holds; `null` when it cannot be evaluated. "Is" is asked of + * the addresses alone; "contains" also of a name beside them. + */ +function addressHolds( + condition: unknown, + addresses: readonly string[], + names: readonly string[] = [], +): boolean | null { + if (!isObject(condition)) return null; + const value = needle(condition.value); + if (value === null) return null; + if (condition.match === "is") return addresses.some((one) => one === value); + if (condition.match === "contains") { + return [...addresses, ...names].some((one) => one.includes(value)); + } + return null; +} + +/** + * Whether every condition of a rule holds; `null` when the rule cannot be evaluated: it has + * no condition, one it does not know, or one that is not what its field takes. + */ +function holds(conditions: unknown, message: Folded): boolean | null { + if (!isObject(conditions)) return null; + const keys = Object.keys(conditions).filter((key) => conditions[key] !== undefined); + if (keys.length === 0) return null; + let all = true; + for (const key of keys) { + let one: boolean | null; + if (key === "from") one = addressHolds(conditions.from, [message.from], [message.fromName]); + else if (key === "to") one = addressHolds(conditions.to, message.recipients); + else if (key === "subject") { + const value = needle(conditions.subject); + one = value === null ? null : message.subject.includes(value); + } else if (key === "words") { + const value = needle(conditions.words); + const words = value === null ? [] : value.split(/\s+/).filter((word) => word !== ""); + one = words.length === 0 ? null : words.every((word) => message.all.includes(word)); + } else if (key === "hasAttachment") { + one = conditions.hasAttachment === true ? message.hasAttachment : null; + } else one = null; + if (one === null) return null; + all = all && one; + } + return all; +} + +/** The actions of a rule as far as they are known; `null` when there is none to apply. */ +function actionsOf(actions: unknown): RuleActions | null { + if (!isObject(actions)) return null; + const place = RULE_PLACES.find((one) => one === actions.place); + if (actions.place !== undefined && place === undefined) return null; + const labels = Array.isArray(actions.labels) + ? actions.labels.filter((one): one is string => typeof one === "string") + : []; + if ( + place === undefined && + actions.read !== true && + actions.star !== true && + labels.length === 0 + ) { + return null; + } + return { + ...(place === undefined ? {} : { place }), + ...(actions.read === true ? { read: true as const } : {}), + ...(actions.star === true ? { star: true as const } : {}), + ...(labels.length > 0 ? { labels: labels.slice(0, RULE_LIMITS.labels) } : {}), + }; +} + +const NOTHING: Folded = { + from: "", + fromName: "", + recipients: [], + subject: "", + all: "", + hasAttachment: false, +}; + +/** + * A stored rule as it is shown, or `null` when it is not one the vocabulary knows: such a rule + * is skipped when mail arrives, and is not offered to be changed either. + */ +export function shownRule(id: string, body: unknown): Rule | null { + if (!isObject(body) || !isObject(body.conditions)) return null; + const actions = actionsOf(body.actions); + if (actions === null || holds(body.conditions, NOTHING) === null) return null; + return { id, conditions: body.conditions as RuleConditions, actions }; +} + +/** + * What the rules of a mailbox decide for a message (decisions 5 and 6): blocked senders + * first, then every rule in order. All matching rules apply. It reads nothing but its + * arguments and never throws: a rule that cannot be evaluated is skipped and counted. + */ +export function evaluate( + blocked: readonly string[], + rules: readonly unknown[], + facts: MessageFacts, +): Outcome { + const outcome: Outcome = { + place: "inbox", + read: false, + starred: false, + labels: [], + blocked: false, + matched: 0, + skipped: 0, + }; + const to = (place: Outcome["place"]) => { + if (WEIGHT[place] > WEIGHT[outcome.place]) outcome.place = place; + }; + let message: Folded; + try { + message = { + from: fold(facts.from), + fromName: fold(facts.fromName), + recipients: facts.recipients.map(fold), + subject: fold(facts.subject), + all: `${fold(facts.subject)}\n${fold(facts.text)}`, + hasAttachment: facts.hasAttachment === true, + }; + if (isBlocked(blocked, message.from)) { + outcome.blocked = true; + to("spam"); + } + } catch { + // Facts that are not facts: nothing can be matched, and the message stays in the Inbox. + outcome.skipped = rules.length; + return outcome; + } + const labels = new Set(); + for (const rule of rules) { + try { + const one = isObject(rule) ? rule : {}; + const matches = holds(one.conditions, message); + const actions = actionsOf(one.actions); + if (matches === null || actions === null) { + outcome.skipped += 1; + continue; + } + if (!matches) continue; + outcome.matched += 1; + if (actions.place !== undefined) to(actions.place); + if (actions.read === true) outcome.read = true; + if (actions.star === true) outcome.starred = true; + for (const label of actions.labels ?? []) { + if (labels.size < FACT_LIMITS.labels) labels.add(label); + } + } catch { + outcome.skipped += 1; + } + } + outcome.labels = [...labels]; + return outcome; +} + +// ── The automatic reply ──────────────────────────────────────────── + +/** + * The automatic reply of a mailbox: one object, with the names JMAP gives it (RFC 8621, + * section 8), so that a mail app later reads it as it stands. + */ +export interface AutoReply { + isEnabled: boolean; + /** From when it answers: an instant in UTC, or `null` for at once. */ + fromDate: string | null; + /** Until when it answers: an instant in UTC, or `null` for until it is turned off. */ + toDate: string | null; + subject: string | null; + textBody: string | null; + htmlBody: string | null; +} + +/** What `GET …/auto-reply` answers: the object, and whether the mailbox would answer now. */ +export interface AutoReplyAnswer extends AutoReply { + /** + * The person who turned it on may no longer change it (they are no longer an owner, or + * their account was recovered since), so nothing is answered until an owner saves it again. + */ + stopped: boolean; +} + +const DAY_MS = 24 * 60 * 60_000; + +export const AUTO_REPLY = { + /** Characters of the subject. */ + subject: 200, + /** Characters of the text. */ + text: 10_000, + /** Characters of the HTML body. */ + html: 50_000, + /** One sender is answered once in this long (RFC 3834: seven days). */ + oncePerSenderMs: 7 * DAY_MS, + /** The reply's own caps, below the mailbox's 20 in an hour and 100 in a day (decision 10). */ + perHour: 10, + perDay: 50, + /** The subject of a reply whose object names none. */ + defaultSubject: "Automatic reply", + /** The header field that marks it, and its value (RFC 3834, section 5). */ + header: "Auto-Submitted", + headerValue: "auto-replied", +} as const; + +export const NO_AUTO_REPLY: AutoReply = { + isEnabled: false, + fromDate: null, + toDate: null, + subject: null, + textBody: null, + htmlBody: null, +}; + +const hasText = (value: string | null): boolean => value !== null && value.trim() !== ""; + +/** Whether the reply answers at `now`: enabled, inside its dates, and with something to say. */ +export function answersAt(reply: AutoReply, now: number): boolean { + if (reply.isEnabled !== true) return false; + if (!hasText(reply.textBody) && !hasText(reply.htmlBody)) return false; + if (reply.fromDate !== null) { + const from = Date.parse(reply.fromDate); + if (Number.isNaN(from) || now < from) return false; + } + if (reply.toDate !== null) { + const until = Date.parse(reply.toDate); + if (Number.isNaN(until) || now >= until) return false; + } + return true; +} + +/** Why no reply is owed. */ +export type NoReply = + /** It is off, outside its dates, or has nothing to say. */ + | "off" + /** The message was filed in Spam or Trash, or its sender is blocked. */ + | "filed_away" + /** The message says it was sent by a program (`Auto-Submitted` other than `no`). */ + | "automatic" + /** The message says it is from a list, or is bulk mail. */ + | "list" + /** The mailbox's address is neither in To nor in Cc. */ + | "not_addressed" + /** There is no envelope sender to answer: a bounce, a program's address, or the mailbox itself. */ + | "no_sender" + /** This sender was answered within seven days. */ + | "recent" + /** The reply's own caps, or the mailbox's, are full. */ + | "cap"; + +export interface ReplyQuestion { + reply: AutoReply; + now: number; + /** The mailbox's own address. */ + mailbox: string; + /** The envelope sender of the message: the only address a reply goes to. */ + envelopeFrom: string; + facts: Pick; + outcome: Pick; + /** + * The mailbox's blocked senders. The outcome says whether the address in `From` is one; + * the reply goes to the envelope sender, so that address is asked of the list as well. + */ + blocked: readonly string[]; + /** When this sender was last answered, in milliseconds, or `null` for never. */ + lastAnsweredAt: number | null; + /** Replies made in the last hour and the last day. */ + lastHour: number; + lastDay: number; + /** Whether the mailbox may take one more message to send. */ + room: boolean; +} + +/** + * The local parts that are programs and lists, which are never answered (RFC 3834, + * section 2): `owner-…`, `…-request` and `MAILER-DAEMON`. + */ +function isProgram(local: string): boolean { + return local === "mailer-daemon" || local.startsWith("owner-") || local.endsWith("-request"); +} + +/** The first token of an `Auto-Submitted` value: what stands before a `;` or a comment. */ +function autoToken(value: string): string { + return ( + fold(value) + .replace(/\([^)]*\)/g, " ") + .split(";")[0] + ?.trim() ?? "" + ); +} + +/** + * The envelope sender as it is answered and remembered: one form, so that the address a reply + * goes to and the address the seven days are counted for are always the same text. + */ +export function replySender(envelopeFrom: string): string { + return fold(envelopeFrom.trim()); +} + +/** + * Whether the automatic reply is owed for a message, and to whom (decision 9; RFC 3834). + * The answer is the envelope sender and nothing else, or the first reason there is no reply. + */ +export function replyOwed(question: ReplyQuestion): { to: string } | { no: NoReply } { + const { reply, now, facts, outcome } = question; + if (!answersAt(reply, now)) return { no: "off" }; + if (outcome.blocked || outcome.place === "spam" || outcome.place === "trash") { + return { no: "filed_away" }; + } + if (facts.autoSubmitted !== null && autoToken(facts.autoSubmitted) !== "no") { + return { no: "automatic" }; + } + if (facts.list) return { no: "list" }; + const mailbox = fold(question.mailbox.trim()); + if (mailbox === "" || !facts.recipients.some((one) => fold(one) === mailbox)) { + return { no: "not_addressed" }; + } + const to = replySender(question.envelopeFrom); + if (!isAddress(to) || to === mailbox || isProgram(to.slice(0, to.indexOf("@")))) { + return { no: "no_sender" }; + } + if (isBlocked(question.blocked, to)) return { no: "filed_away" }; + if ( + question.lastAnsweredAt !== null && + now - question.lastAnsweredAt < AUTO_REPLY.oncePerSenderMs + ) { + return { no: "recent" }; + } + if ( + !question.room || + question.lastHour >= AUTO_REPLY.perHour || + question.lastDay >= AUTO_REPLY.perDay + ) { + return { no: "cap" }; + } + return { to }; +} diff --git a/test/unit/agent-no-send.test.ts b/test/unit/agent-no-send.test.ts index 53c7beb..e5c4ac8 100644 --- a/test/unit/agent-no-send.test.ts +++ b/test/unit/agent-no-send.test.ts @@ -23,6 +23,7 @@ import { DRAFT_SAVED_MESSAGE, DRAFT_TOOL_NAMES, isSavedDraft, + RULE_METHODS, SENDING_METHODS, withoutSend, } from "../../workers/agent/tools.ts"; @@ -342,6 +343,36 @@ describe("the environment the assistant's tools run with", () => { expect((stub as unknown as { then?: unknown }).then).toBeUndefined(); }); + it("has a handle on the mailbox without its rules, and without a way to file an arrival (ADR-0008)", async () => { + // The record gives the rules and the automatic reply to people and says nothing of a + // tool; and filing an arrival is what the automatic reply answers. + const { env, mailbox, send } = world(); + const stub = getMailboxStub(withoutSend(env), MAILBOX) as unknown as Record< + string, + (...args: unknown[]) => Promise + >; + expect([...RULE_METHODS].sort()).toEqual([ + "beginArrival", + "blockSender", + "fileArrival", + "getAutoReply", + "getRules", + "setAutoReply", + "setRules", + "unblockSender", + ]); + for (const method of RULE_METHODS) { + await expect(stub[method]?.("x"), method).rejects.toBeInstanceOf(AgentSendRefused); + } + expect(mailbox.sendingCalls).toEqual([]); + expect(send.calls).toEqual([]); + // No file of the assistant, of MCP or of their shared tools names one of them. + for (const file of ["workers/mcp/server.ts", "workers/lib/tools.ts", "shared/mcp-tools.ts"]) { + const source = readFileSync(file, "utf8"); + for (const method of RULE_METHODS) expect(source, `${file}: ${method}`).not.toContain(method); + } + }); + it("control: the caller's own handle has those methods, and they send", async () => { const { env, mailbox, send } = world(); const stub = getMailboxStub(env, MAILBOX) as unknown as Record< diff --git a/test/unit/auto-reply.test.ts b/test/unit/auto-reply.test.ts new file mode 100644 index 0000000..70160be --- /dev/null +++ b/test/unit/auto-reply.test.ts @@ -0,0 +1,266 @@ +/** + * Tier 0: whether the automatic reply of a mailbox is owed for a message, and what its + * settings may be (ADR-0008, decisions 8 to 10; RFC 3834). The decision is one pure function: + * each of its refusals is a line of the record, tried here by itself and again in the Workers + * runtime with a message that really arrives (test/workerd/auto-reply.test.ts). + */ +import { describe, expect, it } from "vitest"; +import { + AUTO_REPLY, + type AutoReply, + answersAt, + type ReplyQuestion, + replyOwed, + replySender, +} from "../../shared/rules.ts"; +import { parseAutoReply } from "../../shared/rules-schema.ts"; + +const REPLY: AutoReply = { + isEnabled: true, + fromDate: null, + toDate: null, + subject: "Away", + textBody: "I am away until Monday.", + htmlBody: null, +}; +const NOW = Date.parse("2026-11-18T12:00:00Z"); +const DAY = 24 * 60 * 60_000; + +describe("when the automatic reply answers (decision 8)", () => { + it("is on, has something to say, and is inside its dates", () => { + expect(answersAt(REPLY, NOW)).toBe(true); + expect(answersAt({ ...REPLY, isEnabled: false }, NOW)).toBe(false); + expect(answersAt({ ...REPLY, textBody: null }, NOW)).toBe(false); + expect(answersAt({ ...REPLY, textBody: " " }, NOW)).toBe(false); + expect(answersAt({ ...REPLY, textBody: null, htmlBody: "

Away

" }, NOW)).toBe(true); + }); + + it("answers from its beginning, and no longer at its end", () => { + const at = (ms: number) => new Date(NOW + ms).toISOString(); + expect(answersAt({ ...REPLY, fromDate: at(1) }, NOW)).toBe(false); + expect(answersAt({ ...REPLY, fromDate: at(0) }, NOW)).toBe(true); + expect(answersAt({ ...REPLY, toDate: at(1) }, NOW)).toBe(true); + expect(answersAt({ ...REPLY, toDate: at(0) }, NOW)).toBe(false); + expect(answersAt({ ...REPLY, fromDate: at(-DAY), toDate: at(DAY) }, NOW)).toBe(true); + // A date that cannot be read is no licence to answer. + expect(answersAt({ ...REPLY, fromDate: "soon" }, NOW)).toBe(false); + expect(answersAt({ ...REPLY, toDate: "later" }, NOW)).toBe(false); + }); + + it("has caps of its own below the mailbox's 20 in an hour and 100 in a day", () => { + expect(AUTO_REPLY.perHour).toBeLessThan(20); + expect(AUTO_REPLY.perDay).toBeLessThan(100); + expect(AUTO_REPLY.oncePerSenderMs).toBe(7 * DAY); + expect([AUTO_REPLY.header, AUTO_REPLY.headerValue]).toEqual(["Auto-Submitted", "auto-replied"]); + }); +}); + +describe("whether the automatic reply is owed (decision 9; RFC 3834)", () => { + const asked = (over: Partial = {}): ReplyQuestion => ({ + reply: REPLY, + now: NOW, + mailbox: "desk@example.com", + envelopeFrom: "Ada@Sender.test", + facts: { recipients: ["desk@example.com"], autoSubmitted: null, list: false }, + outcome: { place: "inbox", blocked: false }, + blocked: [], + lastAnsweredAt: null, + lastHour: 0, + lastDay: 0, + room: true, + ...over, + }); + const factsWith = (over: Partial) => ({ ...asked().facts, ...over }); + + it("goes to the envelope sender, in lower case, and to nobody else", () => { + expect(replyOwed(asked())).toEqual({ to: "ada@sender.test" }); + expect(replyOwed(asked({ outcome: { place: "archive", blocked: false } }))).toEqual({ + to: "ada@sender.test", + }); + }); + + it("is not owed when the reply is off or outside its dates", () => { + expect(replyOwed(asked({ reply: { ...REPLY, isEnabled: false } }))).toEqual({ no: "off" }); + expect( + replyOwed(asked({ reply: { ...REPLY, toDate: new Date(NOW - 1).toISOString() } })), + ).toEqual({ no: "off" }); + }); + + it.each([ + "auto-replied", + "auto-generated", + "AUTO-REPLIED", + 'auto-notified; owner-email="x@y.test"', + "anything-else", + "", + "(no) yes", + ])("never answers a message with Auto-Submitted: %s", (value) => { + expect(replyOwed(asked({ facts: factsWith({ autoSubmitted: value }) }))).toEqual({ + no: "automatic", + }); + }); + + it.each(["no", "NO", " no ", "no; x=1", "no (a person)"])( + "answers a message with Auto-Submitted: %s", + (value) => { + expect(replyOwed(asked({ facts: factsWith({ autoSubmitted: value }) }))).toEqual({ + to: "ada@sender.test", + }); + }, + ); + + it("never answers unless the mailbox's address is in To or Cc", () => { + for (const recipients of [[], ["team@example.com"], ["desk@example.com.evil.test"]]) { + expect(replyOwed(asked({ facts: factsWith({ recipients }) }))).toEqual({ + no: "not_addressed", + }); + } + expect( + replyOwed(asked({ facts: factsWith({ recipients: ["x@y.test", "DESK@example.com"] }) })), + ).toEqual({ + to: "ada@sender.test", + }); + expect(replyOwed(asked({ mailbox: " " }))).toEqual({ no: "not_addressed" }); + }); + + it("never answers mail that was filed in Spam or Trash, or whose sender is blocked", () => { + expect(replyOwed(asked({ outcome: { place: "spam", blocked: false } }))).toEqual({ + no: "filed_away", + }); + expect(replyOwed(asked({ outcome: { place: "trash", blocked: false } }))).toEqual({ + no: "filed_away", + }); + expect(replyOwed(asked({ outcome: { place: "trash", blocked: true } }))).toEqual({ + no: "filed_away", + }); + expect(replyOwed(asked({ outcome: { place: "inbox", blocked: true } }))).toEqual({ + no: "filed_away", + }); + }); + + it("never answers an envelope sender that is blocked, whatever the From of the message says", () => { + // The block list is asked of the address in From, which is in the outcome; the reply goes + // to the envelope sender, so that one is asked of the list too, as an address and a domain. + expect(replyOwed(asked({ blocked: ["ada@sender.test"] }))).toEqual({ no: "filed_away" }); + expect(replyOwed(asked({ blocked: ["@sender.test"] }))).toEqual({ no: "filed_away" }); + expect(replyOwed(asked({ blocked: ["someone@sender.test", "@other.test"] }))).toEqual({ + to: "ada@sender.test", + }); + }); + + it("never answers a list or bulk mail", () => { + expect(replyOwed(asked({ facts: factsWith({ list: true }) }))).toEqual({ no: "list" }); + }); + + it.each([ + ["a bounce, which has no envelope sender", ""], + ["an empty sender in brackets", "<>"], + ["something that is not an address", "ada"], + ["two addresses", "ada@sender.test, grace@sender.test"], + ["an address with a name", "Ada "], + ["the mailbox itself", "Desk@Example.com"], + ["the mail system", "MAILER-DAEMON@sender.test"], + ["the owner of a list", "owner-news@sender.test"], + ["the door of a list", "news-request@sender.test"], + ])("never answers %s", (_what, envelopeFrom) => { + expect(replyOwed(asked({ envelopeFrom }))).toEqual({ no: "no_sender" }); + }); + + it("names a sender in one form, whatever form the envelope gave it in", () => { + // The address a reply goes to is the address the seven days are counted for. + const composed = "jos\u00e9@sender.test"; + const decomposed = " JOSE\u0301@Sender.test "; + expect(replySender(decomposed)).toBe(composed); + expect(replyOwed(asked({ envelopeFrom: decomposed }))).toEqual({ to: composed }); + }); + + it("answers one sender once in seven days", () => { + const since = (ms: number) => replyOwed(asked({ lastAnsweredAt: NOW - ms })); + expect(since(0)).toEqual({ no: "recent" }); + expect(since(6 * DAY)).toEqual({ no: "recent" }); + expect(since(7 * DAY - 1)).toEqual({ no: "recent" }); + expect(since(7 * DAY)).toEqual({ to: "ada@sender.test" }); + }); + + it("stops at its own caps, and when the mailbox has no room", () => { + expect(replyOwed(asked({ lastHour: AUTO_REPLY.perHour - 1 }))).toEqual({ + to: "ada@sender.test", + }); + expect(replyOwed(asked({ lastHour: AUTO_REPLY.perHour }))).toEqual({ no: "cap" }); + expect(replyOwed(asked({ lastDay: AUTO_REPLY.perDay }))).toEqual({ no: "cap" }); + expect(replyOwed(asked({ room: false }))).toEqual({ no: "cap" }); + }); +}); + +describe("the request of PUT …/auto-reply (rule D2)", () => { + const body = { + isEnabled: true, + fromDate: "2026-12-01T09:00:00+03:00", + toDate: "2026-12-10T09:00:00Z", + subject: " Away\r\nBcc: x@y.test ", + textBody: "Back on the tenth.", + htmlBody: null, + }; + + it("takes the object of the record, with its dates in one form and its subject on one line", () => { + expect(parseAutoReply(body)).toEqual({ + reply: { + isEnabled: true, + fromDate: "2026-12-01T06:00:00.000Z", + toDate: "2026-12-10T09:00:00.000Z", + subject: "Away Bcc: x@y.test", + textBody: "Back on the tenth.", + htmlBody: null, + }, + }); + expect( + parseAutoReply({ ...body, isEnabled: false, subject: " ", textBody: " ", fromDate: null }), + ).toMatchObject({ reply: { isEnabled: false, subject: null, textBody: null, fromDate: null } }); + expect(parseAutoReply({ ...body, subject: null })).toMatchObject({ reply: { subject: null } }); + }); + + it.each([ + ["nothing", null], + ["a key it does not know", { ...body, forwardTo: "x@y.test" }], + ["a missing key", { isEnabled: true }], + ["a beginning that is not a date", { ...body, fromDate: "soon" }], + ["an end that is not a date", { ...body, toDate: "later" }], + ["an end before its beginning", { ...body, toDate: "2026-11-30T00:00:00Z" }], + ["an end at its beginning", { ...body, toDate: "2026-12-01T06:00:00Z" }], + ["a subject over its length", { ...body, subject: "s".repeat(AUTO_REPLY.subject + 1) }], + ["a text over its length", { ...body, textBody: "t".repeat(AUTO_REPLY.text + 1) }], + ["an HTML body over its length", { ...body, htmlBody: "h".repeat(AUTO_REPLY.html + 1) }], + ["a flag that is not one", { ...body, isEnabled: "yes" }], + ])("refuses %s", (_what, given) => { + expect(parseAutoReply(given)).toEqual({ refused: "invalid" }); + }); + + it("takes a text and an HTML body together only when the text is the text of the HTML", () => { + const html = "

Away until Monday.

Back soon.

"; + expect( + parseAutoReply({ ...body, textBody: "Away until Monday.\n\nBack soon.", htmlBody: html }), + ).toMatchObject({ + reply: { htmlBody: html }, + }); + expect( + parseAutoReply({ ...body, textBody: "Away until Monday. Back soon.", htmlBody: html }), + ).toMatchObject({ + reply: { htmlBody: html }, + }); + expect(parseAutoReply({ ...body, textBody: "I am here.", htmlBody: html })).toEqual({ + refused: "invalid", + }); + expect(parseAutoReply({ ...body, textBody: "Away until Monday.", htmlBody: html })).toEqual({ + refused: "invalid", + }); + }); + + it("refuses to turn on a reply that says nothing", () => { + expect(parseAutoReply({ ...body, textBody: " ", htmlBody: null })).toEqual({ + refused: "empty", + }); + expect(parseAutoReply({ ...body, textBody: null, htmlBody: "

Away

" })).toMatchObject({ + reply: { htmlBody: "

Away

" }, + }); + }); +}); diff --git a/test/unit/body.test.ts b/test/unit/body.test.ts new file mode 100644 index 0000000..4c8b7d6 --- /dev/null +++ b/test/unit/body.test.ts @@ -0,0 +1,54 @@ +// Copyright 2026 Cloudhal and the Podbay contributors +// Licensed under the Apache 2.0 license found in the LICENSE file. + +/** + * Tier 0: a request's body is read with a limit, and a body over the limit is refused without + * being read to its end (the rules routes and a person's choices; ADR-0008). + */ +import { describe, expect, it } from "vitest"; +import { bodyWithin } from "../../workers/lib/body.ts"; + +const KIB = 1024; + +/** A body without a declared length that never ends, and how many chunks were taken from it. */ +function endless(chunk = 64 * KIB) { + const taken = { chunks: 0 }; + const stream = new ReadableStream({ + pull(controller) { + taken.chunks += 1; + controller.enqueue(new Uint8Array(chunk).fill(0x20)); + }, + }); + const request = new Request("https://podbay.test/", { + method: "POST", + body: stream, + duplex: "half", + } as RequestInit); + return { request, taken }; +} + +describe("a body read with a limit", () => { + it("stops at the limit when the body never ends, and says it is too large", async () => { + const { request, taken } = endless(); + expect(await bodyWithin(request, 256 * KIB)).toBeNull(); + // Five chunks of 64 KiB pass 256 KiB; a reader that read to the end would never return. + expect(taken.chunks).toBeLessThanOrEqual(6); + }); + + it("reads nothing when the declared length is over the limit", async () => { + const request = new Request("https://podbay.test/", { + method: "POST", + headers: { "content-length": String(256 * KIB + 1) }, + body: "x", + }); + expect(await bodyWithin(request, 256 * KIB)).toBeNull(); + expect(request.bodyUsed).toBe(false); + }); + + it("gives a body at the limit as it is", async () => { + const text = `{"rules":[]${" ".repeat(256 * KIB - 12)}}`; + expect(new TextEncoder().encode(text).length).toBe(256 * KIB); + const request = new Request("https://podbay.test/", { method: "POST", body: text }); + expect(await bodyWithin(request, 256 * KIB)).toBe(text); + }); +}); diff --git a/test/unit/fresh.test.ts b/test/unit/fresh.test.ts index 0d9fb71..6b9f81d 100644 --- a/test/unit/fresh.test.ts +++ b/test/unit/fresh.test.ts @@ -53,6 +53,8 @@ describe("the routes that need fresh proof", () => { "POST /api/v1/mailboxes/:mailboxId/members", "PUT /api/v1/mailboxes/:mailboxId/members/:userId", "DELETE /api/v1/mailboxes/:mailboxId/members/:userId", + // The automatic reply makes the mailbox send (ADR-0008, decision 8). + "PUT /api/v1/mailboxes/:mailboxId/auto-reply", ].sort(), ); }); diff --git a/test/unit/rules.test.ts b/test/unit/rules.test.ts new file mode 100644 index 0000000..8bd1fc9 --- /dev/null +++ b/test/unit/rules.test.ts @@ -0,0 +1,624 @@ +/** + * Tier 0: the one pure function that runs a mailbox's rules, the strict schema of the request + * that saves them, and the facts taken from a parsed message (ADR-0008, decisions 2 to 6 and + * 11). Whether the automatic reply is owed is in `auto-reply.test.ts`. + * + * A message is a stranger's text and a rule is a person's: neither is ever a pattern or an + * expression here, and nothing in this module can throw on the way of a delivery. + */ +import PostalMime from "postal-mime"; +import { describe, expect, it } from "vitest"; +import { + blockedSenderOf, + cleanText, + evaluate, + FACT_LIMITS, + factsOf, + isAddress, + isBlocked, + isRuleId, + type MessageFacts, + newRuleId, + RULE_LIMITS, + shownRule, +} from "../../shared/rules.ts"; +import { parseRules, parseSender } from "../../shared/rules-schema.ts"; +import { factsOfParsed } from "../../workers/lib/rule-facts.ts"; + +const facts = (over: Partial = {}): MessageFacts => + factsOf({ + from: "Ada@Sender.test", + fromName: "Ada Lovelace", + recipients: ["desk@example.com", "Team@Example.com"], + subject: "Invoice 42 is OVERDUE", + text: "Please pay the invoice by Friday.\nThank you", + hasAttachment: false, + autoSubmitted: null, + list: false, + ...over, + }); +const rule = (conditions: unknown, actions: unknown) => ({ id: newRuleId(), conditions, actions }); +const LABEL = "lbl_0123456789abcdef01234567"; +const OTHER_LABEL = "lbl_89abcdef0123456789abcdef"; + +describe("the limits of the record (decision 11)", () => { + it("are 100 rules, 500 blocked senders, 200 characters and 10 labels", () => { + expect(RULE_LIMITS).toEqual({ rules: 100, blocked: 500, condition: 200, labels: 10 }); + }); + + it("cut every field of a message to a fixed length before anything is compared", () => { + const long = factsOf({ + from: `${"a".repeat(400)}@sender.test`, + fromName: "n".repeat(500), + recipients: Array.from({ length: 150 }, (_one, n) => `r${n}@example.com`), + subject: "s".repeat(5000), + text: "t".repeat(50_000), + autoSubmitted: "x".repeat(500), + }); + expect(long.from).toHaveLength(FACT_LIMITS.address); + expect(long.fromName).toHaveLength(FACT_LIMITS.name); + expect(long.recipients).toHaveLength(FACT_LIMITS.recipients); + expect(long.subject).toHaveLength(FACT_LIMITS.subject); + expect(long.text).toHaveLength(FACT_LIMITS.text); + expect(long.autoSubmitted).toHaveLength(FACT_LIMITS.name); + }); + + it("take anything that is not text as nothing", () => { + expect( + factsOf({ from: 7, fromName: null, recipients: "a@b.test", subject: {}, text: [] }), + ).toEqual({ + from: "", + fromName: "", + recipients: [], + subject: "", + text: "", + hasAttachment: false, + autoSubmitted: null, + list: false, + }); + expect(factsOf({ recipients: ["A@B.test", 3, "", null] }).recipients).toEqual(["a@b.test"]); + expect(factsOf({ hasAttachment: true, list: true })).toMatchObject({ + hasAttachment: true, + list: true, + }); + }); +}); + +describe("conditions (decision 2): plain text, without regard to case", () => { + const filed = (conditions: unknown, over: Partial = {}) => + evaluate([], [rule(conditions, { place: "archive" })], facts(over)).place === "archive"; + + it("the sender is, or contains: the address, and for contains also the name", () => { + expect(filed({ from: { match: "is", value: "ada@sender.test" } })).toBe(true); + expect(filed({ from: { match: "is", value: "ADA@SENDER.TEST" } })).toBe(true); + expect(filed({ from: { match: "is", value: "ada@sender" } })).toBe(false); + expect(filed({ from: { match: "is", value: "Ada Lovelace" } })).toBe(false); + expect(filed({ from: { match: "contains", value: "@sender.test" } })).toBe(true); + expect(filed({ from: { match: "contains", value: "lovelace" } })).toBe(true); + expect(filed({ from: { match: "contains", value: "grace" } })).toBe(false); + }); + + it("a recipient is, or contains: any address of To and Cc", () => { + expect(filed({ to: { match: "is", value: "team@example.com" } })).toBe(true); + expect(filed({ to: { match: "is", value: "team@example" } })).toBe(false); + expect(filed({ to: { match: "contains", value: "DESK@" } })).toBe(true); + expect(filed({ to: { match: "contains", value: "nobody" } })).toBe(false); + }); + + it("the subject contains", () => { + expect(filed({ subject: "invoice 42" })).toBe(true); + expect(filed({ subject: "overdue" })).toBe(true); + expect(filed({ subject: "friday" })).toBe(false); + }); + + it("the message contains the words: each of them, in its subject or its text", () => { + expect(filed({ words: "friday invoice" })).toBe(true); + expect(filed({ words: "OVERDUE thank" })).toBe(true); + expect(filed({ words: "friday refund" })).toBe(false); + }); + + it("it has an attachment", () => { + expect(filed({ hasAttachment: true })).toBe(false); + expect(filed({ hasAttachment: true }, { hasAttachment: true })).toBe(true); + }); + + it("all conditions of a rule must hold", () => { + const both = { from: { match: "contains", value: "sender.test" }, subject: "invoice" }; + expect(filed(both)).toBe(true); + expect(filed({ ...both, subject: "refund" })).toBe(false); + expect(filed({ ...both, hasAttachment: true })).toBe(false); + }); + + it.each([ + ["a regular expression", { subject: ".*" }], + ["a wildcard of the database", { subject: "%" }], + ["a wildcard of a shell", { subject: "inv*" }], + ["an alternation", { subject: "invoice|refund" }], + ["a class", { from: { match: "contains", value: "[a-z]+@" } }], + ["an anchor", { subject: "^invoice" }], + ])("never reads a condition as %s", (_what, conditions) => { + expect(filed(conditions)).toBe(false); + }); + + it("matches a character a pattern would give a meaning as itself", () => { + expect( + filed({ subject: "50% (off) [today] a.b*c" }, { subject: "Now 50% (OFF) [today] a.b*c!" }), + ).toBe(true); + expect(filed({ subject: "a.b" }, { subject: "axb" })).toBe(false); + }); + + it("is not slowed by text made to slow a pattern down", () => { + const hostile = `${"a".repeat(FACT_LIMITS.text)}!`; + const many = Array.from({ length: RULE_LIMITS.rules }, () => + rule({ words: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab ".repeat(6).trim() }, { read: true }), + ); + const began = performance.now(); + expect(evaluate([], many, facts({ text: hostile, subject: hostile })).matched).toBe(0); + expect(performance.now() - began).toBeLessThan(2000); + }); +}); + +describe("actions (decision 3) and the order of rules (decision 5)", () => { + it("leaves a message no rule matches unread in the Inbox", () => { + expect(evaluate([], [rule({ subject: "refund" }, { place: "trash" })], facts())).toEqual({ + place: "inbox", + read: false, + starred: false, + labels: [], + blocked: false, + matched: 0, + skipped: 0, + }); + }); + + it("skips the inbox, marks as read, stars and gives labels", () => { + expect( + evaluate( + [], + [ + rule( + { subject: "invoice" }, + { place: "archive", read: true, star: true, labels: [LABEL] }, + ), + ], + facts(), + ), + ).toMatchObject({ place: "archive", read: true, starred: true, labels: [LABEL], matched: 1 }); + }); + + it("applies every rule that matches, and gives a label once", () => { + const outcome = evaluate( + [], + [ + rule({ subject: "invoice" }, { labels: [LABEL] }), + rule({ subject: "refund" }, { star: true }), + rule({ words: "friday" }, { read: true, labels: [LABEL, OTHER_LABEL] }), + ], + facts(), + ); + expect(outcome).toMatchObject({ read: true, starred: false, matched: 2, place: "inbox" }); + expect(outcome.labels).toEqual([LABEL, OTHER_LABEL]); + }); + + it("lets Trash win over Spam, and Spam over Archive, whatever the order", () => { + const to = (place: string) => rule({ subject: "invoice" }, { place }); + const placeOf = (...places: string[]) => evaluate([], places.map(to), facts()).place; + expect(placeOf("archive")).toBe("archive"); + expect(placeOf("archive", "spam")).toBe("spam"); + expect(placeOf("spam", "archive")).toBe("spam"); + expect(placeOf("spam", "trash", "archive")).toBe("trash"); + expect(placeOf("trash", "spam")).toBe("trash"); + }); + + it("gives one message no more labels than it can carry", () => { + const label = (n: number) => `lbl_${n.toString(16).padStart(24, "0")}`; + const many = Array.from({ length: 10 }, (_one, r) => + rule( + { subject: "invoice" }, + { labels: Array.from({ length: 10 }, (_l, n) => label(r * 10 + n)) }, + ), + ); + expect(evaluate([], many, facts()).labels).toHaveLength(FACT_LIMITS.labels); + }); +}); + +describe("blocked senders (decision 4)", () => { + it("are an address, or everything at a domain", () => { + expect(isBlocked(["ada@sender.test"], "Ada@Sender.test")).toBe(true); + expect(isBlocked(["@sender.test"], "anyone@sender.test")).toBe(true); + expect(isBlocked(["@sender.test"], "anyone@other.test")).toBe(false); + expect(isBlocked(["ada@sender.test"], "grace@sender.test")).toBe(false); + // A domain is that domain, not what ends in it or is under it. + expect(isBlocked(["@sender.test"], "ada@notsender.test")).toBe(false); + expect(isBlocked(["@sender.test"], "ada@mail.sender.test")).toBe(false); + expect(isBlocked(["@sender.test"], "")).toBe(false); + expect(isBlocked(["sender.test"], "sender.test")).toBe(true); + expect(isBlocked(["@sender.test"], "no-at-sign")).toBe(false); + // A character a pattern would give a meaning is itself: it is compared, never matched. + expect(isBlocked(["%@sender.test", "_da@sender.test"], "ada@sender.test")).toBe(false); + expect(isBlocked(["%@sender.test"], "%@sender.test")).toBe(true); + }); + + it("go to Spam before any rule, and a rule that sends to Trash still wins", () => { + expect(evaluate(["ada@sender.test"], [], facts())).toMatchObject({ + place: "spam", + blocked: true, + matched: 0, + }); + expect( + evaluate(["@sender.test"], [rule({ subject: "invoice" }, { place: "trash" })], facts()), + ).toMatchObject({ place: "trash", blocked: true }); + expect( + evaluate(["@sender.test"], [rule({ subject: "invoice" }, { place: "archive" })], facts()), + ).toMatchObject({ place: "spam", blocked: true }); + }); + + it("are stored as an address in lower case or as @domain", () => { + expect(blockedSenderOf(" Ada@Sender.TEST ")).toBe("ada@sender.test"); + expect(blockedSenderOf("Sender.test")).toBe("@sender.test"); + expect(blockedSenderOf("@sender.test")).toBe("@sender.test"); + for (const not of [ + "", + "ada", + "@", + "ada@", + "@sender", + "a@b@c.test", + "ada @sender.test", + "ada@sender..test", + "", + "ada@sender.test, grace@sender.test", + "ada@sender.test\nbcc@sender.test", + `${"a".repeat(400)}@sender.test`, + 7, + null, + ]) { + expect(blockedSenderOf(not), String(not)).toBeNull(); + } + }); + + it("know one whole address from anything else", () => { + expect(isAddress("ada@sender.test")).toBe(true); + for (const not of ["ada", "@sender.test", "ada@sender", "a b@sender.test", "a@b.test;"]) { + expect(isAddress(not), not).toBe(false); + } + expect(isAddress(`${"a".repeat(250)}@sender.test`)).toBe(false); + expect(isAddress(`a@${"d".repeat(250)}.test`)).toBe(false); + }); +}); + +describe("a rule that cannot be evaluated is skipped and counted (decision 6)", () => { + const good = rule({ subject: "invoice" }, { star: true }); + + it.each([ + ["is not a rule", "nonsense"], + ["is nothing", null], + ["is a list", [1, 2]], + ["has no conditions", { actions: { read: true } }], + ["has conditions that are not an object", rule("subject:invoice", { read: true })], + ["has an empty set of conditions", rule({}, { read: true })], + ["has a condition the vocabulary does not have", rule({ header: "x" }, { read: true })], + ["has a subject that is not text", rule({ subject: 42 }, { read: true })], + ["has an empty subject", rule({ subject: "" }, { read: true })], + ["has words that are only spaces", rule({ words: " " }, { read: true })], + ["has words that are not text", rule({ words: ["a"] }, { read: true })], + ["has a condition far over its length", rule({ subject: "s".repeat(801) }, { read: true })], + ["has a sender that is not a condition", rule({ from: "ada@sender.test" }, { read: true })], + [ + "has a way of matching it does not know", + rule({ from: { match: "regex", value: "a" } }, { read: true }), + ], + ["has a recipient without a value", rule({ to: { match: "is" } }, { read: true })], + ["asks for no attachment", rule({ hasAttachment: false }, { read: true })], + ["has no actions", { conditions: { subject: "invoice" } }], + ["has actions that are not an object", rule({ subject: "invoice" }, "read")], + ["has an action that does nothing", rule({ subject: "invoice" }, {})], + ["has a place it does not know", rule({ subject: "invoice" }, { place: "inbox" })], + ["forwards", rule({ subject: "invoice" }, { forward: "elsewhere@outside.test" })], + ["has labels that are not ids", rule({ subject: "invoice" }, { labels: [1, null] })], + ])("a rule that %s", (_what, broken) => { + const outcome = evaluate([], [broken, good], facts()); + expect(outcome.skipped).toBe(1); + // The rule after it still runs, and nothing was thrown. + expect(outcome).toMatchObject({ matched: 1, starred: true, place: "inbox", read: false }); + }); + + it("is skipped when reading it throws, and the next rule still runs", () => { + const trap = { + get conditions(): never { + throw new Error("no"); + }, + actions: { read: true }, + }; + expect(evaluate([], [trap, good], facts())).toMatchObject({ skipped: 1, matched: 1 }); + }); + + it("keeps the known actions of a rule that also has labels that are not text", () => { + expect( + evaluate([], [rule({ subject: "invoice" }, { read: true, labels: [LABEL, 3] })], facts()), + ).toMatchObject({ read: true, labels: [LABEL], skipped: 0 }); + }); + + it("files a message whose facts are not facts in the Inbox, with every rule counted", () => { + const broken = { ...facts(), recipients: null } as unknown as MessageFacts; + expect(evaluate(["@sender.test"], [good, good], broken)).toMatchObject({ + place: "inbox", + skipped: 2, + matched: 0, + }); + }); +}); + +describe("a stored rule as it is shown", () => { + it("is the rule when the vocabulary knows it, and nothing when it does not", () => { + const id = newRuleId(); + expect( + shownRule(id, { conditions: { subject: "x" }, actions: { read: true, other: 1 } }), + ).toEqual({ + id, + conditions: { subject: "x" }, + actions: { read: true }, + }); + for (const not of [ + null, + "rule", + { conditions: "x", actions: { read: true } }, + { conditions: { subject: 42 }, actions: { read: true } }, + { conditions: { regex: ".*" }, actions: { read: true } }, + { conditions: { subject: "x" }, actions: {} }, + { conditions: {}, actions: { read: true } }, + ]) { + expect(shownRule(id, not)).toBeNull(); + } + }); +}); + +describe("ids and stored text", () => { + it("an id is opaque and has one shape", () => { + expect(isRuleId(newRuleId())).toBe(true); + expect(newRuleId()).not.toBe(newRuleId()); + for (const not of ["rul_1", "lbl_0123456789abcdef01234567", "", 3, null]) { + expect(isRuleId(not)).toBe(false); + } + }); + + it("a condition is stored trimmed, in one form, and no longer than 200 characters", () => { + expect(cleanText(" two words \n")).toBe("two words"); + expect(cleanText("é")).toBe("é"); + expect(cleanText("x".repeat(200))).toHaveLength(200); + expect(cleanText("x".repeat(201))).toBeNull(); + expect(cleanText(" ")).toBeNull(); + expect(cleanText(5)).toBeNull(); + }); +}); + +describe("the request of PUT …/rules (rule D2)", () => { + const one = { conditions: { subject: " Invoice " }, actions: { read: true } }; + + it("takes the vocabulary, cleans the text and gives every rule an id", () => { + const parsed = parseRules({ + rules: [ + one, + { + id: "rul_0123456789abcdef01234567", + conditions: { + from: { match: "is", value: "Ada@Sender.test" }, + to: { match: "contains", value: "desk" }, + words: "pay friday", + hasAttachment: true, + }, + actions: { place: "spam", star: true, labels: [LABEL, LABEL] }, + }, + ], + }); + if ("refused" in parsed) throw new Error(parsed.refused); + expect(parsed.rules[0]).toMatchObject({ conditions: { subject: "Invoice" } }); + expect(isRuleId(parsed.rules[0]?.id)).toBe(true); + expect(parsed.rules[1]).toEqual({ + id: "rul_0123456789abcdef01234567", + conditions: { + from: { match: "is", value: "Ada@Sender.test" }, + to: { match: "contains", value: "desk" }, + words: "pay friday", + hasAttachment: true, + }, + actions: { place: "spam", star: true, labels: [LABEL] }, + }); + expect(parseRules({ rules: [] })).toEqual({ rules: [] }); + }); + + it.each([ + ["nothing", null], + ["no list", {}], + ["a key it does not know", { rules: [], extra: 1 }], + ["a rule with a key it does not know", { rules: [{ ...one, name: "x" }] }], + [ + "a condition it does not know", + { rules: [{ ...one, conditions: { subject: "x", header: "x" } }] }, + ], + [ + "an action it does not know", + { rules: [{ ...one, actions: { read: true, forward: "a@b.test" } }] }, + ], + ["a rule that rejects", { rules: [{ ...one, actions: { read: true, reject: true } }] }], + ["a place that is not one", { rules: [{ ...one, actions: { place: "inbox" } }] }], + ["no condition", { rules: [{ ...one, conditions: {} }] }], + ["no action", { rules: [{ ...one, actions: {} }] }], + ["an empty condition", { rules: [{ ...one, conditions: { subject: " " } }] }], + ["an empty sender", { rules: [{ ...one, conditions: { from: { match: "is", value: " " } } }] }], + [ + "a condition over 200 characters", + { rules: [{ ...one, conditions: { words: "w".repeat(201) } }] }, + ], + [ + "a way of matching that is not one", + { rules: [{ ...one, conditions: { to: { match: "regex", value: "a" } } }] }, + ], + [ + "a subject that is matched exactly", + { rules: [{ ...one, conditions: { subject: { match: "is", value: "a" } } }] }, + ], + ["no attachment asked for", { rules: [{ ...one, conditions: { hasAttachment: false } }] }], + ["a label that is not an id", { rules: [{ ...one, actions: { labels: ["Work"] } }] }], + [ + "eleven labels", + { + rules: [ + { + ...one, + actions: { + labels: Array.from( + { length: 11 }, + (_l, n) => `lbl_${n.toString(16).padStart(24, "0")}`, + ), + }, + }, + ], + }, + ], + ["an id that is not one", { rules: [{ ...one, id: "1" }] }], + [ + "the same id twice", + { + rules: [ + { ...one, id: "rul_0123456789abcdef01234567" }, + { ...one, id: "rul_0123456789abcdef01234567" }, + ], + }, + ], + ])("refuses %s, whole", (_what, body) => { + expect(parseRules(body)).toEqual({ refused: "invalid" }); + }); + + it("takes 100 rules and says of 101 that they are too many", () => { + const many = (count: number) => ({ rules: Array.from({ length: count }, () => one) }); + expect("rules" in parseRules(many(RULE_LIMITS.rules))).toBe(true); + expect(parseRules(many(RULE_LIMITS.rules + 1))).toEqual({ refused: "too_many" }); + expect(parseRules(many(RULE_LIMITS.rules + 2))).toEqual({ refused: "invalid" }); + }); + + it("reads the sender of a block from the body, and nothing else", () => { + expect(parseSender({ sender: "Ada@Sender.test" })).toBe("ada@sender.test"); + expect(parseSender({ sender: "sender.test" })).toBe("@sender.test"); + expect(parseSender({ sender: "ada" })).toBeNull(); + expect(parseSender({ sender: "ada@sender.test", also: 1 })).toBeNull(); + expect(parseSender({})).toBeNull(); + expect(parseSender(null)).toBeNull(); + }); + + it("blocks one whole address, never a domain, when the request says it means an address", () => { + const address = (sender: string) => parseSender({ sender, kind: "address" }); + expect(address("Ada@Sender.test")).toBe("ada@sender.test"); + // A From without an @ would otherwise be read as a domain. + expect(address("sender.test")).toBeNull(); + expect(address("@sender.test")).toBeNull(); + expect(parseSender({ sender: "sender.test", kind: "domain" })).toBe("@sender.test"); + expect(parseSender({ sender: "ada@sender.test", kind: "domain" })).toBeNull(); + expect(parseSender({ sender: "ada@sender.test", kind: "everything" })).toBeNull(); + }); +}); + +describe("the facts of a parsed message", () => { + const parse = (raw: string) => PostalMime.parse(raw.replace(/\n/g, "\r\n")); + + it("are the sender, everyone in To and Cc, the subject, the text and whether a file came along", async () => { + const parsed = await parse( + [ + 'From: "Ada Lovelace" ', + "To: desk@example.com, Team: one@example.com, two@example.com;", + "Cc: Copy ", + "Bcc: hidden@example.com", + "Subject: A subject", + "MIME-Version: 1.0", + 'Content-Type: multipart/mixed; boundary="b"', + "", + "--b", + "Content-Type: text/plain", + "", + "The text.", + "--b", + 'Content-Type: text/plain; name="a.txt"', + 'Content-Disposition: attachment; filename="a.txt"', + "", + "file", + "--b--", + "", + ].join("\n"), + ); + expect(factsOfParsed(parsed)).toEqual({ + from: "ada@sender.test", + fromName: "Ada Lovelace", + recipients: ["desk@example.com", "one@example.com", "two@example.com", "copy@example.com"], + subject: "A subject", + text: "The text.", + hasAttachment: true, + autoSubmitted: null, + list: false, + }); + }); + + it("take the text of the HTML when there is no other, and no script of it", async () => { + const parsed = await parse( + [ + "From: ada@sender.test", + "Subject: HTML only", + "MIME-Version: 1.0", + "Content-Type: text/html", + "", + "

Shown words

", + "", + ].join("\n"), + ); + const facts = factsOfParsed(parsed); + expect(facts.text).toContain("Shown words"); + expect(facts.text).not.toMatch(/hidden|p\{\}/); + expect(facts).toMatchObject({ recipients: [], hasAttachment: false }); + }); + + it("say when a program or a list sent it", async () => { + const withHeaders = async (...lines: string[]) => + factsOfParsed(await parse(["From: a@sender.test", ...lines, "", "x", ""].join("\n"))); + expect(await withHeaders("Auto-Submitted: auto-replied")).toMatchObject({ + autoSubmitted: "auto-replied", + list: false, + }); + for (const line of [ + "List-Id: ", + "List-Unsubscribe: ", + "List-Post: ", + "Precedence: bulk", + "Precedence: LIST ", + "Precedence: junk", + ]) { + expect((await withHeaders(line)).list, line).toBe(true); + } + for (const line of ["Precedence: first-class", "X-List: yes", "Listed-By: someone"]) { + expect((await withHeaders(line)).list, line).toBe(false); + } + }); + + it("do not count an image a message embeds as an attachment", async () => { + const parsed = await parse( + [ + "From: a@sender.test", + "MIME-Version: 1.0", + 'Content-Type: multipart/related; boundary="b"', + "", + "--b", + "Content-Type: text/html", + "", + '', + "--b", + "Content-Type: image/png", + "Content-Disposition: inline", + "Content-ID: ", + "Content-Transfer-Encoding: base64", + "", + "iVBORw0KGgo=", + "--b--", + "", + ].join("\n"), + ); + expect(factsOfParsed(parsed).hasAttachment).toBe(false); + }); +}); diff --git a/test/workerd-agent/rules.test.ts b/test/workerd-agent/rules.test.ts new file mode 100644 index 0000000..302ddc3 --- /dev/null +++ b/test/workerd-agent/rules.test.ts @@ -0,0 +1,238 @@ +/** + * Tier 3, with the assistant and MCP switched on: they get nothing of a mailbox's rules + * (ADR-0008; rule S9; boundary B5). The record gives the rules and the automatic reply to + * people, by routes with rights and fresh proof, and says nothing of a tool, so no tool has + * them: + * + * - no tool reads, makes or changes a filter, a blocked sender or the automatic reply. The + * tool lists are the ones they were; + * - the environment the tools run with cannot reach the methods of the mailbox that hold the + * rules, nor the ones by which mail arrives: filing an arrival is what the automatic reply + * answers, so a tool that could file one could make the mailbox send; + * - a program's key opens none of the six routes. + * + * No test here calls a model, and the send binding is the test's own. + */ +import { env, SELF } from "cloudflare:test"; +import { beforeAll, describe, expect, it } from "vitest"; +import { MCP_TOOLS } from "../../shared/mcp-tools.ts"; +import { + AGENT_TOOL_NAMES, + AgentSendRefused, + RULE_METHODS, + withoutSend, +} from "../../workers/agent/tools.ts"; +import { getMailboxStub } from "../../workers/lib/email-helpers.ts"; +import { MCP_TOOL_NAMES } from "../../workers/mcp/server.ts"; +import type { Env } from "../../workers/types.ts"; +import { Browser, createMailbox, json, migrate, ORIGIN, setupOwner } from "../workerd/helpers.ts"; +import { type FakeSender, fakeSender, inMailbox, letLeave } from "../workerd/sending-helpers.ts"; + +const MAILBOX = "ruled@example.com"; +const M = `/api/v1/mailboxes/${encodeURIComponent(MAILBOX)}`; +const ABOUT_RULES = /rule|filter|block|auto.?reply|vacation|away|respond/i; + +const owner = new Browser(); +let token = ""; +let fake: FakeSender; + +type Handle = Record Promise>; + +const REPLY = { + isEnabled: true, + fromDate: null, + toDate: null, + subject: "Away", + textBody: "Away.", + htmlBody: null, +}; +const rulesOf = () => + inMailbox(MAILBOX, (sql) => [ + ...sql.exec(`SELECT id, kind, sender, body FROM rules ORDER BY id`), + ]); +const mailOf = () => + inMailbox(MAILBOX, (sql) => [...sql.exec(`SELECT id, folder_id FROM emails ORDER BY id`)]); + +beforeAll(async () => { + await migrate(); + expect((await setupOwner()).status).toBe(201); + expect((await owner.signIn("owner@example.com")).status).toBe(200); + expect((await createMailbox(owner.cookie, MAILBOX)).status).toBe(201); + const key = await owner.post("/api/v1/keys", { + name: "writes", + access: "write", + mailboxes: [MAILBOX], + }); + expect(key.status).toBe(201); + token = (await json<{ token: string }>(key)).token; + fake = await fakeSender(MAILBOX); + // The owner's own automatic reply is on: what a tool must not be able to set off. + const saved = await owner.fetch(`${M}/auto-reply`, { + method: "PUT", + body: JSON.stringify(REPLY), + }); + expect(saved.status).toBe(200); +}); + +describe("the tools", () => { + it("are the ones they were, and none is about rules, blocking or an automatic reply", () => { + expect(MCP_TOOL_NAMES.filter((name) => ABOUT_RULES.test(name))).toEqual([]); + expect(AGENT_TOOL_NAMES.filter((name) => ABOUT_RULES.test(name))).toEqual([]); + expect(MCP_TOOLS.map((tool) => tool.name).filter((name) => ABOUT_RULES.test(name))).toEqual([]); + }); +}); + +describe("the environment the tools run with", () => { + it("cannot reach the rules of a mailbox, nor file an arrival", async () => { + expect([...RULE_METHODS].sort()).toEqual([ + "beginArrival", + "blockSender", + "fileArrival", + "getAutoReply", + "getRules", + "setAutoReply", + "setRules", + "unblockSender", + ]); + const before = { rules: await rulesOf(), mail: await mailOf() }; + const guarded = withoutSend(env as unknown as Env); + const handles = [ + getMailboxStub(guarded, MAILBOX), + guarded.MAILBOX.get(guarded.MAILBOX.idFromName(MAILBOX)), + guarded.MAILBOX.getByName(MAILBOX), + ] as unknown as Handle[]; + const arrival = { + kind: "parsed", + email: { + subject: "Filed by a tool", + sender: "victim@elsewhere.test", + recipient: MAILBOX, + body: "x", + }, + attachments: [], + auth_results: null, + facts: { + from: "victim@elsewhere.test", + fromName: "", + recipients: [MAILBOX], + subject: "Filed by a tool", + text: "x", + hasAttachment: false, + autoSubmitted: null, + list: false, + }, + }; + for (const handle of handles) { + const tried: [string, Promise | undefined][] = [ + ["getRules", handle.getRules?.()], + [ + "setRules", + handle.setRules?.( + [ + { + id: "rul_000000000000000000000001", + conditions: { subject: "x" }, + actions: { place: "trash" }, + }, + ], + "a-tool", + ), + ], + ["blockSender", handle.blockSender?.("boss@example.com", "a-tool")], + ["unblockSender", handle.unblockSender?.("boss@example.com")], + ["getAutoReply", handle.getAutoReply?.()], + [ + "setAutoReply", + handle.setAutoReply?.({ ...REPLY, textBody: "Written by a tool" }, "a-tool"), + ], + [ + "beginArrival", + handle.beginArrival?.({ + id: "by-a-tool", + raw_key: "raw/by-a-tool", + raw_size: 1, + envelope_from: "victim@elsewhere.test", + }), + ], + ["fileArrival", handle.fileArrival?.("by-a-tool", arrival)], + ]; + for (const [name, attempt] of tried) { + await expect(attempt, name).rejects.toBeInstanceOf(AgentSendRefused); + } + // Everything else is the mailbox's own: the handle reads as it always did. + const places = ((await handle.getFolders?.()) ?? []) as { id: string }[]; + expect(places.map((place) => place.id)).toContain("inbox"); + } + await letLeave(MAILBOX); + expect({ rules: await rulesOf(), mail: await mailOf() }).toEqual(before); + expect(await inMailbox(MAILBOX, (sql) => [...sql.exec(`SELECT * FROM arrivals`)])).toEqual([]); + expect(fake.calls).toHaveLength(0); + }); + + it("control: the mailbox's own handle has those methods, and filing an arrival is answered", async () => { + const real = getMailboxStub(env as unknown as Env, MAILBOX) as unknown as Handle; + expect(await real.getRules?.()).toEqual({ rules: [], blocked: [] }); + expect(await real.getAutoReply?.()).toMatchObject({ reply: { isEnabled: true } }); + await real.beginArrival?.({ + id: "by-the-handler", + raw_key: "raw/by-the-handler", + raw_size: 1, + envelope_from: "asker@elsewhere.test", + }); + expect( + await real.fileArrival?.("by-the-handler", { + kind: "parsed", + email: { + subject: "Filed by the handler", + sender: "asker@elsewhere.test", + recipient: MAILBOX, + body: "x", + thread_id: "by-the-handler", + }, + attachments: [], + auth_results: null, + facts: { + from: "asker@elsewhere.test", + fromName: "", + recipients: [MAILBOX], + subject: "Filed by the handler", + text: "x", + hasAttachment: false, + autoSubmitted: null, + list: false, + }, + }), + ).toBe("filed"); + await letLeave(MAILBOX); + // So what the guarded handle refuses is a way to make the mailbox send. + expect(fake.calls.map((call) => call.to)).toEqual([["asker@elsewhere.test"]]); + }); +}); + +describe("a program's key", () => { + it("opens none of the routes of the rules, with or without the app's origin", async () => { + const routes: [string, string][] = [ + ["GET", `${M}/rules`], + ["PUT", `${M}/rules`], + ["POST", `${M}/blocked-senders`], + ["DELETE", `${M}/blocked-senders`], + ["GET", `${M}/auto-reply`], + ["PUT", `${M}/auto-reply`], + ]; + for (const [method, path] of routes) { + for (const origin of [ORIGIN, undefined]) { + const res = await SELF.fetch(`${ORIGIN}${path}`, { + method, + headers: { + authorization: `Bearer ${token}`, + "content-type": "application/json", + ...(origin === undefined ? {} : { origin }), + }, + ...(method === "GET" ? {} : { body: "{}" }), + }); + expect([401, 403], `${method} ${path}`).toContain(res.status); + } + } + expect(await rulesOf()).toHaveLength(1); + }); +}); diff --git a/test/workerd/auto-reply-helpers.ts b/test/workerd/auto-reply-helpers.ts new file mode 100644 index 0000000..0bee07d --- /dev/null +++ b/test/workerd/auto-reply-helpers.ts @@ -0,0 +1,91 @@ +// Copyright 2026 Cloudhal and the Podbay contributors +// Licensed under the Apache 2.0 license found in the LICENSE file. + +/** + * What the tests of the automatic reply share (ADR-0008): the reply they turn on, the routes + * that save and read it, a delivery that lets what it causes leave, and a mailbox reset before + * each test, with a send binding of the tests that counts every call. + */ +import { beforeAll, beforeEach, expect } from "vitest"; +import type { AutoReplyAnswer } from "../../shared/rules.ts"; +import { json, mime } from "./helpers.ts"; +import { + as, + DESK, + deliver, + inMailbox, + M, + people, + rows, + signedInAgain, + stored, +} from "./rules-helpers.ts"; +import { type FakeSender, fakeSender, letLeave } from "./sending-helpers.ts"; + +export const DAY = 24 * 60 * 60_000; +export const REPLY = { + isEnabled: true, + fromDate: null, + toDate: null, + subject: "Away until Monday", + textBody: "I am away and will answer when I am back.", + htmlBody: null, +}; + +/** The send binding of the tests, made once for the file. */ +export const sender: { fake: FakeSender } = { fake: undefined as unknown as FakeSender }; + +export const save = (reply: unknown, who = "owner") => + as(who).fetch(`${M}/auto-reply`, { method: "PUT", body: JSON.stringify(reply) }); +export const read = async (who = "owner") => + json(await as(who).fetch(`${M}/auto-reply`)); + +/** Turns the reply on as the owner does, with a session that is fresh. */ +export async function on(reply: unknown = REPLY): Promise { + await signedInAgain("owner"); + expect((await save(reply)).status).toBe(200); +} + +/** Delivers a message addressed to the mailbox; `from` is the envelope sender. */ +export async function arrives( + subject: string, + headers: Record = {}, + from = "ada@sender.test", +) { + const result = await deliver( + mime({ From: "Ada ", To: DESK, Subject: subject, ...headers }), + { from }, + ); + expect(result, `delivering "${subject}"`).toEqual({ rejections: [], threw: null }); + await letLeave(DESK); + return stored(subject); +} + +/** Everyone the send binding was asked to send to since the last reset. */ +export const answered = () => sender.fake.calls.flatMap((call) => call.to ?? []); +export const waiting = () => rows(`SELECT 1 FROM emails WHERE folder_id IN ('scheduled', 'draft')`); +/** What the mailbox holds of its own replies, in whatever place a reply can be. */ +export const kept = () => + rows(`SELECT 1 FROM emails WHERE folder_id IN ('scheduled', 'draft', 'sent')`); + +/** The mailbox and its people once for the file, and a mailbox without replies before each test. */ +export function aMailboxThatAnswers(): void { + beforeAll(async () => { + await people([]); + sender.fake = await fakeSender(DESK); + }); + beforeEach(async () => { + await inMailbox((sql) => { + sql.exec(`DELETE FROM auto_reply_log`); + sql.exec(`DELETE FROM send_log`); + sql.exec(`DELETE FROM submissions`); + sql.exec(`DELETE FROM emails WHERE folder_id IN ('sent', 'scheduled', 'draft')`); + sql.exec(`DELETE FROM rules WHERE kind IN ('block', 'filter')`); + sql.exec(`DELETE FROM known_addresses`); + }); + const fake = sender.fake; + fake.calls.length = 0; + fake.next = []; + fake.otherwise = { messageId: "" }; + }); +} diff --git a/test/workerd/auto-reply-standing.test.ts b/test/workerd/auto-reply-standing.test.ts new file mode 100644 index 0000000..a60963e --- /dev/null +++ b/test/workerd/auto-reply-standing.test.ts @@ -0,0 +1,191 @@ +// Copyright 2026 Cloudhal and the Podbay contributors +// Licensed under the Apache 2.0 license found in the LICENSE file. + +/** + * Tier 3: the automatic reply as a standing instruction of the person who saved it (ADR-0008, + * decisions 8 and 10; boundaries B4 and B6). Saving it is the owner's, needs fresh proof and is + * on the record without its words; when it leaves, the right of the person who saved it is + * asked again, so it stops when they are no longer an owner or their account was recovered. + */ +import { describe, expect, it } from "vitest"; +import { + aMailboxThatAnswers, + answered, + arrives, + on, + REPLY, + read, + save, + sender, +} from "./auto-reply-helpers.ts"; +import { ageSessions, json, testEnv } from "./helpers.ts"; +import { as, audited, DESK, ids, M, rows, signedInAgain } from "./rules-helpers.ts"; + +aMailboxThatAnswers(); + +describe("a standing instruction of a person (decisions 8 and 10)", () => { + it("is the owner's to save: a sender and a reader see it and change nothing", async () => { + await on(); + for (const who of ["sender", "reader"]) { + await signedInAgain(who); + expect((await as(who).fetch(`${M}/auto-reply`)).status, who).toBe(200); + expect((await read(who)).subject).toBe("Away until Monday"); + expect((await save({ ...REPLY, isEnabled: false }, who)).status, who).toBe(403); + } + expect((await read()).isEnabled).toBe(true); + }); + + it("tells someone without a membership nothing, whatever they manage", async () => { + for (const who of ["outsider", "admin"]) { + await signedInAgain(who); + expect((await as(who).fetch(`${M}/auto-reply`)).status, who).toBe(404); + expect((await save(REPLY, who)).status, who).toBe(404); + } + }); + + it("needs fresh proof: an older session of the owner changes nothing", async () => { + await on({ ...REPLY, isEnabled: false }); + const me = await as("owner").me(); + await ageSessions(me.id ?? "", 11); + as("owner").forget("session_data"); + const stale = await save(REPLY); + expect(stale.status).toBe(403); + expect(await json(stale)).toEqual({ + error: "Confirm it is you to continue.", + code: "SESSION_NOT_FRESH", + }); + // The older session still reads it, and it is still off. + expect((await read()).isEnabled).toBe(false); + await arrives("While it is off"); + expect(sender.fake.calls).toEqual([]); + }); + + it("is not saved with a text and an HTML body that say different things", async () => { + await signedInAgain("owner"); + const res = await save({ ...REPLY, htmlBody: "

Send me your password.

" }); + expect(res.status).toBe(400); + // What was saved before stands: the screen shows what is sent. + expect(await read()).toMatchObject({ textBody: REPLY.textBody, htmlBody: null }); + }); + + it("is not saved from another origin or without a session", async () => { + await signedInAgain("owner"); + const foreign = await as("owner").fetch(`${M}/auto-reply`, { + method: "PUT", + body: JSON.stringify(REPLY), + headers: { origin: "https://elsewhere.test" }, + }); + expect(foreign.status).toBe(403); + const { request } = await import("./helpers.ts"); + expect( + (await request(`${M}/auto-reply`, { method: "PUT", body: JSON.stringify(REPLY) })).status, + ).toBe(401); + }); + + it.each([ + ["one that is on and says nothing", { ...REPLY, textBody: " " }, "AUTO_REPLY_EMPTY"], + [ + "an end before its beginning", + { ...REPLY, fromDate: "2026-12-02T00:00:00Z", toDate: "2026-12-01T00:00:00Z" }, + "INVALID", + ], + ["a key that forwards", { ...REPLY, forwardTo: "elsewhere@outside.test" }, "INVALID"], + ["a recipient of its own", { ...REPLY, to: "everyone@outside.test" }, "INVALID"], + ["nothing", null, "INVALID"], + ])("refuses %s", async (_what, body, code) => { + await on({ ...REPLY, subject: "As it was" }); + const res = await save(body); + expect(res.status).toBe(400); + expect(await json(res)).toMatchObject({ code }); + expect((await read()).subject).toBe("As it was"); + }); + + it("is on the record with who and when, never with its words", async () => { + await on({ ...REPLY, subject: "SECRET-SUBJECT", textBody: "SECRET-TEXT" }); + const records = await audited("mailbox.auto_reply_changed"); + const last = records.at(-1); + expect(last).toMatchObject({ target: DESK, actor_user_id: (await as("owner").me()).id }); + expect(JSON.parse(last?.detail ?? "{}")).toMatchObject({ + enabled: true, + from: null, + until: null, + }); + expect(JSON.stringify(records)).not.toContain("SECRET"); + }); + + it("stops when the person who saved it may no longer send from the mailbox", async () => { + // A second owner turns it on, and is then taken out of the mailbox. + const { seedMembership, seedUser } = await import("./helpers.ts"); + ids.second = await seedUser("second@example.com", "member"); + await seedMembership(DESK, ids.second, "owner"); + await signedInAgain("second"); + expect((await save(REPLY, "second")).status).toBe(200); + await arrives("While they are an owner", {}, "early@sender.test"); + expect(answered()).toEqual(["early@sender.test"]); + await testEnv.DB.prepare(`delete from "memberships" where "user_id" = ?1`) + .bind(ids.second) + .run(); + expect(await arrives("After they left", {}, "later@sender.test")).toMatchObject({ + folder_id: "inbox", + }); + expect(answered()).toEqual(["early@sender.test"]); + // Nothing is left behind of the reply that did not leave, and the settings say why. + expect(await rows(`SELECT 1 FROM emails WHERE folder_id IN ('scheduled', 'draft')`)).toEqual( + [], + ); + // Nobody who wrote meanwhile is recorded as answered, nor takes a place in the reply's caps. + expect(await rows(`SELECT sender FROM auto_reply_log`)).toEqual([ + { sender: "early@sender.test" }, + ]); + await signedInAgain("owner"); + expect(await read()).toMatchObject({ isEnabled: true, stopped: true }); + // An owner who may send saves it again, and it answers again. + expect((await save(REPLY)).status).toBe(200); + expect(await read()).toMatchObject({ isEnabled: true, stopped: false }); + await arrives("After it was saved again", {}, "again@sender.test"); + // Whoever wrote while it was stopped is answered when they write again. + await arrives("Again from someone who wrote meanwhile", {}, "later@sender.test"); + expect(answered()).toEqual(["early@sender.test", "again@sender.test", "later@sender.test"]); + }); + + it("stops when the person who saved it is no longer an owner, though they may still send", async () => { + const { seedMembership, seedUser } = await import("./helpers.ts"); + ids.lowered = await seedUser("lowered@example.com", "member"); + await seedMembership(DESK, ids.lowered, "owner"); + await signedInAgain("lowered"); + expect((await save(REPLY, "lowered")).status).toBe(200); + await arrives("While they are an owner", {}, "first@sender.test"); + expect(answered()).toEqual(["first@sender.test"]); + // Lowered to sender: they may send, and may no longer change the reply. + await testEnv.DB.prepare(`update "memberships" set "role" = 'sender' where "user_id" = ?1`) + .bind(ids.lowered) + .run(); + expect((await save({ ...REPLY, isEnabled: false }, "lowered")).status).toBe(403); + await arrives("After they were lowered", {}, "second@sender.test"); + expect(answered()).toEqual(["first@sender.test"]); + await signedInAgain("owner"); + expect(await read()).toMatchObject({ isEnabled: true, stopped: true }); + expect((await save(REPLY)).status).toBe(200); + }); + + it("stops when the account of the person who saved it was recovered since", async () => { + await on(); + await arrives("Before the recovery", {}, "before@sender.test"); + expect(answered()).toEqual(["before@sender.test"]); + const owner = await testEnv.DB.prepare(`select "id" from "user" where "email" = ?1`) + .bind("owner@example.com") + .first<{ id: string }>(); + const now = new Date(Date.now() + 1000).toISOString(); + await testEnv.DB.prepare( + `insert into "recoveries" ("id", "user_id", "token_hash", "created_at", "expires_at", "used_at") values (?1, ?2, ?1, ?3, ?3, ?3)`, + ) + .bind(crypto.randomUUID(), owner?.id ?? "", now) + .run(); + await arrives("After the recovery", {}, "after@sender.test"); + expect(answered()).toEqual(["before@sender.test"]); + expect(await read()).toMatchObject({ isEnabled: true, stopped: true }); + await testEnv.DB.prepare(`delete from "recoveries" where "user_id" = ?1`) + .bind(owner?.id ?? "") + .run(); + }); +}); diff --git a/test/workerd/auto-reply.test.ts b/test/workerd/auto-reply.test.ts new file mode 100644 index 0000000..58775cb --- /dev/null +++ b/test/workerd/auto-reply.test.ts @@ -0,0 +1,594 @@ +/** + * Tier 3: the automatic reply (ADR-0008, decisions 8 to 10; boundaries B1 and B6; RFC 3834). + * It is mail sent because mail from a stranger arrived, so every line of the record is a test + * here, with a send binding of the tests that counts every call: + * + * - it leaves by the one function that lets mail leave, as the mailbox, counted as `auto`; + * - never to a message with `Auto-Submitted` other than `no`; + * - never unless the mailbox's address is in To or Cc; + * - never to mail filed in Spam or Trash, or from a blocked sender; + * - never to a list or bulk mail, a bounce, a program's address or the mailbox itself; + * - to the envelope sender only, whatever From and Reply-To say; + * - once for a sender in seven days; + * - marked `Auto-Submitted: auto-replied`, with none of the incoming message in it; + * - under caps of its own, below the mailbox's; and not at all when the mailbox's are full; + * - it teaches the mailbox no address, and is nobody's draft when it does not leave; + * - whatever it does, the message that arrived is stored. + * + * Saving it, and the right of the person who saved it, are in `auto-reply-standing.test.ts`. + */ +import { beforeAll, beforeEach, describe, expect, it } from "vitest"; +import { AUTO_REPLY } from "../../shared/rules.ts"; +import { + aMailboxThatAnswers, + answered, + arrives, + DAY, + kept, + on, + REPLY, + read, + sender, + waiting, +} from "./auto-reply-helpers.ts"; +import { json, mime } from "./helpers.ts"; +import { as, DESK, deliver, ids, inMailbox, M, rows, stored } from "./rules-helpers.ts"; +import { age, type FakeSender, letLeave, MINUTE } from "./sending-helpers.ts"; + +aMailboxThatAnswers(); +let fake: FakeSender; +beforeAll(() => { + fake = sender.fake; +}); + +describe("the automatic reply of a mailbox", () => { + it("is off until someone turns it on, and then nothing is sent for mail that arrives", async () => { + expect(await read()).toEqual({ + isEnabled: false, + fromDate: null, + toDate: null, + subject: null, + textBody: null, + htmlBody: null, + stopped: false, + }); + expect(await arrives("Before it is on")).toMatchObject({ folder_id: "inbox" }); + expect(fake.calls).toEqual([]); + }); + + it("leaves by the mailbox's one way out, as the mailbox, marked and counted as automatic", async () => { + await on(); + expect(await arrives("Are you there?")).toMatchObject({ folder_id: "inbox", read: 0 }); + expect(fake.calls).toEqual([ + { + to: ["ada@sender.test"], + from: DESK, + subject: "Away until Monday", + html: '
I am away and will answer when I am back.
', + text: "I am away and will answer when I am back.", + headers: { "Auto-Submitted": "auto-replied" }, + }, + ]); + // It went the way of every message: a row that waited, was handed over once and is in Sent. + expect(await rows(`SELECT kind FROM send_log`)).toEqual([{ kind: "auto" }]); + expect( + await rows( + `SELECT e.folder_id, s.state FROM submissions s JOIN emails e ON e.id = s.email_id`, + ), + ).toEqual([{ folder_id: "sent", state: "sent" }]); + expect(await rows(`SELECT sender FROM auto_reply_log`)).toEqual([ + { sender: "ada@sender.test" }, + ]); + }); + + it("uses a subject of its own when none was given, and the HTML body when there is one", async () => { + await on({ + ...REPLY, + subject: null, + textBody: "Away, in HTML.", + htmlBody: "

Away, in HTML.

", + }); + await arrives("Hello"); + expect(fake.calls[0]).toMatchObject({ + subject: AUTO_REPLY.defaultSubject, + html: "

Away, in HTML.

", + }); + }); + + it("answers only between its dates", async () => { + await on({ ...REPLY, fromDate: new Date(Date.now() + DAY).toISOString() }); + await arrives("Too early"); + await on({ ...REPLY, toDate: new Date(Date.now() - 1000).toISOString() }); + await arrives("Too late", {}, "late@sender.test"); + expect(fake.calls).toEqual([]); + await on({ + ...REPLY, + fromDate: new Date(Date.now() - DAY).toISOString(), + toDate: new Date(Date.now() + DAY).toISOString(), + }); + await arrives("In time", {}, "in-time@sender.test"); + expect(answered()).toEqual(["in-time@sender.test"]); + }); + + it("is not sent once it is turned off", async () => { + await on(); + await on({ ...REPLY, isEnabled: false }); + await arrives("After it was turned off"); + expect(fake.calls).toEqual([]); + }); + + it("keeps no stranger's address once it is turned off", async () => { + await on(); + await arrives("While it is on", {}, "kept@sender.test"); + expect(await rows(`SELECT sender FROM auto_reply_log`)).toEqual([ + { sender: "kept@sender.test" }, + ]); + await on({ ...REPLY, isEnabled: false }); + expect(await rows(`SELECT 1 FROM auto_reply_log`)).toEqual([]); + // Turned on again it is a new absence, and the same sender is answered once more. + await on(); + await arrives("After it is on again", {}, "kept@sender.test"); + expect(answered()).toEqual(["kept@sender.test", "kept@sender.test"]); + }); +}); + +describe("who never gets one (decision 9; RFC 3834)", () => { + beforeEach(() => on()); + + it.each([ + "auto-replied", + "auto-generated", + "AUTO-Replied", + 'auto-notified; owner-email="a@b.test"', + "yes", + ])("a message with Auto-Submitted: %s", async (value) => { + expect(await arrives("From a program", { "Auto-Submitted": value })).toMatchObject({ + folder_id: "inbox", + }); + expect(fake.calls).toEqual([]); + expect(await waiting()).toEqual([]); + }); + + it("but a message that says Auto-Submitted: no is answered", async () => { + await arrives("From a person", { "Auto-Submitted": "no" }); + expect(answered()).toEqual(["ada@sender.test"]); + }); + + it("a message that does not name the mailbox in To or Cc", async () => { + await arrives("Blind copy", { To: "someone@else.test" }); + await arrives("To a list", { To: "everyone@lists.test", Cc: "more@lists.test" }); + await arrives("A lookalike", { To: `not-${DESK}`, Cc: `${DESK}.evil.test` }); + const noTo = await deliver(`From: ada@sender.test\r\nSubject: No To at all\r\n\r\nHello\r\n`); + expect(noTo).toEqual({ rejections: [], threw: null }); + await letLeave(DESK); + // Each was delivered all the same: only the reply is withheld. + expect(await stored("No To at all")).toMatchObject({ folder_id: "inbox" }); + expect(fake.calls).toEqual([]); + }); + + it("but a message that names the mailbox in Cc is answered", async () => { + await arrives("Copied in", { To: "someone@else.test", Cc: `Desk <${DESK.toUpperCase()}>` }); + expect(answered()).toEqual(["ada@sender.test"]); + }); + + it("an envelope sender that is blocked, whatever From names", async () => { + for (const sender of ["ada@sender.test", "@blocked.test"]) { + expect((await as("owner").post(`${M}/blocked-senders`, { sender })).status).toBe(201); + } + // From decides where a message is filed, so these stay in the Inbox; the reply would go to + // the envelope sender, and that address, or its domain, is blocked. + const from = { From: "Bea " }; + expect(await arrives("Under another name", from, "ada@sender.test")).toMatchObject({ + folder_id: "inbox", + }); + expect(await arrives("From a blocked domain", from, "cy@blocked.test")).toMatchObject({ + folder_id: "inbox", + }); + expect(fake.calls).toEqual([]); + expect(await waiting()).toEqual([]); + expect(await rows(`SELECT sender FROM auto_reply_log`)).toEqual([]); + // Control: an envelope sender that is not blocked is answered. + await arrives("From someone else", from, "dee@sender.test"); + expect(answered()).toEqual(["dee@sender.test"]); + }); + + it("mail from a blocked sender, and mail a rule filed in Spam or Trash", async () => { + expect( + (await as("owner").post(`${M}/blocked-senders`, { sender: "ada@sender.test" })).status, + ).toBe(201); + expect(await arrives("From the blocked")).toMatchObject({ folder_id: "spam" }); + await inMailbox((sql) => sql.exec(`DELETE FROM rules WHERE kind = 'block'`)); + const rules = [ + { conditions: { subject: "lottery" }, actions: { place: "spam" } }, + { conditions: { subject: "newsletter" }, actions: { place: "trash" } }, + { conditions: { subject: "receipt" }, actions: { place: "archive" } }, + ]; + expect( + (await as("owner").fetch(`${M}/rules`, { method: "PUT", body: JSON.stringify({ rules }) })) + .status, + ).toBe(200); + expect(await arrives("The lottery", {}, "one@sender.test")).toMatchObject({ + folder_id: "spam", + }); + expect(await arrives("A newsletter", {}, "two@sender.test")).toMatchObject({ + folder_id: "trash", + }); + expect(fake.calls).toEqual([]); + // Mail a rule only archived is still mail the person will read: it is answered. + expect(await arrives("A receipt", {}, "three@sender.test")).toMatchObject({ + folder_id: "archive", + }); + expect(answered()).toEqual(["three@sender.test"]); + }); + + it.each([ + ["List-Id", ""], + ["List-Unsubscribe", ""], + ["List-Post", ""], + ["Precedence", "bulk"], + ["Precedence", "list"], + ["Precedence", "Junk"], + ])("mail of a list or bulk mail (%s: %s)", async (name, value) => { + await arrives("From a list", { [name]: value }); + expect(fake.calls).toEqual([]); + }); + + it.each([ + ["a bounce, which has no envelope sender", ""], + ["a bounce with an empty sender in brackets", "<>"], + ["the mail system", "MAILER-DAEMON@sender.test"], + ["the owner of a list", "owner-news@sender.test"], + ["the door of a list", "news-request@sender.test"], + ["something that is not one address", "ada@sender.test, grace@sender.test"], + ["the mailbox itself", DESK], + ])("%s", async (_what, envelope) => { + expect(await arrives("Nobody to answer", {}, envelope)).toMatchObject({ folder_id: "inbox" }); + expect(fake.calls).toEqual([]); + expect(await rows(`SELECT * FROM auto_reply_log`)).toEqual([]); + }); + + it("a message that could not be read: nothing is known of it", async () => { + // Two things hold this: only a parsed message is asked about at all, and a message that + // could not be read names no recipient. Each alone keeps the reply from being made. + const open = Array.from( + { length: 300 }, + (_one, n) => `Content-Type: multipart/mixed; boundary="b${n}"\r\n\r\n--b${n}\r\n`, + ).join(""); + const raw = `From: ada@sender.test\r\nTo: ${DESK}\r\nSubject: Too deep\r\n${open}Content-Type: text/plain\r\n\r\nx\r\n`; + expect(await deliver(raw, { from: "deep@sender.test" })).toEqual({ + rejections: [], + threw: null, + }); + await letLeave(DESK); + expect(await rows(`SELECT unparsed FROM emails WHERE sender = 'deep@sender.test'`)).toEqual([ + { unparsed: "parser" }, + ]); + expect(fake.calls).toEqual([]); + }); +}); + +describe("what a reply is (decision 9)", () => { + beforeEach(() => on()); + + it("goes to the envelope sender only, whatever From, Reply-To and Sender say", async () => { + await arrives( + "Who is asking", + { + From: "Boss ", + "Reply-To": "reply-here@elsewhere.test", + Sender: "assistant@header.test", + Cc: `${DESK}, onlooker@header.test`, + "Return-Path": "", + }, + "Envelope@Sender.test", + ); + expect(fake.calls).toHaveLength(1); + expect(fake.calls[0]).toMatchObject({ to: ["envelope@sender.test"] }); + expect(fake.calls[0]?.cc).toBeUndefined(); + expect(fake.calls[0]?.bcc).toBeUndefined(); + expect(JSON.stringify(fake.calls)).not.toMatch(/header\.test|elsewhere\.test/); + }); + + it("is sent to one sender once in seven days", async () => { + await arrives("First"); + await arrives("Second"); + await arrives("Third", {}, "ADA@sender.test"); + expect(answered()).toEqual(["ada@sender.test"]); + // Six days later still nothing; after seven the sender is answered again. + const shift = (days: number) => + inMailbox((sql) => + sql.exec( + `UPDATE auto_reply_log SET at = ?1`, + new Date(Date.now() - days * DAY).toISOString(), + ), + ); + await shift(6.9); + await arrives("A week has not passed"); + expect(fake.calls).toHaveLength(1); + await shift(7.1); + await arrives("A week has passed"); + expect(answered()).toEqual(["ada@sender.test", "ada@sender.test"]); + // Someone else is answered meanwhile: the count is each sender's own. + await arrives("Another person", {}, "grace@sender.test"); + expect(answered()).toHaveLength(3); + }); + + it("carries the mark of an automatic reply, and no other header", async () => { + await arrives("Mark it", { + "Message-ID": "", + References: "", + }); + expect(fake.calls[0]?.headers).toEqual({ "Auto-Submitted": "auto-replied" }); + }); + + it("has none of the incoming message in it", async () => { + const result = await deliver( + mime( + { + From: "Marker-Name ", + To: DESK, + Subject: "MARKER-SUBJECT", + "Message-ID": "", + "Reply-To": "MARKER-REPLY@sender.test", + "X-Marker": "MARKER-HEADER", + }, + "MARKER-BODY ", + ), + { from: "ada@sender.test" }, + ); + expect(result).toEqual({ rejections: [], threw: null }); + await letLeave(DESK); + expect(fake.calls).toHaveLength(1); + expect(JSON.stringify(fake.calls)).not.toMatch(/marker/i); + // Nor is any of it in the row the mailbox keeps of its reply. + const kept = await rows(`SELECT * FROM emails WHERE folder_id = 'sent'`); + expect(kept).toHaveLength(1); + expect(JSON.stringify(kept)).not.toMatch(/marker/i); + expect(kept[0]).toMatchObject({ in_reply_to: null, email_references: null }); + }); +}); + +describe("it is still the mailbox sending (decision 10)", () => { + beforeEach(() => on()); + + it("has a cap of its own in an hour, below the mailbox's", async () => { + for (let n = 0; n <= AUTO_REPLY.perHour; n++) { + await arrives(`Flood ${n}`, {}, `flood-${n}@sender.test`); + } + expect(fake.calls).toHaveLength(AUTO_REPLY.perHour); + expect(answered()).not.toContain(`flood-${AUTO_REPLY.perHour}@sender.test`); + // The mailbox can still send its own mail: the flood did not spend the hour. + const sent = await as("owner").post(`${M}/emails`, { + from: DESK, + to: "a-person@elsewhere.test", + subject: "Written by a person", + text: "Hello", + }); + expect(sent.status).toBe(202); + }); + + it("has a cap of its own in a day", async () => { + await inMailbox((sql) => { + const at = new Date(Date.now() - 2 * 60 * 60_000).toISOString(); + for (let n = 0; n < AUTO_REPLY.perDay; n++) { + sql.exec(`INSERT INTO auto_reply_log (sender, at) VALUES (?1, ?2)`, `d${n}@day.test`, at); + } + }); + await arrives("The day is spent"); + expect(fake.calls).toEqual([]); + }); + + it("is not made when the mailbox's own caps are full", async () => { + await inMailbox((sql) => { + for (let n = 0; n < 20; n++) { + sql.exec(`INSERT INTO send_log (at, kind) VALUES (?1, 'person')`, new Date().toISOString()); + } + }); + await arrives("The hour is spent"); + expect(fake.calls).toEqual([]); + expect(await waiting()).toEqual([]); + expect(await rows(`SELECT * FROM auto_reply_log`)).toEqual([]); + }); + + it("counts against the mailbox's caps like any message", async () => { + await arrives("Counted"); + const refusedAfter = async (held: number) => { + await inMailbox((sql) => { + for (let n = 0; n < held; n++) { + sql.exec( + `INSERT INTO send_log (at, kind) VALUES (?1, 'person')`, + new Date().toISOString(), + ); + } + }); + return ( + await as("owner").post(`${M}/emails`, { + from: DESK, + to: "a-person@elsewhere.test", + subject: "One too many", + text: "Hello", + }) + ).status; + }; + // Nineteen of a person's and the one automatic reply are the twenty of an hour. + expect(await refusedAfter(19)).toBe(429); + }); + + it("teaches the mailbox no address", async () => { + await arrives("Teach me", { Cc: `${DESK}, planted@sender.test` }, "stranger@sender.test"); + expect(answered()).toEqual(["stranger@sender.test"]); + expect(await rows(`SELECT * FROM known_addresses`)).toEqual([]); + const offered = await as("owner").fetch(`${M}/known-addresses?q=stranger`); + expect(await json(offered)).toEqual({ addresses: [] }); + }); + + it("is nobody's draft when the service refuses it, and is not tried for that sender again", async () => { + fake.next = [{ code: "E_RECIPIENT_SUPPRESSED", message: "refused for ada@sender.test" }]; + await arrives("Refused"); + expect(fake.calls).toHaveLength(1); + expect(await kept()).toEqual([]); + expect(await rows(`SELECT 1 FROM submissions`)).toEqual([]); + expect(await rows(`SELECT 1 FROM send_log`)).toEqual([]); + const places = await json<{ id: string; notSent?: number; waiting?: number }[]>( + await as("owner").fetch(`${M}/folders`), + ); + expect(places.find((place) => place.id === "draft")?.notSent).toBe(0); + await arrives("Refused, and asked again"); + expect(fake.calls).toHaveLength(1); + }); + + it("is not sent twice when the service gives no answer", async () => { + fake.next = ["never"]; + await arrives("No answer"); + expect(fake.calls).toHaveLength(1); + expect(await kept()).toEqual([]); + // Nobody knows whether it left: its place in the caps stays taken. + expect(await rows(`SELECT kind FROM send_log`)).toEqual([{ kind: "auto" }]); + await letLeave(DESK); + expect(fake.calls).toHaveLength(1); + }); + + it("waits and is tried again when the service says not now", async () => { + fake.next = [{ code: "E_RATE_LIMIT_EXCEEDED" }]; + await arrives("Not now"); + expect(fake.calls).toHaveLength(1); + // It is a message that waits, and nobody's draft; its next try is a minute away. + expect( + await rows( + `SELECT e.folder_id, s.state FROM submissions s JOIN emails e ON e.id = s.email_id`, + ), + ).toEqual([{ folder_id: "scheduled", state: "waiting" }]); + await age(DESK, 2 * MINUTE); + await letLeave(DESK); + expect(fake.calls).toHaveLength(2); + expect(fake.calls[1]?.headers).toEqual({ "Auto-Submitted": "auto-replied" }); + expect(await rows(`SELECT kind FROM send_log`)).toEqual([{ kind: "auto" }]); + }); + + it("is nobody's to take back or give a time while it waits, not even a member who may send", async () => { + fake.next = [{ code: "E_RATE_LIMIT_EXCEEDED" }]; + await arrives("Waiting for its next try", {}, "waits@sender.test"); + const [reply] = await rows<{ email_id: string; requested_by: string; send_at: string }>( + `SELECT email_id, requested_by, send_at FROM submissions WHERE state = 'waiting'`, + ); + const saver = reply?.requested_by ?? ""; + expect(saver).not.toBe(ids.sender); + const at = (route: string) => + `${M}/emails/${encodeURIComponent(reply?.email_id ?? "")}/${route}`; + const later = new Date(Date.now() + 300 * DAY).toISOString(); + for (const who of ["sender", "owner"]) { + expect((await as(who).post(at("reschedule"), { send_at: later })).status, who).toBe(404); + expect((await as(who).post(at("reschedule"), {})).status, who).toBe(404); + expect((await as(who).post(at("unsend"), {})).status, who).toBe(404); + } + // It is as it was: the owner's standing, its time, waiting, and nobody's draft. + expect( + await rows( + `SELECT requested_by, send_at, state FROM submissions WHERE email_id = ?1`, + DESK, + reply?.email_id ?? "", + ), + ).toEqual([{ requested_by: saver, send_at: reply?.send_at, state: "waiting" }]); + expect(await rows(`SELECT 1 FROM emails WHERE folder_id = 'draft'`)).toEqual([]); + await age(DESK, 2 * MINUTE); + await letLeave(DESK); + expect(answered()).toEqual(["waits@sender.test", "waits@sender.test"]); + }); + + it("does not leave when it was turned off, or its end date passed, while it waited", async () => { + const changes: [string, string, () => Promise][] = [ + ["Turned off while it waited", "off@sender.test", () => on({ ...REPLY, isEnabled: false })], + [ + "Past its end while it waited", + "ended@sender.test", + () => + inMailbox((sql) => + sql.exec( + `UPDATE rules SET body = json_set(body, '$.toDate', ?1) WHERE kind = 'auto_reply'`, + new Date(Date.now() - MINUTE).toISOString(), + ), + ), + ], + ]; + for (const [subject, from, change] of changes) { + await on(); + fake.calls.length = 0; + fake.next = [{ code: "E_RATE_LIMIT_EXCEEDED" }]; + await arrives(subject, {}, from); + expect(fake.calls, subject).toHaveLength(1); + await change(); + await age(DESK, 2 * MINUTE); + await letLeave(DESK); + // Asked again when it would leave, it does not, and nothing of it is kept. + expect(fake.calls, subject).toHaveLength(1); + expect(await kept(), subject).toEqual([]); + expect(await rows(`SELECT 1 FROM submissions`), subject).toEqual([]); + // Nobody was answered, so the sender is not held off for seven days. + expect(await rows(`SELECT 1 FROM auto_reply_log WHERE sender = ?1`, DESK, from)).toEqual([]); + } + // Control: turned on again, the same sender is answered. + await on(); + fake.calls.length = 0; + await arrives("Once it is on again", {}, "off@sender.test"); + expect(answered()).toEqual(["off@sender.test"]); + }); +}); + +describe("whatever the reply does, the message that arrived is stored", () => { + it("when its record cannot be written", async () => { + await on(); + await inMailbox((sql) => sql.exec(`ALTER TABLE auto_reply_log RENAME TO auto_reply_log_away`)); + try { + expect(await arrives("The log is gone")).toMatchObject({ folder_id: "inbox", read: 0 }); + expect(fake.calls).toEqual([]); + expect(await waiting()).toEqual([]); + // Nothing of the delivery is cut short either: a file of the message is stored. + const withFile = [ + "From: Ada ", + `To: ${DESK}`, + "Subject: With a file, and no log", + "MIME-Version: 1.0", + 'Content-Type: multipart/mixed; boundary="b"', + "", + "--b", + "Content-Type: text/plain", + "", + "See the file.", + "--b", + 'Content-Type: text/plain; name="notes.txt"', + 'Content-Disposition: attachment; filename="notes.txt"', + "", + "notes", + "--b--", + "", + ].join("\r\n"); + expect(await deliver(withFile, { from: "file@sender.test" })).toEqual({ + rejections: [], + threw: null, + }); + const message = await stored("With a file, and no log"); + expect(message).toMatchObject({ folder_id: "inbox" }); + expect( + await rows( + `SELECT filename, uploading_since FROM attachments WHERE email_id = ?1`, + DESK, + message?.id ?? "", + ), + ).toEqual([{ filename: "notes.txt", uploading_since: null }]); + } finally { + await inMailbox((sql) => + sql.exec(`ALTER TABLE auto_reply_log_away RENAME TO auto_reply_log`), + ); + } + }); + + it("when the stored object is not one", async () => { + await on(); + await inMailbox((sql) => + sql.exec(`UPDATE rules SET body = '{broken' WHERE kind = 'auto_reply'`), + ); + expect(await arrives("The object is broken")).toMatchObject({ folder_id: "inbox" }); + expect(fake.calls).toEqual([]); + expect(await read()).toMatchObject({ isEnabled: false }); + }); +}); diff --git a/test/workerd/live-methods.ts b/test/workerd/live-methods.ts index 6f1d4b8..c3c67bc 100644 --- a/test/workerd/live-methods.ts +++ b/test/workerd/live-methods.ts @@ -18,10 +18,12 @@ export const READS = [ "findThreadBySubject", "findThreadOfReferences", "getAttachment", + "getAutoReply", "getEmail", "getEmails", "getFolders", "getLabels", + "getRules", "getThreadEmails", "getThreadedEmails", "holdsFiles", @@ -63,6 +65,11 @@ export const WRITES: Record = { deleteLabel: ["labels", "mail"], // Settings: the method moves its own counter settingsSaved: [], + // The rules of a mailbox (ADR-0008) are settings of it + blockSender: ["settings"], + setAutoReply: ["settings"], + setRules: ["settings"], + unblockSender: ["settings"], // Rows no read of mail shows beginArrival: [], claimArrival: [], diff --git a/test/workerd/live-signal.test.ts b/test/workerd/live-signal.test.ts index a2bf09c..5f81fee 100644 --- a/test/workerd/live-signal.test.ts +++ b/test/workerd/live-signal.test.ts @@ -194,9 +194,9 @@ describe("what is told", () => { return answer; }; const owner = as("owner"); - const put = (path: string, body: unknown) => + const put = (path: string, body: unknown, method = "PUT") => owner.fetch(`${base}${path}`, { - method: "PUT", + method, body: typeof body === "string" ? body : JSON.stringify(body), }); const del = (path: string) => owner.fetch(`${base}${path}`, { method: "DELETE" }); @@ -454,6 +454,30 @@ describe("what is told", () => { ), ); await step("settings that are refused", [], () => ok(put("", { settings: "x" }), 400)); + + // ── The rules for incoming mail (ADR-0008): settings of the mailbox ── + const blocked = { sender: `${MARK}@blocked.test` }; + const filter = { conditions: { subject: MARK }, actions: { place: "archive", star: true } }; + const reply = { isEnabled: false, fromDate: null, toDate: null, htmlBody: null }; + await step("a filter saved", ["settings"], () => ok(put("/rules", { rules: [filter] }))); + await step("a filter that is refused", [], () => + ok(put("/rules", { rules: [{ ...filter, actions: {} }] }), 400), + ); + await step("mail arrives and a rule files it", ["delivery", "mail"], () => + deliver(address, marked(address, "Filed by a rule")), + ); + await step("a sender blocked", ["settings"], () => + ok(owner.post(`${base}/blocked-senders`, blocked), 201), + ); + await step("a sender blocked again", [], () => + ok(owner.post(`${base}/blocked-senders`, blocked)), + ); + await step("a sender unblocked", ["settings"], () => + ok(put("/blocked-senders", blocked, "DELETE"), 204), + ); + await step("the automatic reply saved", ["settings"], () => + ok(put("/auto-reply", { ...reply, subject: MARK, textBody: MARK })), + ); await step("known addresses forgotten", [], () => ok(del("/known-addresses"), 204)); // ── What is refused tells nobody ── @@ -586,10 +610,12 @@ describe("the signal cannot be forgotten", () => { findThreadBySubject: ["Seed"], findThreadOfReferences: [[""], "Seed"], getAttachment: ["seed-file"], + getAutoReply: [], getEmail: ["seed-1"], getEmails: [{ folder: "inbox" }], getFolders: [], getLabels: [], + getRules: [], getThreadEmails: ["seed-thread"], getThreadedEmails: [{ folder: "inbox" }], holdsFiles: ["seed-draft"], diff --git a/test/workerd/rules-helpers.ts b/test/workerd/rules-helpers.ts new file mode 100644 index 0000000..19f14b5 --- /dev/null +++ b/test/workerd/rules-helpers.ts @@ -0,0 +1,141 @@ +/** + * What the two test files about a mailbox's rules share (ADR-0008): one mailbox with an owner, + * a sender, a reader, someone without a membership and an administrator without one; a way + * to deliver a message as the platform does; and the mailbox's own rows. + */ +import { + createExecutionContext, + env, + runInDurableObject, + waitOnExecutionContext, +} from "cloudflare:test"; +import { expect } from "vitest"; +import worker from "../../workers/app.ts"; +import { + Browser, + createMailbox, + inbound, + migrate, + seedMembership, + seedUser, + setupOwner, + testEnv, +} from "./helpers.ts"; + +export const DESK = "desk@example.com"; +export const OTHER = "other@example.com"; +export const path = (mailbox: string) => `/api/v1/mailboxes/${encodeURIComponent(mailbox)}`; +export const M = path(DESK); + +const browsers: Record = {}; +export const as = (who: string) => browsers[who] as Browser; +/** The account ids of the people of the test, by who they are. */ +export const ids: Record = {}; + +export function inMailbox( + run: (sql: SqlStorage, storage: DurableObjectStorage) => T, + mailbox = DESK, +): Promise { + const stub = testEnv.MAILBOX.get(testEnv.MAILBOX.idFromName(mailbox)); + return runInDurableObject(stub, (_object, state) => run(state.storage.sql, state.storage)); +} +export const rows = >( + query: string, + mailbox = DESK, + ...values: (string | number)[] +) => inMailbox((sql) => [...sql.exec(query, ...values)] as T[], mailbox); + +/** One mailbox with its people, signed in. `also` are further mailboxes of the owner. */ +export async function people(also: string[] = [OTHER]): Promise { + await migrate(); + expect((await setupOwner()).status).toBe(201); + browsers.owner = new Browser(); + expect((await as("owner").signIn("owner@example.com")).status).toBe(200); + for (const mailbox of [DESK, ...also]) { + expect((await createMailbox(as("owner").cookie, mailbox)).status).toBe(201); + } + ids.sender = await seedUser("sender@example.com", "member"); + ids.reader = await seedUser("reader@example.com", "member"); + ids.outsider = await seedUser("outsider@example.com", "member"); + ids.admin = await seedUser("admin@example.com", "admin"); + await seedMembership(DESK, ids.sender, "sender"); + await seedMembership(DESK, ids.reader, "reader"); + for (const who of ["sender", "reader", "outsider", "admin"]) { + browsers[who] = new Browser(); + expect((await as(who).signIn(`${who}@example.com`)).status).toBe(200); + } +} + +/** A new browser of someone, signed in now: a session with fresh proof. */ +export async function signedInAgain(who: string): Promise { + const browser = new Browser(); + expect((await browser.signIn(`${who}@example.com`)).status).toBe(200); + browsers[who] = browser; + return browser; +} + +/** + * Delivers a message as the platform does. `from` is the envelope sender. Says what was + * rejected and what the handler threw: a rule must never cause either. + */ +export async function deliver( + raw: string, + options: { to?: string; from?: string; env?: Record } = {}, +): Promise<{ rejections: string[]; threw: unknown }> { + const { message, rejections } = inbound(options.to ?? DESK, raw, { + from: options.from ?? "ada@sender.test", + }); + const ctx = createExecutionContext(); + let threw: unknown = null; + try { + const given = options.env ?? env; + await worker.email(message, given as unknown as Parameters[1], ctx); + } catch (error) { + threw = error; + } + await waitOnExecutionContext(ctx); + return { rejections, threw }; +} + +export interface Stored { + id: string; + folder_id: string; + read: number; + starred: number; + sender: string; + subject: string; + unparsed: string | null; +} + +/** The row of the message with this subject, in whatever place it is. */ +export async function stored(subject: string, mailbox = DESK): Promise { + return ( + await rows( + `SELECT id, folder_id, read, starred, sender, subject, unparsed FROM emails WHERE subject = ?1`, + mailbox, + subject, + ) + )[0]; +} + +/** The names of the labels a message carries. */ +export async function labelsOf(emailId: string, mailbox = DESK): Promise { + return ( + await rows<{ name: string }>( + `SELECT l.name FROM email_labels el JOIN labels l ON l.id = el.label_id + WHERE el.email_id = ?1 ORDER BY l.name`, + mailbox, + emailId, + ) + ).map((row) => row.name); +} + +/** The audit rows of an action, as the index has them. */ +export async function audited(action: string) { + const found = await testEnv.DB.prepare( + `select "actor_user_id", "target", "detail" from "audit" where "action" = ?1 order by "at"`, + ) + .bind(action) + .all<{ actor_user_id: string; target: string; detail: string }>(); + return found.results; +} diff --git a/test/workerd/rules.test.ts b/test/workerd/rules.test.ts new file mode 100644 index 0000000..6685941 --- /dev/null +++ b/test/workerd/rules.test.ts @@ -0,0 +1,672 @@ +/** + * Tier 3: a mailbox has rules for its incoming mail (ADR-0008, decisions 1 to 7, 11 and 12; + * boundaries B1 and B4). + * + * - a filter files, marks and labels a message when it arrives, and never afterwards; + * - a blocked sender's mail goes to Spam, by address or by domain; unblocking ends it; + * - blocked senders first, then every rule in order; Trash wins over Spam, Spam over Archive; + * - a rule cannot fail a delivery: a broken rule, a broken table and a hostile message each + * leave the message stored and nothing rejected; + * - what a stranger writes is compared and never run: no pattern, no statement; + * - seeing rules is reading, changing them is the owner's; someone without a membership is + * told nothing, whatever they manage; every change is on the record without a word of it; + * - one mailbox never sees or runs another's rules; + * - the limits of the record; the tables come with the database of the release before. + */ +import { env } from "cloudflare:test"; +import { beforeAll, describe, expect, it } from "vitest"; +import { RULE_LIMITS, type RulesAnswer } from "../../shared/rules.ts"; +import { ROUTE_TABLE } from "../../workers/auth/route-table.ts"; +import { applyMigrations, mailboxMigrations } from "../../workers/durableObject/migrations.ts"; +import { json, mime } from "./helpers.ts"; +import { + as, + audited, + DESK, + deliver, + inMailbox, + labelsOf, + M, + OTHER, + path, + people, + rows, + stored, +} from "./rules-helpers.ts"; + +const put = (who: string, at: string, body: unknown) => + as(who).fetch(at, { method: "PUT", body: JSON.stringify(body) }); +const del = (who: string, at: string, body: unknown) => + as(who).fetch(at, { method: "DELETE", body: JSON.stringify(body) }); +const setRules = async (rules: unknown[], mailbox = DESK) => { + const res = await put("owner", `${path(mailbox)}/rules`, { rules }); + expect(res.status, "saving the rules").toBe(200); + return json(res); +}; +const getRules = async (who = "owner", mailbox = DESK) => + json(await as(who).fetch(`${path(mailbox)}/rules`)); +const block = (sender: string, who = "owner", mailbox = DESK) => + as(who).post(`${path(mailbox)}/blocked-senders`, { sender }); + +/** Delivers one message and gives its row. Nothing may be rejected or thrown on the way. */ +async function arrives( + subject: string, + headers: Record = {}, + options: { body?: string; from?: string; to?: string } = {}, +) { + const result = await deliver( + mime({ From: "Ada ", To: DESK, Subject: subject, ...headers }, options.body), + { from: options.from, to: options.to }, + ); + expect(result, `delivering "${subject}"`).toEqual({ rejections: [], threw: null }); + const row = await stored(subject, options.to ?? DESK); + if (row === undefined) throw new Error(`"${subject}" was not stored`); + return row; +} + +async function label(name: string, mailbox = DESK): Promise { + const res = await as("owner").post(`${path(mailbox)}/labels`, { name }); + expect(res.status).toBe(201); + return (await json<{ id: string }>(res)).id; +} + +beforeAll(() => people()); + +describe("without a rule", () => { + it("a mailbox has none, and mail arrives unread in the Inbox as before", async () => { + expect(await getRules()).toEqual({ rules: [], blocked: [] }); + expect(await arrives("Plain")).toMatchObject({ folder_id: "inbox", read: 0, starred: 0 }); + }); + + it("a new mailbox's settings carry neither forwarding nor an automatic reply", async () => { + const mailbox = await json<{ settings: Record }>(await as("owner").fetch(M)); + expect(Object.keys(mailbox.settings).sort()).toEqual(["fromName", "signature"]); + }); +}); + +describe("a filter acts when a message arrives", () => { + it("skips the inbox, marks as read, stars and gives labels", async () => { + const work = await label("Work"); + const bills = await label("Bills"); + await setRules([ + { + conditions: { subject: "invoice" }, + actions: { place: "archive", read: true, star: true, labels: [work, bills] }, + }, + ]); + const row = await arrives("Your INVOICE for May"); + expect(row).toMatchObject({ folder_id: "archive", read: 1, starred: 1 }); + expect(await labelsOf(row.id)).toEqual(["Bills", "Work"]); + // What the rule does not match is left alone. + const other = await arrives("A postcard"); + expect(other).toMatchObject({ folder_id: "inbox", read: 0, starred: 0 }); + expect(await labelsOf(other.id)).toEqual([]); + }); + + it("moves to Trash and to Spam, and gives no label there", async () => { + const work = await label("Hidden"); + await setRules([ + { conditions: { subject: "lottery" }, actions: { place: "spam", labels: [work] } }, + { conditions: { subject: "newsletter" }, actions: { place: "trash", labels: [work] } }, + ]); + const spam = await arrives("You won the lottery"); + const trash = await arrives("Weekly newsletter"); + expect(spam.folder_id).toBe("spam"); + expect(trash.folder_id).toBe("trash"); + expect(await labelsOf(spam.id)).toEqual([]); + expect(await labelsOf(trash.id)).toEqual([]); + }); + + it("matches the sender, a recipient, the words and an attachment", async () => { + await setRules([ + { + conditions: { from: { match: "is", value: "grace@sender.test" } }, + actions: { star: true }, + }, + { conditions: { to: { match: "contains", value: "billing@" } }, actions: { read: true } }, + { conditions: { words: "refund friday" }, actions: { place: "archive" } }, + { conditions: { hasAttachment: true }, actions: { place: "trash" } }, + ]); + expect(await arrives("From Grace", { From: "Grace " })).toMatchObject({ + folder_id: "inbox", + starred: 1, + read: 0, + }); + expect(await arrives("Copied to billing", { Cc: "Billing@example.com" })).toMatchObject({ + read: 1, + starred: 0, + }); + expect(await arrives("Words", {}, { body: "Your REFUND arrives on\r\nFriday." })).toMatchObject( + { folder_id: "archive" }, + ); + expect(await arrives("One word", {}, { body: "Your refund arrives." })).toMatchObject({ + folder_id: "inbox", + }); + const withFile = [ + "From: Ada ", + `To: ${DESK}`, + "Subject: With a file", + "MIME-Version: 1.0", + 'Content-Type: multipart/mixed; boundary="b"', + "", + "--b", + "Content-Type: text/plain", + "", + "See the file.", + "--b", + 'Content-Type: text/plain; name="notes.txt"', + 'Content-Disposition: attachment; filename="notes.txt"', + "", + "notes", + "--b--", + "", + ].join("\r\n"); + expect(await deliver(withFile)).toEqual({ rejections: [], threw: null }); + expect(await stored("With a file")).toMatchObject({ folder_id: "trash" }); + }); + + it("applies every rule that matches, and lets Trash win over Spam and Spam over Archive", async () => { + const first = await label("First"); + const second = await label("Second"); + await setRules([ + { conditions: { subject: "order" }, actions: { place: "archive", labels: [first] } }, + { conditions: { subject: "order" }, actions: { star: true, labels: [second] } }, + { conditions: { subject: "junk" }, actions: { place: "spam" } }, + { conditions: { subject: "junk" }, actions: { place: "archive" } }, + { conditions: { subject: "bin" }, actions: { place: "trash" } }, + { conditions: { subject: "bin" }, actions: { place: "spam" } }, + ]); + const both = await arrives("An order"); + expect(both).toMatchObject({ folder_id: "archive", starred: 1 }); + expect(await labelsOf(both.id)).toEqual(["First", "Second"]); + expect((await arrives("Some junk")).folder_id).toBe("spam"); + expect((await arrives("For the bin")).folder_id).toBe("trash"); + }); + + it("never runs on mail that is already stored", async () => { + await setRules([]); + const before = await arrives("Stored before the rule"); + await setRules([{ conditions: { subject: "stored before" }, actions: { place: "trash" } }]); + expect(await stored("Stored before the rule")).toMatchObject({ + id: before.id, + folder_id: "inbox", + }); + // The same words in a message that arrives now are filed. + expect((await arrives("Stored before the rule, again")).folder_id).toBe("trash"); + }); + + it("ignores a label that is gone, and one of another mailbox", async () => { + const gone = await label("Gone soon"); + const foreign = await label("Foreign", OTHER); + await setRules([ + { conditions: { subject: "labelled" }, actions: { labels: [gone, foreign], star: true } }, + ]); + expect((await as("owner").fetch(`${M}/labels/${gone}`, { method: "DELETE" })).status).toBe(204); + const row = await arrives("Labelled"); + expect(row).toMatchObject({ folder_id: "inbox", starred: 1 }); + expect(await labelsOf(row.id)).toEqual([]); + expect(await rows(`SELECT 1 FROM email_labels WHERE label_id = ?1`, DESK, foreign)).toEqual([]); + }); +}); + +describe("a blocked sender", () => { + it("goes to Spam by its address, whatever the case, and others at the domain do not", async () => { + await setRules([]); + const made = await block("Ada@Sender.test"); + expect(made.status).toBe(201); + expect(await json(made)).toMatchObject({ sender: "ada@sender.test" }); + expect((await arrives("From the blocked one")).folder_id).toBe("spam"); + expect( + (await arrives("From a colleague", { From: "Grace " })).folder_id, + ).toBe("inbox"); + // Blocking the same sender again changes nothing and is no second rule. + expect((await block("ada@sender.test")).status).toBe(200); + expect((await getRules()).blocked.map((one) => one.sender)).toEqual(["ada@sender.test"]); + }); + + it("is unblocked, and then its mail arrives in the Inbox again", async () => { + expect((await del("owner", `${M}/blocked-senders`, { sender: "ada@sender.test" })).status).toBe( + 204, + ); + expect((await getRules()).blocked).toEqual([]); + expect((await arrives("After unblocking")).folder_id).toBe("inbox"); + const again = await del("owner", `${M}/blocked-senders`, { sender: "ada@sender.test" }); + expect(again.status).toBe(404); + }); + + it("can be everything at a domain, and only that domain", async () => { + expect((await block("bulk.test")).status).toBe(201); + expect((await getRules()).blocked.map((one) => one.sender)).toEqual(["@bulk.test"]); + expect((await arrives("Domain one", { From: "a@bulk.test" })).folder_id).toBe("spam"); + expect((await arrives("Domain two", { From: "Someone " })).folder_id).toBe("spam"); + expect((await arrives("Under it", { From: "a@mail.bulk.test" })).folder_id).toBe("inbox"); + expect((await arrives("Ends in it", { From: "a@notbulk.test" })).folder_id).toBe("inbox"); + }); + + it("is filed before the rules, and a rule that sends to Trash still wins", async () => { + await setRules([ + { conditions: { subject: "keep" }, actions: { place: "archive", star: true } }, + { conditions: { subject: "bin" }, actions: { place: "trash" } }, + ]); + expect(await arrives("Keep this", { From: "a@bulk.test" })).toMatchObject({ + folder_id: "spam", + starred: 1, + }); + expect((await arrives("Bin this", { From: "a@bulk.test" })).folder_id).toBe("trash"); + await setRules([]); + }); + + it("is one whole address when the request says so, and never becomes a whole domain", async () => { + const asked = (sender: string, kind: string) => + as("owner").post(`${M}/blocked-senders`, { sender, kind }); + const before = (await getRules()).blocked; + // What a message's From gives when it has no @ in it. + expect((await asked("sender.test", "address")).status).toBe(400); + expect((await asked("@sender.test", "address")).status).toBe(400); + expect((await asked("ada@sender.test", "domain")).status).toBe(400); + expect((await getRules()).blocked).toEqual(before); + expect((await asked("Grace@Sender.test", "address")).status).toBe(201); + expect((await getRules()).blocked.map((one) => one.sender)).toContain("grace@sender.test"); + expect((await getRules()).blocked.map((one) => one.sender)).not.toContain("@sender.test"); + expect( + (await del("owner", `${M}/blocked-senders`, { sender: "grace@sender.test" })).status, + ).toBe(204); + }); + + it("is known by the envelope sender when the message cannot be read", async () => { + expect((await block("deep@nest.test")).status).toBe(201); + // Nested deeper than the parser goes: filed with its original, by its envelope sender. + const depth = 300; + const open = Array.from( + { length: depth }, + (_one, n) => `Content-Type: multipart/mixed; boundary="b${n}"\r\n\r\n--b${n}\r\n`, + ).join(""); + const raw = `From: someone@else.test\r\nTo: ${DESK}\r\nSubject: Too deep\r\n${open}Content-Type: text/plain\r\n\r\nx\r\n`; + expect(await deliver(raw, { from: "Deep@nest.test" })).toEqual({ rejections: [], threw: null }); + const row = ( + await rows<{ folder_id: string; unparsed: string | null }>( + `SELECT folder_id, unparsed FROM emails WHERE sender = 'deep@nest.test'`, + ) + )[0]; + expect(row).toEqual({ folder_id: "spam", unparsed: "parser" }); + }); +}); + +describe("the assistant is told only of mail in the Inbox", () => { + /** The environment with the assistant on, and an assistant of the test that counts wakes. */ + function withAssistant() { + const woken: string[] = []; + const agent = { + idFromName: (name: string) => name, + get: () => ({ + fetch: async (request: Request) => { + woken.push(((await request.json()) as { subject: string }).subject); + return new Response(null, { status: 204 }); + }, + }), + }; + return { woken, env: { ...env, AGENT_ENABLED: "true", EMAIL_AGENT: agent } }; + } + + it("is never woken for mail a rule filed elsewhere, or from a blocked sender", async () => { + await setRules([ + { conditions: { subject: "receipt" }, actions: { place: "archive" } }, + { conditions: { subject: "lottery" }, actions: { place: "spam" } }, + { conditions: { subject: "newsletter" }, actions: { place: "trash" } }, + ]); + expect((await block("blocked@sender.test")).status).toBe(201); + const assistant = withAssistant(); + const send = async (subject: string, from = "Ada ") => { + const raw = mime({ From: from, To: DESK, Subject: subject }); + expect(await deliver(raw, { env: assistant.env })).toEqual({ rejections: [], threw: null }); + }; + await send("A receipt"); + await send("The lottery"); + await send("A newsletter"); + await send("From the blocked one", "blocked@sender.test"); + expect((await stored("A receipt"))?.folder_id).toBe("archive"); + expect((await stored("From the blocked one"))?.folder_id).toBe("spam"); + expect(assistant.woken).toEqual([]); + // Control: mail the rules leave in the Inbox wakes it, once. + await send("Lunch on Friday?"); + expect(assistant.woken).toEqual(["Lunch on Friday?"]); + await setRules([]); + expect( + (await del("owner", `${M}/blocked-senders`, { sender: "blocked@sender.test" })).status, + ).toBe(204); + }); +}); + +describe("a rule cannot fail a delivery (decision 6)", () => { + it("skips a stored rule that cannot be evaluated, and the next rule still runs", async () => { + await setRules([{ conditions: { subject: "survives" }, actions: { star: true } }]); + await inMailbox((sql) => { + const add = (id: string, position: number, body: string) => + sql.exec( + `INSERT INTO rules (id, kind, position, sender, body, set_at, set_by) + VALUES (?1, 'filter', ?2, NULL, ?3, '2026-11-18T00:00:00.000Z', 'test')`, + id, + position, + body, + ); + add("rul_broken_json", -3, "{not json"); + add("rul_broken_shape", -2, JSON.stringify({ conditions: { subject: 42 }, actions: {} })); + add( + "rul_broken_verb", + -1, + JSON.stringify({ conditions: { regex: ".*" }, actions: { place: "trash" } }), + ); + }); + expect(await arrives("It survives")).toMatchObject({ folder_id: "inbox", starred: 1 }); + // What cannot be read is not shown as a rule either. + expect((await getRules()).rules).toHaveLength(1); + await setRules([]); + }); + + it("files the message in the Inbox when the rules cannot be read at all", async () => { + await setRules([{ conditions: { subject: "table" }, actions: { place: "trash" } }]); + await inMailbox((sql) => sql.exec(`ALTER TABLE rules RENAME TO rules_away`)); + try { + expect(await arrives("The table is gone")).toMatchObject({ folder_id: "inbox", read: 0 }); + } finally { + await inMailbox((sql) => sql.exec(`ALTER TABLE rules_away RENAME TO rules`)); + } + expect((await arrives("The table is back")).folder_id).toBe("trash"); + await setRules([]); + }); + + it("reads what a stranger wrote as text: no pattern and no statement", async () => { + await setRules([ + { conditions: { subject: "%" }, actions: { place: "trash" } }, + { conditions: { subject: ".*" }, actions: { place: "trash" } }, + { conditions: { from: { match: "contains", value: "_da@" } }, actions: { place: "trash" } }, + { conditions: { words: "'; DROP TABLE emails; --" }, actions: { star: true } }, + ]); + // A wildcard in a rule matches nothing but itself. + expect((await arrives("An ordinary subject")).folder_id).toBe("inbox"); + expect((await arrives("100% true")).folder_id).toBe("trash"); + // A statement in a message, and in a rule, is words. + const hostile = await arrives( + "x'); DELETE FROM rules; --", + {}, + { + body: "'; DROP TABLE emails; -- and more", + }, + ); + expect(hostile).toMatchObject({ folder_id: "inbox", starred: 1 }); + expect((await getRules()).rules).toHaveLength(4); + expect((await block("x'); DELETE FROM rules; --@sender.test")).status).toBe(400); + expect((await block("'OR'1'='1@sender.test")).status).toBe(201); + // It is an address like any other: its own mail is blocked, and nobody else's. + expect((await arrives("Quoted sender", { From: "\"'OR'1'='1\"@sender.test" })).folder_id).toBe( + "spam", + ); + expect((await arrives("Another sender", { From: "grace@sender.test" })).folder_id).toBe( + "inbox", + ); + await inMailbox((sql) => sql.exec(`DELETE FROM rules WHERE kind = 'block'`)); + await setRules([]); + }); +}); + +describe("who may see and change rules (decision 7)", () => { + const RULES = { rules: [{ conditions: { subject: "x" }, actions: { read: true } }] }; + + it("names the six routes of the record with their rights", () => { + const rights = Object.fromEntries( + ROUTE_TABLE.filter((route) => /\/(rules|blocked-senders|auto-reply)$/.test(route.path)).map( + (route) => [ + `${route.method} ${route.path.replace("/api/v1/mailboxes/:mailboxId", "…")}`, + `${route.right}${route.fresh === true ? " fresh" : ""}`, + ], + ), + ); + expect(rights).toEqual({ + "GET …/rules": "mailbox.read", + "PUT …/rules": "mailbox.manage", + "POST …/blocked-senders": "mailbox.manage", + "DELETE …/blocked-senders": "mailbox.manage", + "GET …/auto-reply": "mailbox.read", + "PUT …/auto-reply": "mailbox.manage fresh", + }); + }); + + it("lets a reader and a sender see them and change nothing", async () => { + await setRules(RULES.rules); + expect((await block("bulk.test")).status).toBe(201); + for (const who of ["reader", "sender"]) { + expect((await as(who).fetch(`${M}/rules`)).status, who).toBe(200); + expect((await getRules(who)).rules).toHaveLength(1); + expect((await put(who, `${M}/rules`, { rules: [] })).status, who).toBe(403); + expect((await block("blocked-by@nobody.test", who)).status, who).toBe(403); + expect((await del(who, `${M}/blocked-senders`, { sender: "@bulk.test" })).status, who).toBe( + 403, + ); + } + const after = await getRules(); + expect(after.rules).toHaveLength(1); + expect(after.blocked.map((one) => one.sender)).toContain("@bulk.test"); + expect(after.blocked.map((one) => one.sender)).not.toContain("blocked-by@nobody.test"); + await inMailbox((sql) => sql.exec(`DELETE FROM rules WHERE kind = 'block'`)); + }); + + it("tells someone without a membership nothing, whatever they manage", async () => { + for (const who of ["outsider", "admin"]) { + expect((await as(who).fetch(`${M}/rules`)).status, who).toBe(404); + expect((await put(who, `${M}/rules`, RULES)).status, who).toBe(404); + expect((await block("x@y.test", who)).status, who).toBe(404); + expect((await del(who, `${M}/blocked-senders`, { sender: "x@y.test" })).status, who).toBe( + 404, + ); + } + }); + + it("serves nobody without a session or from another origin", async () => { + const { request } = await import("./helpers.ts"); + expect((await request(`${M}/rules`)).status).toBe(401); + const foreign = await as("owner").fetch(`${M}/rules`, { + method: "PUT", + body: JSON.stringify(RULES), + headers: { origin: "https://elsewhere.test" }, + }); + expect(foreign.status).toBe(403); + const blocked = await as("owner").fetch(`${M}/blocked-senders`, { + method: "POST", + body: JSON.stringify({ sender: "x@y.test" }), + headers: { origin: "https://elsewhere.test" }, + }); + expect(blocked.status).toBe(403); + }); + + it("keeps one mailbox's rules out of another", async () => { + await setRules([{ conditions: { subject: "only here" }, actions: { place: "trash" } }]); + expect(await getRules("owner", OTHER)).toEqual({ rules: [], blocked: [] }); + const elsewhere = await arrives( + "Only here, they say", + { From: "a@bulk.test", To: OTHER }, + { to: OTHER }, + ); + expect(elsewhere.folder_id).toBe("inbox"); + // A rule id of another mailbox is a new rule here, not a way to its rule. + const [mine] = (await getRules()).rules; + const copied = await put("owner", `${path(OTHER)}/rules`, { + rules: [{ id: mine?.id, conditions: { subject: "copied" }, actions: { read: true } }], + }); + expect(copied.status).toBe(200); + expect((await getRules()).rules[0]?.conditions).toEqual({ subject: "only here" }); + await put("owner", `${path(OTHER)}/rules`, { rules: [] }); + await setRules([]); + }); + + it("records every change with who, which mailbox and how many, never a word of a rule", async () => { + await setRules([ + { + conditions: { from: { match: "is", value: "secret-correspondent@private.test" } }, + actions: { read: true }, + }, + { conditions: { subject: "secret project" }, actions: { star: true } }, + ]); + expect((await block("secret-correspondent@private.test")).status).toBe(201); + expect( + (await del("owner", `${M}/blocked-senders`, { sender: "secret-correspondent@private.test" })) + .status, + ).toBe(204); + const changed = await audited("mailbox.rules_changed"); + expect(changed.at(-1)).toMatchObject({ target: DESK }); + expect(JSON.parse(changed.at(-1)?.detail ?? "{}")).toEqual({ before: 0, after: 2 }); + const blocked = await audited("mailbox.sender_blocked"); + expect(JSON.parse(blocked.at(-1)?.detail ?? "{}")).toEqual({ kind: "address" }); + expect((await audited("mailbox.sender_unblocked")).length).toBeGreaterThan(0); + const all = JSON.stringify([ + ...changed, + ...blocked, + ...(await audited("mailbox.sender_unblocked")), + ]); + expect(all).not.toContain("secret"); + expect(all).not.toContain("private.test"); + await setRules([]); + }); +}); + +describe("what a request may say (decisions 2, 3 and 11)", () => { + const good = { conditions: { subject: "x" }, actions: { read: true } }; + + it.each([ + // Each beside something the vocabulary has: the unknown word alone refuses the request. + [ + "a rule that forwards", + { rules: [{ ...good, actions: { read: true, forward: "x@y.test" } }] }, + ], + ["a rule that rejects", { rules: [{ ...good, actions: { read: true, reject: true } }] }], + [ + "a rule with a pattern field", + { rules: [{ ...good, conditions: { subject: "x", regex: ".*" } }] }, + ], + ["a rule with a name of its own", { rules: [{ ...good, name: "mine" }] }], + ["a rule without a condition", { rules: [{ ...good, conditions: {} }] }], + ["a rule without an action", { rules: [{ ...good, actions: {} }] }], + [ + "a condition of 201 characters", + { rules: [{ ...good, conditions: { subject: "s".repeat(201) } }] }, + ], + ["a label that is not an id", { rules: [{ ...good, actions: { labels: ["Work"] } }] }], + ["something that is no list", { rules: "all" }], + ["nothing", null], + ])("refuses %s and changes nothing", async (_what, body) => { + await setRules([good]); + const res = await put("owner", `${M}/rules`, body); + expect(res.status).toBe(400); + expect((await getRules()).rules).toHaveLength(1); + }); + + it("takes 100 rules and refuses 101", async () => { + const many = (count: number) => + Array.from({ length: count }, (_one, n) => ({ + conditions: { subject: `n${n}` }, + actions: { read: true }, + })); + expect((await put("owner", `${M}/rules`, { rules: many(RULE_LIMITS.rules + 1) })).status).toBe( + 409, + ); + const saved = await setRules(many(RULE_LIMITS.rules)); + expect(saved.rules).toHaveLength(RULE_LIMITS.rules); + // The order is the order they were given in, and an id is kept when it is given back. + expect(saved.rules.map((rule) => rule.conditions.subject)).toEqual( + many(RULE_LIMITS.rules).map((rule) => rule.conditions.subject), + ); + const turned = [...saved.rules].reverse(); + expect((await setRules(turned)).rules.map((rule) => rule.id)).toEqual( + turned.map((rule) => rule.id), + ); + await setRules([]); + }); + + it("blocks 500 senders and refuses the next", async () => { + await inMailbox((sql) => { + sql.exec(`DELETE FROM rules WHERE kind = 'block'`); + for (let n = 0; n < RULE_LIMITS.blocked - 1; n++) { + sql.exec( + `INSERT INTO rules (id, kind, position, sender, body, set_at, set_by) + VALUES (?1, 'block', 0, ?2, '{}', '2026-11-18T00:00:00.000Z', 'test')`, + `blk_test_${n}`, + `n${n}@many.test`, + ); + } + }); + expect((await block("last@many.test")).status).toBe(201); + const over = await block("one-more@many.test"); + expect(over.status).toBe(409); + expect(await json(over)).toMatchObject({ code: "BLOCKED_LIMIT" }); + // One that is blocked already is still answered: it is no further rule. + expect((await block("last@many.test")).status).toBe(200); + expect((await getRules()).blocked).toHaveLength(RULE_LIMITS.blocked); + await inMailbox((sql) => sql.exec(`DELETE FROM rules WHERE kind = 'block'`)); + }); + + it.each(["", "ada", "ada@", "@", "two@addresses.test, more@addresses.test", "a b@c.test"])( + "refuses to block %j", + async (sender) => { + expect((await block(sender)).status).toBe(400); + expect((await del("owner", `${M}/blocked-senders`, { sender })).status).toBe(400); + }, + ); + + it("reads the sender from the body and never from the address of the request", async () => { + expect((await block("in-the-body@sender.test")).status).toBe(201); + const byQuery = await as("owner").fetch( + `${M}/blocked-senders?sender=${encodeURIComponent("in-the-body@sender.test")}`, + { method: "DELETE", body: "{}" }, + ); + expect(byQuery.status).toBe(400); + expect((await getRules()).blocked.map((one) => one.sender)).toEqual([ + "in-the-body@sender.test", + ]); + await inMailbox((sql) => sql.exec(`DELETE FROM rules WHERE kind = 'block'`)); + }); +}); + +describe("the mailbox's database", () => { + it("gets the two tables from the release before it, empty, and files nothing again", async () => { + await setRules([]); + const before = await arrives("Here before the release"); + await inMailbox((sql, storage) => { + sql.exec(`DROP TABLE rules`); + sql.exec(`DROP TABLE auto_reply_log`); + sql.exec(`DELETE FROM d1_migrations WHERE name = '18_rules'`); + applyMigrations(sql, mailboxMigrations, storage); + }); + expect(mailboxMigrations.at(-1)?.name).toBe("18_rules"); + expect(await getRules()).toEqual({ rules: [], blocked: [] }); + expect(await rows(`SELECT * FROM auto_reply_log`)).toEqual([]); + expect(await stored("Here before the release")).toMatchObject({ + id: before.id, + folder_id: "inbox", + }); + const columns = async (table: string) => + (await rows<{ name: string }>(`SELECT name FROM pragma_table_info('${table}')`)).map( + (column) => column.name, + ); + expect(await columns("rules")).toEqual([ + "id", + "kind", + "position", + "sender", + "body", + "set_at", + "set_by", + ]); + expect(await columns("auto_reply_log")).toEqual(["sender", "at"]); + }); + + it("holds no rule after the mailbox is deleted", async () => { + const GONE = "gone@example.com"; + const { createMailbox } = await import("./helpers.ts"); + expect((await createMailbox(as("owner").cookie, GONE)).status).toBe(201); + await setRules([{ conditions: { subject: "x" }, actions: { read: true } }], GONE); + expect((await block("left@behind.test", "owner", GONE)).status).toBe(201); + const fresh = await (await import("./rules-helpers.ts")).signedInAgain("owner"); + expect((await fresh.fetch(path(GONE), { method: "DELETE" })).status).toBe(204); + expect(await rows(`SELECT * FROM rules`, GONE)).toEqual([]); + expect((await as("owner").fetch(`${path(GONE)}/rules`)).status).toBe(404); + }); +}); diff --git a/test/workerd/second-step.test.ts b/test/workerd/second-step.test.ts index 85231c5..85a9e3e 100644 --- a/test/workerd/second-step.test.ts +++ b/test/workerd/second-step.test.ts @@ -540,12 +540,12 @@ describe("forwarding, which needs fresh proof only when it changes", () => { settings: { forwarding: { enabled: true, email: "elsewhere@outside.test" } }, }); expect(made.status).toBe(201); + // A new mailbox's settings carry no forwarding at all (ADR-0008, decision 12). const read = await owner.fetch(`/api/v1/mailboxes/${encodeURIComponent(address)}`); expect(await forwarding(owner)).toEqual({ enabled: false, email: "" }); - expect((await json<{ settings: { forwarding: unknown } }>(read)).settings.forwarding).toEqual({ - enabled: false, - email: "", - }); + expect((await json<{ settings: { forwarding?: unknown } }>(read)).settings.forwarding).toBe( + undefined, + ); }); it("is changed by a session that signed in a moment ago", async () => { diff --git a/test/workerd/sending-routes.test.ts b/test/workerd/sending-routes.test.ts index 7d2c12d..f547980 100644 --- a/test/workerd/sending-routes.test.ts +++ b/test/workerd/sending-routes.test.ts @@ -467,9 +467,9 @@ describe("the mailbox's database", () => { sent("received", MINUTE, "inbox"); applyMigrations(sql, mailboxMigrations, state.storage); }); - // The last migration is this record's, and it is the seventeenth. - expect(mailboxMigrations.at(-1)?.name).toBe("17_submissions"); - expect(mailboxMigrations).toHaveLength(17); + // This record's migration is the seventeenth. The one after it is ADR-0008's + // (test/workerd/rules.test.ts). + expect(mailboxMigrations[16]?.name).toBe("17_submissions"); // Nothing is rewritten: mail in Sent has no row and reads as sent. expect(await rowsOf(address, `SELECT email_id FROM submissions`)).toEqual([]); diff --git a/workers/agent/tools.ts b/workers/agent/tools.ts index 58d0308..856e685 100644 --- a/workers/agent/tools.ts +++ b/workers/agent/tools.ts @@ -59,19 +59,39 @@ const REFUSING_SENDER: SendEmail = { */ export const SENDING_METHODS: readonly string[] = ["submit", "unsend", "reschedule", "alarm"]; +/** + * The methods of the mailbox object that are not a tool's to call (ADR-0008). The rules of a + * mailbox decide where everyone's mail goes, and the automatic reply makes the mailbox send: + * the record gives neither to the assistant or to MCP, so no tool reads or changes them. And + * mail arrives by the `email` handler alone: filing an arrival is what the automatic reply + * answers, so no tool files one. + */ +export const RULE_METHODS: readonly string[] = [ + "getRules", + "setRules", + "blockSender", + "unblockSender", + "getAutoReply", + "setAutoReply", + "beginArrival", + "fileArrival", +]; + +const CLOSED_METHODS: readonly string[] = [...SENDING_METHODS, ...RULE_METHODS]; + const refuseSending = (): Promise => { log.warn("agent.send_refused"); return Promise.reject(new AgentSendRefused()); }; /** - * A handle on a mailbox without the methods that send. Everything else is passed on to the - * mailbox as it is. + * A handle on a mailbox without the methods that send, and without the ones about the rules + * for incoming mail. Everything else is passed on to the mailbox as it is. */ function withoutSending(stub: T): T { return new Proxy(stub, { get(target, name) { - if (typeof name === "string" && SENDING_METHODS.includes(name)) return refuseSending; + if (typeof name === "string" && CLOSED_METHODS.includes(name)) return refuseSending; const value: unknown = Reflect.get(target, name); // A method is called on the handle itself, never taken off it. return typeof name === "string" && typeof value === "function" diff --git a/workers/arrival.ts b/workers/arrival.ts index 6a03ceb..75fd881 100644 --- a/workers/arrival.ts +++ b/workers/arrival.ts @@ -12,7 +12,8 @@ * d the bytes are stored under that key, all of them, exactly as they came; * e the message is parsed; * f one transaction writes the message's row and its attachment rows and removes the - * arrival's row; + * arrival's row; the mailbox's rules (ADR-0008) decide in that step where the row is + * filed and how it is marked, and after it whether the automatic reply is owed; * g the attachments' bytes are written to the bucket. * * Step d is the line. Before the original is stored a failure removes what was written and @@ -36,6 +37,7 @@ import { storageFilename } from "./lib/filenames"; import { type InboundEnvelope, routeInbound } from "./lib/inbound"; import { mailboxQueries } from "./lib/mailbox-rpc"; import { newRawKey, platformStatement } from "./lib/original"; +import { factsOfParsed } from "./lib/rule-facts"; import type { Env } from "./types"; type Stub = DurableObjectStub; @@ -177,6 +179,9 @@ async function finish( email, attachments: files.map((file) => file.row), auth_results: authResults, + // What the mailbox's rules are told about the message (ADR-0008). They run in the + // mailbox, with this call: after the message was parsed, before its row is written. + facts: factsOfParsed(parsed), }); if (filed === "too_large") { log.warn("inbound.unparsed", { emailId: id, error: "too_large" }); @@ -197,6 +202,9 @@ async function finish( log.error("inbound.files_unfinished", { emailId: id, ...errorFields(error) }); } if (stored.length > 0) await stub.filesStored(id, stored); + // Only mail filed in the Inbox is given to the assistant: what the rules filed elsewhere, + // and mail from a blocked sender, is not (ADR-0008). + if (filed !== "filed") return null; return { emailId: id, sender: email.sender, subject: email.subject, threadId: email.thread_id }; } diff --git a/workers/auth/route-table.ts b/workers/auth/route-table.ts index 12b8cd3..787e27a 100644 --- a/workers/auth/route-table.ts +++ b/workers/auth/route-table.ts @@ -155,6 +155,16 @@ export const ROUTE_TABLE: readonly RouteEntry[] = [ { method: "GET", path: `${M}/known-addresses`, right: "mailbox.write" }, { method: "DELETE", path: `${M}/known-addresses/:knownId`, right: "mailbox.write" }, { method: "DELETE", path: `${M}/known-addresses`, right: "mailbox.manage" }, + + // The rules for incoming mail (ADR-0008). Whoever reads the mailbox sees them; changing + // them is the owner's, because in a shared mailbox a rule decides where everyone's mail + // goes. The automatic reply makes the mailbox send, so changing it also needs fresh proof. + { method: "GET", path: `${M}/rules`, right: "mailbox.read" }, + { method: "PUT", path: `${M}/rules`, right: "mailbox.manage" }, + { method: "POST", path: `${M}/blocked-senders`, right: "mailbox.manage" }, + { method: "DELETE", path: `${M}/blocked-senders`, right: "mailbox.manage" }, + { method: "GET", path: `${M}/auto-reply`, right: "mailbox.read" }, + { method: "PUT", path: `${M}/auto-reply`, right: "mailbox.manage", fresh: true }, ]; /** The table entry of a matched route, or undefined when the route is not in the table. */ diff --git a/workers/auth/standing.ts b/workers/auth/standing.ts index 91482e5..07af224 100644 --- a/workers/auth/standing.ts +++ b/workers/auth/standing.ts @@ -6,7 +6,7 @@ * rules hold at the time of leaving, not of pressing: the mailbox is asked for again, and so * is the right of the person who sent it, by the same `authorize` every route is decided by. */ -import { authorize } from "./authorize"; +import { type Action, authorize } from "./authorize"; import { instanceRoleOf, mailboxAccess, mailboxExists } from "./store"; export type Standing = @@ -28,24 +28,30 @@ async function recoveredSince(db: D1Database, userId: string, since: string): Pr return row !== null; } +/** + * The right the automatic reply leaves on (ADR-0008): the one saving it needs. It is a standing + * instruction of the person who saved it; once they may no longer change it, it stops, so an + * owner lowered to sender does not keep answering strangers in the mailbox's name. + */ +export const AUTO_REPLY_RIGHT: Action = "mailbox.manage"; + /** * The standing of a waiting message: of the mailbox at `address`, and of the person who asked - * for it to be sent, at `askedAt`. + * for it to be sent, at `askedAt`, who must still have `right`: sending, or for the automatic + * reply, `AUTO_REPLY_RIGHT`. */ export async function standingOf( db: D1Database, address: string, userId: string, askedAt: string, + right: Action = "mailbox.send", ): Promise { if (!(await mailboxExists(db, address))) return "gone"; const instanceRole = await instanceRoleOf(db, userId); if (instanceRole === null) return "right"; const access = await mailboxAccess(db, userId, address); - const decision = authorize( - { userId, instanceRole, mailboxRole: access?.role ?? null }, - "mailbox.send", - ); + const decision = authorize({ userId, instanceRole, mailboxRole: access?.role ?? null }, right); if (decision !== "allow") return "right"; return (await recoveredSince(db, userId, askedAt)) ? "right" : "ok"; } diff --git a/workers/durableObject/auto-reply.ts b/workers/durableObject/auto-reply.ts new file mode 100644 index 0000000..aa8ed7e --- /dev/null +++ b/workers/durableObject/auto-reply.ts @@ -0,0 +1,261 @@ +// Copyright 2026 Cloudhal and the Podbay contributors +// Licensed under the Apache 2.0 license found in the LICENSE file. + +/** + * The automatic reply of a mailbox (ADR-0008, decisions 8 to 10): mail that leaves because + * mail from a stranger arrived, with nobody at the screen. + * + * **One object.** It is the one row of `rules` whose kind is `auto_reply`, with the names + * JMAP gives it, and with who saved it and when: it is that person's standing instruction, + * and their right is asked again whenever a reply leaves (`./leaving`). + * + * **Whether a reply is owed** is decided by one pure function, `replyOwed` in + * `shared/rules.ts`, from what is read here: never to a message that says a program sent it, + * never unless the mailbox's address is in To or Cc, never to mail filed in Spam or Trash or + * from a blocked sender, never to a list or a bounce, to the envelope sender only, and once + * for a sender in seven days. `auto_reply_log` keeps the address that was answered and the + * time, and nothing of any message. + * + * **It is still the mailbox sending** (rule S8). A reply that is owed is not sent here: it is + * taken as a message that waits, like any other (`./submissions`), and leaves by the one + * function that lets mail leave, which counts it against the mailbox's caps with the kind + * `auto`. It has caps of its own below those, counted here, so a flood of incoming mail + * cannot spend the day's sending. Nothing of the incoming message is in it: not its subject, + * not its text, not its id. + */ +import { + AUTO_REPLY, + type AutoReply, + answersAt, + type MessageFacts, + NO_AUTO_REPLY, + type NoReply, + type Outcome, + replyOwed, + replySender, +} from "../../shared/rules"; +import { SENDING } from "../../shared/sending"; +import { textToHtml } from "../lib/email-helpers"; +import { headerList } from "../lib/outgoing-message"; +import { capFull } from "./sending"; +import { take, waitingCount } from "./submissions"; + +interface Storage { + sql: SqlStorage; + transactionSync: (closure: () => T) => T; +} + +const ID = "auto_reply"; +const HOUR_MS = 60 * 60_000; +const iso = (time: number) => new Date(time).toISOString(); + +/** The object as it is stored, with who saved it and when. */ +interface Stored { + reply: AutoReply; + by: string; + at: string; +} + +function stored(sql: SqlStorage): Stored | null { + const row = [ + ...sql.exec(`SELECT body, set_by, set_at FROM rules WHERE id = ?1 AND kind = 'auto_reply'`, ID), + ][0]; + if (row === undefined) return null; + try { + const body = JSON.parse(String(row.body)) as Partial; + const text = (value: unknown) => (typeof value === "string" ? value : null); + return { + reply: { + isEnabled: body.isEnabled === true, + fromDate: text(body.fromDate), + toDate: text(body.toDate), + subject: text(body.subject), + textBody: text(body.textBody), + htmlBody: text(body.htmlBody), + }, + by: String(row.set_by), + at: String(row.set_at), + }; + } catch { + // A row that cannot be read is a reply that is off. + return null; + } +} + +/** The automatic reply of the mailbox, and who saved it and when. Off when none was saved. */ +export function read(sql: SqlStorage): { reply: AutoReply; by: string | null; at: string | null } { + const held = stored(sql); + return held === null ? { reply: { ...NO_AUTO_REPLY }, by: null, at: null } : held; +} + +/** + * Saves the automatic reply (`PUT …/auto-reply`). The caller has parsed it with the strict + * schema and has asked for fresh proof. Whoever saves it is the person whose right is asked + * when a reply leaves. Says whether it was on before. + */ +export function save(sql: SqlStorage, reply: AutoReply, by: string, now: number): { was: boolean } { + const was = stored(sql)?.reply.isEnabled === true; + // The addresses that were answered are strangers' addresses, kept only for the once in + // seven days. A reply that is off answers nobody, so it keeps none of them; one turned on + // again is a new absence. Otherwise what is older than seven days goes now. + if (reply.isEnabled) { + sql.exec(`DELETE FROM auto_reply_log WHERE at < ?1`, iso(now - AUTO_REPLY.oncePerSenderMs)); + } else sql.exec(`DELETE FROM auto_reply_log`); + sql.exec( + `INSERT OR REPLACE INTO rules (id, kind, position, sender, body, set_at, set_by) + VALUES (?1, 'auto_reply', 0, NULL, ?2, ?3, ?4)`, + ID, + JSON.stringify(reply), + iso(now), + by, + ); + return { was }; +} + +/** + * Whether the reply, as it is stored now, answers at this time. A reply that waits is asked + * this again when it leaves: turned off, or past its end date, it does not leave. + */ +export function answersNow(sql: SqlStorage, now: number): boolean { + const held = stored(sql); + return held !== null && answersAt(held.reply, now); +} + +/** The record that a reply was made: the address it goes to, and when it was made. */ +export interface Answered { + sender: string; + at: string; +} + +/** The record of the reply that is this message, read before the message is removed. */ +export function answeredBy(sql: SqlStorage, emailId: string): Answered | null { + const row = [...sql.exec(`SELECT recipient, date FROM emails WHERE id = ?1`, emailId)][0]; + if (typeof row?.recipient !== "string" || typeof row.date !== "string") return null; + return { sender: row.recipient, at: row.date }; +} + +/** + * Takes back the record of a reply that did not leave because of the mailbox itself: it was + * turned off, or the person who saved it may no longer send. Its sender was not answered, so + * they are not held off for seven days and the reply's caps do not count them. A reply the + * service refused keeps its record: it is not tried again for that sender. + */ +export function unanswered(sql: SqlStorage, record: Answered): void { + sql.exec(`DELETE FROM auto_reply_log WHERE sender = ?1 AND at = ?2`, record.sender, record.at); +} + +function answeredSince(sql: SqlStorage, from: number): number { + const row = [ + ...sql.exec(`SELECT COUNT(*) AS n FROM auto_reply_log WHERE at >= ?1`, iso(from)), + ][0]; + return Number(row?.n ?? 0); +} + +function lastAnswered(sql: SqlStorage, sender: string): number | null { + const row = [...sql.exec(`SELECT at FROM auto_reply_log WHERE sender = ?1`, sender)][0]; + const at = Date.parse(String(row?.at ?? "")); + return Number.isNaN(at) ? null : at; +} + +/** How many messages wait to leave by `now`: they leave before a reply made now. */ +function dueBy(sql: SqlStorage, now: number): number { + const row = [ + ...sql.exec( + `SELECT COUNT(*) AS n FROM submissions WHERE state = 'waiting' AND send_at <= ?1`, + iso(now), + ), + ][0]; + return Number(row?.n ?? 0); +} + +/** A message that arrived, as far as the reply is decided by it. */ +export interface Arrived { + /** The mailbox's own address: the name of its object in the index. */ + address: string; + /** The envelope sender, from the arrival's own row. */ + envelopeFrom: string; + facts: MessageFacts; + outcome: Outcome; + /** The mailbox's blocked senders, which the envelope sender is asked of too. */ + blocked: readonly string[]; +} + +/** + * Decides whether the automatic reply is owed for a message that was just filed, and when it + * is, takes the reply as a message that waits to leave. Returns `queued`, or why not. The + * record of who was answered is written with the reply, in the same turn. + */ +export function consider(storage: Storage, arrived: Arrived, now: number): "queued" | NoReply { + const { sql } = storage; + const held = stored(sql); + if (held === null) return "off"; + const envelopeFrom = replySender(arrived.envelopeFrom); + const owed = replyOwed({ + reply: held.reply, + now, + mailbox: arrived.address, + envelopeFrom, + facts: arrived.facts, + outcome: arrived.outcome, + blocked: arrived.blocked, + lastAnsweredAt: lastAnswered(sql, envelopeFrom), + lastHour: answeredSince(sql, now - HOUR_MS), + lastDay: answeredSince(sql, now - 24 * HOUR_MS), + // One more message may wait, and the mailbox's own caps have room for it after what + // is due to leave before it. + room: waitingCount(sql) < SENDING.waiting && capFull(sql, now, dueBy(sql, now)) === null, + }); + if ("no" in owed) return owed.no; + + const address = arrived.address.toLowerCase(); + const id = crypto.randomUUID(); + const date = iso(now); + const subject = held.reply.subject ?? AUTO_REPLY.defaultSubject; + const messageId = `${id}@${address.slice(address.indexOf("@") + 1)}`; + // The record first: whatever happens next, this sender is not answered a second time. + sql.exec(`DELETE FROM auto_reply_log WHERE at < ?1`, iso(now - AUTO_REPLY.oncePerSenderMs)); + sql.exec(`INSERT OR REPLACE INTO auto_reply_log (sender, at) VALUES (?1, ?2)`, owed.to, date); + const taken = take( + storage, + { + id, + fromDraft: false, + // It leaves at once, and the caps were asked above: `later` only says "not again here". + sendAt: now, + later: true, + by: held.by, + askedAt: Date.parse(held.at), + fields: { + subject, + sender: address, + recipient: owed.to, + cc: null, + bcc: null, + date, + // The reply is the person's own words. Nothing of the message that arrived is read here. + body: held.reply.htmlBody ?? textToHtml(held.reply.textBody ?? ""), + in_reply_to: null, + email_references: null, + thread_id: null, + message_id: messageId, + raw_headers: headerList({ + from: address, + to: owed.to, + subject, + date, + messageId, + inReplyTo: null, + references: [], + automatic: true, + }), + }, + }, + now, + ); + if ("refused" in taken) { + // No message was made, so nobody was answered. + sql.exec(`DELETE FROM auto_reply_log WHERE sender = ?1 AND at = ?2`, owed.to, date); + return "cap"; + } + return "queued"; +} diff --git a/workers/durableObject/index.ts b/workers/durableObject/index.ts index 5c67a5c..0bb88bf 100644 --- a/workers/durableObject/index.ts +++ b/workers/durableObject/index.ts @@ -13,6 +13,7 @@ import * as schema from "../db/schema"; import type { ListOrder, SearchFilterOptions } from "../lib/list-options"; import type { Env } from "../types"; import * as alarm from "./alarm"; +import * as autoReply from "./auto-reply"; import { countConversations, listConversations } from "./conversations"; import * as steps from "./deed-steps"; import * as deeds from "./deeds"; @@ -23,6 +24,7 @@ import * as labels from "./labels"; import * as live from "./live"; import { applyMigrations, mailboxMigrations } from "./migrations"; import * as originals from "./originals"; +import * as rules from "./rules"; import { countSearchResults, searchEmails } from "./search"; import type { AttachmentData, EmailData, GetEmailsOptions } from "./shapes"; import * as submissions from "./submissions"; @@ -305,8 +307,9 @@ export class MailboxDO extends DurableObject { async dropArrival(id: string) { return originals.dropArrival(this.ctx.storage, id); } + /** Files an arrival where the mailbox's rules put it (ADR-0008), and answers it when owed. */ async fileArrival(id: string, filed: originals.Filed) { - return originals.fileArrival(this.ctx.storage, id, filed, Date.now()); + return rules.file(this.ctx, id, filed, Date.now()); } async filesStored(emailId: string, fileIds: string[]) { return originals.filesStored(this.ctx.storage, emailId, fileIds); @@ -393,6 +396,28 @@ export class MailboxDO extends DurableObject { await alarm.wake({ ctx: this.ctx, env: this.env, answerWithinMs: this.answerWithinMs }); } + // ── Rules for incoming mail (ADR-0008, ./rules, ./auto-reply) ── + // The assistant and MCP get a handle on the mailbox without these (`workers/agent/tools.ts`). + + async getRules() { + return rules.list(this.ctx.storage.sql); + } + async setRules(list: Parameters[1], by: string) { + return rules.replace(this.ctx.storage, list, by, Date.now()); + } + async blockSender(sender: string, by: string) { + return rules.block(this.ctx.storage, sender, by, Date.now()); + } + async unblockSender(sender: string) { + return rules.unblock(this.ctx.storage.sql, sender); + } + async getAutoReply() { + return autoReply.read(this.ctx.storage.sql); + } + async setAutoReply(reply: Parameters[1], by: string) { + return autoReply.save(this.ctx.storage.sql, reply, by, Date.now()); + } + // ── Known addresses (ADR-0012, ./known) ──────────────────────── // A mailbox learns an address in one place: where a message has left (`./leaving`). diff --git a/workers/durableObject/leaving.ts b/workers/durableObject/leaving.ts index 174efe9..ad940ec 100644 --- a/workers/durableObject/leaving.ts +++ b/workers/durableObject/leaving.ts @@ -17,25 +17,28 @@ * * **The rules hold when it leaves** (decision 15), asked in this order: is the mailbox still * in the index; may the person who sent it still send here, and is the account still theirs; - * is `From` still this mailbox's address; does the message still fit what the service takes; - * do the caps allow one more. A message 24 hours past its time does not leave. + * for a reply the mailbox makes by itself, is that reply still on; is `From` still this + * mailbox's address; does the message still fit what the service takes; do the caps allow + * one more. A message 24 hours past its time does not leave. * * A log line carries opaque ids, the state and a code: never the service's own words, which * may hold an address. */ import { errorFields, log } from "../../shared/log"; import { type Outcome, outcomeOfRefusal, SENDING } from "../../shared/sending"; -import { type Standing, standingOf } from "../auth/standing"; +import { AUTO_REPLY_RIGHT, type Standing, standingOf } from "../auth/standing"; import { type SendEmailParams, sendEmail } from "../email-sender"; import { SenderValidationError, validateSender } from "../lib/email-helpers"; -import { fits, fromStored, type StoredMessage } from "../lib/outgoing-message"; +import { fits, fromStored, isAutomatic, type StoredMessage } from "../lib/outgoing-message"; import type { Env } from "../types"; +import * as autoReply from "./auto-reply"; import { type FileRow, fileKey, storedFiles } from "./drafts"; import * as known from "./known"; import { due, fail, failUnanswered, + forgetFailed, handOver, markSent, type SubmissionRow, @@ -121,8 +124,21 @@ async function leaveOne( const storage = ctx.storage; const id = seen.email_id; const mailbox = ctx.id.toString(); + // Mail the mailbox sends by itself (ADR-0008) is known by its own stored header list. It + // is counted as `auto`, teaches the mailbox no address, and is nobody's draft when it does + // not leave: it is removed, and never made again for the same message. + const stored = storedMessage(storage.sql, id); + const automatic = stored !== null && isAutomatic(stored); + const notLeft = (reason: string, counted = false): boolean => { + if (!fail(storage, id, reason, clock(), counted)) return false; + if (automatic) forgetFailed(storage, id); + return true; + }; const failed = (reason: string): Left => { - if (!fail(storage, id, reason, clock())) return "skipped"; + // A reply that does not leave for a reason of the mailbox answered nobody (ADR-0008). + const record = automatic ? autoReply.answeredBy(storage.sql, id) : null; + if (!notLeft(reason)) return "skipped"; + if (record !== null) autoReply.unanswered(storage.sql, record); log.info("send.failed", { mailbox, message: id, reason }); return "failed"; }; @@ -132,12 +148,15 @@ async function leaveOne( address, seen.requested_by, seen.requested_at, + automatic ? AUTO_REPLY_RIGHT : "mailbox.send", ); if (standing === "gone") return "gone"; if (clock() - Date.parse(seen.send_at) > SENDING.lateAfterMs) return failed("late"); if (standing === "right") return failed("right"); + // The automatic reply is asked again when it leaves: turned off, or past its end date + // since it was made, it does not leave. + if (automatic && !autoReply.answersNow(storage.sql, clock())) return failed("off"); - const stored = storedMessage(storage.sql, id); if (stored === null) return "skipped"; const message = fromStored(stored); try { @@ -153,7 +172,7 @@ async function leaveOne( if (attachments === null) return failed("limits"); // The handover. From here the row is `sending`, and nothing hands it over again. - const began = handOver(storage, seen, clock()); + const began = handOver(storage, seen, clock(), automatic ? "auto" : "person"); if (began === "held") return "held"; if (!began) return "skipped"; await storage.sync(); @@ -174,28 +193,32 @@ async function leaveOne( if (outcome.is === "sent") { if (!markSent(storage, id, outcome.serviceId, clock())) return "skipped"; - // The mailbox learns whom it wrote to when the message has left, once (ADR-0012). - const { skipped } = known.learn( - storage.sql, - { - sender: stored.sender ?? "", - recipient: stored.recipient ?? "", - cc: stored.cc, - bcc: stored.bcc, - }, - clock(), - ); - if (skipped > 0) log.warn("known_addresses.skipped", { skipped }); + // The mailbox learns whom it wrote to when a message a person sent has left, once + // (ADR-0012). An automatic reply goes to an address a stranger gave: it teaches nothing. + if (!automatic) { + const { skipped } = known.learn( + storage.sql, + { + sender: stored.sender ?? "", + recipient: stored.recipient ?? "", + cc: stored.cc, + bcc: stored.bcc, + }, + clock(), + ); + if (skipped > 0) log.warn("known_addresses.skipped", { skipped }); + } log.info("send.left", { mailbox, message: id, attempt: seen.attempts + 1 }); return "sent"; } if (outcome.is === "again") { const now = tryAgain(storage, id, outcome.code, clock()); + if (now === "failed" && automatic) forgetFailed(storage, id); log.info("send.refused", { mailbox, message: id, code: outcome.code, state: now }); return now; } // `unknown` keeps its place in the caps: the message may have left. - if (!fail(storage, id, outcome.error, clock(), outcome.error === "unknown")) return "skipped"; + if (!notLeft(outcome.error, outcome.error === "unknown")) return "skipped"; log.warn("send.failed", { mailbox, message: id, reason: outcome.error }); return "failed"; } @@ -209,7 +232,18 @@ async function leaveOne( export async function leave(leaver: Leaver, clock: () => number): Promise { const { ctx } = leaver; const mailbox = ctx.id.toString(); + // Which of the messages that were handed over are the mailbox's own: asked before they are + // given up, because a message that comes back to Drafts loses its header list. + const own = new Set( + [...ctx.storage.sql.exec(`SELECT email_id FROM submissions WHERE state = 'sending'`)] + .map((row) => String(row.email_id)) + .filter((id) => { + const stored = storedMessage(ctx.storage.sql, id); + return stored !== null && isAutomatic(stored); + }), + ); for (const id of failUnanswered(ctx.storage, clock())) { + if (own.has(id)) forgetFailed(ctx.storage, id); log.warn("send.failed", { mailbox, message: id, reason: "unknown" }); } const waiting = due(ctx.storage.sql, clock(), SENDING.perRun); diff --git a/workers/durableObject/live.ts b/workers/durableObject/live.ts index e38aa74..cfd9a31 100644 --- a/workers/durableObject/live.ts +++ b/workers/durableObject/live.ts @@ -99,6 +99,12 @@ export const ABOUT: Readonly Kinds) typeof answer === "object" && answer !== null && "more" in answer ? MAIL : NOTHING, // It moves its own counter: the settings are not in this database. settingsSaved: NOTHING, + // The rules of a mailbox (ADR-0008) are settings of it: an open settings screen reads them + // again. What a rule does to a message is told by the arrival itself. + setRules: ["settings"], + blockSender: ["settings"], + unblockSender: ["settings"], + setAutoReply: ["settings"], // An arrival before it is filed, and a file before its bytes are there, are not mail yet. beginArrival: NOTHING, dropArrival: NOTHING, diff --git a/workers/durableObject/migrations.ts b/workers/durableObject/migrations.ts index fecd081..4a68064 100644 --- a/workers/durableObject/migrations.ts +++ b/workers/durableObject/migrations.ts @@ -357,4 +357,35 @@ export const mailboxMigrations: Migration[] = [ WHERE folder_id = 'sent' AND date >= strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '-1 day'); `, }, + { + // ADR-0008: a mailbox has rules for its incoming mail. `rules` has one row for each + // rule: a blocked sender (`block`: an address or `@domain` in `sender`), a filter + // (`filter`: its conditions and actions as JSON in `body`, its place in the order in + // `position`), and the one automatic reply (`auto_reply`, with the id `auto_reply` and + // the object in `body`). `set_by` and `set_at` say who made it and when: for the + // automatic reply that person's right is asked again whenever a reply leaves. + // `auto_reply_log` has one row for each address that was answered, with the time and + // nothing of any message: a sender is answered once in seven days. Both tables start + // empty: no mail that is already here is filed again, and nobody is answered for it. + name: "18_rules", + sql: ` + CREATE TABLE rules ( + id TEXT PRIMARY KEY, + kind TEXT NOT NULL, + position INTEGER NOT NULL DEFAULT 0, + sender TEXT, + body TEXT NOT NULL DEFAULT '{}', + set_at TEXT NOT NULL, + set_by TEXT NOT NULL DEFAULT '' + ); + CREATE UNIQUE INDEX idx_rules_kind_sender ON rules(kind, sender); + CREATE INDEX idx_rules_kind_position ON rules(kind, position); + + CREATE TABLE auto_reply_log ( + sender TEXT PRIMARY KEY, + at TEXT NOT NULL + ) WITHOUT ROWID; + CREATE INDEX idx_auto_reply_log_at ON auto_reply_log(at); + `, + }, ]; diff --git a/workers/durableObject/originals.ts b/workers/durableObject/originals.ts index 0f26902..e19be77 100644 --- a/workers/durableObject/originals.ts +++ b/workers/durableObject/originals.ts @@ -16,6 +16,7 @@ import { Folders } from "../../shared/folders"; import type { Unparsed } from "../../shared/original"; +import type { MessageFacts, Outcome } from "../../shared/rules"; import { fileKey } from "./drafts"; import type { AttachmentData, EmailData } from "./shapes"; @@ -50,11 +51,21 @@ export type Filed = email: Omit; attachments: Omit[]; auth_results: string | null; + /** What the rules are told about the message (ADR-0008); without them it is not answered. */ + facts?: MessageFacts; } | { kind: "unparsed"; why: Unparsed; recipient: string; auth_results: string | null }; export type FiledAs = "filed" | "gone" | "too_large"; +/** The places the rules can file a message in. Nothing else is ever a target here. */ +const PLACES: Readonly> = { + inbox: Folders.INBOX, + archive: Folders.ARCHIVE, + spam: Folders.SPAM, + trash: Folders.TRASH, +}; + /** Step c: the arrival's row, before a byte is stored. */ export function beginArrival( storage: Storage, @@ -109,10 +120,21 @@ const tooBig = (error: unknown): boolean => * own row, never from the caller. The attachment rows are written before their bytes, marked * as on their way (`uploading_since`), so a file is never in the bucket without a row. * + * `outcome` is what the mailbox's rules decided for the message (ADR-0008, decision 5): the + * place it is filed in, whether it is read or starred, and the labels it gets. Without one it + * is unread in the Inbox. A label is given only when it is a label of this mailbox, and never + * to a message that is filed in Spam or Trash. + * * `gone` when there is no such arrival: it was filed or removed already, and nothing is * written. `too_large` when the row would not fit: the caller files the message as unparsed. */ -export function fileArrival(storage: Storage, id: string, filed: Filed, now: number): FiledAs { +export function fileArrival( + storage: Storage, + id: string, + filed: Filed, + now: number, + outcome?: Pick, +): FiledAs { const { sql } = storage; if (filed.kind === "parsed" && rowWeight(filed.email) > MAX_ROW_BYTES) return "too_large"; try { @@ -120,13 +142,14 @@ export function fileArrival(storage: Storage, id: string, filed: Filed, now: num const arrival = arrivalOf(sql, id); if (arrival === null) return "gone"; const email = filed.kind === "parsed" ? filed.email : null; + const place = PLACES[outcome?.place ?? "inbox"] ?? Folders.INBOX; sql.exec( `INSERT INTO emails (id, folder_id, subject, sender, recipient, cc, bcc, date, read, starred, body, in_reply_to, email_references, thread_id, message_id, raw_headers, raw_key, raw_size, unparsed, auth_results) - VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, 0, 0, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18)`, + VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?19, ?20, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18)`, arrival.id, - Folders.INBOX, + place, email?.subject ?? "", // A message that could not be read has the envelope sender as its sender. email === null ? arrival.envelope_from.toLowerCase() : email.sender, @@ -146,7 +169,19 @@ export function fileArrival(storage: Storage, id: string, filed: Filed, now: num arrival.raw_size, filed.kind === "parsed" ? null : filed.why, filed.auth_results, + outcome?.read === true ? 1 : 0, + outcome?.starred === true ? 1 : 0, ); + if (place !== Folders.SPAM && place !== Folders.TRASH) { + for (const label of outcome?.labels ?? []) { + sql.exec( + `INSERT OR IGNORE INTO email_labels (email_id, label_id) + SELECT ?1, id FROM labels WHERE id = ?2`, + arrival.id, + label, + ); + } + } if (filed.kind === "parsed") { const since = new Date(now).toISOString(); for (const file of filed.attachments) { diff --git a/workers/durableObject/rules.ts b/workers/durableObject/rules.ts new file mode 100644 index 0000000..44228e6 --- /dev/null +++ b/workers/durableObject/rules.ts @@ -0,0 +1,240 @@ +// Copyright 2026 Cloudhal and the Podbay contributors +// Licensed under the Apache 2.0 license found in the LICENSE file. + +/** + * The rules of a mailbox in its own database (ADR-0008): blocked senders and filters, in the + * table `rules`, and the step that runs them when a message arrives. + * + * **Rules run once, when a message arrives** (decision 5): after it was parsed and before its + * row is written. `file` reads the rules, asks the one pure function of `shared/rules.ts` for + * the outcome, and hands it to the write that files the message, so the message is stored in + * its place with its marks and labels in one transaction. Nothing here runs on mail that is + * already stored. + * + * **A rule cannot fail a delivery** (decision 6). The pure function never throws, and what + * surrounds it here is guarded: when the rules cannot be read, the message is filed in the + * Inbox as if there were none, and the failure is logged as a number. + * + * Every query is a constant with bound values: nothing a person or a message wrote is ever + * part of a statement, and nothing is compared as a pattern. + */ +import { errorFields, log } from "../../shared/log"; +import { + type BlockedSender, + evaluate, + factsOf, + type MessageFacts, + type Outcome, + RULE_LIMITS, + type Rule, + type RulesAnswer, + shownRule, +} from "../../shared/rules"; +import * as alarm from "./alarm"; +import * as autoReply from "./auto-reply"; +import * as originals from "./originals"; + +interface Storage { + sql: SqlStorage; + transactionSync: (closure: () => T) => T; +} + +const iso = (time: number) => new Date(time).toISOString(); +const rows = (cursor: Iterable): T[] => [...cursor] as T[]; + +// ── Reading and changing ─────────────────────────────────────────── + +function blockedOf(sql: SqlStorage): BlockedSender[] { + return rows<{ sender: string; set_at: string }>( + sql.exec( + `SELECT sender, set_at FROM rules WHERE kind = 'block' AND sender IS NOT NULL + ORDER BY sender LIMIT ?1`, + RULE_LIMITS.blocked, + ), + ).map((row) => ({ sender: row.sender, created_at: row.set_at })); +} + +/** The stored filters, in their order. A body that is not JSON is given as it is not: `null`. */ +function storedFilters(sql: SqlStorage): { id: string; body: unknown }[] { + return rows<{ id: string; body: string }>( + sql.exec( + `SELECT id, body FROM rules WHERE kind = 'filter' ORDER BY position, id LIMIT ?1`, + RULE_LIMITS.rules, + ), + ).map((row) => { + try { + return { id: row.id, body: JSON.parse(row.body) as unknown }; + } catch { + return { id: row.id, body: null }; + } + }); +} + +/** The rules of the mailbox: its filters in order, and its blocked senders. */ +export function list(sql: SqlStorage): RulesAnswer { + const rules: Rule[] = []; + for (const { id, body } of storedFilters(sql)) { + const rule = shownRule(id, body); + if (rule !== null) rules.push(rule); + } + return { rules, blocked: blockedOf(sql) }; +} + +/** + * Replaces the filters with this list, in this order (`PUT …/rules`). The caller has parsed + * the list with the strict schema. Says how many there were and are. + */ +export function replace( + storage: Storage, + rules: readonly Rule[], + by: string, + now: number, +): { before: number; after: number } | "limit" { + if (rules.length > RULE_LIMITS.rules) return "limit"; + const { sql } = storage; + return storage.transactionSync(() => { + const before = Number( + [...sql.exec(`SELECT COUNT(*) AS n FROM rules WHERE kind = 'filter'`)][0]?.n ?? 0, + ); + sql.exec(`DELETE FROM rules WHERE kind = 'filter'`); + rules.forEach((rule, position) => { + sql.exec( + `INSERT INTO rules (id, kind, position, sender, body, set_at, set_by) + VALUES (?1, 'filter', ?2, NULL, ?3, ?4, ?5)`, + rule.id, + position, + JSON.stringify({ conditions: rule.conditions, actions: rule.actions }), + iso(now), + by, + ); + }); + return { before, after: rules.length }; + }); +} + +export type Blocked = { blocked: BlockedSender; made: boolean } | "limit"; + +/** + * Blocks a sender (decision 4): a rule of its own kind. `sender` is an address or `@domain` + * as `blockedSenderOf` gives it. Blocking one that is blocked changes nothing. + */ +export function block(storage: Storage, sender: string, by: string, now: number): Blocked { + const { sql } = storage; + return storage.transactionSync((): Blocked => { + const held = rows<{ sender: string; set_at: string }>( + sql.exec(`SELECT sender, set_at FROM rules WHERE kind = 'block' AND sender = ?1`, sender), + )[0]; + if (held !== undefined) { + return { blocked: { sender: held.sender, created_at: held.set_at }, made: false }; + } + const count = Number( + [...sql.exec(`SELECT COUNT(*) AS n FROM rules WHERE kind = 'block'`)][0]?.n ?? 0, + ); + if (count >= RULE_LIMITS.blocked) return "limit"; + const bytes = crypto.getRandomValues(new Uint8Array(12)); + const id = `blk_${[...bytes].map((byte) => byte.toString(16).padStart(2, "0")).join("")}`; + sql.exec( + `INSERT INTO rules (id, kind, position, sender, body, set_at, set_by) + VALUES (?1, 'block', 0, ?2, '{}', ?3, ?4)`, + id, + sender, + iso(now), + by, + ); + return { blocked: { sender, created_at: iso(now) }, made: true }; + }); +} + +/** Unblocks a sender. False when it was not blocked. */ +export function unblock(sql: SqlStorage, sender: string): boolean { + return sql.exec(`DELETE FROM rules WHERE kind = 'block' AND sender = ?1`, sender).rowsWritten > 0; +} + +// ── When a message arrives ───────────────────────────────────────── + +const INBOX: Outcome = { + place: "inbox", + read: false, + starred: false, + labels: [], + blocked: false, + matched: 0, + skipped: 0, +}; + +/** + * What the rules decide for a message with these facts. Never throws: when the rules cannot + * be read the message goes to the Inbox, and the failure is logged without a word of it. + */ +export function outcomeFor(sql: SqlStorage, facts: MessageFacts, mailbox: string): Outcome { + try { + const outcome = evaluate( + blockedOf(sql).map((one) => one.sender), + storedFilters(sql).map((one) => one.body), + facts, + ); + if (outcome.skipped > 0) log.warn("rules.skipped", { mailbox, skipped: outcome.skipped }); + return outcome; + } catch (error) { + log.error("rules.not_read", { mailbox, ...errorFields(error) }); + return { ...INBOX }; + } +} + +/** The envelope sender of an arrival that is not filed yet. */ +function envelopeOf(sql: SqlStorage, id: string): string { + const row = [...sql.exec(`SELECT envelope_from FROM arrivals WHERE id = ?1`, id)][0]; + return typeof row?.envelope_from === "string" ? row.envelope_from : ""; +} + +/** + * How an arrival was filed: as `originals.fileArrival` says, and `filed_away` for a message + * that was stored but not in the Inbox, because the rules filed it elsewhere or its sender is + * blocked. Only mail in the Inbox wakes the assistant: before the rules, nothing arrived + * anywhere else, and mail a person filed away is not given to a model. + */ +export type Filing = originals.FiledAs | "filed_away"; + +/** + * Files an arrival with the rules applied (ADR-0015, step f; ADR-0008, decision 5), and then + * asks whether the automatic reply is owed. The message is stored whatever the rules and the + * reply do: neither can fail the delivery, and a reply that cannot be made is not made. + */ +export async function file( + ctx: DurableObjectState, + id: string, + filed: originals.Filed, + now: number, +): Promise { + const { storage } = ctx; + const mailbox = ctx.id.toString(); + const envelopeFrom = envelopeOf(storage.sql, id); + // A message that could not be read has its envelope sender and nothing else. + const facts = + filed.kind === "parsed" && filed.facts !== undefined + ? factsOf(filed.facts) + : factsOf({ from: envelopeFrom }); + const outcome = outcomeFor(storage.sql, facts, mailbox); + const stored = originals.fileArrival(storage, id, filed, now, outcome); + const result: Filing = + stored === "filed" && (outcome.blocked || outcome.place !== "inbox") ? "filed_away" : stored; + // Only a message that was read can be answered: of any other nothing is known. + if (stored !== "filed" || filed.kind !== "parsed" || filed.facts === undefined) return result; + try { + const address = ctx.id.name; + if (address === undefined) return result; + const blocked = blockedOf(storage.sql).map((one) => one.sender); + const answer = autoReply.consider( + storage, + { address, envelopeFrom, facts, outcome, blocked }, + now, + ); + if (answer === "queued") { + log.info("auto_reply.queued", { mailbox }); + await alarm.set(storage, now); + } + } catch (error) { + log.error("auto_reply.not_made", { mailbox, ...errorFields(error) }); + } + return result; +} diff --git a/workers/durableObject/submissions.ts b/workers/durableObject/submissions.ts index b07c086..55ffef9 100644 --- a/workers/durableObject/submissions.ts +++ b/workers/durableObject/submissions.ts @@ -26,6 +26,7 @@ */ import { Folders } from "../../shared/folders"; import { retryAfterMs, SENDING, type Submission, type SubmissionState } from "../../shared/sending"; +import { isAutomatic } from "../lib/outgoing-message"; import { dropUnfinished, type SentFields } from "./drafts"; import { type CapFull, capFull, capsFreeAt, logSend, type SendKind, unlogSend } from "./sending"; @@ -104,6 +105,11 @@ export interface ToTake { later: boolean; /** The person whose right is asked again when it leaves. */ by: string; + /** + * When that person asked, when it was not now: an automatic reply (ADR-0008) leaves on a + * standing instruction, and the right is asked as of the time that was given. + */ + askedAt?: number; } export type Taken = @@ -192,7 +198,7 @@ export function take(storage: Storage, message: ToTake, now: number): Taken { VALUES (?1, 'waiting', ?2, NULL, 0, NULL, ?3, ?4, NULL, NULL, NULL)`, id, sendAt, - iso(now), + iso(message.askedAt ?? now), message.by, ); return { id, send_at: sendAt, swept }; @@ -229,11 +235,23 @@ export type TakenBack = "draft" | "started" | "not_found"; * Takes a message back (decision 10): a waiting one is a draft again, and a failed one loses * its mark. `started` when it was handed to the service already: too late. */ +/** + * Whether a message is a reply the mailbox made by itself (ADR-0008). It waits on the standing + * of the person who saved the reply, to the stranger who wrote: nobody takes it back as their + * draft, and nobody gives it a time, which would make it theirs to send. + */ +function mailboxOwn(sql: SqlStorage, id: string): boolean { + const row = [...sql.exec(`SELECT raw_headers FROM emails WHERE id = ?1`, id)][0]; + return ( + row !== undefined && isAutomatic({ raw_headers: (row.raw_headers as string | null) ?? null }) + ); +} + export function takeBack(storage: Storage, id: string, now: number): TakenBack { const { sql } = storage; return storage.transactionSync((): TakenBack => { const row = submissionOf(sql, id); - if (row === null) return "not_found"; + if (row === null || mailboxOwn(sql, id)) return "not_found"; if (row.state === "sending" || row.state === "sent") return "started"; sql.exec(`DELETE FROM submissions WHERE email_id = ?1`, id); if (row.state === "waiting") toDraft(sql, id, now); @@ -256,6 +274,7 @@ export function giveTime( ): NewTime { const { sql } = storage; return storage.transactionSync((): NewTime => { + if (mailboxOwn(sql, id)) return "not_found"; const changed = sql.exec( `UPDATE submissions SET send_at = ?1, retry_at = NULL, attempts = 0, error = NULL, requested_by = ?2, requested_at = ?3 @@ -396,6 +415,22 @@ export function fail( }); } +/** + * Removes a message that failed and that nobody wrote: an automatic reply (ADR-0008) that did + * not leave is not a draft of anyone's. It has no file and no original, so its rows are all + * there is. False when the message is not one that failed. + */ +export function forgetFailed(storage: Storage, id: string): boolean { + const { sql } = storage; + return storage.transactionSync(() => { + const row = submissionOf(sql, id); + if (row === null || row.state !== "failed") return false; + sql.exec(`DELETE FROM submissions WHERE email_id = ?1`, id); + sql.exec(`DELETE FROM emails WHERE id = ?1 AND folder_id = ?2`, id, Folders.DRAFT); + return true; + }); +} + /** * The service said "not now" (decision 8): the message waits for its next try, or has failed * with that code when the tries are used up. Says which. diff --git a/workers/index.ts b/workers/index.ts index dfea707..1b09ec8 100644 --- a/workers/index.ts +++ b/workers/index.ts @@ -33,6 +33,7 @@ import { people } from "./routes/people"; import { places } from "./routes/places"; import { recovery } from "./routes/recovery"; import { handleForwardEmail, handleReplyEmail } from "./routes/reply-forward"; +import { rules } from "./routes/rules"; import { sending } from "./routes/sending"; type AppContext = Context; @@ -85,6 +86,7 @@ app.route("/", bulk); app.route("/", knownAddresses); app.route("/", live); app.route("/", sending); +app.route("/", rules); app.route("/", recovery); // -- Config --------------------------------------------------------- diff --git a/workers/lib/body.ts b/workers/lib/body.ts new file mode 100644 index 0000000..c8ccc52 --- /dev/null +++ b/workers/lib/body.ts @@ -0,0 +1,33 @@ +// Copyright 2026 Cloudhal and the Podbay contributors +// Licensed under the Apache 2.0 license found in the LICENSE file. + +/** + * Reading a request's body with a limit. A route that takes a bounded body reads it here, so + * a body over the limit is refused without being read to its end. + */ + +/** The body as text, or `null` when it is longer than `max` bytes. Never reads past the limit. */ +export async function bodyWithin(request: Request, max: number): Promise { + if (Number(request.headers.get("content-length") ?? 0) > max) return null; + if (request.body === null) return ""; + const reader = request.body.getReader(); + const chunks: Uint8Array[] = []; + let size = 0; + for (;;) { + const { done, value } = await reader.read(); + if (done) break; + size += value.byteLength; + if (size > max) { + await reader.cancel(); + return null; + } + chunks.push(value); + } + const bytes = new Uint8Array(size); + let at = 0; + for (const chunk of chunks) { + bytes.set(chunk, at); + at += chunk.byteLength; + } + return new TextDecoder().decode(bytes); +} diff --git a/workers/lib/outgoing-message.ts b/workers/lib/outgoing-message.ts index 255b4d0..2eeada6 100644 --- a/workers/lib/outgoing-message.ts +++ b/workers/lib/outgoing-message.ts @@ -10,10 +10,14 @@ * Pure functions: no binding and no storage. */ import { plainTextOf } from "../../shared/html-text"; +import { AUTO_REPLY } from "../../shared/rules"; import { MAX_ATTACHMENTS, MAX_MESSAGE_BYTES, MAX_RECIPIENTS } from "../../shared/sending-limits"; import type { SendEmailParams } from "../email-sender"; import { buildThreadingHeaders } from "./email-helpers"; +/** The key of the field that marks an automatic reply, as a stored header list has it. */ +const AUTOMATIC = AUTO_REPLY.header.toLowerCase(); + /** Who a message is from, as a request gives it. */ export type From = string | { email: string; name: string }; @@ -36,6 +40,8 @@ export function headerList(message: { messageId: string; inReplyTo: string | null; references: readonly string[]; + /** Mail the mailbox sends by itself (ADR-0008): marked, so that no program answers it. */ + automatic?: true; }): string { const { from, cc, bcc, inReplyTo, references } = message; const headers: Header[] = [ @@ -50,6 +56,7 @@ export function headerList(message: { ...(references.length > 0 ? [{ key: "references", value: references.map((one) => `<${one}>`).join(" ") }] : []), + ...(message.automatic === true ? [{ key: AUTOMATIC, value: AUTO_REPLY.headerValue }] : []), ]; return JSON.stringify(headers); } @@ -108,14 +115,27 @@ function referencesOf(stored: string | null): string[] { } } +/** + * Whether a stored message is mail the mailbox sends by itself (ADR-0008): its header list + * says so, and only the automatic reply writes that. A send route never does, and a message + * that is taken back to Drafts loses its header list. + */ +export function isAutomatic(message: Pick): boolean { + return headerOf(message.raw_headers, AUTOMATIC) !== null; +} + /** What the service is given for a stored message, without its files. */ export function fromStored(message: StoredMessage): SendEmailParams & { to: string[] } { const html = message.body ?? ""; const cc = addresses(message.cc); const bcc = addresses(message.bcc); - const headers = message.in_reply_to - ? buildThreadingHeaders(message.in_reply_to, referencesOf(message.email_references)) - : {}; + const headers: Record = { + ...(message.in_reply_to + ? buildThreadingHeaders(message.in_reply_to, referencesOf(message.email_references)) + : {}), + // RFC 3834: an automatic reply says that it is one, whatever else it carries. + ...(isAutomatic(message) ? { [AUTO_REPLY.header]: AUTO_REPLY.headerValue } : {}), + }; return { to: addresses(message.recipient), ...(cc.length > 0 ? { cc } : {}), diff --git a/workers/lib/rule-facts.ts b/workers/lib/rule-facts.ts new file mode 100644 index 0000000..1619f4c --- /dev/null +++ b/workers/lib/rule-facts.ts @@ -0,0 +1,58 @@ +// Copyright 2026 Cloudhal and the Podbay contributors +// Licensed under the Apache 2.0 license found in the LICENSE file. + +/** + * What the rules of a mailbox are told about a message that was parsed (ADR-0008, decisions 2 + * and 9). The message is a stranger's bytes: everything taken from it here is text, cut to a + * fixed length by `factsOf`, and is compared later and never run. + * + * Pure: no binding and no storage. + */ +import type { Email } from "postal-mime"; +import { plainTextOf } from "../../shared/html-text"; +import { FACT_LIMITS, factsOf, type MessageFacts } from "../../shared/rules"; + +/** A header field of a list (RFC 2369, RFC 2919): a message that has one came through a list. */ +const LIST_FIELD = /^list-(id|unsubscribe|unsubscribe-post|post|help|subscribe|owner|archive)$/; +/** The values of `Precedence` that mean bulk mail (RFC 3834, section 2). */ +const BULK = ["bulk", "list", "junk"]; + +type Addresses = Email["to"]; + +/** Every address of a field; a group gives the addresses of its members. */ +function addressesOf(list: Addresses): string[] { + const found: string[] = []; + for (const one of list ?? []) { + if (typeof one.address === "string" && one.address !== "") found.push(one.address); + for (const member of one.group ?? []) { + if (typeof member.address === "string" && member.address !== "") found.push(member.address); + } + } + return found; +} + +/** The text of a message: its text part, or the text of its HTML when it has no other. */ +function textOf(parsed: Email): string { + if (typeof parsed.text === "string" && parsed.text !== "") { + return parsed.text.slice(0, FACT_LIMITS.text); + } + // The HTML is cut before its text is taken, so a large body costs no more than a small one. + const html = typeof parsed.html === "string" ? parsed.html.slice(0, FACT_LIMITS.text * 2) : ""; + return html === "" ? "" : plainTextOf(html); +} + +export function factsOfParsed(parsed: Email): MessageFacts { + const headers = parsed.headers ?? []; + const field = (key: string) => headers.find((one) => one.key === key)?.value ?? null; + const precedence = (field("precedence") ?? "").trim().toLowerCase(); + return factsOf({ + from: parsed.from?.address ?? "", + fromName: parsed.from?.name ?? "", + recipients: [...addressesOf(parsed.to), ...addressesOf(parsed.cc)], + subject: parsed.subject ?? "", + text: textOf(parsed), + hasAttachment: (parsed.attachments ?? []).some((part) => part.disposition !== "inline"), + autoSubmitted: field("auto-submitted"), + list: headers.some((one) => LIST_FIELD.test(one.key)) || BULK.includes(precedence), + }); +} diff --git a/workers/routes/choices.ts b/workers/routes/choices.ts index 1045fc3..60734a8 100644 --- a/workers/routes/choices.ts +++ b/workers/routes/choices.ts @@ -15,36 +15,11 @@ import { MAX_CHOICES_BYTES } from "../../shared/choices"; import { parseChange } from "../../shared/choices-schema"; import { choicesOf, mergeChoices } from "../auth/choices"; import type { ApiContext } from "../auth/middleware"; +import { bodyWithin } from "../lib/body"; const TOO_LARGE = { error: "The request is too large.", code: "TOO_LARGE" } as const; const INVALID = { error: "Invalid request.", code: "INVALID" } as const; -/** The body as text, or `null` when it is longer than `max` bytes. Never reads past the limit. */ -async function bodyWithin(request: Request, max: number): Promise { - if (Number(request.headers.get("content-length") ?? 0) > max) return null; - if (request.body === null) return ""; - const reader = request.body.getReader(); - const chunks: Uint8Array[] = []; - let size = 0; - for (;;) { - const { done, value } = await reader.read(); - if (done) break; - size += value.byteLength; - if (size > max) { - await reader.cancel(); - return null; - } - chunks.push(value); - } - const bytes = new Uint8Array(size); - let at = 0; - for (const chunk of chunks) { - bytes.set(chunk, at); - at += chunk.byteLength; - } - return new TextDecoder().decode(bytes); -} - export const choices = new Hono(); choices.get("/api/v1/me/choices", async (c) => c.json(await choicesOf(c.env.DB, c.var.user.id))); diff --git a/workers/routes/mailboxes.ts b/workers/routes/mailboxes.ts index a74ebd8..27b0e35 100644 --- a/workers/routes/mailboxes.ts +++ b/workers/routes/mailboxes.ts @@ -78,14 +78,14 @@ mailboxes.post("/api/v1/mailboxes", async (c) => { return c.json({ error: "Mailbox creation is restricted to configured EMAIL_ADDRESSES" }, 403); } + // A mailbox's settings carry no `forwarding` and no `autoReply` (ADR-0008, decision 12): + // nothing forwards, and the automatic reply is an object of its own in the mailbox + // (`./rules`). A value an older mailbox has stored under either key is read by nothing. + const { forwarding: _forwarding, autoReply: _autoReply, ...given } = parsed.data.settings ?? {}; const settings = { fromName: parsed.data.name, signature: { enabled: false, text: "" }, - autoReply: { enabled: false, subject: "", message: "" }, - ...parsed.data.settings, - // A new mailbox never forwards. Whoever creates one may not be able to read it, and - // forwarding is changed on its own route, with fresh proof. - forwarding: { enabled: false, email: "" }, + ...given, }; if (JSON.stringify(settings).length > MAX_SETTINGS_BYTES) { return c.json({ error: "Settings are too large" }, 400); diff --git a/workers/routes/rules.ts b/workers/routes/rules.ts new file mode 100644 index 0000000..6e8fb36 --- /dev/null +++ b/workers/routes/rules.ts @@ -0,0 +1,166 @@ +// Copyright 2026 Cloudhal and the Podbay contributors +// Licensed under the Apache 2.0 license found in the LICENSE file. + +/** + * The rules of a mailbox for its incoming mail (ADR-0008, decisions 7, 8 and 12). + * + * - `GET …/rules` answers the filters in their order and the blocked senders. + * - `PUT …/rules` replaces the filters with the list it is given, in that order. + * - `POST …/blocked-senders` blocks an address, or everything at a domain; `DELETE` unblocks + * it. The sender is named in the body, never in the address of the request. + * - `GET …/auto-reply` answers the automatic reply; `PUT` saves it. + * + * Seeing them needs `mailbox.read`. Changing them needs `mailbox.manage`: in a shared mailbox + * a rule decides where everyone's mail goes. The automatic reply makes the mailbox send, so + * saving it also needs fresh proof (`workers/auth/route-table.ts`). Every change is on the + * record with who, which mailbox and how many, never with a word of a rule or an address. + * + * The guard has already checked the right and picked the mailbox, so what a request names + * only ever reaches the database of the mailbox in the address. A body is held to the strict + * schema of `shared/rules-schema.ts`: nothing is stored that the one function which runs the + * rules does not know. No tool of the assistant or of MCP calls any of this. + */ +import { Hono } from "hono"; +import { type AutoReplyAnswer, RULE_CODES, RULE_LIMITS } from "../../shared/rules"; +import { parseAutoReply, parseRules, parseSender } from "../../shared/rules-schema"; +import type { ApiContext } from "../auth/middleware"; +import { AUTO_REPLY_RIGHT, standingOf } from "../auth/standing"; +import { writeAudit } from "../auth/store"; +import { bodyWithin } from "../lib/body"; + +const M = "/api/v1/mailboxes/:mailboxId"; + +/** A body is read up to this size and no further. */ +const MAX_BODY_BYTES = 256 * 1024; + +const INVALID = { error: "Invalid request.", code: RULE_CODES.invalid } as const; +const NO_SENDER = { + error: "Give an address, or a domain such as example.com.", + code: RULE_CODES.invalid, +} as const; + +/** + * The JSON of a request's body, or `null` when it is too large or is not JSON. Reading stops + * at the limit: a larger body is never read to its end. + */ +async function bodyOf(request: Request): Promise { + try { + const text = await bodyWithin(request, MAX_BODY_BYTES); + return text === null ? null : (JSON.parse(text) as unknown); + } catch { + return null; + } +} + +export const rules = new Hono(); + +rules.get(`${M}/rules`, async (c) => c.json(await c.var.mailboxStub.getRules())); + +rules.put(`${M}/rules`, async (c) => { + const parsed = parseRules(await bodyOf(c.req.raw)); + if ("refused" in parsed) { + return parsed.refused === "too_many" + ? c.json( + { + error: `A mailbox has at most ${RULE_LIMITS.rules} filters.`, + code: RULE_CODES.tooMany, + }, + 409, + ) + : c.json(INVALID, 400); + } + const stub = c.var.mailboxStub; + const saved = await stub.setRules(parsed.rules, c.var.user.id); + if (saved === "limit") return c.json(INVALID, 400); + // Who, which mailbox and how many. Never a condition: it may hold a correspondent's address. + await writeAudit(c.env.DB, { + actorUserId: c.var.user.id, + action: "mailbox.rules_changed", + target: c.var.mailbox.address, + detail: JSON.stringify(saved), + }); + return c.json(await stub.getRules()); +}); + +rules.post(`${M}/blocked-senders`, async (c) => { + const sender = parseSender(await bodyOf(c.req.raw)); + if (sender === null) return c.json(NO_SENDER, 400); + const made = await c.var.mailboxStub.blockSender(sender, c.var.user.id); + if (made === "limit") { + return c.json( + { + error: `A mailbox blocks at most ${RULE_LIMITS.blocked} senders.`, + code: RULE_CODES.blockedLimit, + }, + 409, + ); + } + if (made.made) { + await writeAudit(c.env.DB, { + actorUserId: c.var.user.id, + action: "mailbox.sender_blocked", + target: c.var.mailbox.address, + detail: JSON.stringify({ kind: sender.startsWith("@") ? "domain" : "address" }), + }); + } + return c.json(made.blocked, made.made ? 201 : 200); +}); + +rules.delete(`${M}/blocked-senders`, async (c) => { + const sender = parseSender(await bodyOf(c.req.raw)); + if (sender === null) return c.json(NO_SENDER, 400); + if (!(await c.var.mailboxStub.unblockSender(sender))) { + return c.json({ error: "This sender is not blocked.", code: RULE_CODES.notBlocked }, 404); + } + await writeAudit(c.env.DB, { + actorUserId: c.var.user.id, + action: "mailbox.sender_unblocked", + target: c.var.mailbox.address, + detail: JSON.stringify({ kind: sender.startsWith("@") ? "domain" : "address" }), + }); + return c.body(null, 204); +}); + +rules.get(`${M}/auto-reply`, async (c) => { + const held = await c.var.mailboxStub.getAutoReply(); + // A reply leaves on the standing of the person who saved it. When that person may no longer + // send here nothing is answered, and the answer says so instead of showing a reply that is + // on and does nothing. + const stopped = + held.reply.isEnabled && held.by !== null && held.at !== null + ? (await standingOf(c.env.DB, c.var.mailbox.address, held.by, held.at, AUTO_REPLY_RIGHT)) !== + "ok" + : false; + const answer: AutoReplyAnswer = { ...held.reply, stopped }; + return c.json(answer); +}); + +rules.put(`${M}/auto-reply`, async (c) => { + const parsed = parseAutoReply(await bodyOf(c.req.raw)); + if ("refused" in parsed) { + return parsed.refused === "empty" + ? c.json( + { + error: "An automatic reply that is on needs a message.", + code: RULE_CODES.noBody, + }, + 400, + ) + : c.json(INVALID, 400); + } + const { was } = await c.var.mailboxStub.setAutoReply(parsed.reply, c.var.user.id); + // Whether it is on and between which dates. Never its words. + await writeAudit(c.env.DB, { + actorUserId: c.var.user.id, + action: "mailbox.auto_reply_changed", + target: c.var.mailbox.address, + detail: JSON.stringify({ + was, + enabled: parsed.reply.isEnabled, + from: parsed.reply.fromDate, + until: parsed.reply.toDate, + }), + }); + const answer: AutoReplyAnswer = { ...parsed.reply, stopped: false }; + return c.json(answer); +}); diff --git a/workers/routes/sending.ts b/workers/routes/sending.ts index aa3c5f2..c30d113 100644 --- a/workers/routes/sending.ts +++ b/workers/routes/sending.ts @@ -11,6 +11,8 @@ * - `POST …/emails/:id/reschedule` gives a waiting message a new time: `{ send_at }`, or * nothing for "now". Whoever does it becomes the person whose right is asked when the * message leaves. + * - Neither route takes the automatic reply the mailbox made by itself (ADR-0008): it is + * nobody's draft and nobody's to send, and both answer 404 for it. * * The guard has already checked the right (`mailbox.send` for both) and picked the mailbox, * so an id here only ever looks a row up in the database of the mailbox in the address. From 5d3ae71d9fc3885a42f5e0911edb10a036418ac6 Mon Sep 17 00:00:00 2001 From: Madraka <8898640+Madraka@users.noreply.github.com> Date: Wed, 18 Nov 2026 16:25:43 +0300 Subject: [PATCH 2/3] feat(app): filters, blocked senders and the automatic reply in the app Two settings tabs, "Filters and blocked senders" and "Automatic reply", and on a message "Block the sender" and "Filter messages like this" for an owner of the mailbox. A block is offered only for a whole address and says so in its request; the automatic reply shows the text of what is sent. Co-Authored-By: Claude Fable 5.2 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Madraka <8898640+Madraka@users.noreply.github.com> --- app/components/EmailPanel.tsx | 5 + app/components/email-panel/MessageHeader.tsx | 33 ++ app/components/rules/FilterDialog.tsx | 328 ++++++++++++++++++ app/components/settings/AutoReplySettings.tsx | 214 ++++++++++++ app/components/settings/FilterSettings.tsx | 268 ++++++++++++++ app/hooks/useLive.ts | 2 + app/hooks/useSenderRules.tsx | 101 ++++++ app/i18n/tr/a-to-m.ts | 52 +++ app/i18n/tr/n-to-z.ts | 41 +++ app/lib/rule-words.ts | 61 ++++ app/queries/keys.ts | 4 + app/queries/rules.ts | 79 +++++ app/routes/settings.tsx | 10 + app/services/api.ts | 25 +- app/types/index.ts | 2 - test/e2e/policy.spec.ts | 2 + test/e2e/rules.spec.ts | 299 ++++++++++++++++ 17 files changed, 1522 insertions(+), 4 deletions(-) create mode 100644 app/components/rules/FilterDialog.tsx create mode 100644 app/components/settings/AutoReplySettings.tsx create mode 100644 app/components/settings/FilterSettings.tsx create mode 100644 app/hooks/useSenderRules.tsx create mode 100644 app/lib/rule-words.ts create mode 100644 app/queries/rules.ts create mode 100644 test/e2e/rules.spec.ts diff --git a/app/components/EmailPanel.tsx b/app/components/EmailPanel.tsx index 4f682af..d517aa2 100644 --- a/app/components/EmailPanel.tsx +++ b/app/components/EmailPanel.tsx @@ -22,6 +22,7 @@ import { LabelChips } from "~/components/LabelChip"; import { useConversationActions } from "~/hooks/useConversationActions"; import { useDisplay } from "~/hooks/useDisplay"; import { useOpenConversation } from "~/hooks/useOpenConversation"; +import { useSenderRules } from "~/hooks/useSenderRules"; import { useSending } from "~/hooks/useSending"; import { useShortcuts } from "~/hooks/useShortcuts"; import { useUIStore } from "~/hooks/useUIStore"; @@ -73,6 +74,7 @@ export default function EmailPanel({ emailId, siblings = [], inPane = false }: E const { data: currentMailbox } = useMailbox(mailboxId) as { data?: Mailbox; }; + const senderRules = useSenderRules(mailboxId, currentMailbox); const { closePanel, startCompose } = useUIStore(); const toastManager = useKumoToastManager(); const confirm = useConfirm(); @@ -406,6 +408,8 @@ export default function EmailPanel({ emailId, siblings = [], inPane = false }: E onShowOriginal: msg.has_original ? () => setOriginalOf(msg) : undefined, onDownloadOriginal: msg.has_original ? () => setSavingOf(msg) : undefined, onUnsubscribe: leaveList(msg), + // Blocking the sender and filtering mail like this change the mailbox's rules (ADR-0008). + ...senderRules.actionsFor(msg), }); const allExpanded = allMessages.every((msg) => expandedMessages.has(msg.id)); // With five messages or more, the ones between the first and the last two that are folded @@ -584,6 +588,7 @@ export default function EmailPanel({ emailId, siblings = [], inPane = false }: E setSavingOf(msg); }} /> + {senderRules.dialog} void; /** Only for mail from a list that says how to leave it. */ onUnsubscribe?: () => void; + /** Only for an owner of the mailbox (ADR-0008): mail from this sender goes to Spam. */ + onBlockSender?: () => void; + /** In place of blocking, for a sender whose address is blocked. */ + onUnblockSender?: () => void; + /** Only for an owner: opens a new filter that starts with this sender. */ + onFilterLikeThis?: () => void; } interface MessageHeaderProps { @@ -163,6 +171,16 @@ export default function MessageHeader({ > {t("Copy the address")} + {actions.onBlockSender && ( + + {t("Block the sender")} + + )} + {actions.onUnblockSender && ( + + {t("Unblock the sender")} + + )} @@ -259,7 +277,22 @@ export default function MessageHeader({ {t("Report spam")} )} + {actions.onBlockSender && ( + + {t("Block the sender")} + + )} + {actions.onUnblockSender && ( + + {t("Unblock the sender")} + + )} + {actions.onFilterLikeThis && ( + + {t("Filter messages like this")} + + )} {t("Find messages from this sender")} diff --git a/app/components/rules/FilterDialog.tsx b/app/components/rules/FilterDialog.tsx new file mode 100644 index 0000000..1712720 --- /dev/null +++ b/app/components/rules/FilterDialog.tsx @@ -0,0 +1,328 @@ +// Copyright 2026 Cloudhal and the Podbay contributors +// Licensed under the Apache 2.0 license found in the LICENSE file. + +import { Input, LayerDialog } from "@cloudflare/kumo"; +import { type FormEvent, useEffect, useState } from "react"; +import { + RULE_LIMITS, + type Rule, + type RuleActions, + type RuleConditions, + type RuleMatch, + type RulePlace, +} from "shared/rules"; +import { t } from "~/i18n"; +import { labelDot } from "~/lib/labels"; +import { useLabels } from "~/queries/labels"; +import { type RuleDraft, useSaveRules } from "~/queries/rules"; + +const NATIVE_FIELD = + "h-9 rounded-lg border border-kumo-line bg-kumo-control px-2 text-sm text-kumo-default"; + +interface Form { + fromMatch: RuleMatch; + from: string; + toMatch: RuleMatch; + to: string; + subject: string; + words: string; + hasAttachment: boolean; + place: "inbox" | RulePlace; + read: boolean; + star: boolean; + labels: string[]; +} + +function formOf(conditions: RuleConditions = {}, actions: RuleActions = {}): Form { + return { + fromMatch: conditions.from?.match ?? "contains", + from: conditions.from?.value ?? "", + toMatch: conditions.to?.match ?? "contains", + to: conditions.to?.value ?? "", + subject: conditions.subject ?? "", + words: conditions.words ?? "", + hasAttachment: conditions.hasAttachment === true, + place: actions.place ?? "inbox", + read: actions.read === true, + star: actions.star === true, + labels: actions.labels ?? [], + }; +} + +/** The rule a filled form is. A field left empty is no condition. */ +function ruleOf(form: Form): { conditions: RuleConditions; actions: RuleActions } { + const conditions: RuleConditions = {}; + if (form.from.trim() !== "") conditions.from = { match: form.fromMatch, value: form.from.trim() }; + if (form.to.trim() !== "") conditions.to = { match: form.toMatch, value: form.to.trim() }; + if (form.subject.trim() !== "") conditions.subject = form.subject.trim(); + if (form.words.trim() !== "") conditions.words = form.words.trim(); + if (form.hasAttachment) conditions.hasAttachment = true; + const actions: RuleActions = {}; + if (form.place !== "inbox") actions.place = form.place; + if (form.read) actions.read = true; + if (form.star) actions.star = true; + if (form.labels.length > 0) actions.labels = form.labels; + return { conditions, actions }; +} + +function Match({ + name, + value, + onChange, +}: { + name: string; + value: RuleMatch; + onChange: (value: RuleMatch) => void; +}) { + return ( + + ); +} + +/** + * Make a filter, or change one (ADR-0008). A filter is what the form says and nothing else: + * its conditions are plain words, compared without regard to case, and all of them must hold. + * It acts on mail that arrives from now on, never on mail that is already here. + */ +export default function FilterDialog({ + open, + onOpenChange, + mailboxId, + rules, + rule, + like, +}: { + open: boolean; + onOpenChange: (open: boolean) => void; + mailboxId: string; + /** The filters the mailbox has now: the one that is saved joins or replaces one of them. */ + rules: readonly Rule[]; + /** The filter to change; leave out to make a new one. */ + rule?: Rule; + /** What a new filter starts with: "Filter messages like this". */ + like?: RuleConditions; +}) { + const { data: labels = [] } = useLabels(mailboxId); + const saveRules = useSaveRules(); + const [form, setForm] = useState
(formOf()); + const [error, setError] = useState(null); + const set = (change: Partial) => setForm((held) => ({ ...held, ...change })); + + // biome-ignore lint/correctness/useExhaustiveDependencies: filled once, when the dialog opens + useEffect(() => { + if (!open) return; + setForm(rule ? formOf(rule.conditions, rule.actions) : formOf(like)); + setError(null); + }, [open]); + + const made = ruleOf(form); + const noCondition = Object.keys(made.conditions).length === 0; + const noAction = Object.keys(made.actions).length === 0; + const full = rule === undefined && rules.length >= RULE_LIMITS.rules; + + async function submit(event: FormEvent) { + event.preventDefault(); + event.stopPropagation(); + if (noCondition || noAction || full) return; + setError(null); + const next: RuleDraft[] = rule + ? rules.map((one) => (one.id === rule.id ? { id: rule.id, ...made } : one)) + : [...rules, made]; + try { + await saveRules.mutateAsync({ mailboxId, rules: next }); + onOpenChange(false); + } catch { + setError(t("The filter could not be saved.")); + } + } + + const toggleLabel = (id: string, give: boolean) => + set({ labels: give ? [...form.labels, id] : form.labels.filter((one) => one !== id) }); + + return ( + + + {rule ? t("Edit filter") : t("New filter")} + + {t( + "A filter acts on mail when it arrives, not on mail that is already here. Words are compared as they are written, without regard to capitals.", + )} + + + +
+ + {t("When a message arrives and all of this is true")} + +
+ set({ fromMatch })} + /> +
+ set({ from: event.target.value })} + /> +
+
+
+ set({ toMatch })} + /> +
+ set({ to: event.target.value })} + /> +
+
+ set({ subject: event.target.value })} + /> + set({ words: event.target.value })} + /> + +
+ +
+ + {t("Do this")} + +
+ + +
+ + + {labels.length > 0 && ( +
+ + {t("Give it these labels")} + +
+ {labels.map((label) => { + const given = form.labels.includes(label.id); + return ( + + ); + })} +
+
+ )} +
+ + {(noCondition || noAction) && ( +

+ {noCondition + ? t("Say at least one thing the filter looks for.") + : t("Say at least one thing the filter does.")} +

+ )} + {full && ( +

+ {t("This mailbox has as many filters as it can hold.")} +

+ )} + {error && ( +

+ {error} +

+ )} + +
+ + + {rule ? t("Save") : t("Create filter")} + + +
+
+ ); +} diff --git a/app/components/settings/AutoReplySettings.tsx b/app/components/settings/AutoReplySettings.tsx new file mode 100644 index 0000000..82058c9 --- /dev/null +++ b/app/components/settings/AutoReplySettings.tsx @@ -0,0 +1,214 @@ +// Copyright 2026 Cloudhal and the Podbay contributors +// Licensed under the Apache 2.0 license found in the LICENSE file. + +import { Button, Input, useKumoToastManager } from "@cloudflare/kumo"; +import { type FormEvent, useEffect, useState } from "react"; +import { plainTextOf } from "shared/html-text"; +import { AUTO_REPLY, type AutoReply } from "shared/rules"; +import { useFreshProof } from "~/components/FreshProof"; +import { t } from "~/i18n"; +import { fieldValue } from "~/lib/sending-words"; +import { useAutoReply, useSaveAutoReply } from "~/queries/rules"; +import Row from "./Row"; + +const NATIVE_FIELD = + "h-9 w-full rounded-lg border border-kumo-line bg-kumo-control px-3 text-sm text-kumo-default"; + +/** An instant as the date and time field of this device shows it; empty for none. */ +const shown = (instant: string | null): string => + instant === null || Number.isNaN(Date.parse(instant)) ? "" : fieldValue(new Date(instant)); +/** What the field holds as an instant in UTC; `null` for an empty field. */ +const instantOf = (field: string): string | null => + field === "" || Number.isNaN(Date.parse(field)) ? null : new Date(field).toISOString(); + +/** + * The words the screen shows: the text of the reply, or the text of its HTML when it has only + * HTML. The server keeps the two in agreement, so what is shown is what is sent. + */ +const shownText = (reply: Pick): string => + reply.textBody ?? (reply.htmlBody === null ? "" : plainTextOf(reply.htmlBody)); + +/** + * The automatic reply of the mailbox (ADR-0008): a message the mailbox sends by itself to + * people who write to it, while it is on. It is the one way mail leaves without a person + * pressing Send, so saving it is the owner's and asks who they are; the Worker decides both. + */ +export default function AutoReplySettings({ + mailboxId, + canChange, +}: { + mailboxId: string; + /** Only an owner of the mailbox changes its automatic reply. */ + canChange: boolean; +}) { + const { data, isError } = useAutoReply(mailboxId); + const saveAutoReply = useSaveAutoReply(); + const withFreshProof = useFreshProof(); + const toasts = useKumoToastManager(); + const [enabled, setEnabled] = useState(false); + const [from, setFrom] = useState(""); + const [until, setUntil] = useState(""); + const [subject, setSubject] = useState(""); + const [text, setText] = useState(""); + const [error, setError] = useState(null); + + // Filled when the reply arrives, and again when it is saved. + useEffect(() => { + if (!data) return; + setEnabled(data.isEnabled); + setFrom(shown(data.fromDate)); + setUntil(shown(data.toDate)); + setSubject(data.subject ?? ""); + setText(shownText(data)); + }, [data]); + + const backwards = from !== "" && until !== "" && new Date(until) <= new Date(from); + const empty = enabled && text.trim() === "" && !data?.htmlBody; + + async function save(event: FormEvent) { + event.preventDefault(); + setError(null); + if (backwards || empty) return; + const reply: AutoReply = { + isEnabled: enabled, + fromDate: instantOf(from), + toDate: instantOf(until), + subject: subject.trim() === "" ? null : subject.trim(), + textBody: text.trim() === "" ? null : text, + // A body written elsewhere as HTML is kept as long as its text, which is what this screen + // shows, was not changed here. The server takes the two together only when they agree. + htmlBody: data?.htmlBody && text === shownText(data) ? data.htmlBody : null, + }; + try { + await withFreshProof(() => saveAutoReply.mutateAsync({ mailboxId, reply })); + toasts.add({ + title: enabled ? t("The automatic reply is on") : t("The automatic reply is off"), + }); + } catch { + setError(t("The automatic reply could not be saved.")); + } + } + + if (isError) { + return ( +

+ {t("The automatic reply could not be loaded.")} +

+ ); + } + + return ( +
+

+ {t( + "While it is on, this mailbox answers people who write to it, by itself and in its own name. A sender is answered once in seven days. Lists, automatic mail, mail in Spam and mail that does not name this address are never answered, and nothing of the message that arrived is in the answer.", + )} +

+ {data?.stopped && ( +

+ {t( + "It is on, but nothing is being answered: the person who turned it on is no longer an owner of this mailbox, or their account was recovered since. An owner saves it again to turn it on.", + )} +

+ )} +
+ + + + +
+
+ + setFrom(event.target.value)} + /> +
+
+ + setUntil(event.target.value)} + /> +
+
+ {backwards && ( +

+ {t("The end must be after the beginning.")} +

+ )} +
+ +
+
+ setSubject(event.target.value)} + /> +
+
+
+ +