emailengine-app 2.79.9 → 2.80.1

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 (146) hide show
  1. package/.release-please-manifest.json +1 -1
  2. package/CHANGELOG.md +73 -0
  3. package/README.md +6 -0
  4. package/config/default.toml +5 -1
  5. package/data/google-crawlers.json +1 -1
  6. package/encrypt.js +43 -0
  7. package/lib/account/account-state.js +62 -0
  8. package/lib/account.js +34 -2
  9. package/lib/api-routes/account-routes.js +101 -1
  10. package/lib/api-routes/blocklist-routes.js +24 -0
  11. package/lib/api-routes/delivery-test-routes.js +6 -0
  12. package/lib/api-routes/gateway-routes.js +30 -0
  13. package/lib/api-routes/index.js +1 -1
  14. package/lib/api-routes/license-routes.js +10 -0
  15. package/lib/api-routes/mcp-oauth-routes.js +3 -3
  16. package/lib/api-routes/mcp-routes.js +39 -21
  17. package/lib/api-routes/oauth2-app-routes.js +38 -1
  18. package/lib/api-routes/outbox-routes.js +6 -0
  19. package/lib/api-routes/permission-map.js +208 -43
  20. package/lib/api-routes/pubsub-routes.js +8 -0
  21. package/lib/api-routes/route-helpers.js +58 -11
  22. package/lib/api-routes/settings-routes.js +67 -6
  23. package/lib/api-routes/stats-routes.js +6 -0
  24. package/lib/api-routes/template-routes.js +21 -0
  25. package/lib/api-routes/token-routes.js +34 -2
  26. package/lib/api-routes/webhook-route-routes.js +13 -0
  27. package/lib/autodetect-imap-settings.js +349 -47
  28. package/lib/cert-handler.js +62 -0
  29. package/lib/consts.js +50 -6
  30. package/lib/db.js +12 -0
  31. package/lib/egress-fetch.js +21 -9
  32. package/lib/email-client/base-client.js +238 -67
  33. package/lib/email-client/credential-errors.js +122 -0
  34. package/lib/email-client/gmail/gmail-api.js +2 -1
  35. package/lib/email-client/gmail-client.js +70 -64
  36. package/lib/email-client/imap/connection-errors.js +52 -0
  37. package/lib/email-client/imap/listing-diff.js +47 -6
  38. package/lib/email-client/imap/login-failure.js +52 -0
  39. package/lib/email-client/imap/mailbox.js +457 -379
  40. package/lib/email-client/imap/subconnection.js +17 -12
  41. package/lib/email-client/imap/sync-operations.js +46 -59
  42. package/lib/email-client/imap-client.js +139 -101
  43. package/lib/email-client/message-builder.js +11 -11
  44. package/lib/email-client/outlook/graph-api.js +2 -1
  45. package/lib/email-client/outlook-client.js +219 -106
  46. package/lib/enum-descriptions.js +12 -2
  47. package/lib/imap-proxy-auth.js +58 -3
  48. package/lib/imapproxy/imap-server.js +53 -62
  49. package/lib/lua/h-del-if-equals.lua +39 -0
  50. package/lib/lua/h-set-if-equals.lua +39 -0
  51. package/lib/mcp/oauth.js +46 -8
  52. package/lib/mcp/protocol.js +78 -35
  53. package/lib/mcp/tools.js +53 -17
  54. package/lib/privileged-settings.js +147 -0
  55. package/lib/routes-ui.js +4 -34
  56. package/lib/schemas.js +92 -16
  57. package/lib/security-headers.js +15 -0
  58. package/lib/settings.js +13 -0
  59. package/lib/tls/catalog.js +428 -0
  60. package/lib/tls/context.js +302 -0
  61. package/lib/tls/der.js +223 -0
  62. package/lib/tls/listeners.js +62 -0
  63. package/lib/tls/provision.js +555 -0
  64. package/lib/tls/self-signed.js +282 -0
  65. package/lib/tls/status.js +480 -0
  66. package/lib/tls/store.js +667 -0
  67. package/lib/token-permission-view.js +252 -102
  68. package/lib/token-permissions.js +176 -26
  69. package/lib/tokens.js +7 -6
  70. package/lib/tools.js +73 -27
  71. package/lib/ui-routes/account-routes.js +43 -30
  72. package/lib/ui-routes/admin-config-routes.js +63 -22
  73. package/lib/ui-routes/admin-entities-routes.js +23 -13
  74. package/lib/ui-routes/auth-routes.js +16 -1
  75. package/lib/ui-routes/internals-routes.js +3 -5
  76. package/lib/ui-routes/mcp-consent-routes.js +72 -31
  77. package/lib/ui-routes/network-config-routes.js +54 -45
  78. package/lib/ui-routes/tls-config-routes.js +628 -0
  79. package/lib/utils/mailbox-path.js +51 -0
  80. package/lib/utils/severity.js +31 -0
  81. package/package.json +12 -12
  82. package/sbom.json +1 -1
  83. package/server.js +23 -1
  84. package/static/css/flyonui.css +1 -1
  85. package/static/css/src/app.tailwind.css +11 -0
  86. package/static/js/app.js +34 -0
  87. package/static/js/public.js +6 -2
  88. package/static/js/ui.js +97 -61
  89. package/static/licenses.html +341 -1252
  90. package/translations/messages.pot +62 -56
  91. package/views/account/login-redirect.hbs +22 -0
  92. package/views/account/passkeys.hbs +2 -2
  93. package/views/accounts/account.hbs +2 -2
  94. package/views/accounts/browse.hbs +1 -1
  95. package/views/accounts/edit.hbs +1 -1
  96. package/views/accounts/register/imap-server.hbs +1 -1
  97. package/views/config/ai.hbs +1 -1
  98. package/views/config/branding.hbs +2 -2
  99. package/views/config/document-store/chat.hbs +1 -1
  100. package/views/config/document-store/index.hbs +1 -1
  101. package/views/config/imap-proxy.hbs +1 -4
  102. package/views/config/license.hbs +1 -1
  103. package/views/config/mcp.hbs +88 -52
  104. package/views/config/network.hbs +1 -1
  105. package/views/config/oauth/app.hbs +2 -2
  106. package/views/config/service.hbs +1 -1
  107. package/views/config/smtp.hbs +1 -4
  108. package/views/config/tls-upload.hbs +81 -0
  109. package/views/config/tls.hbs +593 -0
  110. package/views/config/webhooks.hbs +2 -2
  111. package/views/internals/index.hbs +1 -1
  112. package/views/layout/app.hbs +6 -6
  113. package/views/layout/login.hbs +5 -5
  114. package/views/layout/main.hbs +2 -2
  115. package/views/layout/prompt.hbs +3 -3
  116. package/views/layout/public.hbs +1 -1
  117. package/views/mcp/authorize.hbs +47 -45
  118. package/views/partials/ace_assets.hbs +1 -1
  119. package/views/partials/add_account_modal.hbs +1 -1
  120. package/views/partials/filter_editor_js.hbs +2 -2
  121. package/views/partials/gateway_js.hbs +1 -1
  122. package/views/partials/mcp_access_levels.hbs +16 -9
  123. package/views/partials/mcp_tool_count.hbs +15 -14
  124. package/views/partials/oauth_form.hbs +1 -1
  125. package/views/partials/public_theme_init.hbs +1 -1
  126. package/views/partials/side_menu.hbs +1 -0
  127. package/views/partials/template_form.hbs +1 -1
  128. package/views/partials/theme_init.hbs +1 -1
  129. package/views/partials/tls_security_card.hbs +25 -53
  130. package/views/partials/ui/alert-banner.hbs +3 -2
  131. package/views/partials/ui/badge.hbs +6 -2
  132. package/views/partials/ui/btn.hbs +5 -2
  133. package/views/partials/webhooks_editor_functions.hbs +1 -1
  134. package/views/reference/index.hbs +1 -1
  135. package/views/reference/redirect.hbs +1 -1
  136. package/views/reference/tag.hbs +1 -1
  137. package/views/reference/token.hbs +1 -1
  138. package/views/templates/template.hbs +1 -1
  139. package/views/tokens/new.hbs +175 -74
  140. package/views/unsubscribe.hbs +1 -1
  141. package/views/webhooks/webhook.hbs +1 -1
  142. package/workers/api.js +102 -142
  143. package/workers/imap-proxy.js +4 -1
  144. package/workers/smtp.js +51 -44
  145. package/views/partials/tls_security_js.hbs +0 -64
  146. package/views/partials/tls_security_modal.hbs +0 -22
