emailengine-app 2.84.1 → 2.85.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 (147) hide show
  1. package/.dockerignore +6 -0
  2. package/.ncurc.js +5 -3
  3. package/.prettierignore +5 -1
  4. package/.prettierrc.js +2 -0
  5. package/.release-please-manifest.json +1 -1
  6. package/CHANGELOG.md +61 -0
  7. package/Dockerfile +8 -2
  8. package/README.md +1 -1
  9. package/bin/emailengine.js +444 -403
  10. package/copy-static-files.sh +39 -14
  11. package/data/google-crawlers.json +1 -1
  12. package/data/public_suffix_list.dat +16562 -0
  13. package/docker-compose.yml +1 -1
  14. package/eslint.config.js +81 -99
  15. package/lib/account/own-account-key.js +42 -0
  16. package/lib/account-assignment.js +1 -33
  17. package/lib/account.js +68 -24
  18. package/lib/add-trackers.js +11 -4
  19. package/lib/api-reference/format.js +4 -3
  20. package/lib/api-routes/account-routes.js +244 -26
  21. package/lib/api-routes/blocklist-routes.js +23 -2
  22. package/lib/api-routes/delivery-test-routes.js +26 -3
  23. package/lib/api-routes/export-routes.js +28 -2
  24. package/lib/api-routes/gateway-routes.js +37 -5
  25. package/lib/api-routes/mailbox-routes.js +41 -2
  26. package/lib/api-routes/message-routes.js +104 -4
  27. package/lib/api-routes/mint-bounds.js +11 -0
  28. package/lib/api-routes/oauth2-app-routes.js +48 -15
  29. package/lib/api-routes/outbox-routes.js +45 -11
  30. package/lib/api-routes/permission-map.js +12 -1
  31. package/lib/api-routes/route-helpers.js +125 -3
  32. package/lib/api-routes/settings-routes.js +23 -2
  33. package/lib/api-routes/submit-routes.js +338 -309
  34. package/lib/api-routes/template-routes.js +38 -2
  35. package/lib/api-routes/token-routes.js +142 -96
  36. package/lib/api-routes/webhook-route-routes.js +13 -2
  37. package/lib/auth-token.js +23 -10
  38. package/lib/bounce-detect.js +414 -210
  39. package/lib/consts.js +39 -4
  40. package/lib/create-imap-client.js +41 -2
  41. package/lib/email-client/base-client.js +30 -41
  42. package/lib/email-client/gmail-client.js +30 -18
  43. package/lib/email-client/imap/mailbox.js +13 -23
  44. package/lib/email-client/imap/response-metrics.js +75 -0
  45. package/lib/email-client/imap/subconnection.js +1 -1
  46. package/lib/email-client/imap-client.js +6 -3
  47. package/lib/email-client/message-builder.js +7 -2
  48. package/lib/email-client/notification-handler.js +1 -2
  49. package/lib/email-client/outlook/graph-api.js +17 -2
  50. package/lib/email-client/outlook-client.js +6 -3
  51. package/lib/email-client/smtp-pool-manager.js +6 -0
  52. package/lib/encrypt.js +9 -0
  53. package/lib/export.js +3 -1
  54. package/lib/gateway.js +2 -3
  55. package/lib/get-raw-email.js +4 -2
  56. package/lib/hapi-request-logger.js +107 -0
  57. package/lib/imap-proxy-auth.js +14 -1
  58. package/lib/imapproxy/imap-core/README.md +6 -5
  59. package/lib/imapproxy/imap-core/lib/commands/authenticate-plain.js +1 -0
  60. package/lib/imapproxy/imap-core/lib/commands/login.js +1 -0
  61. package/lib/imapproxy/imap-core/lib/imap-command.js +86 -43
  62. package/lib/imapproxy/imap-core/lib/imap-stream.js +58 -13
  63. package/lib/imapproxy/imap-core/lib/indexer/indexer.js +3 -1
  64. package/lib/mcp/listen.js +10 -1
  65. package/lib/mcp/tools.js +37 -8
  66. package/lib/notify-attachments.js +17 -0
  67. package/lib/oauth/external-account-config.js +3 -1
  68. package/lib/oauth/gmail.js +21 -48
  69. package/lib/oauth/mail-ru.js +5 -38
  70. package/lib/oauth/outlook-mailbox.js +35 -0
  71. package/lib/oauth/outlook.js +26 -44
  72. package/lib/oauth/state.js +24 -0
  73. package/lib/oauth2-apps.js +1 -1
  74. package/lib/pbkdf2-phc.js +19 -2
  75. package/lib/privileged-settings.js +6 -3
  76. package/lib/redis-scan-delete.js +15 -1
  77. package/lib/schemas.js +79 -33
  78. package/lib/settings.js +13 -1
  79. package/lib/smtp-auth.js +23 -0
  80. package/lib/sso.js +1 -1
  81. package/lib/swagger-options.js +1 -1
  82. package/lib/thread-diagnostics.js +46 -0
  83. package/lib/timezones.js +109 -0
  84. package/lib/token-permission-view.js +3 -3
  85. package/lib/tools.js +285 -66
  86. package/lib/totp.js +2 -5
  87. package/lib/translations.js +1 -2
  88. package/lib/ui-routes/account-routes.js +491 -491
  89. package/lib/ui-routes/account-setup.js +99 -0
  90. package/lib/ui-routes/admin-config-routes.js +98 -48
  91. package/lib/ui-routes/admin-entities-routes.js +113 -31
  92. package/lib/ui-routes/auth-routes.js +137 -62
  93. package/lib/ui-routes/connection-security-form.js +74 -0
  94. package/lib/ui-routes/form-lists.js +46 -0
  95. package/lib/ui-routes/internals-routes.js +5 -0
  96. package/lib/ui-routes/mcp-consent-routes.js +5 -1
  97. package/lib/ui-routes/oauth-config-routes.js +133 -100
  98. package/lib/ui-routes/route-helpers.js +19 -3
  99. package/lib/ui-routes/sign-in-helpers.js +59 -0
  100. package/lib/ui-routes/tls-config-routes.js +58 -15
  101. package/lib/ui-routes/unsubscribe-routes.js +6 -3
  102. package/lib/utils/back-off.js +82 -0
  103. package/lib/utils/map-concurrent.js +36 -0
  104. package/lib/utils/murmurhash3.js +66 -0
  105. package/lib/utils/network.js +28 -0
  106. package/lib/utils/provider-id.js +27 -0
  107. package/lib/utils/public-suffix.js +122 -0
  108. package/lib/utils/random-id.js +26 -0
  109. package/lib/utils/require-tls.js +33 -0
  110. package/lib/utils/version.js +74 -0
  111. package/lib/web-safe-html.js +5 -6
  112. package/lib/webhook-penalty.js +75 -0
  113. package/lib/webhook-routing.js +49 -2
  114. package/lib/webhooks.js +41 -6
  115. package/package.json +37 -39
  116. package/sbom.json +7980 -1
  117. package/server.js +659 -455
  118. package/static/js/ace/ace.js +1 -1
  119. package/static/js/ace/mode-handlebars.js +1 -1
  120. package/static/js/ace/mode-html.js +1 -1
  121. package/static/js/ace/mode-javascript.js +1 -1
  122. package/static/js/ace/mode-markdown.js +1 -1
  123. package/static/js/ace/worker-html.js +1 -1
  124. package/static/js/ace/worker-javascript.js +1 -1
  125. package/static/js/ace/worker-json.js +1 -1
  126. package/static/js/ee-client.js +5 -0
  127. package/static/licenses.html +191 -1016
  128. package/translations/messages.pot +70 -71
  129. package/views/account/security.hbs +10 -0
  130. package/views/accounts/account.hbs +11 -11
  131. package/views/accounts/edit.hbs +6 -2
  132. package/views/config/ai.hbs +1 -1
  133. package/views/config/security.hbs +2 -2
  134. package/views/config/service.hbs +3 -0
  135. package/views/internals/index.hbs +8 -0
  136. package/views/partials/gateway_form.hbs +3 -1
  137. package/views/partials/webhook_form.hbs +1 -1
  138. package/views/suppression-lists/index.hbs +1 -1
  139. package/workers/api.js +780 -585
  140. package/workers/export.js +1 -1
  141. package/workers/submit.js +1 -1
  142. package/workers/webhooks.js +54 -34
  143. package/config/test.toml +0 -46
  144. package/install.sh +0 -428
  145. package/setup-production.sh +0 -215
  146. package/systemd/emailengine.service +0 -97
  147. package/systemd/nginx-proxy.conf +0 -77
