emailengine-app 2.74.2 → 2.75.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 (41) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/data/google-crawlers.json +1 -1
  3. package/lib/account.js +94 -18
  4. package/lib/api-routes/message-routes.js +3 -1
  5. package/lib/consts.js +6 -0
  6. package/lib/db.js +4 -1
  7. package/lib/egress-filter.js +220 -0
  8. package/lib/email-client/base-client.js +5 -8
  9. package/lib/email-client/gmail-client.js +2 -5
  10. package/lib/email-client/imap/mailbox.js +3 -9
  11. package/lib/email-client/notification-handler.js +11 -12
  12. package/lib/email-client/outlook-client.js +2 -5
  13. package/lib/imapproxy/imap-core/README.md +69 -0
  14. package/lib/oauth/external-account-config.js +41 -1
  15. package/lib/oauth/external-account-signer.js +7 -2
  16. package/lib/oauth/verify-app.js +18 -3
  17. package/lib/queue-retention.js +56 -0
  18. package/lib/respawn-backoff.js +95 -0
  19. package/lib/schemas.js +9 -2
  20. package/lib/settings.js +4 -1
  21. package/lib/tools.js +20 -0
  22. package/lib/ui-routes/admin-config-routes.js +22 -18
  23. package/lib/utils/network.js +122 -3
  24. package/lib/web-safe-html.js +401 -0
  25. package/lib/webhook-request.js +40 -2
  26. package/lib/webhooks.js +2 -3
  27. package/package.json +10 -8
  28. package/sbom.json +1 -1
  29. package/server.js +33 -8
  30. package/static/js/ee-client.js +55 -6
  31. package/static/js/ui.js +13 -0
  32. package/static/licenses.html +325 -285
  33. package/translations/messages.pot +26 -26
  34. package/views/config/webhooks.hbs +2 -2
  35. package/views/partials/webhooks_editor_functions.hbs +1 -1
  36. package/workers/api.js +32 -17
  37. package/workers/documents.js +6 -0
  38. package/workers/export.js +8 -1
  39. package/workers/imap.js +17 -19
  40. package/workers/submit.js +10 -0
  41. package/workers/webhooks.js +23 -2
package/server.js CHANGED
@@ -133,6 +133,7 @@ const { QueueEvents } = require('bullmq');
133
133
  const getSecret = require('./lib/get-secret');
134
134
 
135
135
  const { rejectWorkerCalls } = require('./lib/reject-worker-calls');
136
+ const { RespawnTracker } = require('./lib/respawn-backoff');
136
137
 
137
138
  const msgpack = require('msgpack5')();
138
139
 
@@ -713,6 +714,14 @@ const MAX_ASSIGN_RETRIES = 3; // Max consecutive safety-net retries before givin
713
714
  // Worker management
714
715
  let imapInitialWorkersLoaded = false; // Have all initial IMAP workers started?
715
716
  let workers = new Map(); // Map of type -> Set of workers
717
+ const respawnTracker = new RespawnTracker(); // paces respawns for worker types that keep crashing
718
+
719
+ // Shutdown drain budget. A worker with no in-flight job answers immediately, so this only costs
720
+ // real time when there is genuinely something to finish - which is exactly when waiting is worth
721
+ // it, because the alternative is a re-sent message. Kept under Docker's 10s default SIGKILL grace
722
+ // so the drain completes rather than being cut halfway.
723
+ const SHUTDOWN_DRAIN_TIMEOUT = 8000;
724
+ const WORKER_DRAIN_TIMEOUT = 7000;
716
725
  let workersMeta = new WeakMap(); // Worker metadata
717
726
  let availableIMAPWorkers = new Set(); // IMAP workers ready to accept accounts
718
727
 
@@ -1056,6 +1065,7 @@ let spawnWorker = async (type, opts) => {
1056
1065
  }
1057
1066
 
1058
1067
  // Create new worker thread