@@ -4,6 +4,9 @@ const { ImapFlow } = require('imapflow');
4
4
  const { backOff } = require('exponential-backoff');
5
5
  const { ReconnectBackoff } = require('../reconnect-backoff');
6
6
  const { detachImapClient } = require('./detach-client');
7
+ const { connectionUnavailableError } = require('./connection-errors');
8
+ const { isRefusedImapLogin } = require('./login-failure');
9
+ const { samePath } = require('../../utils/mailbox-path');
7
10
  const EventEmitter = require('events').EventEmitter;
8
11
 
9
12
  const MAX_WAIT_DELAY = 450; //ms
@@ -20,7 +23,6 @@ class Subconnection extends EventEmitter {
20
23
  this.account = opts.account;
21
24
 
22
25
  this.imapClient = null;
23
- this.getImapConfig = opts.getImapConfig;
24
26
 
25
27
  this.isClosing = false;
26
28
  this.isClosed = false;
@@ -41,10 +43,7 @@ class Subconnection extends EventEmitter {
41
43
 
42
44
  checkIMAPConnection() {
43
45
  if (!this.isConnected()) {
44
- let err = new Error('IMAP connection is currently not available for requested account');
45
- err.code = 'IMAPUnavailable';
46
- err.statusCode = 503;
47
- throw err;
46
+ throw connectionUnavailableError();
48
47
  }
49
48
  }
50
49
 
@@ -129,11 +128,13 @@ class Subconnection extends EventEmitter {
129
128
  let folders = await this.imapClient.run('LIST', '', this.mailbox.path, { listOnly: true });
130
129
  // The path goes into the LIST command as the match pattern
131
130
  // verbatim, so IMAP wildcards (% and *) in the folder name
132
- // can match sibling folders. Only an entry with the exact
133
- // same path proves the monitored mailbox still exists
134
- // (monitored paths come from the account listing, so the
135
- // stored spelling matches what the server returns)
136
- if (!folders || !folders.some(folder => folder.path === this.mailbox.path)) {
131
+ // can match sibling folders. Only an entry naming the same
132
+ // folder proves the monitored mailbox still exists - matched
133
+ // through samePath() because a server is free to answer with
134
+ // a different case for INBOX than the one it listed earlier,
135
+ // and reading that as a missing folder disables the
136
+ // subconnection until the folder is seen again
137
+ if (!folders || !folders.some(folder => samePath(folder.path, this.mailbox.path))) {
137
138
  err.mailboxMissing = true;
138
139
  }
139
140
  } catch (E) {
@@ -194,7 +195,11 @@ class Subconnection extends EventEmitter {
194
195
  }
195
196
  }
196
197
 
197
- let imapConfig = await this.getImapConfig(null, this);
198
+ // Built as this subconnection, not as the primary connection: a credential failure of its
199
+ // own must not notify or park the account, which is why notify() below is a no-op. The
200
+ // parent used to be reached through an injected zero-argument arrow that silently dropped
201
+ // this context
202
+ let imapConfig = await this.parent.getImapConfig(null, this);
198
203
 
199
204
  let imapClient = new ImapFlow(
200
205
  Object.assign({}, imapConfig, {
@@ -252,7 +257,7 @@ class Subconnection extends EventEmitter {
252
257
  await this.parent.untrackConnection(imapClient);
253
258
  }
254
259
  } catch (err) {
255
- if (err.authenticationFailed) {
260
+ if (isRefusedImapLogin(err)) {
256
261
  this.logger.warn({ msg: 'Failed to authenticate subconnection', account: this.account, err });
257
262
  this.state = 'authenticationError';
258
263
  this.disabledReason = 'Authentication failed';
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const { samePath } = require('../../utils/mailbox-path');
4
+ const { connectionClosedError, connectionNotAvailableError } = require('./connection-errors');
3
5
  const crypto = require('crypto');
4
6
  const { compareExisting, calculateFetchBackoff, readEnvValue, validUidValidity } = require('../../tools');
5
7
  const config = require('@zone-eu/wild-config');
@@ -11,16 +13,6 @@ const FETCH_BATCH_SIZE = Number(readEnvValue('EENGINE_FETCH_BATCH_SIZE') || conf
11
13
  // Do not check for flag updates using full sync more often than this value (30 minutes)
12
14
  const FULL_SYNC_DELAY = 30 * 60 * 1000;
13
15
 
14
- // Signals that the sync was interrupted rather than that the command itself failed. The original
15
- // failure is kept as `cause`, because on this path it is the only record of why the server dropped
16
- // the connection (NO [OVERQUOTA], * BYE Too many connections, [SERVERBUG]).
17
- function connectionClosedError(cause) {
18
- const err = new Error('IMAP connection closed during sync', { cause });
19
- err.code = 'IMAPConnectionClosing';
20
- err.statusCode = 503;
21
- return err;
22
- }
23
-
24
16
  function checkFetchRetryTimeout(retryCount, startTime) {
25
17
  if (retryCount >= FETCH_RETRY_MIN_ATTEMPTS && Date.now() - startTime > FETCH_RETRY_MAX_TIME) {
26
18
  const err = new Error('FETCH retry time limit exceeded');
@@ -66,13 +58,15 @@ function isRecentFullSync(storedStatus) {
66
58
  }
67
59
 
68
60
  /**
69
- * Checks if UIDVALIDITY has changed (mailbox was recreated)
61
+ * Checks if UIDVALIDITY has changed (mailbox was recreated). getStoredStatus() reports a
62
+ * missing UIDVALIDITY as false, never as an absent key, so the value is tested rather than
63
+ * the presence of the key
70
64
  * @param {Object} storedStatus - Stored mailbox status
71
65
  * @param {Object} mailboxStatus - Current mailbox status from IMAP
72
66
  * @returns {Boolean} True if UIDVALIDITY changed
73
67
  */
74
68
  function hasUidValidityChanged(storedStatus, mailboxStatus) {
75
- return 'uidValidity' in storedStatus && mailboxStatus.uidValidity !== storedStatus.uidValidity;
69
+ return validUidValidity(storedStatus.uidValidity) && mailboxStatus.uidValidity !== storedStatus.uidValidity;
76
70
  }
77
71
 
78
72
  /**
@@ -161,6 +155,17 @@ class SyncOperations {
161
155
  this.logger = mailbox.logger;
162
156
  }
163
157
 
158
+ /**
159
+ * Ends a sync operation: gives the mailbox lock back (its release reports the task
160
+ * to the connection, see Mailbox.getMailboxLock()) and clears the syncing flags
161
+ * @param {Object} lock - Lock returned by Mailbox.getMailboxLock()
162
+ */
163
+ releaseSyncLock(lock) {
164
+ lock.release();
165
+ this.connection.syncing = false;
166
+ this.mailbox.syncing = false;
167
+ }
168
+
164
169
  /**
165
170
  * Performs full synchronization based on indexer type
166
171
  * @returns {Promise} Resolves when sync is complete
@@ -178,18 +183,17 @@ class SyncOperations {
178
183
 
179
184
  /**
180
185
  * Performs partial synchronization based on indexer type
181
- * @param {Object} storedStatus - Current stored mailbox status
182
186
  * @returns {Promise} Resolves when sync is complete
183
187
  */
184
- async partialSync(storedStatus) {
188
+ async partialSync() {
185
189
  const imapIndexer = this.mailbox.imapIndexer;
186
190
 
187
191
  this.logger.trace({ msg: 'Running partial sync', imapIndexer });
188
192
 
189
193
  if (imapIndexer === 'fast') {
190
- return this.runFastSync(storedStatus);
194
+ return this.runFastSync();
191
195
  }
192
- return this.runPartialSync(storedStatus);
196
+ return this.runPartialSync();
193
197
  }
194
198
 
195
199
  /**
@@ -251,10 +255,7 @@ class SyncOperations {
251
255
 
252
256
  let imapClient = this.connection.imapClient;
253
257
  if (!imapClient) {
254
- let err = new Error('IMAP connection not available');
255
- err.code = 'IMAPConnectionClosing';
256
- err.statusCode = 503;
257
- throw err;
258
+ throw connectionNotAvailableError();
258
259
  }
259
260
 
260
261
  let imapIndexer = this.mailbox.imapIndexer;
@@ -325,32 +326,33 @@ class SyncOperations {
325
326
 
326
327
  return indexed;
327
328
  } finally {
328
- lock.release();
329
- this.connection.syncing = false;
330
- this.mailbox.syncing = false;
329
+ this.releaseSyncLock(lock);
331
330
  }
332
331
  }
333
332
 
334
333
  /**
335
334
  * Fast sync mode - only tracks new messages, doesn't maintain full message list
336
335
  * More efficient for large mailboxes where we only care about new messages
337
- * @param {Object} storedStatus - Current stored mailbox status
338
336
  */
339
- async runFastSync(storedStatus) {
340
- storedStatus = storedStatus || (await this.mailbox.getStoredStatus());
341
- let mailboxStatus = this.mailbox.getMailboxStatus();
342
-
337
+ async runFastSync() {
343
338
  let lock = await this.mailbox.getMailboxLock(null, { description: 'Fast sync' });
344
339
  this.connection.syncing = true;
345
340
  this.mailbox.syncing = true;
346
341
  try {
347
342
  if (!this.connection.imapClient) {
348
- let err = new Error('IMAP connection not available');
349
- err.code = 'IMAPConnectionClosing';
350
- err.statusCode = 503;
351
- throw err;
343
+ throw connectionNotAvailableError();
352
344
  }
353
345
 
346
+ // Both snapshots are read under the lock, which guarantees this folder is the
347
+ // selected one: read before it, the mailbox counters could belong to a folder
348
+ // an API command opened in the meantime, and the stored state could predate a
349
+ // sync of this folder that ran just ahead of this one. Neither is accepted from
350
+ // the caller for that reason - onOpen() reads its own copy before the lock, to
351
+ // pick a strategy from, and a sync ahead of this one in the lock queue leaves
352
+ // that copy behind
353
+ let storedStatus = await this.mailbox.getStoredStatus();
354
+ let mailboxStatus = this.mailbox.getMailboxStatus();
355
+
354
356
  let knownUidNext = typeof storedStatus.uidNext === 'number' ? storedStatus.uidNext || 1 : 1;
355
357
 
356
358
  if (knownUidNext && mailboxStatus.messages) {
@@ -485,32 +487,27 @@ class SyncOperations {
485
487
 
486
488
  await this.mailbox.publishSyncedEvents(storedStatus);
487
489
  } finally {
488
- lock.release();
489
- this.connection.syncing = false;
490
- this.mailbox.syncing = false;
490
+ this.releaseSyncLock(lock);
491
491
  }
492
492
  }
493
493
 
494
494
  /**
495
495
  * Partial sync - fetches only changed messages using MODSEQ or UID range
496
496
  * Used for incremental updates when we know something changed
497
- * @param {Object} storedStatus - Current stored mailbox status
498
497
  */
499
- async runPartialSync(storedStatus) {
500
- storedStatus = storedStatus || (await this.mailbox.getStoredStatus());
501
- let mailboxStatus = this.mailbox.getMailboxStatus();
502
-
498
+ async runPartialSync() {
503
499
  let lock = await this.mailbox.getMailboxLock(null, { description: 'Partial sync' });
504
500
  this.connection.syncing = true;
505
501
  this.mailbox.syncing = true;
506
502
  try {
507
503
  if (!this.connection.imapClient) {
508
- let err = new Error('IMAP connection not available');
509
- err.code = 'IMAPConnectionClosing';
510
- err.statusCode = 503;
511
- throw err;
504
+ throw connectionNotAvailableError();
512
505
  }
513
506
 
507
+ // Read under the lock for the same reason as in runFastSync()
508
+ let storedStatus = await this.mailbox.getStoredStatus();
509
+ let mailboxStatus = this.mailbox.getMailboxStatus();
510
+
514
511
  let fields = { uid: true, flags: true, modseq: true, emailId: true, labels: true, internalDate: true };
515
512
  let range = '1:*';
516
513
  let opts = {
@@ -536,10 +533,7 @@ class SyncOperations {
536
533
  let imapClient = this.connection.imapClient;
537
534
  if (!imapClient || !imapClient.usable) {
538
535
  this.logger.debug({ msg: 'IMAP client not available for partial sync' });
539
- let err = new Error('IMAP connection not available');
540
- err.code = 'IMAPConnectionClosing';
541
- err.statusCode = 503;
542
- throw err;
536
+ throw connectionNotAvailableError();
543
537
  }
544
538
 
545
539
  try {
@@ -637,9 +631,7 @@ class SyncOperations {
637
631
 
638
632
  await this.mailbox.publishSyncedEvents(storedStatus);
639
633
  } finally {
640
- lock.release();
641
- this.connection.syncing = false;
642
- this.mailbox.syncing = false;
634
+ this.releaseSyncLock(lock);
643
635
  }
644
636
  }
645
637
 
@@ -720,10 +712,7 @@ class SyncOperations {
720
712
  const imapClient = this.connection.imapClient;
721
713
  if (!imapClient || !imapClient.usable) {
722
714
  this.logger.debug({ msg: 'IMAP client not available for FETCH' });
723
- let err = new Error('IMAP connection not available');
724
- err.code = 'IMAPConnectionClosing';
725
- err.statusCode = 503;
726
- throw err;
715
+ throw connectionNotAvailableError();
727
716
  }
728
717
 
729
718
  try {
@@ -904,7 +893,7 @@ class SyncOperations {
904
893
  // Verify we're still on the correct mailbox after the delay
905
894
  // Another operation might have changed the mailbox while we were waiting
906
895
  const currentMailbox = this.connection.imapClient?.mailbox;
907
- if (!currentMailbox || currentMailbox.path !== this.mailbox.path) {
896
+ if (!currentMailbox || !samePath(currentMailbox.path, this.mailbox.path)) {
908
897
  this.logger.warn({
909
898
  msg: 'Mailbox changed during retry delay, aborting sync',
910
899
  expectedPath: this.mailbox.path,
@@ -969,9 +958,7 @@ class SyncOperations {
969
958
 
970
959
  await this.mailbox.publishSyncedEvents(storedStatus);
971
960
  } finally {
972
- this.connection.syncing = false;
973
- this.mailbox.syncing = false;
974
- lock.release();
961
+ this.releaseSyncLock(lock);
975
962
  }
976
963
  }
977
964
  }