emailengine-app 2.79.0 → 2.79.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 (110) hide show
  1. package/.release-please-manifest.json +1 -1
  2. package/CHANGELOG.md +79 -0
  3. package/bin/emailengine.js +8 -1
  4. package/config/default.toml +6 -0
  5. package/data/google-crawlers.json +1 -1
  6. package/lib/account.js +137 -19
  7. package/lib/api-routes/account-routes.js +13 -1
  8. package/lib/api-routes/index.js +12 -0
  9. package/lib/api-routes/mailbox-routes.js +6 -0
  10. package/lib/api-routes/mcp-oauth-routes.js +259 -0
  11. package/lib/api-routes/mcp-routes.js +172 -0
  12. package/lib/api-routes/message-routes.js +125 -4
  13. package/lib/api-routes/outbox-routes.js +7 -0
  14. package/lib/api-routes/permission-map.js +101 -1
  15. package/lib/api-routes/route-helpers.js +87 -4
  16. package/lib/api-routes/route-metadata.js +24 -4
  17. package/lib/api-routes/settings-routes.js +37 -14
  18. package/lib/api-routes/submit-routes.js +41 -0
  19. package/lib/api-routes/template-routes.js +7 -0
  20. package/lib/api-routes/token-routes.js +292 -122
  21. package/lib/arf-detect.js +17 -4
  22. package/lib/bounce-detect.js +28 -17
  23. package/lib/consts.js +36 -1
  24. package/lib/db.js +6 -0
  25. package/lib/email-client/base-client.js +102 -117
  26. package/lib/email-client/gmail-client.js +91 -29
  27. package/lib/email-client/imap/mailbox.js +175 -101
  28. package/lib/email-client/imap-client.js +49 -2
  29. package/lib/email-client/outlook-client.js +20 -24
  30. package/lib/enum-descriptions.js +2 -1
  31. package/lib/field-usage.js +17 -1
  32. package/lib/lua/h-set-new-mark.lua +36 -0
  33. package/lib/lua/s-list-accounts.lua +5 -2
  34. package/lib/mcp/index.js +29 -0
  35. package/lib/mcp/inject.js +70 -0
  36. package/lib/mcp/json-schema.js +120 -0
  37. package/lib/mcp/listen.js +171 -0
  38. package/lib/mcp/oauth.js +324 -0
  39. package/lib/mcp/protocol.js +489 -0
  40. package/lib/mcp/resources.js +139 -0
  41. package/lib/mcp/tools.js +655 -0
  42. package/lib/message-port-stream.js +84 -1
  43. package/lib/oauth2-apps.js +84 -15
  44. package/lib/openapi/build-document.js +5 -1
  45. package/lib/openapi/index.js +3 -1
  46. package/lib/response-stream.js +52 -21
  47. package/lib/routes-ui.js +4 -0
  48. package/lib/schemas.js +26 -5
  49. package/lib/settings.js +23 -9
  50. package/lib/token-audit-log.js +32 -9
  51. package/lib/token-permission-view.js +104 -8
  52. package/lib/tokens.js +194 -68
  53. package/lib/tools.js +96 -0
  54. package/lib/ui-routes/account-routes.js +55 -6
  55. package/lib/ui-routes/admin-config-routes.js +114 -1
  56. package/lib/ui-routes/admin-entities-routes.js +103 -19
  57. package/lib/ui-routes/auth-routes.js +13 -0
  58. package/lib/ui-routes/mcp-consent-routes.js +330 -0
  59. package/lib/ui-routes/oauth-config-routes.js +7 -7
  60. package/lib/ui-routes/route-helpers.js +143 -1
  61. package/lib/ui-routes/suppression-list-routes.js +14 -9
  62. package/lib/utils/decode-headers.js +42 -0
  63. package/lib/utils/header-map.js +48 -0
  64. package/lib/utils/network.js +9 -1
  65. package/lib/utils/unsafe-key.js +59 -0
  66. package/lib/web-safe-html.js +61 -0
  67. package/lib/webhook-routing.js +6 -2
  68. package/lib/webhooks.js +31 -8
  69. package/package.json +9 -8
  70. package/sbom.json +1 -1
  71. package/scripts/README.md +25 -0
  72. package/scripts/build-icon.js +77 -0
  73. package/server.js +39 -17
  74. package/static/css/flyonui.css +1 -1
  75. package/static/css/src/app.tailwind.css +98 -0
  76. package/static/emailengine.ico +0 -0
  77. package/static/js/ui.js +465 -1
  78. package/static/licenses.html +42 -22
  79. package/translations/messages.pot +57 -57
  80. package/views/accounts/account.hbs +8 -8
  81. package/views/accounts/browse.hbs +1 -1
  82. package/views/accounts/edit.hbs +2 -2
  83. package/views/config/mcp.hbs +361 -0
  84. package/views/config/oauth/index.hbs +7 -0
  85. package/views/gateways/edit.hbs +1 -1
  86. package/views/gateways/gateway.hbs +2 -2
  87. package/views/gateways/index.hbs +7 -7
  88. package/views/layout/prompt.hbs +62 -0
  89. package/views/mcp/authorize.hbs +136 -0
  90. package/views/partials/accounts_table.hbs +7 -7
  91. package/views/partials/head_assets.hbs +3 -3
  92. package/views/partials/mcp_access_levels.hbs +13 -0
  93. package/views/partials/mcp_account_limit.hbs +14 -0
  94. package/views/partials/mcp_tool_count.hbs +19 -0
  95. package/views/partials/side_menu.hbs +1 -0
  96. package/views/partials/ui/account-picker.hbs +59 -0
  97. package/views/partials/ui/code-block.hbs +26 -0
  98. package/views/partials/ui/menu-item.hbs +5 -0
  99. package/views/partials/ui/radio.hbs +41 -0
  100. package/views/partials/ui/tab.hbs +15 -10
  101. package/views/partials/ui/tabs.hbs +8 -5
  102. package/views/suppression-lists/list.hbs +1 -1
  103. package/views/templates/index.hbs +2 -2
  104. package/views/templates/template.hbs +5 -4
  105. package/views/tokens/index.hbs +30 -4
  106. package/views/tokens/new.hbs +517 -239
  107. package/winconf.js +148 -48
  108. package/workers/api.js +239 -95
  109. package/workers/imap.js +65 -72
  110. package/workers/webhooks.js +21 -1
