domma-cms 1.28.4 → 1.29.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/CHANGELOG.md +5 -0
  2. package/admin/js/lib/contacts-arrange.js +1 -1
  3. package/docs/DommaFeatures.md +5 -3
  4. package/package.json +4 -1
  5. package/plugins/_lib/admin/mail/body-menu.js +2 -0
  6. package/plugins/_lib/admin/mail/cms-host.js +22 -0
  7. package/plugins/_lib/admin/mail/compose-window.js +1027 -1021
  8. package/plugins/_lib/admin/mail/contacts.js +10 -8
  9. package/plugins/_lib/admin/mail/diagnostics-section.js +2 -0
  10. package/plugins/_lib/admin/mail/drag-drop.js +46 -0
  11. package/plugins/_lib/admin/mail/folder-tree.js +2 -0
  12. package/plugins/_lib/admin/mail/font-controls.js +6 -4
  13. package/plugins/_lib/admin/mail/fonts.js +1 -1
  14. package/plugins/_lib/admin/mail/host-context.js +87 -0
  15. package/plugins/_lib/admin/mail/identity.js +2 -0
  16. package/plugins/_lib/admin/mail/image-senders-section.js +2 -0
  17. package/plugins/_lib/admin/mail/keyboard.js +2 -0
  18. package/plugins/_lib/admin/mail/mail.css +1 -1
  19. package/plugins/_lib/admin/mail/mail.html +4 -0
  20. package/plugins/_lib/admin/mail/message-actions.js +2 -0
  21. package/plugins/_lib/admin/mail/oauth-panel.js +2 -0
  22. package/plugins/_lib/admin/mail/panes.js +2 -0
  23. package/plugins/_lib/admin/mail/reader-kit.js +2 -0
  24. package/plugins/_lib/admin/mail/reader-runtime.js +6534 -0
  25. package/plugins/_lib/admin/mail/reader-view.js +9 -6380
  26. package/plugins/_lib/admin/mail/resizable.js +2 -0
  27. package/plugins/_lib/admin/mail/restore-spam.js +13 -0
  28. package/plugins/_lib/admin/mail/rules.js +14 -0
  29. package/plugins/_lib/admin/mail/scheduling.js +4 -1
  30. package/plugins/_lib/admin/mail/section-kit.js +4 -1
  31. package/plugins/_lib/admin/mail/security-note.js +2 -0
  32. package/plugins/_lib/admin/mail/settings-section.js +11 -10
  33. package/plugins/_lib/admin/mail/templates.js +5 -2
  34. package/plugins/_lib/admin/mail/threads.js +2 -0
  35. package/plugins/_lib/admin/mail/vacation.js +16 -2
  36. package/plugins/_lib/admin/mail/when-closed.js +24 -2
  37. package/plugins/_lib/admin/ui/dates.js +2 -0
  38. package/plugins/_lib/admin/ui/resizable.js +7 -5
  39. package/plugins/_lib/contacts/mailContact.js +69 -0
  40. package/plugins/_lib/contacts/model.js +199 -0
  41. package/plugins/_lib/domma-mail-contacts.lock.json +10 -0
  42. package/plugins/_lib/domma-mail.lock.json +85 -0
  43. package/plugins/_lib/mail/accountFileStore.js +66 -0
  44. package/plugins/_lib/mail/accountRepository.js +617 -0
  45. package/plugins/_lib/mail/accountRoutes.js +130 -0
  46. package/plugins/_lib/mail/accounts.js +66 -940
  47. package/plugins/_lib/mail/automationLock.js +10 -0
  48. package/plugins/_lib/mail/bodyTokenProvider.js +36 -0
  49. package/plugins/_lib/mail/bodyTokens.js +5 -99
  50. package/plugins/_lib/mail/compose.js +5 -3
  51. package/plugins/_lib/mail/defaults.js +2 -0
  52. package/plugins/_lib/mail/envelope.js +3 -1
  53. package/plugins/_lib/mail/folderActions.js +83 -0
  54. package/plugins/_lib/mail/folderRuntime.js +109 -0
  55. package/plugins/_lib/mail/folders.js +4 -9
  56. package/plugins/_lib/mail/imapPool.js +11 -389
  57. package/plugins/_lib/mail/imapPoolRuntime.js +396 -0
  58. package/plugins/_lib/mail/mbox.js +2 -0
  59. package/plugins/_lib/mail/message-action.js +2 -0
  60. package/plugins/_lib/mail/messageSecurity.js +4 -111
  61. package/plugins/_lib/mail/messageSecurityProvider.js +116 -0
  62. package/plugins/_lib/mail/networkAddresses.js +77 -0
  63. package/plugins/_lib/mail/networkPolicy.js +62 -0
  64. package/plugins/_lib/mail/oauth.js +3 -451
  65. package/plugins/_lib/mail/oauthAccounts.js +178 -0
  66. package/plugins/_lib/mail/oauthRouteRuntime.js +282 -0
  67. package/plugins/_lib/mail/oauthRoutes.js +7 -289
  68. package/plugins/_lib/mail/oauthRuntime.js +456 -0
  69. package/plugins/_lib/mail/pollSchedule.js +2 -0
  70. package/plugins/_lib/mail/poller.js +4 -136
  71. package/plugins/_lib/mail/pollerRuntime.js +150 -0
  72. package/plugins/_lib/mail/priority.js +2 -0
  73. package/plugins/_lib/mail/publicMailbox.js +27 -0
  74. package/plugins/_lib/mail/readRoutes.js +14 -827
  75. package/plugins/_lib/mail/readRuntime.js +701 -0
  76. package/plugins/_lib/mail/render.js +2 -0
  77. package/plugins/_lib/mail/ruleRunner.js +27 -10
  78. package/plugins/_lib/mail/scheduler.js +11 -279
  79. package/plugins/_lib/mail/schedulerRuntime.js +259 -0
  80. package/plugins/_lib/mail/secretProvider.js +112 -0
  81. package/plugins/_lib/mail/secretbox.js +12 -225
  82. package/plugins/_lib/mail/send.js +21 -537
  83. package/plugins/_lib/mail/sieve.js +211 -0
  84. package/plugins/_lib/mail/sieveHost.js +24 -0
  85. package/plugins/_lib/mail/smtpService.js +553 -0
  86. package/plugins/_lib/mail/snoozeRuntime.js +60 -0
  87. package/plugins/_lib/mail/store.js +122 -64
  88. package/plugins/_lib/mail/subscriptions.js +11 -0
  89. package/plugins/_lib/mail/sync.js +64 -290
  90. package/plugins/_lib/mail/syncPlan.js +2 -0
  91. package/plugins/_lib/mail/syncRuntime.js +255 -0
  92. package/plugins/_lib/mail/syncSelection.js +2 -0
  93. package/plugins/_lib/mail/unreadBadge.js +2 -0
  94. package/plugins/_lib/mail/unsubscribe.js +2 -0
  95. package/plugins/_lib/mail/vacationRunner.js +3 -116
  96. package/plugins/_lib/mail/vacationRuntime.js +70 -0
  97. package/plugins/_lib/mail/write.js +44 -13
  98. package/server/routes/api/contacts.js +2 -196