1068
+ let spawnedAt = Date.now();
1059
1069
  let worker = new WorkerThread(pathlib.join(__dirname, 'workers', `${type.replace(/[A-Z]/g, c => `-${c.toLowerCase()}`)}.js`), {
1060
1070
  argv,
1061
1071
  env: SHARE_ENV,
@@ -1218,8 +1228,15 @@ let spawnWorker = async (type, opts) => {
1218
1228
  logger.error({ msg: 'Worker unexpectedly exited', exitCode, type });
1219
1229
  }
1220
1230
 
1221
- // Respawn worker after delay, preserving any spawn options (e.g. API worker index)
1222
- await new Promise(r => setTimeout(r, 1000));
1231
+ // Respawn worker after delay, preserving any spawn options (e.g. API worker index).
1232
+ // The delay grows while a worker type keeps dying on startup, so a deterministic
1233
+ // crash no longer respawns once a second forever.
1234
+ let respawnDelay = respawnTracker.recordExit(type, Date.now() - spawnedAt);
1235
+ let streak = respawnTracker.streakFor(type);
1236
+ if (streak > 1) {
1237
+ logger.warn({ msg: 'Worker is crash looping, delaying respawn', type, consecutiveFailures: streak, respawnDelay });
1238
+ }
1239
+ await new Promise(r => setTimeout(r, respawnDelay));
1223
1240
  await spawnWorker(type, opts);
1224
1241
  };
1225
1242
 
@@ -2999,12 +3016,19 @@ async function collectMetrics() {
2999
3016
  const closeQueues = cb => {
3000
3017
  let proms = [];
3001
3018
 
3002
- // Signal webhooks workers to stop Pub/Sub pull loops before exiting
3003
- if (workers.has('webhooks')) {
3004
- for (let worker of workers.get('webhooks')) {
3019
+ // Signal every worker to wind down before exiting. Signals only reach this thread, so a worker
3020
+ // cannot notice the shutdown on its own - without this its sockets are simply cut and any
3021
+ // BullMQ job it was running is recovered as stalled and re-run, which for the submit queue
3022
+ // means re-sending a message the MTA had already accepted.
3023
+ //
3024
+ // Broadcast rather than an allowlist of types: a worker with nothing to drain answers the
3025
+ // unknown command with 999 and costs one round trip, whereas a list here has to be kept in
3026
+ // sync with every worker that later grows a drain.
3027
+ for (let [type, typeWorkers] of workers) {
3028
+ for (let worker of typeWorkers) {
3005
3029
  proms.push(
3006
- call(worker, { cmd: 'close', timeout: 4000 }).catch(err => {
3007
- logger.error({ msg: 'Failed to signal webhooks worker to close', err });
3030
+ call(worker, { cmd: 'close', timeout: WORKER_DRAIN_TIMEOUT }).catch(err => {
3031
+ logger.error({ msg: 'Failed to signal worker to close', type, err });
3008
3032
  })
3009
3033
  );
3010
3034
  }
@@ -3026,9 +3050,10 @@ const closeQueues = cb => {
3026
3050
  if (returned) {
3027
3051
  return;
3028
3052
  }
3053
+ logger.warn({ msg: 'Shutdown drain did not finish in time, exiting anyway', timeout: SHUTDOWN_DRAIN_TIMEOUT });
3029
3054
  returned = true;
3030
3055
  cb();
3031
- }, 5000);
3056
+ }, SHUTDOWN_DRAIN_TIMEOUT);
3032
3057
 
3033
3058
  Promise.allSettled(proms).then(() => {
3034
3059
  clearTimeout(closeTimeout);
@@ -488,10 +488,11 @@ export class EmailEngineClient {
488
488
  const style = document.createElement('style');
489
489
  style.textContent = `
490
490
  .ee-client {
491
+ --ee-font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
491
492
  display: flex;
492
493
  height: 100%;
493
494
  min-height: 400px;
494
- font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
495
+ font-family: var(--ee-font);
495
496
  font-size: 14px;
496
497
  line-height: 1.5;
497
498
  color: #333;
@@ -685,7 +686,8 @@ export class EmailEngineClient {
685
686
  box-shadow: 0 1px 3px rgba(0,0,0,0.1);
686
687
  }
687
688
 
688
- .ee-button {
689
+ .ee-button,
690
+ .ee-collapsed-thread-toggle {
689
691
  padding: 4px 10px;
690
692
  border: 1px solid #ddd;
691
693
  background: white;
@@ -702,7 +704,8 @@ export class EmailEngineClient {
702
704
  height: 24px;
703
705
  }
704
706
 
705
- .ee-button:hover:not(:disabled) {
707
+ .ee-button:hover:not(:disabled),
708
+ .ee-collapsed-thread-toggle:hover {
706
709
  background: #f0f0f0;
707
710
  }
708
711
 
@@ -744,7 +747,30 @@ export class EmailEngineClient {
744
747
  max-width: 100%;
745
748
  height: auto;
746
749
  }
747
-
750
+
751
+ /* Quoted thread history, folded away by EmailEngine - see _labelCollapsedThreads() */
752
+ .ee-collapsed-thread {
753
+ margin-top: 12px;
754
+ }
755
+
756
+ .ee-collapsed-thread-toggle {
757
+ display: inline-block;
758
+ height: auto;
759
+ /* The message brings its own fonts along; the control keeps the client's */
760
+ font-family: var(--ee-font);
761
+ user-select: none;
762
+ list-style: none;
763
+ }
764
+
765
+ /* Same, for Safari before it supported list-style on a summary */
766
+ .ee-collapsed-thread-toggle::-webkit-details-marker {
767
+ display: none;
768
+ }
769
+
770
+ .ee-collapsed-thread[open] > .ee-collapsed-thread-toggle {
771
+ margin-bottom: 12px;
772
+ }
773
+
748
774
  .ee-attachments {
749
775
  margin-top: 16px;
750
776
  padding-top: 16px;
@@ -1184,13 +1210,15 @@ export class EmailEngineClient {
1184
1210
  border-color: #333;
1185
1211
  }
1186
1212
 
1187
- .ee-dark-mode .ee-button {
1213
+ .ee-dark-mode .ee-button,
1214
+ .ee-dark-mode .ee-collapsed-thread-toggle {
1188
1215
  background: #333;
1189
1216
  border-color: #444;
1190
1217
  color: #e0e0e0;
1191
1218
  }
1192
1219
 
1193
- .ee-dark-mode .ee-button:hover {
1220
+ .ee-dark-mode .ee-button:hover,
1221
+ .ee-dark-mode .ee-collapsed-thread-toggle:hover {
1194
1222
  background: #444;
1195
1223
  border-color: #555;
1196
1224
  }
@@ -1610,6 +1638,8 @@ export class EmailEngineClient {
1610
1638
  `;
1611
1639
  viewer.innerHTML = html;
1612
1640
 
1641
+ this._labelCollapsedThreads(viewer);
1642
+
1613
1643
  viewer.querySelector('[data-action="toggle-read"]').addEventListener('click', () => {
1614
1644
  const currentlyUnseen = msg.unseen;
1615
1645
  this.markAsRead(msg.id, currentlyUnseen);
@@ -1650,6 +1680,25 @@ export class EmailEngineClient {
1650
1680
  // Message content scrolling is now handled in loadMessage method
1651
1681
  }
1652
1682
 
1683
+ // EmailEngine's web-safe HTML folds quoted thread history (reply history, forwarded content,
1684
+ // disclaimers) into a <details class="ee-collapsed-thread"> whose <summary> it leaves empty on
1685
+ // purpose, for the renderer to label. The fold is closed until the reader opens it.
1686
+ //
1687
+ // A sender can put the same markup in their own HTML - it survives sanitization - so every
1688
+ // match is relabelled rather than trusted, and the control always reads as one of these two
1689
+ // labels instead of whatever text came with the message.
1690
+ _labelCollapsedThreads(viewer) {
1691
+ viewer.querySelectorAll('details.ee-collapsed-thread > summary.ee-collapsed-thread-toggle').forEach(toggle => {
1692
+ const details = toggle.parentElement;
1693
+ const applyLabel = () => {
1694
+ toggle.textContent = details.open ? 'Hide quoted text' : 'Show quoted text';
1695
+ };
1696
+
1697
+ applyLabel();
1698
+ details.addEventListener('toggle', applyLabel);
1699
+ });
1700
+ }
1701
+
1653
1702
  createLayout() {
1654
1703
  if (typeof document === 'undefined' || !this.container) {
1655
1704
  return;
package/static/js/ui.js CHANGED
@@ -398,6 +398,19 @@ window.uiPostJson = async (url, payload) => {
398
398
  return await res.json();
399
399
  };
400
400
 
401
+ // Report the outcome of an admin JSON action as a toast. These endpoints answer
402
+ // with {success, error, statusCode}, and every caller used to spell out the same
403
+ // three-way ternary, which is why a failing endpoint named the HTTP status it got
404
+ // on one page and not on the next.
405
+ window.uiToastResult = (data, okMessage, failMessage) => {
406
+ if (data.success) {
407
+ window.showToast(okMessage, 'check-circle');
408
+ return;
409
+ }
410
+ const status = data.statusCode ? `HTTP ${data.statusCode}: ` : '';
411
+ window.showToast(data.error ? status + data.error : failMessage, 'alert-triangle');
412
+ };
413
+
401
414
  // Toggle an async action button's busy state: disable the button and swap its
402
415
  // icon span to a spinner while busy, restoring the original icon after
403
416
  window.uiButtonBusy = (btn, busy) => {