@@ -215,7 +215,14 @@ const SURFACE_GRANTS = {
215
215
  { action: ACTION.DESTRUCTIVE, group: GROUP.MESSAGE },
216
216
  { action: ACTION.SEND, group: GROUP.SUBMIT },
217
217
  { action: ACTION.READ, group: GROUP.OUTBOX },
218
- { action: ACTION.READ, group: GROUP.TEMPLATE }
218
+ { action: ACTION.READ, group: GROUP.TEMPLATE },
219
+ // Folder create and rename (the Mail agent level) and folder delete (Full access), added
220
+ // 2026-10-20 by explicit operator decision. Unlike the pairs above, these widened tokens
221
+ // already issued: an mcp-scoped token without a permissions record is bounded by this
222
+ // table alone, and those - including every one minted from the "Full access" consent in
223
+ // 2.79.2 to 2.80.0 - gained folder create, rename and delete with them
224
+ { action: ACTION.WRITE, group: GROUP.MAILBOX },
225
+ { action: ACTION.DESTRUCTIVE, group: GROUP.MAILBOX }
219
226
  ],
220
227
 
221
228
  // The management half of the MCP endpoint: the same door, checked per request the same way,
@@ -428,6 +435,10 @@ const GROUP_ROUTES = {
428
435
  'DELETE /v1/account/{account}',
429
436
  'PUT /v1/account/{account}/flush',
430
437
  'PUT /v1/account/{account}/reconnect',
438
+ // Lifts the auth-failure park and reconnects: operating the connection like the reconnect
439
+ // above, and no more than that - it never touches the stored credentials or where they
440
+ // are sent, and an operator's own send-only switch is not lifted by it
441
+ 'PUT /v1/account/{account}/resume',
431
442
  'PUT /v1/account/{account}/sync',
432
443
  'GET /v1/account/{account}/server-signatures'
433
444
  ],
