emailengine-app 2.74.2 → 2.76.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 (51) hide show
  1. package/.github/workflows/test.yml +0 -10
  2. package/CHANGELOG.md +40 -0
  3. package/config/e2e.toml +1 -1
  4. package/data/google-crawlers.json +1 -1
  5. package/lib/account.js +94 -18
  6. package/lib/api-routes/message-routes.js +3 -1
  7. package/lib/api-routes/route-helpers.js +3 -0
  8. package/lib/api-routes/submit-routes.js +159 -52
  9. package/lib/consts.js +6 -0
  10. package/lib/db.js +4 -1
  11. package/lib/egress-filter.js +220 -0
  12. package/lib/email-client/base-client.js +169 -27
  13. package/lib/email-client/gmail/send-request.js +16 -1
  14. package/lib/email-client/gmail-client.js +91 -10
  15. package/lib/email-client/imap/mailbox.js +3 -9
  16. package/lib/email-client/imap-client.js +103 -1
  17. package/lib/email-client/message-builder.js +24 -7
  18. package/lib/email-client/notification-handler.js +11 -12
  19. package/lib/email-client/outlook/send-request.js +18 -1
  20. package/lib/email-client/outlook-client.js +81 -9
  21. package/lib/imapproxy/imap-core/README.md +69 -0
  22. package/lib/oauth/external-account-config.js +41 -1
  23. package/lib/oauth/external-account-signer.js +7 -2
  24. package/lib/oauth/verify-app.js +18 -3
  25. package/lib/queue-retention.js +56 -0
  26. package/lib/respawn-backoff.js +95 -0
  27. package/lib/schemas.js +9 -2
  28. package/lib/settings.js +4 -1
  29. package/lib/tools.js +20 -0
  30. package/lib/ui-routes/admin-config-routes.js +22 -18
  31. package/lib/utils/network.js +122 -3
  32. package/lib/web-safe-html.js +401 -0
  33. package/lib/webhook-request.js +40 -2
  34. package/lib/webhooks.js +2 -3
  35. package/package.json +17 -20
  36. package/playwright.config.js +1 -1
  37. package/sbom.json +1 -1
  38. package/server.js +45 -8
  39. package/static/js/ee-client.js +55 -6
  40. package/static/js/ui.js +13 -0
  41. package/static/licenses.html +332 -292
  42. package/translations/messages.pot +52 -52
  43. package/views/config/webhooks.hbs +2 -2
  44. package/views/partials/webhooks_editor_functions.hbs +1 -1
  45. package/workers/api.js +32 -17
  46. package/workers/documents.js +6 -0
  47. package/workers/export.js +8 -1
  48. package/workers/imap.js +17 -19
  49. package/workers/submit.js +10 -0
  50. package/workers/webhooks.js +23 -2
  51. package/Gruntfile.js +0 -58
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
 
