emailengine-app 2.84.0 → 2.84.2

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 (93) hide show
  1. package/.dockerignore +6 -0
  2. package/.release-please-manifest.json +1 -1
  3. package/CHANGELOG.md +35 -0
  4. package/Dockerfile +2 -2
  5. package/bin/emailengine.js +15 -0
  6. package/data/google-crawlers.json +1 -1
  7. package/docker-compose.yml +1 -1
  8. package/lib/account/own-account-key.js +42 -0
  9. package/lib/account-assignment.js +1 -33
  10. package/lib/account.js +32 -19
  11. package/lib/add-trackers.js +11 -4
  12. package/lib/api-routes/account-routes.js +67 -14
  13. package/lib/api-routes/gateway-routes.js +9 -3
  14. package/lib/api-routes/message-routes.js +7 -1
  15. package/lib/api-routes/mint-bounds.js +11 -0
  16. package/lib/api-routes/route-helpers.js +125 -3
  17. package/lib/api-routes/submit-routes.js +4 -2
  18. package/lib/auth-token.js +23 -10
  19. package/lib/bounce-detect.js +17 -5
  20. package/lib/consts.js +34 -0
  21. package/lib/create-imap-client.js +41 -2
  22. package/lib/email-client/base-client.js +20 -10
  23. package/lib/email-client/gmail-client.js +21 -7
  24. package/lib/email-client/imap/mailbox.js +10 -4
  25. package/lib/email-client/imap/response-metrics.js +75 -0
  26. package/lib/email-client/imap-client.js +5 -2
  27. package/lib/email-client/message-builder.js +7 -2
  28. package/lib/email-client/outlook/graph-api.js +17 -2
  29. package/lib/email-client/outlook-client.js +6 -3
  30. package/lib/email-client/smtp-pool-manager.js +6 -0
  31. package/lib/encrypt.js +9 -0
  32. package/lib/export.js +3 -1
  33. package/lib/get-raw-email.js +4 -2
  34. package/lib/imap-proxy-auth.js +14 -1
  35. package/lib/imapproxy/imap-core/lib/commands/authenticate-plain.js +1 -0
  36. package/lib/imapproxy/imap-core/lib/commands/login.js +1 -0
  37. package/lib/imapproxy/imap-core/lib/imap-command.js +86 -43
  38. package/lib/imapproxy/imap-core/lib/imap-stream.js +58 -13
  39. package/lib/imapproxy/imap-core/lib/indexer/indexer.js +3 -1
  40. package/lib/mcp/listen.js +10 -1
  41. package/lib/oauth/external-account-config.js +3 -1
  42. package/lib/oauth/gmail.js +21 -48
  43. package/lib/oauth/mail-ru.js +5 -38
  44. package/lib/oauth/outlook.js +26 -44
  45. package/lib/pbkdf2-phc.js +18 -1
  46. package/lib/privileged-settings.js +6 -3
  47. package/lib/redis-scan-delete.js +15 -1
  48. package/lib/schemas.js +78 -32
  49. package/lib/settings.js +13 -1
  50. package/lib/smtp-auth.js +23 -0
  51. package/lib/tools.js +245 -56
  52. package/lib/ui-routes/account-routes.js +16 -37
  53. package/lib/ui-routes/admin-config-routes.js +10 -38
  54. package/lib/ui-routes/admin-entities-routes.js +22 -24
  55. package/lib/ui-routes/auth-routes.js +47 -24
  56. package/lib/ui-routes/connection-security-form.js +74 -0
  57. package/lib/ui-routes/form-lists.js +46 -0
  58. package/lib/ui-routes/mcp-consent-routes.js +5 -1
  59. package/lib/ui-routes/oauth-config-routes.js +9 -18
  60. package/lib/ui-routes/sign-in-helpers.js +59 -0
  61. package/lib/ui-routes/unsubscribe-routes.js +6 -3
  62. package/lib/utils/map-concurrent.js +36 -0
  63. package/lib/utils/network.js +28 -0
  64. package/lib/utils/provider-id.js +27 -0
  65. package/lib/utils/require-tls.js +33 -0
  66. package/lib/web-safe-html.js +5 -6
  67. package/lib/webhook-penalty.js +75 -0
  68. package/lib/webhook-routing.js +22 -2
  69. package/package.json +21 -21
  70. package/sbom.json +1 -1
  71. package/server.js +74 -38
  72. package/static/js/ace/ace.js +1 -1
  73. package/static/js/ace/mode-handlebars.js +1 -1
  74. package/static/js/ace/mode-html.js +1 -1
  75. package/static/js/ace/mode-javascript.js +1 -1
  76. package/static/js/ace/mode-markdown.js +1 -1
  77. package/static/js/ace/worker-html.js +1 -1
  78. package/static/js/ace/worker-javascript.js +1 -1
  79. package/static/js/ace/worker-json.js +1 -1
  80. package/static/js/ee-client.js +5 -0
  81. package/static/licenses.html +23 -23
  82. package/translations/messages.pot +66 -66
  83. package/views/account/security.hbs +10 -0
  84. package/views/accounts/account.hbs +11 -11
  85. package/views/accounts/edit.hbs +6 -2
  86. package/views/partials/gateway_form.hbs +3 -1
  87. package/workers/api.js +43 -3
  88. package/workers/webhooks.js +39 -3
  89. package/config/test.toml +0 -46
  90. package/install.sh +0 -428
  91. package/setup-production.sh +0 -215
  92. package/systemd/emailengine.service +0 -97
  93. package/systemd/nginx-proxy.conf +0 -77
