import { test } from "node:test"; import assert from "node:assert/strict"; import { createAddress, deleteExpiredWebviewMessages, deleteWebviewMessagesForOwner, getWebviewMessage, getWebviewPreference, setWebviewPreference, storeWebviewMessage, } from "../src/core/db.ts"; import { createDispatcher } from "../src/core/dispatch.ts"; import { handleInboundEmail } from "../src/core/email.ts"; import type { MailAdapter, ParsedMail } from "../src/core/types.ts"; import { isWebviewId, openWebviewPayload, sealWebviewPayload, webviewLink, WEBVIEW_RETENTION_SECONDS, } from "../src/core/webview.ts"; import { owner, testDb } from "./helpers.ts"; const DAY = 86400; const BASE = "https://mail.example.com"; const RAW_MAIL = "From: s@e.com\r\nTo: x\r\nSubject: hello\r\n\r\nbody text"; // Captures what the adapter was handed, since the webview's whole // user-visible effect is one extra field on the delivered mail. function capturingAdapter(): { adapter: MailAdapter; last: () => ParsedMail | null } { let last: ParsedMail | null = null; return { adapter: { name: "discord", deliver: async (_owner, mail) => { last = mail; return { success: true }; }, notify: async () => ({ success: true }), }, last: () => last, }; } test("sealing a webview payload", async (t) => { await t.test("round-trips through the key from the link", async () => { const payload = { from: "s@e.com", to: "x@ex.com", subject: "hi", text: "body", html: "
body
" }; const sealed = await sealWebviewPayload(payload); assert.deepEqual(await openWebviewPayload(sealed.key, sealed.iv, sealed.ciphertext), payload); }); await t.test("is useless without the key", async () => { const sealed = await sealWebviewPayload({ from: "a", to: "b", subject: "c", text: "secret" }); const other = await sealWebviewPayload({ from: "a", to: "b", subject: "c", text: "secret" }); // The ciphertext is all the database ever holds. Another message's key // (or any key at all) must not open it. await assert.rejects(() => openWebviewPayload(other.key, sealed.iv, sealed.ciphertext)); assert.ok(!sealed.ciphertext.includes("secret")); }); await t.test("never reuses an id, iv or key", async () => { const a = await sealWebviewPayload({ from: "a", to: "b", subject: "c", text: "d" }); const b = await sealWebviewPayload({ from: "a", to: "b", subject: "c", text: "d" }); assert.notEqual(a.id, b.id); assert.notEqual(a.iv, b.iv); assert.notEqual(a.key, b.key); }); await t.test("puts the key in the fragment, which browsers never send", async () => { const sealed = await sealWebviewPayload({ from: "a", to: "b", subject: "c", text: "d" }); const link = webviewLink(`${BASE}/`, sealed); const url = new URL(link); assert.equal(url.pathname, `/v/${sealed.id}`); assert.equal(url.hash, `#${sealed.key}`); assert.equal(url.search, ""); assert.ok(isWebviewId(sealed.id)); }); await t.test("rejects ids that aren't ours, so a path can't reach a query", () => { assert.ok(!isWebviewId("../../etc")); assert.ok(!isWebviewId("short")); assert.ok(!isWebviewId("")); assert.ok(!isWebviewId("a".repeat(23))); }); }); test("stored webview messages", async (t) => { await t.test("are readable by id until they expire", async () => { const { db } = testDb(); const sealed = await sealWebviewPayload({ from: "a", to: "b", subject: "c", text: "d" }); await storeWebviewMessage(db, owner("u1"), sealed.id, sealed, WEBVIEW_RETENTION_SECONDS); const row = await getWebviewMessage(db, sealed.id); assert.deepEqual(await openWebviewPayload(sealed.key, row!.iv, row!.ciphertext), { from: "a", to: "b", subject: "c", text: "d", }); }); // The nightly delete is the backstop, not the guarantee: a read between // cron runs must already refuse an expired row. await t.test("stop opening the moment they expire, before any cleanup runs", async () => { const { db } = testDb(); const sealed = await sealWebviewPayload({ from: "a", to: "b", subject: "c", text: "d" }); await storeWebviewMessage(db, owner("u1"), sealed.id, sealed, -1); assert.equal(await getWebviewMessage(db, sealed.id), null); }); await t.test("are deleted by the cleanup once expired, and not before", async () => { const { db } = testDb(); const live = await sealWebviewPayload({ from: "a", to: "b", subject: "c", text: "d" }); const stale = await sealWebviewPayload({ from: "a", to: "b", subject: "c", text: "d" }); await storeWebviewMessage(db, owner("u1"), live.id, live, WEBVIEW_RETENTION_SECONDS); await storeWebviewMessage(db, owner("u1"), stale.id, stale, -1); assert.equal(await deleteExpiredWebviewMessages(db), 1); assert.ok(await getWebviewMessage(db, live.id)); }); await t.test("are dropped on request, for that owner only", async () => { const { db } = testDb(); const mine = await sealWebviewPayload({ from: "a", to: "b", subject: "c", text: "d" }); const theirs = await sealWebviewPayload({ from: "a", to: "b", subject: "c", text: "d" }); await storeWebviewMessage(db, owner("u1"), mine.id, mine, WEBVIEW_RETENTION_SECONDS); await storeWebviewMessage(db, owner("u2"), theirs.id, theirs, WEBVIEW_RETENTION_SECONDS); assert.equal(await deleteWebviewMessagesForOwner(db, owner("u1")), 1); assert.equal(await getWebviewMessage(db, mine.id), null); assert.ok(await getWebviewMessage(db, theirs.id)); }); }); test("the webview preference", async (t) => { await t.test("is off until it is turned on", async () => { const { db } = testDb(); assert.equal(await getWebviewPreference(db, owner("u1")), false); await setWebviewPreference(db, owner("u1"), true); assert.equal(await getWebviewPreference(db, owner("u1")), true); await setWebviewPreference(db, owner("u1"), false); assert.equal(await getWebviewPreference(db, owner("u1")), false); }); // The two preferences share a row, so writing one must not clear the other. await t.test("does not disturb expiry reminders", async () => { const { db } = testDb(); const { setExpiryReminderPreference, getExpiryReminderPreference } = await import("../src/core/db.ts"); await setExpiryReminderPreference(db, owner("u1"), true); await setWebviewPreference(db, owner("u1"), true); assert.equal(await getExpiryReminderPreference(db, owner("u1")), true); await setWebviewPreference(db, owner("u1"), false); assert.equal(await getExpiryReminderPreference(db, owner("u1")), true); }); }); test("inbound mail and the webview", async (t) => { await t.test("stores nothing and links nothing when nobody opted in", async () => { const { db, raw } = testDb(); const { adapter, last } = capturingAdapter(); const address = await createAddress(db, owner("u1"), "ex.com", DAY, false, null); await handleInboundEmail({ to: address, from: "s@e.com", raw: RAW_MAIL }, db, createDispatcher([adapter]), { baseUrl: BASE, }); assert.equal(last()!.webviewUrl, undefined); assert.equal(raw.prepare("SELECT COUNT(*) AS n FROM webview_messages").get()!.n, 0); }); await t.test("stores a sealed copy and links it once opted in", async () => { const { db, raw } = testDb(); const { adapter, last } = capturingAdapter(); const address = await createAddress(db, owner("u1"), "ex.com", DAY, false, null); await setWebviewPreference(db, owner("u1"), true); await handleInboundEmail({ to: address, from: "s@e.com", raw: RAW_MAIL }, db, createDispatcher([adapter]), { baseUrl: BASE, }); const url = new URL(last()!.webviewUrl!); const id = url.pathname.slice("/v/".length); const row = await getWebviewMessage(db, id); const opened = await openWebviewPayload(url.hash.slice(1), row!.iv, row!.ciphertext); assert.equal(opened.subject, "hello"); assert.equal(opened.to, address); assert.match(opened.text, /body text/); // Nothing identifying is stored outside the ciphertext: no address, no // subject, no sender. const stored = raw.prepare("SELECT * FROM webview_messages").get() as Record