@@ -3,6 +3,7 @@
3
3
  const Boom = require('@hapi/boom');
4
4
  const { isDeepStrictEqual } = require('node:util');
5
5
  const tokenPermissions = require('../token-permissions');
6
+ const { sessionTokenAdmits } = require('./permission-map');
6
7
  const settings = require('../settings');
7
8
 
8
9
  // Shared helpers for the extracted API route modules under lib/api-routes/.
@@ -375,7 +376,109 @@ function isRestrictedCredential(request) {
375
376
  if (request.app && request.app.sessionToken) {
376
377
  return true;
377
378
  }
378
- return tokenPermissions.inspect(request.auth && request.auth.artifacts).narrowed;
379
+ const tokenData = request.auth && request.auth.artifacts;
380
+ if (isSurfaceBoundToken(tokenData)) {
381
+ return true;
382
+ }
383
+ return tokenPermissions.inspect(tokenData).narrowed;
384
+ }
385
+
386
+ /**
387
+ * Whether a token reaches the API only through an MCP surface scope.
388
+ *
389
+ * `mcp` and `mcp-manage` admit the operations their surface table covers, on requests the MCP
390
+ * endpoint dispatched itself. A token holding one of them and neither `api` nor `*` is therefore
391
+ * bounded below the full API whether or not it carries a permissions record: the ones minted before
392
+ * every MCP token got an explicit grant list have no record, and read as unnarrowed to inspect().
393
+ *
394
+ * @param {Object} tokenData - the token record (request.auth.artifacts)
395
+ * @returns {Boolean}
396
+ */
397
+ function isSurfaceBoundToken(tokenData) {
398
+ const scopes = tokenData && Array.isArray(tokenData.scopes) ? tokenData.scopes : null;
399
+ if (!scopes || scopes.includes('*') || scopes.includes('api')) {
400
+ return false;
401
+ }
402
+ return scopes.includes('mcp') || scopes.includes('mcp-manage');
403
+ }
404
+
405
+ /**
406
+ * Whether the credential behind a request is bound to one account.
407
+ *
408
+ * A bound token is admitted to its own account's routes only (the api-token strategy checks the
409
+ * binding against the path), but it is not narrowed in the permission-record sense and so reads as
410
+ * unrestricted to isRestrictedCredential(). The payload rules that decide whether an account may
411
+ * borrow another account's identity need both answers.
412
+ *
413
+ * @param {Object} request - the Hapi request
414
+ * @returns {Boolean}
415
+ */
416
+ function isBoundCredential(request) {
417
+ const tokenData = request.auth && request.auth.artifacts;
418
+ return !!(tokenData && tokenData.account);
419
+ }
420
+
421
+ /**
422
+ * Refuses an account payload that would make the account authenticate as a different one, when the
423
+ * credential sending it is bound to an account or otherwise restricted.
424
+ *
425
+ * `oauth2.auth.delegatedAccount` makes the account connect with the named account's stored OAuth2
426
+ * token, and nothing else checks that the two belong together: an account-bound token could name
427
+ * any other account on the instance and read its mailbox through its own routes, which is exactly
428
+ * what the binding promises cannot happen. `delegatedUser` alone retargets a Microsoft 365 shared
429
+ * mailbox. And on an application-credentials app (`gmailService`, `outlookService`) there is no
430
+ * per-user credential at all, so changing `auth.user` points the account at any mailbox in the
431
+ * tenant. The admin interface and an unrestricted, unbound token keep every one of these.
432
+ *
433
+ * `isServiceApp` is asked only when the payload names a user and the credential is one this guard
434
+ * applies to, so the lookup it performs is not paid on the common path.
435
+ *
436
+ * @param {Object} request - the Hapi request
437
+ * @param {Object} [opts]
438
+ * @param {Function} [opts.isServiceApp] - async, resolves true when the account's OAuth2 app uses application credentials
439
+ */
440
+ async function assertNoIdentityRetarget(request, opts) {
441
+ opts = opts || {};
442
+ const auth = request.payload && request.payload.oauth2 && request.payload.oauth2.auth;
443
+ if (!auth || typeof auth !== 'object') {
444
+ return;
445
+ }
446
+
447
+ if (!isBoundCredential(request) && !isRestrictedCredential(request)) {
448
+ return;
449
+ }
450
+
451
+ if (auth.delegatedAccount || auth.delegatedUser) {
452
+ throw Boom.forbidden("A token with restricted permissions can not delegate an account to another account's credentials or to a shared mailbox");
453
+ }
454
+
455
+ if (auth.user && typeof opts.isServiceApp === 'function' && (await opts.isServiceApp())) {
456
+ throw Boom.forbidden('A token with restricted permissions can not change the mailbox of an account that authenticates with application credentials');
457
+ }
458
+ }
459
+
460
+ /**
461
+ * Refuses a request whose credential does not hold an operation the handler is about to perform in
462
+ * addition to the one its route resolves to.
463
+ *
464
+ * The permission check runs once per route, against routeGrant(). A route that writes when asked
465
+ * to (`GET .../message/{message}?markAsSeen=true` sets the `\Seen` flag) therefore has to ask for
466
+ * the second grant itself. A session token is held to SESSION_TOKEN_GRANTS, a narrowed token to its
467
+ * record, through the same two predicates the strategy uses; every other credential holds
468
+ * everything and passes.
469
+ *
470
+ * @param {Object} request - the Hapi request
471
+ * @param {{action: String, group: String}} operation - the (action, group) pair the handler needs
472
+ * @param {String} what - the thing being refused, for the message
473
+ */
474
+ function assertCredentialAllows(request, operation, what) {
475
+ const allowed =
476
+ request.app && request.app.sessionToken
477
+ ? sessionTokenAdmits(operation, { account: request.params && request.params.account })
478
+ : tokenPermissions.check({ tokenData: request.auth && request.auth.artifacts, operation }).allowed;
479
+ if (!allowed) {
480
+ throw Boom.forbidden(`A token with restricted permissions can not ${what}`);
481
+ }
379
482
  }