package/server.js CHANGED
@@ -20,19 +20,23 @@
20
20
  // same way. Set before anything can require bullmq; worker threads share this environment.
21
21
  process.env.MSGPACKR_NATIVE_ACCELERATION_DISABLED = 'true';
22
22
 
23
- // Load environment variables if not already loaded
24
- if (!process.env.EE_ENV_LOADED) {
25
- require('dotenv').config({ quiet: true });
26
- process.env.EE_ENV_LOADED = 'true';
27
- }
28
-
29
- // Attempt to change working directory to script location
23
+ // Attempt to change working directory to script location. Before anything reads a file by a
24
+ // relative path: `.env` below and the `config/` directory wild-config loads (including executable
25
+ // `.js` configuration files, run with the real require) are both resolved against the working
26
+ // directory, and the directory the operator happened to start from is not where this instance's
27
+ // configuration lives
30
28
  try {
31
29
  process.chdir(__dirname);
32
30
  } catch (err) {
33
31
  // ignore - may fail in some containerized environments
34
32
  }
35
33
 
34
+ // Load environment variables if not already loaded
35
+ if (!process.env.EE_ENV_LOADED) {
36
+ require('dotenv').config({ quiet: true });
37
+ process.env.EE_ENV_LOADED = 'true';
38
+ }
39
+
36
40
  // Set process title for easier identification in process lists
37
41
  process.title = 'emailengine';
38
42
 
@@ -146,10 +150,10 @@ const {
146
150
  releaseWorkerAccounts,
147
151
  requeueFailedAccounts,
148
152
  forgetAccount,
149
- mapConcurrent,
150
153
  shouldRequeueFailedAssignment,
151
154
  planRebalance
152
155
  } = require('./lib/account-assignment');
156
+ const { mapConcurrent } = require('./lib/utils/map-concurrent');
153
157
  const { RespawnTracker } = require('./lib/respawn-backoff');
154
158
 
155
159
  const msgpack = require('./lib/msgpack');
@@ -793,6 +797,7 @@ let workerAssigned = new WeakMap(); // Map of worker -> Set of accounts
793
797
  let onlineWorkers = new WeakSet(); // Set of workers that are online
794
798
  let reassignmentTimer = null; // Timer for failsafe reassignment
795
799
  let reassignmentPending = false; // Flag to track if reassignment is pending
800
+ const REASSIGNMENT_FAILSAFE_DELAY = 10 * 1000; // How long an exited IMAP worker gets to restart before its accounts move
796
801
  let assignRetryCount = 0; // Counter for safety-net retries in assignAccounts
797
802
  const MAX_ASSIGN_RETRIES = 3; // Max consecutive safety-net retries before giving up
798
803
 