@@ -249,6 +250,12 @@ const REQUIRE_API_AUTH = hasEnvValue('EENGINE_REQUIRE_API_AUTH') ? getBoolean(re
249
250
  // Opt-out for the license-validation feature beacon (telemetry rides on the existing license call)
250
251
  const BEACON_DISABLED = hasEnvValue('EENGINE_BEACON_DISABLED') ? getBoolean(readEnvValue('EENGINE_BEACON_DISABLED')) : false;
251
252
 
253
+ // Opt-out for the GitHub releases update check, the only background network
254
+ // call that is not tied to a subscription license. With this and a perpetual
255
+ // license an instance makes no background calls at all (strict-egress and
256
+ // air-gapped deployments).
257
+ const UPDATE_CHECK_DISABLED = hasEnvValue('EENGINE_UPDATE_CHECK_DISABLED') ? getBoolean(readEnvValue('EENGINE_UPDATE_CHECK_DISABLED')) : false;
258
+
252
259
  // OAuth2 token access configuration
253
260
  const ENABLE_OAUTH_TOKENS_API = hasEnvValue('EENGINE_ENABLE_OAUTH_TOKENS_API') ? getBoolean(readEnvValue('EENGINE_ENABLE_OAUTH_TOKENS_API')) : null;
254
261
 
@@ -713,6 +720,14 @@ const MAX_ASSIGN_RETRIES = 3; // Max consecutive safety-net retries before givin
713
720
  // Worker management
714
721
  let imapInitialWorkersLoaded = false; // Have all initial IMAP workers started?
715
722
  let workers = new Map(); // Map of type -> Set of workers
723
+ const respawnTracker = new RespawnTracker(); // paces respawns for worker types that keep crashing
724
+
725
+ // Shutdown drain budget. A worker with no in-flight job answers immediately, so this only costs
726
+ // real time when there is genuinely something to finish - which is exactly when waiting is worth
727
+ // it, because the alternative is a re-sent message. Kept under Docker's 10s default SIGKILL grace
728
+ // so the drain completes rather than being cut halfway.
729
+ const SHUTDOWN_DRAIN_TIMEOUT = 8000;
730
+ const WORKER_DRAIN_TIMEOUT = 7000;
716
731
  let workersMeta = new WeakMap(); // Worker metadata
717
732
  let availableIMAPWorkers = new Set(); // IMAP workers ready to accept accounts
718
733
 
@@ -1056,6 +1071,7 @@ let spawnWorker = async (type, opts) => {
1056
1071
  }
1057
1072
 
1058
1073
  // Create new worker thread
1074
+ let spawnedAt = Date.now();
1059
1075
  let worker = new WorkerThread(pathlib.join(__dirname, 'workers', `${type.replace(/[A-Z]/g, c => `-${c.toLowerCase()}`)}.js`), {
1060
1076
  argv,
1061
1077
  env: SHARE_ENV,
@@ -1218,8 +1234,15 @@ let spawnWorker = async (type, opts) => {
1218
1234
  logger.error({ msg: 'Worker unexpectedly exited', exitCode, type });
1219
1235
  }
1220
1236
 
1221
- // Respawn worker after delay, preserving any spawn options (e.g. API worker index)
1222
- await new Promise(r => setTimeout(r, 1000));
1237
+ // Respawn worker after delay, preserving any spawn options (e.g. API worker index).
1238
+ // The delay grows while a worker type keeps dying on startup, so a deterministic
1239
+ // crash no longer respawns once a second forever.
1240
+ let respawnDelay = respawnTracker.recordExit(type, Date.now() - spawnedAt);
1241
+ let streak = respawnTracker.streakFor(type);
1242
+ if (streak > 1) {
1243
+ logger.warn({ msg: 'Worker is crash looping, delaying respawn', type, consecutiveFailures: streak, respawnDelay });
1244
+ }
1245
+ await new Promise(r => setTimeout(r, respawnDelay));
1223
1246
  await spawnWorker(type, opts);
1224
1247
  };
1225
1248
 
@@ -1983,6 +2006,12 @@ function checkActiveLicense() {
1983
2006
  */
1984
2007
  let processCheckUpgrade = async () => {
1985
2008
  try {
2009
+ if (UPDATE_CHECK_DISABLED) {
2010
+ // Clear any marker stored before the check was disabled, otherwise
2011
+ // the dashboard banner would advertise a stale version forever
2012
+ await redis.hdel(`${REDIS_PREFIX}settings`, 'upgrade');
2013
+ return;
2014
+ }
1986
2015
  let updateInfo = await checkForUpgrade();
1987
2016
  if (updateInfo.canUpgrade) {
1988
2017
  logger.info({ msg: 'EmailEngine update available', updateInfo });
@@ -2999,12 +3028,19 @@ async function collectMetrics() {
2999
3028
  const closeQueues = cb => {
3000
3029
  let proms = [];
3001
3030
 
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')) {
3031
+ // Signal every worker to wind down before exiting. Signals only reach this thread, so a worker
3032
+ // cannot notice the shutdown on its own - without this its sockets are simply cut and any
3033
+ // BullMQ job it was running is recovered as stalled and re-run, which for the submit queue
3034
+ // means re-sending a message the MTA had already accepted.
3035
+ //
3036
+ // Broadcast rather than an allowlist of types: a worker with nothing to drain answers the
3037
+ // unknown command with 999 and costs one round trip, whereas a list here has to be kept in
3038
+ // sync with every worker that later grows a drain.
3039
+ for (let [type, typeWorkers] of workers) {
3040
+ for (let worker of typeWorkers) {
3005
3041
  proms.push(
3006
- call(worker, { cmd: 'close', timeout: 4000 }).catch(err => {
3007
- logger.error({ msg: 'Failed to signal webhooks worker to close', err });
3042
+ call(worker, { cmd: 'close', timeout: WORKER_DRAIN_TIMEOUT }).catch(err => {
3043
+ logger.error({ msg: 'Failed to signal worker to close', type, err });
3008
3044
  })
3009
3045
  );
3010
3046
  }
@@ -3026,9 +3062,10 @@ const closeQueues = cb => {
3026
3062
  if (returned) {
3027
3063
  return;
3028
3064
  }
3065
+ logger.warn({ msg: 'Shutdown drain did not finish in time, exiting anyway', timeout: SHUTDOWN_DRAIN_TIMEOUT });
3029
3066
  returned = true;
3030
3067
  cb();
3031
- }, 5000);
3068
+ }, SHUTDOWN_DRAIN_TIMEOUT);
3032
3069
 
3033
3070
  Promise.allSettled(proms).then(() => {
3034
3071
  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) => {