380
483
 
381
484
  /**
@@ -408,6 +511,13 @@ function assertNoPrivilegedSettings(request, keys, verb = 'change') {
408
511
  throw Boom.forbidden(`A token with restricted permissions can not ${verb} ${privileged.join(', ')}`);
409
512
  }
410
513
 
514
+ // The service-account authentication methods that act with something the instance holds rather than
515
+ // with a credential the caller supplied, and what a refusal calls each
516
+ const ATTACHED_IDENTITY_METHODS = new Map([
517
+ ['metadataServer', 'the attached service account (metadataServer)'],
518
+ ['externalAccount', 'a credential source read from this host (externalAccount)']
519
+ ]);
520
+
411
521
  /**
412
522
  * Refuses an OAuth2 application that would authenticate as the instance's own cloud identity, when
413
523
  * the credential creating it is narrowed.
@@ -427,23 +537,35 @@ function assertNoPrivilegedSettings(request, keys, verb = 'change') {
427
537
  * unrestricted token remain. A saved app's method is pinned (isAuthMethodLocked() in
428
538
  * lib/oauth2-apps.js), so the stored one is what an update checks.
429
539
  *
540
+ * `authMethod: 'externalAccount'` is refused on the same grounds. Its credential source is a file
541
+ * path the instance reads, or a URL the instance fetches with caller-chosen headers from the host
542
+ * it runs on, outside the HTTP proxy and the egress policy because the endpoint it is meant for is
543
+ * the local identity server. Either one reads the deployment's own environment, and the verify
544
+ * report says how the read went, so a narrowed `oauth2` token could use it to read files and probe
545
+ * the instance's network. Same remedy: the admin interface or an unrestricted token.
546
+ *
430
547
  * @param {Object} request - the Hapi request
431
548
  * @param {string} [authMethod] - the method of the app being created (the payload's) or modified (the stored one's)
432
549
  * @param {string} verb - 'create' or 'modify', for the refusal message
433
550
  */