@@ -1155,7 +1160,7 @@ let spawnWorker = async (type, opts, slot) => {
1155
1160
 
1156
1161
  // A fresh spawn opens its own slot. Counting the crash streak per type made every short-lived
1157
1162
  // exit of ANY IMAP worker double the next respawn delay of all of them, so unrelated exits
1158
- // pushed a restart past the 10s failsafe reassignment.
1163
+ // pushed a restart past the failsafe reassignment (REASSIGNMENT_FAILSAFE_DELAY).
1159
1164
  if (!slot) {
1160
1165
  slot = `${type}:${++spawnSlotCounter}`;
1161
1166
  }
@@ -1297,24 +1302,8 @@ let spawnWorker = async (type, opts, slot) => {
1297
1302
  if (unassigned && unassigned.size > 0) {
1298
1303
  reassignmentPending = true;
1299
1304
 
1300
- // Clear any existing timer
1301
- if (reassignmentTimer) {
1302
- clearTimeout(reassignmentTimer);
1303
- }
1304
-
1305
- // Set a failsafe timer - if worker doesn't restart in 10 seconds, reassign anyway
1306
- reassignmentTimer = setTimeout(() => {
1307
- if (reassignmentPending && unassigned && unassigned.size > 0) {
1308
- logger.warn({
1309
- msg: 'Failsafe reassignment triggered - worker restart timeout',
1310
- unassignedAccounts: unassigned.size,
1311
- currentWorkers: availableIMAPWorkers.size,
1312
- expectedWorkers: config.workers.imap
1313
- });
1314
- reassignmentPending = false;
1315
- assignAccounts().catch(err => logger.error({ msg: 'Unable to reassign accounts (failsafe)', err }));
1316
- }
1317
- }, 10000); // 10 second timeout
1305
+ // If the worker does not restart in time, its accounts go to the others anyway
1306
+ armReassignmentFailsafe();
1318
1307
 
1319
1308
  logger[exitLevel]({
1320
1309
  msg: 'Worker exited, waiting for restart before reassignment',
@@ -1658,6 +1647,11 @@ let spawnWorker = async (type, opts, slot) => {
1658
1647
  expectedWorkers: config.workers.imap,
1659
1648
  unassignedAccounts: unassigned.size
1660
1649
  });
1650
+ // The failsafe found no worker at all and stopped; give the rest
1651
+ // of the fleet the same time to come back from here
1652
+ if (!reassignmentTimer) {
1653
+ armReassignmentFailsafe();
1654
+ }
1661
1655
  }
1662
1656
  } else if (imapInitialWorkersLoaded) {
1663
1657
  // The failsafe timer already gave this worker's accounts to the
@@ -1791,6 +1785,43 @@ async function unassignFromWorker(worker, account) {
1791
1785
  }
1792
1786
  }
1793
1787
 
1788
+ /**
1789
+ * Starts (or restarts) the failsafe timer that gives the accounts an exited IMAP worker left behind
1790
+ * to the remaining workers when it does not come back in time
1791
+ */
1792
+ function armReassignmentFailsafe() {
1793
+ clearTimeout(reassignmentTimer);
1794
+
1795
+ reassignmentTimer = setTimeout(() => {
1796
+ reassignmentTimer = null;
1797
+ if (!reassignmentPending || !unassigned || !unassigned.size) {
1798
+ return;
1799
+ }
1800
+
1801
+ if (!availableIMAPWorkers.size) {
1802
+ // Every worker exited at once (the clean exit after a Redis reconnection) and none is
1803
+ // back yet. A pass now would find no worker and clear the pending flag that makes the
1804
+ // returning workers pick the accounts up, stranding them until a process restart. Keep
1805
+ // it pending instead; the first worker to come back arms this timer again
1806
+ logger.warn({
1807
+ msg: 'Failsafe reassignment postponed - no IMAP worker is available yet',
1808
+ unassignedAccounts: unassigned.size,
1809
+ expectedWorkers: config.workers.imap
1810
+ });
1811
+ return;
1812
+ }
1813
+
1814
+ logger.warn({
1815
+ msg: 'Failsafe reassignment triggered - worker restart timeout',
1816
+ unassignedAccounts: unassigned.size,
1817
+ currentWorkers: availableIMAPWorkers.size,
1818
+ expectedWorkers: config.workers.imap
1819
+ });
1820
+ reassignmentPending = false;
1821
+ assignAccounts().catch(err => logger.error({ msg: 'Unable to reassign accounts (failsafe)', err }));
1822
+ }, REASSIGNMENT_FAILSAFE_DELAY);
1823
+ }
1824
+
1794
1825
  /**
1795
1826
  * Moves a fair share of accounts onto an IMAP worker that became ready while the rest of the fleet
1796
1827
  * already served every account. That is the normal outcome of a restart slower than the failsafe
@@ -1809,9 +1840,6 @@ async function rebalanceOnto(worker) {
1809
1840
  }
1810
1841
 
1811
1842
  let moves = planRebalance({ workerAssigned, availableWorkers: availableIMAPWorkers }, worker);
1812
- if (!moves.length) {
1813
- return;
1814
- }
1815
1843
 
1816
1844
  assigning = true;
1817
1845
  let moved = 0;
@@ -1843,8 +1871,9 @@ async function rebalanceOnto(worker) {
1843
1871
  logger.info({ msg: 'Rebalancing accounts onto a restarted IMAP worker', threadId: worker.threadId, accounts: moved });
1844
1872
  }
1845
1873
  // Also run when nothing moved: an account added while this held the assignment lock found the
1846
- // pass refused and is waiting in unassigned. Every worker is available again, so the pass
1847
- // picks the least loaded one, which is the newcomer.
1874
+ // pass refused and is waiting in unassigned, or nothing was there to move because every worker
1875
+ // had exited and the accounts are all waiting. The pass picks the least loaded worker, which
1876
+ // is the newcomer.
1848
1877
  if (moved || unassigned.size) {
1849
1878
  await assignAccounts();
1850
1879
  }
@@ -2523,10 +2552,15 @@ const openAiRequestOpts = () => ({
2523
2552
 
2524
2553
  const aiValueSet = value => value !== undefined && value !== null && value !== '';
2525
2554
 
2555
+ const aiUrlValue = value => (value === undefined || value === null ? '' : String(value).trim());
2556
+
2526
2557
  /**
2527
2558
  * Options for one request to the AI provider. The configuration page's test sends every field as
2528
2559
  * its form shows it, so the test runs with what the page shows; the sync path sends none and gets
2529
- * the stored settings. The key is the exception: the form leaves it blank to keep the stored one
2560
+ * the stored settings. The key is the exception: the form leaves it blank to keep the stored one,
2561
+ * and a request made with the stored key goes to the stored URL, since a write-only secret must
2562
+ * not be extractable by pointing a request that falls back to it at a server of one's own. The
2563
+ * rule is here rather than on the page's routes so every request the form can make is under it
2530
2564
  * @param {Object} data - The command's data
2531
2565
  * @returns {Promise<{openAiAPIKey: string, requestOpts: Object}>}
2532
2566
  */
@@ -2538,6 +2572,9 @@ async function aiRequestOptions(data) {
2538
2572
  if (!openAiAPIKey) {
2539
2573
  throw new Error(`OpenAI API key is not configured`);
2540
2574
  }
2575
+ if (data.options && !data.options.openAiAPIKey && aiUrlValue(data.options.openAiAPIUrl) !== aiUrlValue(stored.openAiAPIUrl)) {
2576
+ throw new Error('Enter the API key to use a different API URL');
2577
+ }
2541
2578
 
2542
2579
  const requestOpts = openAiRequestOpts();
2543
2580
  requestOpts.gptModel = aiValueSet(values.openAiModel) ? values.openAiModel : openAiDefaultModel;
@@ -3429,11 +3466,10 @@ const startApplication = async () => {
3429
3466
  await settings.set('enableApiProxy', HAS_API_PROXY_SET ? API_PROXY : true);
3430
3467
  }
3431
3468
 
3432
- // Service settings
3433
- let existingServiceSecret = await settings.get('serviceSecret');
3434
- if (existingServiceSecret === null) {
3435
- await settings.set('serviceSecret', crypto.randomBytes(16).toString('hex'));
3436
- }
3469
+ // Service settings. HSETNX rather than read-then-write: a stored secret that does not decrypt
3470
+ // (the wrong EENGINE_SECRET) reads as null, and overwriting it would destroy a value the
3471
+ // rotation CLI can still recover once the right secret is back
3472
+ await settings.setIfMissing('serviceSecret', crypto.randomBytes(16).toString('hex'));
3437
3473
 
3438
3474
  let existingQueueKeep = await settings.get('queueKeep');
3439
3475
  if (existingQueueKeep === null) {