@@ -1,840 +1,27 @@
1
- /**
2
- * The read-only mail surface, shared by every edition.
3
- *
4
- * `mail-reader` (free) is exactly these routes and nothing else. `email-pro`
5
- * registers them too and adds writing, sending and a synchronised store on
6
- * top - forking them instead would mean every IMAP quirk, every sanitiser
7
- * decision and every ownership check being fixed twice.
8
- *
9
- * Routes are relative, so they mount under whichever plugin registers them;
10
- * `pluginName` is needed only for the body URL and the log tags.
11
- *
12
- * ## Access control
13
- *
14
- * Every route resolves the mailbox through `accounts.getAccount(userId,
15
- * accountId)`, which takes the owner's id as well as the account's. There is
16
- * no lookup by account id alone, so one user's mailbox cannot be reached with
17
- * another user's token, and an id that is not the caller's answers 404 rather
18
- * than 403 - a wrong id and someone else's id should be indistinguishable.
19
- *
20
- * ## Read-only
21
- *
22
- * Nothing here writes to a mailbox. Mailboxes open with EXAMINE and fetches
23
- * use BODY.PEEK[], so reading never sets \Seen. `email-pro` must take its own
24
- * writable handle rather than relax this one: the free reader depends on it.
25
- *
26
- * @module _lib/mail/readRoutes
27
- */
28
-
1
+ /** CMS read-route adapter. Shared implementation is maintained in domma-mail. */
29
2
  import {listUsers} from '../../../server/services/users.js';
30
3
  import {getConfig} from '../../../server/config.js';
31
4
  import {checkMailHost, siteSmtpHost} from './hostGuard.js';
32
5
  import * as accounts from './accounts.js';
33
6
  import * as bodyTokens from './bodyTokens.js';
7
+ import * as renderer from './render.js';
34
8
  import {buildDiagnostics} from './diagnostics.js';
35
- import {ENVELOPE_QUERY, hasAttachment, parseHeaderBlock} from './envelope.js';
36
- import {describeError} from './imapPool.js';
37
9
  import {resolveSubscriptions} from './folders.js';
38
- import {readPriority} from './priority.js';
39
- import {listAttachments, MENU_BRIDGE_CSP, renderBody} from './render.js';
40
- import {readUnsubscribe} from './unsubscribe.js';
41
- import {MailKeyError} from './secretbox.js';
42
- import {unreadBadge} from './unreadBadge.js';
43
10
  import {parseReadable} from './messageSecurity.js';
11
+ import {registerReadRuntime} from './readRuntime.js';
44
12
 