package/workers/imap.js CHANGED
@@ -19,7 +19,7 @@ const { BaseClient } = require('../lib/email-client/base-client');
19
19
  const { Account } = require('../lib/account');
20
20
  const { oauth2Apps, isApiBasedApp } = require('../lib/oauth2-apps');
21
21
  const { redis, notifyQueue, submitQueue, documentsQueue, getFlowProducer } = require('../lib/db');
22
- const { MessagePortWritable, pipeToMessagePort } = require('../lib/message-port-stream');
22
+ const { sendToMessagePort, MessagePortWritable } = require('../lib/message-port-stream');
23
23
  const { packRpcError, unpackRpcError } = require('../lib/worker-rpc-error');
24
24
  const { getESClient } = require('../lib/document-store');
25
25
  const settings = require('../lib/settings');
@@ -741,89 +741,82 @@ class ConnectionHandler {
741
741
  }
742
742
 
743
743
  async getRawMessage(message) {
744
- if (!this.accounts.has(message.account)) {
745
- throw NO_ACTIVE_HANDLER_RESP_ERR;
746
- }
744
+ // Built before the fetch rather than after it: the fetch can take minutes, and a consumer
745
+ // whose thread dies in the meantime closes the port with nothing queued, which only reaches
746
+ // a listener that was already attached. See the MessagePortWritable constructor.
747
+ const writable = new MessagePortWritable(message.port);
748
+ try {
749
+ if (!this.accounts.has(message.account)) {
750
+ throw NO_ACTIVE_HANDLER_RESP_ERR;
751
+ }
747
752
 
748
- let accountData = this.accounts.get(message.account);
749
- if (!accountData.connection) {
750
- throw NO_ACTIVE_HANDLER_RESP_ERR;
751
- }
752
- let source = await accountData.connection.getRawMessage(message.message);
753
- if (!source) {
754
- let err = new Error('Requested file not found');
755
- err.statusCode = 404;
753
+ let accountData = this.accounts.get(message.account);
754
+ if (!accountData.connection) {
755
+ throw NO_ACTIVE_HANDLER_RESP_ERR;
756
+ }
757
+ let source = await accountData.connection.getRawMessage(message.message);
758
+ if (!source) {
759
+ let err = new Error('Requested file not found');
760
+ err.statusCode = 404;
761
+ throw err;
762
+ }
763
+
764
+ sendToMessagePort(writable, source, accountData.connection.logger);
765
+
766
+ return {
767
+ headers: {
768
+ 'content-type': 'message/rfc822',
769
+ 'content-disposition': `attachment; filename=message.eml`
770
+ },
771
+ contentType: 'message/rfc822'
772
+ };
773
+ } catch (err) {
774
+ // Nothing to send, so the port goes back untouched - the consumer is about to be handed
775
+ // this error, and a closed port would reach it as a failed transfer instead.
776
+ writable.releaseListeners();
756
777
  throw err;
757
778
  }
779
+ }
758
780
 
759
- // Build the cross-thread writable only after we know there is content to send.
760
- // Constructing it earlier leaks the transferred MessagePort (and its message
761
- // listener) whenever the lookup above throws (e.g. a 404).
762
- let stream = new MessagePortWritable(message.port);
781
+ async getAttachment(message) {
782
+ // Built before the download starts, not after: an attachment is the longest wait on this
783
+ // worker, and a consumer thread that dies during it closes the port with nothing queued -
784
+ // which only a listener attached beforehand ever sees. See the MessagePortWritable
785
+ // constructor.
786
+ const writable = new MessagePortWritable(message.port);
787
+ try {
788
+ if (!this.accounts.has(message.account)) {
789
+ throw NO_ACTIVE_HANDLER_RESP_ERR;
790
+ }
763
791
 
764
- setImmediate(() => {
765
- if (Buffer.isBuffer(source)) {
766
- // The consumer may have aborted and destroyed the writable (via a cancel
767
- // message) before this runs; ending a destroyed stream would emit an
768
- // unhandled 'error' and take down the worker. The streaming branch is
769
- // safe because pipeline() tears down a destroyed destination cleanly.
770
- if (!stream.destroyed) {
771
- stream.end(source);
772
- }
773
- } else {
774
- pipeToMessagePort(source, stream, accountData.connection.logger);
792
+ let accountData = this.accounts.get(message.account);
793
+ if (!accountData.connection) {
794
+ throw NO_ACTIVE_HANDLER_RESP_ERR;
775
795
  }
776
- });
777
796
 
778
- return {
779
- headers: {
780
- 'content-type': 'message/rfc822',
781
- 'content-disposition': `attachment; filename=message.eml`
782
- },
783
- contentType: 'message/rfc822'
784
- };
785
- }
797
+ let source = await accountData.connection.getAttachment(message.attachment);
798
+ if (!source) {
799
+ let err = new Error('Requested file not found');
800
+ err.statusCode = 404;
801
+ throw err;
802
+ }
786
803
 
787
- async getAttachment(message) {
788
- if (!this.accounts.has(message.account)) {
789
- throw NO_ACTIVE_HANDLER_RESP_ERR;
790
- }
804
+ // IMAP resolves an attachment to the download stream itself, Gmail and Graph to a Buffer
805
+ // wrapped alongside the headers.
806
+ let payload = Buffer.isBuffer(source.data) ? source.data : source;
791
807
 
792
- let accountData = this.accounts.get(message.account);
793
- if (!accountData.connection) {
794
- throw NO_ACTIVE_HANDLER_RESP_ERR;
795
- }
808
+ sendToMessagePort(writable, payload, accountData.connection.logger);
796
809
 
797
- let source = await accountData.connection.getAttachment(message.attachment);
798
- if (!source) {
799
- let err = new Error('Requested file not found');
800
- err.statusCode = 404;
810
+ return {
811
+ headers: source.headers,
812
+ contentType: source.contentType
813
+ };
814
+ } catch (err) {
815
+ // Nothing to send, so the port goes back untouched - the consumer is about to be handed
816
+ // this error, and a closed port would reach it as a failed transfer instead.
817
+ writable.releaseListeners();
801
818
  throw err;
802
819
  }
803
-
804
- // Build the cross-thread writable only after we know there is content to send.
805
- // Constructing it earlier leaks the transferred MessagePort (and its message
806
- // listener) whenever the lookup above throws (e.g. a 404).
807
- let stream = new MessagePortWritable(message.port);
808
-
809
- setImmediate(() => {
810
- if (Buffer.isBuffer(source.data)) {
811
- // The consumer may have aborted and destroyed the writable (via a cancel
812
- // message) before this runs; ending a destroyed stream would emit an
813
- // unhandled 'error' and take down the worker. The streaming branch is
814
- // safe because pipeline() tears down a destroyed destination cleanly.
815
- if (!stream.destroyed) {
816
- stream.end(source.data);
817
- }
818
- } else {
819
- pipeToMessagePort(source, stream, accountData.connection.logger);
820
- }
821
- });
822
-
823
- return {
824
- headers: source.headers,
825
- contentType: source.contentType
826
- };
827
820
  }
828
821
 
829
822
  // Shutdown teardown: closes every account connection this worker owns. The main thread owns
@@ -227,6 +227,15 @@ const notifyWorker = new Worker(
227
227
 
228
228
  let webhooksEnabled = await settings.get('webhooksEnabled');
229
229
  if (!webhooksEnabled) {
230
+ logger.debug({
231
+ msg: 'Webhooks are disabled',
232
+ action: 'webhook',
233
+ queue: job.queue.name,
234
+ code: 'webhooks_disabled',
235
+ job: job.id,
236
+ event: job.name,
237
+ account: job.data.account
238
+ });
230
239
  return;
231
240
  }
232
241
 
@@ -246,7 +255,18 @@ const notifyWorker = new Worker(
246
255
  // the global setting is only fetched when neither a custom route nor the account override decides the target
247
256
  let webhooks = resolveTargetUrl(customRoute && customRoute.targetUrl, accountWebhooks, null).url || (await settings.get('webhooks'));
248
257
  if (!webhooks) {
249
- // logger.debug({ msg: 'Webhook URL is not set', action: 'webhook', event: job.name, account: job.data.account });
258
+ // A skipped delivery still reports the job as completed, so without this line a
259
+ // missing target is indistinguishable from a successful delivery - it reads as the
260
+ // receiver being slow rather than as nothing having been sent
261
+ logger.debug({
262
+ msg: 'Webhook URL is not set',
263
+ action: 'webhook',
264
+ queue: job.queue.name,
265
+ code: 'no_target_url',
266
+ job: job.id,
267
+ event: job.name,
268
+ account: job.data.account
269
+ });
250
270
  return;
251
271
  }
252
272