Skip to content

📧✨ Beautiful email. For everyone.

fix(jmap): use valid raw unsubscribe header properties (#53)

Paul Koeckd5cdfedab71e
diff --git a/packages/jmap/src/index.ts b/packages/jmap/src/index.ts
index 9d54377..ca6f598 100644
--- a/packages/jmap/src/index.ts
+++ b/packages/jmap/src/index.ts
@@ -194,8 +194,8 @@ function asMessage(account: Account, raw: Json, bodies: boolean): Message {
for (const part of raw.attachments || []) visit(part)
result.attachments = [...attachments.values()]
result.unsubscribe = parseUnsubscribe(
- raw['header:List-Unsubscribe:asText'],
- raw['header:List-Unsubscribe-Post:asText'],
+ raw['header:List-Unsubscribe'],
+ raw['header:List-Unsubscribe-Post'],
)
}
if (
@@ -417,8 +417,9 @@ export class JmapProvider implements MailProvider {
'textBody',
'htmlBody',
'attachments',
- 'header:List-Unsubscribe:asText',
- 'header:List-Unsubscribe-Post:asText',
+ // RFC 8621 forbids asText for List-Unsubscribe; keep raw values for parsing.
+ 'header:List-Unsubscribe',
+ 'header:List-Unsubscribe-Post',
]
: summaryProperties,
...(bodies
diff --git a/tests/jmap.test.ts b/tests/jmap.test.ts
index fc59b99..992f5cb 100644
--- a/tests/jmap.test.ts
+++ b/tests/jmap.test.ts
@@ -38,6 +38,10 @@ function fixture(overrides: Record<string, (args: any) => any> = {}, downloadBod
return Response.json({
methodResponses: request.methodCalls.map(([name, args, tag]: any) => {
calls.push({ name, args })
+ // RFC 8621 §4.1.2.2 forbids Text form for RFC 2369 list headers.
+ // Stalwart validates this even when the message has no such header.
+ if (name === 'Email/get' && args.properties?.includes('header:List-Unsubscribe:asText'))
+ return ['error', { type: 'invalidArguments' }, tag]
const response =
overrides[name]?.(args) ??
(
@@ -432,6 +436,50 @@ describe('JMAP provider', () => {
).toEqual({ url: 'https://list.example/unsubscribe', oneClick: true })
expect(parseUnsubscribe('<javascript:alert(1)>')).toBeUndefined()
})
+ it.each([
+ [undefined, undefined, undefined],
+ [
+ '<mailto:[email protected]>',
+ undefined,
+ { url: 'mailto:[email protected]', oneClick: false },
+ ],
+ [
+ '<mailto:[email protected]>,\r\n <https://list.example/unsubscribe>',
+ 'List-Unsubscribe=One-Click',
+ { url: 'https://list.example/unsubscribe', oneClick: true },
+ ],
+ ])('opens conversations with raw unsubscribe headers (%s)', async (value, post, expected) => {
+ const { provider, calls } = fixture({
+ 'Email/get': (args) => ({
+ list: [
+ {
+ ...raw('newsletter'),
+ textBody: [{ partId: 'text', type: 'text/plain' }],
+ bodyValues: { text: { value: 'Newsletter content' } },
+ ...Object.fromEntries(
+ args.properties
+ .filter((property: string) => property.startsWith('header:'))
+ .map((property: string) => [
+ property,
+ {
+ 'header:List-Unsubscribe': value ?? null,
+ 'header:List-Unsubscribe-Post': post ?? null,
+ }[property],
+ ]),
+ ),
+ },
+ ],
+ }),
+ })
+ const [account] = await provider.connect()
+ const [message] = await provider.conversation(account, 'thread-newsletter')
+ expect(message.text).toBe('Newsletter content')
+ expect(message.unsubscribe).toEqual(expected)
+ expect(calls.find((call) => call.name === 'Email/get')?.args.properties).toEqual(
+ expect.arrayContaining(['header:List-Unsubscribe', 'header:List-Unsubscribe-Post']),
+ )
+ provider.dispose()
+ })
it('discovers bounded Autocrypt headers without requesting bodies or message blobs', async () => {
const { provider, calls } = fixture({
'Email/get': () => ({
diff --git a/tests/stalwart.integration.test.ts b/tests/stalwart.integration.test.ts
index 1c8f723..f098000 100644
--- a/tests/stalwart.integration.test.ts
+++ b/tests/stalwart.integration.test.ts
@@ -43,6 +43,12 @@ describe.skipIf(!serverUrl || !username || !password)('disposable Stalwart integ
from: [{ name: 'Fixture', email: identities[0].email }],
to: [{ name: 'Test', email: identities[0].email }],
subject: tag + ' ' + i,
+ ...(i === 60
+ ? {
+ 'header:List-Unsubscribe': '<https://list.example/unsubscribe>',
+ 'header:List-Unsubscribe-Post': 'List-Unsubscribe=One-Click',
+ }
+ : {}),
receivedAt: new Date(Date.UTC(2026, 8, 20, 0, i)).toISOString(),
textBody: [{ partId: 'text', type: 'text/plain' }],
bodyValues: { text: { value: 'Unicode: Grüße aus Wien. Search needle ' + i } },
@@ -76,6 +82,10 @@ describe.skipIf(!serverUrl || !username || !password)('disposable Stalwart integ
expect(new Set([...first.items, ...second.items].map((c) => c.key)).size).toBe(61)
const body = await provider.conversation(account, first.items[0].id)
expect(body[0].text).toContain('Grüße')
+ expect(body[0].unsubscribe).toEqual({
+ url: 'https://list.example/unsubscribe',
+ oneClick: true,
+ })
const result = await provider.update(account, {
[body[0].id]: { keywords: { $flagged: true, $seen: false } },
})