434
551
  function assertNoAttachedIdentity(request, authMethod, verb) {
435
- if (authMethod !== 'metadataServer' || !isRestrictedCredential(request)) {
552
+ const what = ATTACHED_IDENTITY_METHODS.get(authMethod);
553
+ if (!what || !isRestrictedCredential(request)) {
436
554
  return;
437
555
  }
438
556
 
439
557
  throw Boom.forbidden(
440
- `A token with restricted permissions can not ${verb} an application that uses the attached service account (metadataServer). Use the admin interface or an unrestricted token`
558
+ `A token with restricted permissions can not ${verb} an application that uses ${what}. Use the admin interface or an unrestricted token`
441
559
  );
442
560
  }
443
561
 
444
562
  module.exports = {
445
563
  handleError,
446
564
  assertNoAttachedIdentity,
565
+ assertNoIdentityRetarget,
566
+ assertCredentialAllows,
567
+ isRestrictedCredential,
568
+ isBoundCredential,
447
569
  logApiError,
448
570
  throwNotFound,
449
571
  assertNoPrivilegedSettings,
@@ -43,8 +43,8 @@ settingsOutputSchema.eventTypes = Joi.array()
43
43
  // agree.
44
44
  const MCP_SETTINGS_KEYS = Object.keys(settingsSchema).filter(key => !settings.privilegedKeys.includes(key) && !isHiddenSetting(key));
45
45
 
46
- async function init(args) {
47
- const { server, call, notify, CORS_CONFIG } = args;
46
+ function registerGetSettings(server, args) {
47
+ const { CORS_CONFIG } = args;
48
48
 
49
49
  server.route({
50
50
  method: 'GET',
@@ -135,6 +135,10 @@ async function init(args) {
135
135
  }
136
136
  }
137
137
  });
138
+ }
139
+
140
+ function registerUpdateSettings(server, args) {
141
+ const { call, notify, CORS_CONFIG } = args;
138
142
 
139
143
  server.route({
140
144
  method: 'POST',
@@ -256,6 +260,10 @@ async function init(args) {
256
260
  }
257
261
  }
258
262
  });
263
+ }
264
+
265
+ function registerGetQueueSettings(server, args) {
266
+ const { CORS_CONFIG } = args;
259
267
 
260
268
  server.route({
261
269
  method: 'GET',
@@ -360,6 +368,10 @@ async function init(args) {
360
368
  }
361
369
  }
362
370
  });
371
+ }
372
+
373
+ function registerUpdateQueueSettings(server, args) {
374
+ const { CORS_CONFIG } = args;
363
375
 
364
376
  server.route({
365
377
  method: 'PUT',
@@ -452,4 +464,13 @@ async function init(args) {
452
464
  });
453
465
  }
454
466
 
467
+ async function init(args) {
468
+ const { server } = args;
469
+
470
+ registerGetSettings(server, args);
471
+ registerUpdateSettings(server, args);
472
+ registerGetQueueSettings(server, args);
473
+ registerUpdateQueueSettings(server, args);
474
+ }
475
+
455
476
  module.exports = init;