45
- /**
46
- * The security facts the reader shows - nothing else of the provider's
47
- * answer (certificates, chains) goes to the browser.
48
- *
49
- * @param {object|null} security
50
- * @returns {object|null}
51
- */
52
- export function publicSecurity(security) {
53
- if (!security) return null;
54
- const signer = security.signer ?? null;
55
- return {
56
- encrypted: security.encrypted === true,
57
- decrypted: security.decrypted === true,
58
- signed: security.signed === true,
59
- verified: security.verified === true,
60
- trusted: security.trusted === true,
61
- fromMatches: security.fromMatches === true,
62
- signer: signer ? {
63
- emails: Array.isArray(signer.emails) ? signer.emails.map(String).slice(0, 10) : [],
64
- subject: String(signer.subject ?? ''),
65
- fingerprint: String(signer.fingerprint ?? ''),
66
- notAfter: signer.notAfter ?? null
67
- } : null,
68
- problem: security.problem ? String(security.problem) : null
69
- };
70
- }
71
-
72
- /**
73
- * Flatten an envelope address list into something the browser can render.
74
- *
75
- * @param {Array<{name?: string, address?: string}>} list
76
- * @returns {{name: string, address: string}[]}
77
- */
78
- function addresses(list) {
79
- return (list ?? []).map(entry => ({
80
- name: entry.name ?? '',
81
- address: entry.address ?? ''
82
- }));
83
- }
84
-
85
- export function registerReadRoutes(fastify, {auth, hooks, config, pool, pluginName, cachedList = null, diagnostics = null, unreadBadge: withUnreadBadge = false}) {
86
- const {authenticate} = auth;
87
-
88
- // A deleted user's stored mailbox passwords must not outlive their
89
- // account. The hook is the fast path; the sweep below is what catches a
90
- // user deleted while this plugin happened to be disabled.
91
- hooks?.on?.('user:deleted', async ({id} = {}) => {
92
- if (!id) return;
93
- try {
94
- pool.releaseUser(id);
95
- const removed = await accounts.deleteAccountsForUser(id);
96
- if (removed) {
97
- fastify.log.info(`[${pluginName}] removed ${removed} stored mailbox(es) for deleted user ${id}`);
98
- }
99
- } catch (err) {
100
- fastify.log.error({err}, `[${pluginName}] failed to clean up mailboxes for a deleted user`);
101
- }
102
- });
103
-
104
- // Fire-and-forget: a slow or failing sweep must never delay boot, and
105
- // there is nothing a user can do about it if it does fail.
106
- (async () => {
107
- try {
108
- const removed = await accounts.pruneOrphans(new Set((await listUsers()).map(u => u.id)));
109
- if (removed) {
110
- fastify.log.info(`[${pluginName}] pruned ${removed} stored mailbox(es) with no matching user`);
111
- }
112
- } catch (err) {
113
- fastify.log.warn({err}, `[${pluginName}] orphan mailbox sweep failed`);
114
- }
115
- })();
116
-
117
- /**
118
- * Resolve the caller's user id.
119
- *
120
- * @param {object} request
121
- * @returns {string|null}
122
- */
123
- function userId(request) {
124
- return request.user?.id ?? request.user?.sub ?? null;
125
- }
126
-
127
- /**
128
- * Load the mailbox named by ?account= and open its stored password.
129
- *
130
- * Returns null after replying, so handlers can `if (!ctx) return;`.
131
- *
132
- * @param {object} request
133
- * @param {object} reply
134
- * @returns {Promise<{uid: string, account: object, connection: object, poolKey: string}|null>}
135
- */
136
- async function context(request, reply) {
137
- const uid = userId(request);
138
- if (!uid) {
139
- reply.code(401).send({error: 'Authentication required.'});
140
- return null;
141
- }
142
-
143
- const requested = request.query.account ? String(request.query.account) : null;
144
- const owned = await accounts.listAccounts(uid);
145
-
146
- if (!owned.length) {
147
- reply.code(404).send({error: 'No mailbox is set up yet.', needsSetup: true});
148
- return null;
149
- }
150
-
151
- // No ?account= means "the first one" - the reader opens on a mailbox
152
- // rather than asking which before it has shown anything.
153
- const account = requested ? owned.find(a => a.id === requested) : owned[0];
154
- if (!account) {
155
- reply.code(404).send({error: 'That mailbox does not exist.'});
156
- return null;
157
- }
158
-
159
- try {
160
- return {
161
- uid,
162
- account,
163
- connection: accounts.toConnection(account),
164
- // Keyed per account, not per user: one user may hold several
165
- // mailboxes and each needs its own connection.
166
- poolKey: `${uid}:${account.id}`
167
- };
168
- } catch (err) {
169
- if (err instanceof MailKeyError) {
170
- // needsPassword stays true for a dead sign-in too: older
171
- // screens already open settings on it, which is the right place.
172
- reply.code(409).send({error: err.message, needsPassword: true,
173
- ...(err.needsReconnect ? {needsReconnect: true} : {}), accountId: account.id});
174
- return null;
175
- }
176
- throw err;
177
- }
178
- }
179
-
180
- /**
181
- * Run an IMAP operation, turning transport failures into tidy 502s
182
- * (resolving to undefined after replying).
183
- *
184
- * @param {object} reply
185
- * @param {() => Promise<any>} fn
186
- * @returns {Promise<any>}
187
- */
188
- async function imap(reply, fn) {
189
- try {
190
- return await fn();
191
- } catch (err) {
192
- fastify.log.error({err}, `[${pluginName}] IMAP operation failed`);
193
- reply.code(502).send({error: describeError(err)});
194
- // undefined, NOT null: an async handler that resolves to anything
195
- // but undefined has it sent as well, and Fastify logs "Reply was
196
- // already sent" over the 502 just sent (every IMAP failure did it,
197
- // /folders?counts=1 most visibly).
198
- return undefined;
199
- }
200
- }
201
-
202
- // -------------------------------------------------------------------------
203
- // Accounts
204
- // -------------------------------------------------------------------------
205
-
206
- /**
207
- * GET /unread - the sidebar badge (the plugin.json countUrl). Opt-in:
208
- * Email Pro counts from its store and registers its own. INBOX only, by
209
- * STATUS (no select, so nothing can be marked read), and kept a minute
210
- * per user - the sidebar asks every minute from every open admin tab.
211
- */
212
- // The badge's minute-long cache, per user. Forgotten whenever that user's
213
- // mailboxes change or a read goes to the server anyway (a Refresh, the
214
- // folder counts), so the screen's `dm:sidebar-count` after it gets the
215
- // new number rather than the one from before.
216
- //
217
- // Each forget also bumps the user's generation: a badge request that was
218
- // already on its way (asking IMAP) when the forget happened carries the
219
- // old one, and must not write its now-stale count back into the cache.
220
- const badgeCache = new Map();
221
- const badgeGen = new Map();
222
- const forgetBadge = (uid) => {
223
- if (!uid) return;
224
- badgeCache.delete(uid);
225
- badgeGen.set(uid, (badgeGen.get(uid) ?? 0) + 1);
226
- };
227
- // A mailbox that failed (a changed password, a server down) is not asked
228
- // again every minute: that is a failed IMAP login per minute per open tab,
229
- // enough to trip fail2ban or a provider lockout. It backs off 5 min, doubling
230
- // to an hour; saving the account (a new updatedAt) retries straight away, and
231
- // so does the mailbox answering anywhere else (a Refresh, a folder listing) -
232
- // or a working Refresh would leave the badge red for up to an hour.
233
- const failures = new Map();
234
- const failKey = (uid, account) => `${uid}:${account.id}:${account.updatedAt || account.createdAt || ''}`;
235
- const clearFailures = (uid, accountId) => {
236
- const prefix = `${uid}:${accountId}:`;
237
- for (const key of failures.keys()) if (key.startsWith(prefix)) failures.delete(key);
238
- };
239
- if (withUnreadBadge) {
240
- fastify.get('/unread', {preHandler: [authenticate]}, async (request, reply) => {
241
- const uid = userId(request);
242
- if (!uid) return reply.code(401).send({error: 'Authentication required.'});
243
- const hit = badgeCache.get(uid);
244
- if (hit && Date.now() - hit.at < 60_000) return hit.value;
245
- const gen = badgeGen.get(uid) ?? 0;
246
- const boxes = [];
247
- for (const account of await accounts.listAccounts(uid)) {
248
- const key = failKey(uid, account);
249
- const failed = failures.get(key);
250
- if (failed && Date.now() < failed.until) { boxes.push({label: account.label, unseen: null}); continue; }
251
- try {
252
- const status = await pool.withClient(`${uid}:${account.id}`, accounts.toConnection(account),
253
- client => client.status('INBOX', {unseen: true, uidNext: true}));
254
- failures.delete(key);
255
- boxes.push({id: account.id, label: account.label, unseen: status?.unseen ?? 0, uidNext: status?.uidNext ?? null});
256
- } catch {
257
- const wait = Math.min((failed?.wait || 150_000) * 2, 3_600_000);
258
- failures.set(key, {wait, until: Date.now() + wait});
259
- boxes.push({label: account.label, unseen: null});
260
- }
261
- }
262
- const value = unreadBadge(boxes);
263
- // Only if nothing changed while IMAP was being asked.
264
- if ((badgeGen.get(uid) ?? 0) === gen) badgeCache.set(uid, {at: Date.now(), value});
265
- return value;
266
- });
267
- }
268
-
269
- /** Where this server may be asked to connect - see hostGuard.js. */
270
- const guardHost = (host, port) => checkMailHost(host, {allowPrivate: config.allowPrivateMailHosts === true, ownSmtpHost: siteSmtpHost(getConfig), port, kind: 'imap'});
271
- // Each test is a connection attempt whose answer says what is listening: a few a minute per address is plenty.
272
- const testLimit = {config: {rateLimit: {max: 10, timeWindow: '1 minute'}}};
273
-
274
- /** GET /accounts - every mailbox this user owns, without passwords. */
275
- fastify.get('/accounts', {preHandler: [authenticate]}, async (request, reply) => {
276
- const uid = userId(request);
277
- if (!uid) return reply.code(401).send({error: 'Authentication required.'});
278
- return {accounts: (await accounts.listAccounts(uid)).map(accounts.toPublic)};
279
- });
280
-
281
- /** POST /accounts - add a mailbox. */
282
- fastify.post('/accounts', {preHandler: [authenticate]}, async (request, reply) => {
283
- const uid = userId(request);
284
- if (!uid) return reply.code(401).send({error: 'Authentication required.'});
285
-
286
- const result = accounts.validate(request.body ?? {}, null);
287
- if (!result.ok) return reply.code(400).send({error: result.error});
288
- const where = await guardHost(result.value.host, result.value.port);
289
- if (!where.ok) return reply.code(400).send({error: where.error});
290
-
291
- const saved = await accounts.saveAccount(uid, null, result.value);
292
- forgetBadge(uid);
293
- return {account: accounts.toPublic(saved)};
294
- });
295
-
296
- /** PUT /accounts/:id - update one. A blank password keeps the stored one. */
297
- fastify.put('/accounts/:id', {preHandler: [authenticate]}, async (request, reply) => {
298
- const uid = userId(request);
299
- if (!uid) return reply.code(401).send({error: 'Authentication required.'});
300
-
301
- const existing = await accounts.getAccount(uid, request.params.id);
302
- if (!existing) return reply.code(404).send({error: 'That mailbox does not exist.'});
303
-
304
- const result = accounts.validate(request.body ?? {}, existing);
305
- if (!result.ok) return reply.code(400).send({error: result.error});
306
- // A signed-in mailbox's servers are the provider's and cannot be edited here.
307
- const where = result.value.oauth ? {ok: true} : await guardHost(result.value.host, result.value.port);
308
- if (!where.ok) return reply.code(400).send({error: where.error});
13
+ export {publicSecurity} from './readRuntime.js';
309
14
 
310
- const saved = await accounts.saveAccount(uid, request.params.id, result.value);
311
- // The pooled connection was opened with the old details.
312
- pool.release(`${uid}:${request.params.id}`);
313
- forgetBadge(uid);
314
- return {account: accounts.toPublic(saved)};
315
- });
316
-
317
- /** PUT /accounts/:id/default - make it the default mailbox ({isDefault: false} clears it). */
318
- fastify.put('/accounts/:id/default', {preHandler: [authenticate]}, async (request, reply) => {
319
- const uid = userId(request);
320
- if (!uid) return reply.code(401).send({error: 'Authentication required.'});
321
-
322
- const saved = await accounts.setDefaultAccount(uid, request.params.id, request.body?.isDefault !== false);
323
- if (!saved) return reply.code(404).send({error: 'That mailbox does not exist.'});
324
- return {account: accounts.toPublic(saved)};
325
- });
326
-
327
- /** DELETE /accounts/:id - forget a mailbox and its password. */
328
- fastify.delete('/accounts/:id', {preHandler: [authenticate]}, async (request, reply) => {
329
- const uid = userId(request);
330
- if (!uid) return reply.code(401).send({error: 'Authentication required.'});
331
-
332
- pool.release(`${uid}:${request.params.id}`);
333
- const existing = await accounts.getAccount(uid, request.params.id);
334
- const deleted = await accounts.deleteAccount(uid, request.params.id);
335
- if (!deleted) return reply.code(404).send({error: 'That mailbox does not exist.'});
336
- forgetBadge(uid);
337
- // A signed-in mailbox hands its sign-in back where the provider allows
338
- // it (Google). Best-effort and after the delete: forgetting it here
339
- // must not wait on, or fail because of, the provider.
340
- if (accounts.isOAuth?.(existing)) {
341
- import('./oauth.js').then(m => m.revokeAccount(existing)).catch(() => {});
15
+ export function registerReadRoutes(fastify, options) {
16
+ return registerReadRuntime(fastify, {
17
+ ...options, accounts, bodyTokens, renderer, parseReadable, buildDiagnostics, resolveSubscriptions,
18
+ listUserIds: async () => (await listUsers()).map(user => user.id),
19
+ guardHost: (host, port) => checkMailHost(host, {
20
+ allowPrivate: options.config.allowPrivateMailHosts === true,
21
+ ownSmtpHost: siteSmtpHost(getConfig), port, kind: 'imap'
22
+ }),
23
+ onDelete: async account => {
24
+ if (accounts.isOAuth?.(account)) await (await import('./oauth.js')).revokeAccount(account);
342
25
  }
343
- return {deleted: true};
344
- });
345
-
346
- /**
347
- * POST /accounts/test - try details without saving them.
348
- *
349
- * Accepts a body so a mailbox can be tested before it is saved. When the
350
- * password field is blank and `id` names an existing mailbox, the stored
351
- * password is used - otherwise editing a hostname would mean retyping it.
352
- */
353
- fastify.post('/accounts/test', {preHandler: [authenticate], ...testLimit}, async (request, reply) => {
354
- const uid = userId(request);
355
- if (!uid) return reply.code(401).send({error: 'Authentication required.'});
356
-
357
- const body = request.body ?? {};
358
- const existing = body.id ? await accounts.getAccount(uid, body.id) : null;
359
- if (body.id && !existing) return reply.code(404).send({error: 'That mailbox does not exist.'});
360
-
361
- const result = accounts.validate(body, existing);
362
- if (!result.ok) return reply.code(400).send({error: result.error});
363
- // A signed-in mailbox is tested as stored: the provider's own server,
364
- // its token renewed if need be. Nothing typed is involved.
365
- if (result.value.oauth) {
366
- try {
367
- return await pool.test(accounts.toConnection(existing));
368
- } catch (err) {
369
- if (err instanceof MailKeyError) return {ok: false, error: err.message};
370
- throw err;
371
- }
372
- }
373
- const where = await guardHost(result.value.host, result.value.port);
374
- if (!where.ok) return reply.code(400).send({error: where.error});
375
-
376
- let pass = result.value.password;
377
- if (!pass) {
378
- try {
379
- pass = accounts.toConnection(existing).auth.pass;
380
- } catch (err) {
381
- if (err instanceof MailKeyError) return reply.code(409).send({error: err.message});
382
- throw err;
383
- }
384
- }
385
-
386
- return pool.test({
387
- host: result.value.host,
388
- port: result.value.port,
389
- secure: result.value.secure,
390
- auth: {user: result.value.username, pass}
391
- });
392
- });
393
-
394
- // -------------------------------------------------------------------------
395
- // Reading
396
- // -------------------------------------------------------------------------
397
-
398
- /** GET /folders - the mailbox tree, flattened. */
399
- fastify.get('/folders', {preHandler: [authenticate]}, async (request, reply) => {
400
- const ctx = await context(request, reply);
401
- if (!ctx) return;
402
-
403
- return imap(reply, async () => {
404
- const list = await pool.withClient(ctx.poolKey, ctx.connection, client => client.list());
405
- // `subscribed !== false` was wrong: imapflow reports an
406
- // unsubscribed mailbox as `undefined`, so every hidden folder read
407
- // as visible. See resolveSubscriptions for why it is not simply
408
- // `=== true` either.
409
- const visible = resolveSubscriptions(list);
410
- const folders = list
411
- .filter(box => !box.flags?.has('\\Noselect'))
412
- .map(box => ({
413
- path: box.path,
414
- name: box.name,
415
- delimiter: box.delimiter,
416
- specialUse: box.specialUse ?? null,
417
- subscribed: visible.get(box.path) !== false
418
- }));
419
-
420
- // Unread counts are opt-in. STATUS is a round trip per folder and
421
- // a real mailbox here has over a hundred of them, so asking for
422
- // them is a choice the caller makes rather than a cost every
423
- // folder listing pays.
424
- if (request.query.counts === '1') {
425
- await withUnreadCounts(ctx, folders);
426
- forgetBadge(ctx.uid);
427
- }
428
- // The mailbox answered: it is not failing any more.
429
- clearFailures(ctx.uid, ctx.account.id);
430
-
431
- return {accountId: ctx.account.id, folders};
432
- });
433
- });
434
-
435
- /**
436
- * Fill in each folder's unread count.
437
- *
438
- * STATUS asks without selecting, so it does not disturb the open mailbox
439
- * and cannot mark anything read. Run with a small concurrency: a hundred
440
- * simultaneous commands on one connection is not faster, and on several
441
- * connections it is how a server starts refusing them.
442
- *
443
- * A folder that refuses STATUS is left without a count rather than
444
- * failing the listing - some servers will not report on some folders, and
445
- * a missing badge is better than an empty folder pane.
446
- *
447
- * @param {object} ctx
448
- * @param {object[]} folders
449
- * @returns {Promise<void>}
450
- */
451
- async function withUnreadCounts(ctx, folders) {
452
- const queue = [...folders];
453
- const worker = async () => {
454
- while (queue.length) {
455
- const folder = queue.shift();
456
- try {
457
- const status = await pool.withClient(ctx.poolKey, ctx.connection,
458
- client => client.status(folder.path, {unseen: true, messages: true}));
459
- folder.unseen = status?.unseen ?? 0;
460
- folder.messages = status?.messages ?? 0;
461
- } catch {
462
- folder.unseen = null;
463
- }
464
- }
465
- };
466
- // One connection, so the commands serialise anyway - this just avoids
467
- // building a hundred pending promises.
468
- await Promise.all([worker(), worker()]);
469
- }
470
-
471
- /**
472
- * GET /messages - one page of envelopes, newest first.
473
- *
474
- * Paged by sequence number from the end of the mailbox rather than by
475
- * search: it is one round trip, and "newest first" is what a reader wants.
476
- */
477
- fastify.get('/messages', {preHandler: [authenticate]}, async (request, reply) => {
478
- const ctx = await context(request, reply);
479
- if (!ctx) return;
480
-
481
- const folder = String(request.query.folder || 'INBOX');
482
- const page = Math.max(1, Number(request.query.page) || 1);
483
- const limit = Math.min(
484
- config.maxListLimit,
485
- Math.max(1, Number(request.query.limit) || config.listLimit)
486
- );
487
-
488
- // The Pro edition mirrors chosen folders, and a mirrored folder is
489
- // answered from the store - instant, and no connection needed. It
490
- // returns null for anything not mirrored, which falls through to the
491
- // wire exactly as the free edition always does.
492
- if (request.query.fresh === '1') {
493
- // A Refresh: ask the server again for the badge, back-off or not.
494
- forgetBadge(ctx.uid);
495
- clearFailures(ctx.uid, ctx.account.id);
496
- }
497
- if (cachedList && request.query.fresh !== '1') {
498
- try {
499
- const cached = await cachedList({
500
- account: ctx.account, userId: ctx.uid, folder, page, limit
501
- });
502
- if (cached) {
503
- return {...cached, page, limit, folder, accountId: ctx.account.id, source: 'cache'};
504
- }
505
- } catch (err) {
506
- // A broken mirror must never make mail unreadable; fall back.
507
- fastify.log.warn({err}, `[${pluginName}] cached list failed - falling back to IMAP`);
508
- }
509
- }
510
-
511
- return imap(reply, () => pool.withMailbox(ctx.poolKey, ctx.connection, folder, async (client, mailbox) => {
512
- const total = mailbox.exists;
513
- if (!total) return {messages: [], total: 0, page, limit, folder, accountId: ctx.account.id};
514
-
515
- const end = total - (page - 1) * limit;
516
- if (end < 1) return {messages: [], total, page, limit, folder, accountId: ctx.account.id};
517
- const start = Math.max(1, end - limit + 1);
518
-
519
- const messages = [];
520
- // The same query the mirror uses, so a folder listed live and one
521
- // listed from the mirror produce identical rows - including the
522
- // priority headers, which are not part of the envelope.
523
- for await (const message of client.fetch(`${start}:${end}`, ENVELOPE_QUERY)) {
524
- messages.push({
525
- uid: message.uid,
526
- seq: message.seq,
527
- subject: message.envelope?.subject ?? '',
528
- from: addresses(message.envelope?.from),
529
- to: addresses(message.envelope?.to),
530
- date: message.envelope?.date ?? null,
531
- size: message.size ?? 0,
532
- // The raw flag list, not just the booleans derived from it.
533
- // The list view keeps an observable of this per row, so
534
- // omitting it left every message on a non-mirrored folder
535
- // initialised with no flags - and therefore drawn unread,
536
- // whatever the server said.
537
- flags: [...(message.flags ?? [])],
538
- seen: message.flags?.has('\\Seen') ?? false,
539
- flagged: message.flags?.has('\\Flagged') ?? false,
540
- answered: message.flags?.has('\\Answered') ?? false,
541
- hasAttachment: hasAttachment(message.bodyStructure),
542
- priority: readPriority(parseHeaderBlock(message.headers))
543
- });
544
- }
545
-
546
- // The server returns ascending sequence order regardless of range.
547
- messages.reverse();
548
- return {messages, total, page, limit, folder, accountId: ctx.account.id};
549
- }));
550
- });
551
-
552
- /** GET /messages/:uid - one message, rendered for the reader pane. */
553
- fastify.get('/messages/:uid', {preHandler: [authenticate]}, async (request, reply) => {
554
- const ctx = await context(request, reply);
555
- if (!ctx) return;
556
-
557
- const folder = String(request.query.folder || 'INBOX');
558
- const uid = Number(request.params.uid);
559
- if (!Number.isInteger(uid) || uid < 1) {
560
- return reply.code(400).send({error: 'Invalid message id.'});
561
- }
562
- // Three ways images may load, and only one of them is remembered.
563
- // The per-message override is deliberately not stored - it is a
564
- // decision about one message. A sender the user has explicitly chosen
565
- // to trust is stored, listed in settings and revocable, which is a
566
- // different act from silently remembering a click.
567
- const requestedNow = request.query.images === 'load';
568
-
569
- return imap(reply, () => pool.withMailbox(ctx.poolKey, ctx.connection, folder, async (client) => {
570
- const fetched = await client.fetchOne(uid, {source: true, flags: true}, {uid: true});
571
- if (!fetched || !fetched.source) {
572
- reply.code(404).send({error: 'That message is no longer in this folder.'});
573
- return null;
574
- }
575
- if (fetched.source.length > config.maxMessageBytes) {
576
- reply.code(413).send({error: 'That message is too large to display.'});
577
- return null;
578
- }
579
-
580
- const {parsed, security} = await parseReadable(fetched.source, {account: ctx.account, userId: ctx.uid});
581
- const from = addresses(parsed.from?.value);
582
- const senderTrusted = accounts.sendersImagesTrusted(ctx.account, from);
583
- const allowRemoteImages = requestedNow || config.allowRemoteImages || senderTrusted;
584
-
585
- const rendered = renderBody(parsed, {
586
- allowRemoteImages,
587
- inlineImageMaxBytes: config.inlineImageMaxBytes
588
- });
589
-
590
- return {
591
- uid,
592
- folder,
593
- accountId: ctx.account.id,
594
- senderTrusted,
595
- // Offered only when remembering is switched on for this mailbox.
596
- canTrustSender: ctx.account.rememberImageSenders !== false
597
- && !senderTrusted
598
- && !!from[0]?.address,
599
- trustableSender: from[0]?.address ?? null,
600
- subject: parsed.subject ?? '',
601
- from,
602
- to: addresses(parsed.to?.value),
603
- cc: addresses(parsed.cc?.value),
604
- // Carried so the expanded header can show them. Reply-To
605
- // especially: a sender who sets it is asking for replies
606
- // somewhere other than where the message came from, and until
607
- // now the reader hid that entirely. Bcc only ever appears on a
608
- // copy in Sent - a received message never carries one, which
609
- // is the point of it.
610
- replyTo: addresses(parsed.replyTo?.value),
611
- bcc: addresses(parsed.bcc?.value),
612
- date: parsed.date ?? null,
613
- messageId: parsed.messageId ?? null,
614
- // From the parsed message here rather than a header fetch:
615
- // the whole source is already in hand at this point.
616
- priority: readPriority(parsed.headers),
617
- // A URL, not the markup: an iframe filled with srcdoc inherits
618
- // the admin's CSP (img-src 'self' data: blob:), which silently
619
- // blocks every remote image no matter what the reader asks for.
620
- // Served from a route, the document carries its own policy.
621
- bodyUrl: `/api/plugins/${pluginName}/body?token=${encodeURIComponent(bodyTokens.sign({
622
- userId: ctx.uid,
623
- accountId: ctx.account.id,
624
- folder,
625
- uid,
626
- images: allowRemoteImages
627
- }))}`,
628
- isHtml: rendered.isHtml,
629
- blockedImages: rendered.blockedImages,
630
- remoteImagesLoaded: allowRemoteImages,
631
- // What this message offers by way of getting off the list it
632
- // came from. Read in both editions, because knowing there is
633
- // an unsubscribe link is reading; acting on it is a write and
634
- // lives in the Pro edition.
635
- unsubscribe: readUnsubscribe(parsed.headers),
636
- // Signed and/or encrypted (S/MIME), and what came of checking -
637
- // null for an ordinary message, or where no edition can tell.
638
- security: publicSecurity(security),
639
- attachments: listAttachments(parsed.attachments)
640
- };
641
- }));
642
- });
643
-
644
- /**
645
- * GET /body - the rendered message, as its own document.
646
- *
647
- * Authorised by a short-lived signed token rather than a bearer header,
648
- * because an `<iframe src>` cannot send one. The token names a single
649
- * message for a single user and expires in minutes.
650
- *
651
- * The CSP here is the point of the whole route: served as its own
652
- * document, this policy applies instead of the admin's, so a message the
653
- * reader has chosen to load images for can actually load them - while
654
- * scripts, frames and everything else stay refused.
655
- */
656
- fastify.get('/body', {
657
- // Helmet adds X-Frame-Options in an onSend hook, which runs after
658
- // the handler - so removing it in there was too early and the
659
- // browser kept warning, once per message, that it was ignoring the
660
- // header in favour of the frame-ancestors this route already sets.
661
- // Helmet writes onto the raw response rather than Fastify's header
662
- // store, so reply.removeHeader() never sees it - it has to come off
663
- // the raw response, and only for this route.
664
- onSend: async (request, reply, payload) => {
665
- reply.removeHeader('X-Frame-Options');
666
- reply.raw.removeHeader?.('X-Frame-Options');
667
- return payload;
668
- }
669
- }, async (request, reply) => {
670
- const grant = bodyTokens.verify(String(request.query.token ?? ''));
671
- if (!grant) {
672
- return reply.code(403).type('text/plain').send('This message link has expired. Reopen the message.');
673
- }
674
-
675
- const account = await accounts.getAccount(grant.userId, grant.accountId);
676
- if (!account) return reply.code(404).type('text/plain').send('That mailbox no longer exists.');
677
-
678
- let connection;
679
- try {
680
- connection = accounts.toConnection(account);
681
- } catch (err) {
682
- if (err instanceof MailKeyError) return reply.code(409).type('text/plain').send(err.message);
683
- throw err;
684
- }
685
-
686
- const poolKey = `${grant.userId}:${account.id}`;
687
- try {
688
- const document = await pool.withMailbox(poolKey, connection, grant.folder, async (client) => {
689
- const fetched = await client.fetchOne(grant.uid, {source: true}, {uid: true});
690
- if (!fetched || !fetched.source) return null;
691
- if (fetched.source.length > config.maxMessageBytes) return null;
692
-
693
- const {parsed} = await parseReadable(fetched.source, {account, userId: grant.userId});
694
- return renderBody(parsed, {
695
- allowRemoteImages: grant.images,
696
- inlineImageMaxBytes: config.inlineImageMaxBytes
697
- }).document;
698
- });
699
-
700
- if (!document) return reply.code(404).type('text/plain').send('That message is no longer available.');
701
-
702
- return reply
703
- .type('text/html; charset=utf-8')
704
- .header('Content-Security-Policy',
705
- "default-src 'none'; " +
706
- (grant.images ? "img-src data: http: https:; " : "img-src data:; ") +
707
- `style-src 'unsafe-inline'; font-src data:; script-src ${MENU_BRIDGE_CSP}; frame-ancestors 'self'`)
708
- .header('X-Content-Type-Options', 'nosniff')
709
- .header('Referrer-Policy', 'no-referrer')
710
- // Someone else's mail has no business in a cache.
711
- .header('Cache-Control', 'no-store, no-cache, must-revalidate, private')
712
- .send(document);
713
- } catch (err) {
714
- fastify.log.error({err}, `[${pluginName}] failed to render a message body`);
715
- return reply.code(502).type('text/plain').send(describeError(err));
716
- }
717
- });
718
-
719
- /**
720
- * GET /diagnostics - what this build is, and what it is configured with.
721
- *
722
- * Exists so a bug report can start from evidence. No secrets: hostnames
723
- * and usernames identify a mailbox, the key SOURCE is reported without the
724
- * key, and no password appears sealed or otherwise.
725
- */
726
- fastify.get('/diagnostics', {preHandler: [authenticate]}, async (request, reply) => {
727
- const uid = userId(request);
728
- if (!uid) return reply.code(401).send({error: 'Authentication required.'});
729
- return buildDiagnostics({pluginName, userId: uid, config, extra: diagnostics});
730
- });
731
-
732
- /**
733
- * GET /image-senders - the senders whose images always load.
734
- */
735
- fastify.get('/image-senders', {preHandler: [authenticate]}, async (request, reply) => {
736
- const ctx = await context(request, reply);
737
- if (!ctx) return;
738
- return {
739
- accountId: ctx.account.id,
740
- remember: ctx.account.rememberImageSenders !== false,
741
- senders: Array.isArray(ctx.account.imageSenders) ? ctx.account.imageSenders : []
742
- };
743
- });
744
-
745
- /**
746
- * POST /image-senders - trust a sender's images from now on.
747
- */
748
- fastify.post('/image-senders', {preHandler: [authenticate]}, async (request, reply) => {
749
- const ctx = await context(request, reply);
750
- if (!ctx) return;
751
-
752
- if (ctx.account.rememberImageSenders === false) {
753
- return reply.code(409).send({error: 'Remembering senders is switched off for this mailbox.'});
754
- }
755
-
756
- const address = String(request.body?.address ?? '').trim();
757
- if (!address) return reply.code(400).send({error: 'A sender address is required.'});
758
-
759
- const saved = await accounts.trustImageSender(ctx.uid, ctx.account.id, address);
760
- if (!saved) return reply.code(404).send({error: 'That mailbox does not exist.'});
761
- return {senders: saved.imageSenders ?? []};
762
- });
763
-
764
- /**
765
- * DELETE /image-senders - stop trusting one.
766
- */
767
- fastify.delete('/image-senders', {preHandler: [authenticate]}, async (request, reply) => {
768
- const ctx = await context(request, reply);
769
- if (!ctx) return;
770
- const address = String(request.query.address ?? '').trim();
771
- if (!address) return reply.code(400).send({error: 'A sender address is required.'});
772
-
773
- const saved = await accounts.untrustImageSender(ctx.uid, ctx.account.id, address);
774
- if (!saved) return reply.code(404).send({error: 'That mailbox does not exist.'});
775
- return {senders: saved.imageSenders ?? []};
776
- });
777
-
778
- /**
779
- * PUT /image-senders/remember - turn the whole feature on or off.
780
- *
781
- * Switching it off ignores the list without discarding it: turning it back
782
- * on should not mean rebuilding something someone curated.
783
- */
784
- fastify.put('/image-senders/remember', {preHandler: [authenticate]}, async (request, reply) => {
785
- const ctx = await context(request, reply);
786
- if (!ctx) return;
787
- const saved = await accounts.setRememberImageSenders(ctx.uid, ctx.account.id, request.body?.remember !== false);
788
- if (!saved) return reply.code(404).send({error: 'That mailbox does not exist.'});
789
- return {remember: saved.rememberImageSenders !== false, senders: saved.imageSenders ?? []};
790
- });
791
-
792
- /**
793
- * GET /messages/:uid/attachments/:index - download one part.
794
- *
795
- * Re-fetches and re-parses the message rather than caching it. Phase 1
796
- * keeps no message store at all, and correctness beats a second copy of
797
- * someone's mail sitting in memory.
798
- */
799
- fastify.get('/messages/:uid/attachments/:index', {preHandler: [authenticate]}, async (request, reply) => {
800
- const ctx = await context(request, reply);
801
- if (!ctx) return;
802
-
803
- const folder = String(request.query.folder || 'INBOX');
804
- const uid = Number(request.params.uid);
805
- const index = Number(request.params.index);
806
- if (!Number.isInteger(uid) || !Number.isInteger(index) || index < 0) {
807
- return reply.code(400).send({error: 'Invalid attachment reference.'});
808
- }
809
-
810
- return imap(reply, () => pool.withMailbox(ctx.poolKey, ctx.connection, folder, async (client) => {
811
- const fetched = await client.fetchOne(uid, {source: true}, {uid: true});
812
- if (!fetched || !fetched.source) {
813
- reply.code(404).send({error: 'That message is no longer in this folder.'});
814
- return null;
815
- }
816
- // The same ceiling as /messages/:uid and /body: without it a hostile
817
- // 200 MB message cost a 200 MB buffer plus mailparser output per click.
818
- if (fetched.source.length > config.maxMessageBytes) {
819
- reply.code(413).send({error: 'That message is too large to open its attachments.'});
820
- return null;
821
- }
822
-
823
- const {parsed} = await parseReadable(fetched.source, {account: ctx.account, userId: ctx.uid});
824
- const attachment = parsed.attachments?.[index];
825
- if (!attachment) {
826
- reply.code(404).send({error: 'That attachment is not on this message.'});
827
- return null;
828
- }
829
-
830
- const filename = (attachment.filename || `attachment-${index + 1}`).replace(/["\\\r\n]/g, '_');
831
- reply
832
- .header('Content-Type', attachment.contentType || 'application/octet-stream')
833
- // Always an attachment, never inline: nothing from a stranger's
834
- // mail should render in the admin's own origin.
835
- .header('Content-Disposition', `attachment; filename="${filename}"`)
836
- .header('X-Content-Type-Options', 'nosniff');
837
- return reply.send(attachment.content);
838
- }));
839
26
  });
840
27
  }