emailengine-app 2.84.2 → 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 (86) hide show
  1. package/.ncurc.js +5 -3
  2. package/.prettierignore +5 -1
  3. package/.prettierrc.js +2 -0
  4. package/.release-please-manifest.json +1 -1
  5. package/CHANGELOG.md +33 -0
  6. package/Dockerfile +6 -0
  7. package/README.md +1 -1
  8. package/bin/emailengine.js +434 -408
  9. package/copy-static-files.sh +39 -14
  10. package/data/google-crawlers.json +1 -1
  11. package/data/public_suffix_list.dat +16562 -0
  12. package/eslint.config.js +81 -99
  13. package/lib/account.js +36 -5
  14. package/lib/api-reference/format.js +4 -3
  15. package/lib/api-routes/account-routes.js +177 -12
  16. package/lib/api-routes/blocklist-routes.js +23 -2
  17. package/lib/api-routes/delivery-test-routes.js +26 -3
  18. package/lib/api-routes/export-routes.js +28 -2
  19. package/lib/api-routes/gateway-routes.js +28 -2
  20. package/lib/api-routes/mailbox-routes.js +41 -2
  21. package/lib/api-routes/message-routes.js +97 -3
  22. package/lib/api-routes/oauth2-app-routes.js +48 -15
  23. package/lib/api-routes/outbox-routes.js +45 -11
  24. package/lib/api-routes/permission-map.js +12 -1
  25. package/lib/api-routes/settings-routes.js +23 -2
  26. package/lib/api-routes/submit-routes.js +336 -309
  27. package/lib/api-routes/template-routes.js +38 -2
  28. package/lib/api-routes/token-routes.js +142 -96
  29. package/lib/api-routes/webhook-route-routes.js +13 -2
  30. package/lib/bounce-detect.js +397 -205
  31. package/lib/consts.js +5 -4
  32. package/lib/email-client/base-client.js +10 -31
  33. package/lib/email-client/gmail-client.js +9 -11
  34. package/lib/email-client/imap/mailbox.js +3 -19
  35. package/lib/email-client/imap/subconnection.js +1 -1
  36. package/lib/email-client/imap-client.js +1 -1
  37. package/lib/email-client/notification-handler.js +1 -2
  38. package/lib/gateway.js +2 -3
  39. package/lib/hapi-request-logger.js +107 -0
  40. package/lib/imapproxy/imap-core/README.md +6 -5
  41. package/lib/mcp/tools.js +37 -8
  42. package/lib/notify-attachments.js +17 -0
  43. package/lib/oauth/outlook-mailbox.js +35 -0
  44. package/lib/oauth/state.js +24 -0
  45. package/lib/oauth2-apps.js +1 -1
  46. package/lib/pbkdf2-phc.js +1 -1
  47. package/lib/schemas.js +1 -1
  48. package/lib/sso.js +1 -1
  49. package/lib/swagger-options.js +1 -1
  50. package/lib/thread-diagnostics.js +46 -0
  51. package/lib/timezones.js +109 -0
  52. package/lib/token-permission-view.js +3 -3
  53. package/lib/tools.js +40 -10
  54. package/lib/totp.js +2 -5
  55. package/lib/translations.js +1 -2
  56. package/lib/ui-routes/account-routes.js +475 -454
  57. package/lib/ui-routes/account-setup.js +99 -0
  58. package/lib/ui-routes/admin-config-routes.js +88 -10
  59. package/lib/ui-routes/admin-entities-routes.js +91 -7
  60. package/lib/ui-routes/auth-routes.js +90 -38
  61. package/lib/ui-routes/internals-routes.js +5 -0
  62. package/lib/ui-routes/oauth-config-routes.js +124 -82
  63. package/lib/ui-routes/route-helpers.js +19 -3
  64. package/lib/ui-routes/tls-config-routes.js +58 -15
  65. package/lib/utils/back-off.js +82 -0
  66. package/lib/utils/murmurhash3.js +66 -0
  67. package/lib/utils/public-suffix.js +122 -0
  68. package/lib/utils/random-id.js +26 -0
  69. package/lib/utils/version.js +74 -0
  70. package/lib/webhook-routing.js +27 -0
  71. package/lib/webhooks.js +41 -6
  72. package/package.json +27 -29
  73. package/sbom.json +7980 -1
  74. package/server.js +634 -441
  75. package/static/licenses.html +181 -1006
  76. package/translations/messages.pot +62 -63
  77. package/views/config/ai.hbs +1 -1
  78. package/views/config/security.hbs +2 -2
  79. package/views/config/service.hbs +3 -0
  80. package/views/internals/index.hbs +8 -0
  81. package/views/partials/webhook_form.hbs +1 -1
  82. package/views/suppression-lists/index.hbs +1 -1
  83. package/workers/api.js +738 -583
  84. package/workers/export.js +1 -1
  85. package/workers/submit.js +1 -1
  86. package/workers/webhooks.js +16 -32
@@ -15,8 +15,8 @@ const { MCP_MAX_PAGE_SIZE } = require('../consts');
15
15
  // Template owner field shared by all template response schemas
16
16
  const templateAccountIdSchema = accountIdSchema.required().allow(null).description('Account ID. Null for public templates');
17
17
 
18
- async function init(args) {
19
- const { server, call, CORS_CONFIG } = args;
18
+ function registerCreateTemplate(server, args) {
19
+ const { call, CORS_CONFIG } = args;
20
20
 
21
21
  server.route({
22
22
  method: 'POST',
@@ -126,6 +126,10 @@ async function init(args) {
126
126
  }
127
127
  }
128
128
  });
129
+ }
130
+
131
+ function registerUpdateTemplate(server, args) {
132
+ const { CORS_CONFIG } = args;
129
133
 
130
134
  server.route({
131
135
  method: 'PUT',
@@ -214,6 +218,10 @@ async function init(args) {
214
218
  }
215
219
  }
216
220
  });
221
+ }
222
+
223
+ function registerListTemplates(server, args) {
224
+ const { call, CORS_CONFIG } = args;
217
225
 
218
226
  server.route({
219
227
  method: 'GET',
@@ -312,6 +320,10 @@ async function init(args) {
312
320
  }
313
321
  }
314
322
  });
323
+ }
324
+
325
+ function registerGetTemplate(server, args) {
326
+ const { CORS_CONFIG } = args;
315
327
 
316
328
  server.route({
317
329
  method: 'GET',
@@ -335,6 +347,11 @@ async function init(args) {
335
347
  tags: ['api', 'Templates'],
336
348
 
337
349
  plugins: {
350
+ mcp: {
351
+ name: 'get_template',
352
+ title: 'Get template',
353
+ description: 'Get one stored email template with its subject and content (html, text, previewText), by the template id from list_templates.'
354
+ },
338
355
  openapi: {
339
356
  responses: apiResponses('Returns the template, including its content.', 400, 401, 403, 404, 429, 500)
340
357
  }
@@ -387,6 +404,10 @@ async function init(args) {
387
404
  }
388
405
  }
389
406
  });
407
+ }
408
+
409
+ function registerDeleteTemplate(server, args) {
410
+ const { CORS_CONFIG } = args;
390
411
 
391
412
  server.route({
392
413
  method: 'DELETE',
@@ -445,6 +466,10 @@ async function init(args) {
445
466
  }
446
467
  }
447
468
  });
469
+ }
470
+
471
+ function registerFlushAccountTemplates(server, args) {
472
+ const { call, CORS_CONFIG } = args;
448
473
 
449
474
  server.route({
450
475
  method: 'DELETE',
@@ -520,4 +545,15 @@ async function init(args) {
520
545
  });
521
546
  }
522
547
 
548
+ async function init(args) {
549
+ const { server } = args;
550
+
551
+ registerCreateTemplate(server, args);
552
+ registerUpdateTemplate(server, args);
553
+ registerListTemplates(server, args);
554
+ registerGetTemplate(server, args);
555
+ registerDeleteTemplate(server, args);
556
+ registerFlushAccountTemplates(server, args);
557
+ }
558
+
523
559
  module.exports = init;
@@ -120,88 +120,83 @@ const tokenListItemFields = {
120
120
  id: tokenIdSchema
121
121
  };
122
122
 
123
- async function init(args) {
124
- const { server, call, CORS_CONFIG } = args;
123
+ // The provision and delete routes are registered twice - the current path and the
124
+ // deprecated pre-2.79 alias at the bottom of this file - so the handlers and the request/
125
+ // response schemas are shared. Sharing the schema INSTANCES matters beyond reuse: the
126
+ // OpenAPI generator names components after joi labels, and two different schema objects
127
+ // carrying the same label would come out numbered (CreateToken1).
128
+ const createTokenPayloadSchema = Joi.object({
129
+ account: accountIdSchema.description('Bind the token to one account. Omit for an instance-wide token, which then has to set `permissions`'),
130
+
131
+ description: Joi.string().empty('').trim().max(1024).required().example('Token description').description('Token description'),
132
+
133
+ scopes: Joi.array()
134
+ .items(
135
+ Joi.string().valid('api', 'smtp', 'imap-proxy', 'mcp', 'mcp-manage').meta({ enumDescriptions: ENUM_DESCRIPTIONS.tokenScope }).label('TokenScope')
136
+ )
137
+ .single()
138
+ .default(['api'])
139
+ .required()
140
+ .description(
141
+ 'Token permission scopes: "api" for REST API access, "smtp" for SMTP submission, "imap-proxy" for IMAP proxy authentication, "mcp-manage" for the instance management tools of the MCP endpoint, "mcp" for its mail tools'
142
+ )
143
+ .label('Scopes'),
144
+
145
+ metadata: tokenMetadataSchema,
125
146
 
126
- // The provision and delete routes are registered twice - the current path and the
127
- // deprecated pre-2.79 alias at the bottom of this file - so the handlers and the request/
128
- // response schemas are shared. Sharing the schema INSTANCES matters beyond reuse: the
129
- // OpenAPI generator names components after joi labels, and two different schema objects
130
- // carrying the same label would come out numbered (CreateToken1).
131
- const createTokenPayloadSchema = Joi.object({
132
- account: accountIdSchema.description('Bind the token to one account. Omit for an instance-wide token, which then has to set `permissions`'),
133
-
134
- description: Joi.string().empty('').trim().max(1024).required().example('Token description').description('Token description'),
135
-
136
- scopes: Joi.array()
137
- .items(
138
- Joi.string()
139
- .valid('api', 'smtp', 'imap-proxy', 'mcp', 'mcp-manage')
140
- .meta({ enumDescriptions: ENUM_DESCRIPTIONS.tokenScope })
141
- .label('TokenScope')
142
- )
143
- .single()
144
- .default(['api'])
145
- .required()
146
- .description(
147
- 'Token permission scopes: "api" for REST API access, "smtp" for SMTP submission, "imap-proxy" for IMAP proxy authentication, "mcp-manage" for the instance management tools of the MCP endpoint, "mcp" for its mail tools'
148
- )
149
- .label('Scopes'),
150
-
151
- metadata: tokenMetadataSchema,
152
-
153
- restrictions: tokenRestrictionsSchema,
154
-
155
- // An unbound token reaches instance-wide endpoints and every account, so it may
156
- // only be issued narrowed. A bound one is already limited to its account, so the
157
- // narrowing stays optional there.
158
- // `.invalid(null)` matters as much as `.required()`: tokenPermissionsSchema allows
159
- // null so the admin form can post "not narrowed", and joi's required() is a
160
- // presence check that an explicit null satisfies. Without this, sending
161
- // `permissions: null` with no account - which any template rendering an unset
162
- // variable produces - passed the rule and minted a full instance-wide token.
163
- permissions: tokenPermissionsSchema.when('account', {
164
- is: Joi.exist(),
165
- otherwise: tokenPermissionsSchema.required().invalid(null).messages({
166
- 'any.required': 'A token with no account must set permissions, so that an instance-wide credential is always a narrowed one',
167
- 'any.invalid': 'A token with no account must set permissions, so that an instance-wide credential is always a narrowed one'
168
- })
169
- }),
170
-
171
- // provision() has always accepted an expiry; the route did not, so until now the
172
- // API could not mint a short-lived token at all. A narrow token that never
173
- // expires is only half of what a per-task credential is for.
174
- expires: Joi.date()
175
- .iso()
176
- .greater('now')
177
- .example('2021-02-17T14:43:18.860Z')
178
- .description('Optional time when this token stops working. Omit for a token that never expires')
179
- // Distinct from the TokenExpires the listing uses: a joi label is a public
180
- // type name in the generated document, so reusing it would collide and the
181
- // generator would number one of them
182
- .label('CreateTokenExpires'),
183
-
184
- ip: ipSchema.description('IP address of the requester').label('TokenIP')
185
- }).label('CreateToken');
186
-
187
- const createTokenResponseSchema = Joi.object({
188
- token: Joi.string()
189
- .length(64)
190
- .hex()
191
- .required()
192
- .example('9a6d24f8f43e01b1f2c4f11a5c5b8e51fe0a4e0eda06e1be5b8d6cc2c66b95dc')
193
- .description('The access token. This is the only time it is returned - store it now'),
194
- id: tokenIdSchema.required().description('Identifier for this token, as reported by the token listings')
195
- }).label('CreateTokenResponse');
196
-
197
- const deleteTokenParamsSchema = Joi.object({
198
- token: tokenReferenceSchema
199
- }).label('DeleteTokenRequest');
200
-
201
- const deleteTokenResponseSchema = Joi.object({
202
- deleted: apiBoolean().default(true).description('Was the token deleted')
203
- }).label('DeleteTokenRequestResponse');
147
+ restrictions: tokenRestrictionsSchema,
204
148
 
149
+ // An unbound token reaches instance-wide endpoints and every account, so it may
150
+ // only be issued narrowed. A bound one is already limited to its account, so the
151
+ // narrowing stays optional there.
152
+ // `.invalid(null)` matters as much as `.required()`: tokenPermissionsSchema allows
153
+ // null so the admin form can post "not narrowed", and joi's required() is a
154
+ // presence check that an explicit null satisfies. Without this, sending
155
+ // `permissions: null` with no account - which any template rendering an unset
156
+ // variable produces - passed the rule and minted a full instance-wide token.
157
+ permissions: tokenPermissionsSchema.when('account', {
158
+ is: Joi.exist(),
159
+ otherwise: tokenPermissionsSchema.required().invalid(null).messages({
160
+ 'any.required': 'A token with no account must set permissions, so that an instance-wide credential is always a narrowed one',
161
+ 'any.invalid': 'A token with no account must set permissions, so that an instance-wide credential is always a narrowed one'
162
+ })
163
+ }),
164
+
165
+ // provision() has always accepted an expiry; the route did not, so until now the
166
+ // API could not mint a short-lived token at all. A narrow token that never
167
+ // expires is only half of what a per-task credential is for.
168
+ expires: Joi.date()
169
+ .iso()
170
+ .greater('now')
171
+ .example('2021-02-17T14:43:18.860Z')
172
+ .description('Optional time when this token stops working. Omit for a token that never expires')
173
+ // Distinct from the TokenExpires the listing uses: a joi label is a public
174
+ // type name in the generated document, so reusing it would collide and the
175
+ // generator would number one of them
176
+ .label('CreateTokenExpires'),
177
+
178
+ ip: ipSchema.description('IP address of the requester').label('TokenIP')
179
+ }).label('CreateToken');
180
+
181
+ const createTokenResponseSchema = Joi.object({
182
+ token: Joi.string()
183
+ .length(64)
184
+ .hex()
185
+ .required()
186
+ .example('9a6d24f8f43e01b1f2c4f11a5c5b8e51fe0a4e0eda06e1be5b8d6cc2c66b95dc')
187
+ .description('The access token. This is the only time it is returned - store it now'),
188
+ id: tokenIdSchema.required().description('Identifier for this token, as reported by the token listings')
189
+ }).label('CreateTokenResponse');
190
+
191
+ const deleteTokenParamsSchema = Joi.object({
192
+ token: tokenReferenceSchema
193
+ }).label('DeleteTokenRequest');
194
+
195
+ const deleteTokenResponseSchema = Joi.object({
196
+ deleted: apiBoolean().default(true).description('Was the token deleted')
197
+ }).label('DeleteTokenRequestResponse');
198
+
199
+ function createProvisionTokenHandler(call) {
205
200
  const provisionTokenHandler = async request => {
206
201
  let accountObject = new Account({
207
202
  redis,
@@ -261,13 +256,19 @@ async function init(args) {
261
256
  }
262
257
  };
263
258
 
264
- const deleteTokenHandler = async request => {
265
- try {
266
- return { deleted: await tokens.delete(request.params.token, { remoteAddress: request.app.ip }) };
267
- } catch (err) {
268
- handleError(request, err);
269
- }
270
- };
259
+ return provisionTokenHandler;
260
+ }
261
+
262
+ const deleteTokenHandler = async request => {
263
+ try {
264
+ return { deleted: await tokens.delete(request.params.token, { remoteAddress: request.app.ip }) };
265
+ } catch (err) {
266
+ handleError(request, err);
267
+ }
268
+ };
269
+
270
+ function registerCreateToken(server, ctx) {
271
+ const { CORS_CONFIG, provisionTokenHandler } = ctx;
271
272
 
272
273
  server.route({
273
274
  method: 'POST',
@@ -312,6 +313,10 @@ async function init(args) {
312
313
  }
313
314
  }
314
315
  });
316
+ }
317
+
318
+ function registerDeleteToken(server, ctx) {
319
+ const { CORS_CONFIG } = ctx;
315
320
 
316
321
  server.route({
317
322
  method: 'DELETE',
@@ -359,6 +364,10 @@ async function init(args) {
359
364
  }
360
365
  }
361
366
  });
367
+ }
368
+
369
+ function registerListTokens(server, ctx) {
370
+ const { CORS_CONFIG } = ctx;
362
371
 
363
372
  server.route({
364
373
  method: 'GET',
@@ -456,6 +465,10 @@ async function init(args) {
456
465
  }
457
466
  }
458
467
  });
468
+ }
469
+
470
+ function registerGetToken(server, ctx) {
471
+ const { CORS_CONFIG } = ctx;
459
472
 
460
473
  server.route({
461
474
  method: 'GET',
@@ -542,6 +555,10 @@ async function init(args) {
542
555
  }
543
556
  }
544
557
  });
558
+ }
559
+
560
+ function registerGetTokenLog(server, ctx) {
561
+ const { CORS_CONFIG } = ctx;
545
562
 
546
563
  server.route({
547
564
  method: 'GET',
@@ -667,16 +684,10 @@ async function init(args) {
667
684
  }
668
685
  }
669
686
  });
687
+ }
670
688
 
671
- // --------------------------------------------------------------------------------------------
672
- // Deprecated aliases. v2.79.0 renamed the token endpoints (POST /v1/token -> POST /v1/tokens,
673
- // DELETE /v1/token/{token} -> DELETE /v1/tokens/{token}) and folded the per-account listing
674
- // into GET /v1/tokens?account=..., which handed every pre-2.79 integration that provisions,
675
- // revokes or lists tokens a 404 from a minor upgrade - token-rotation pipelines failing
676
- // first. The old paths stay registered against the same handlers and schema instances, but are
677
- // left OUT of the published document and the API reference (plugins.openapi.deprecated - see
678
- // lib/openapi/build-document.js): they exist for old integrations to keep working, and
679
- // documenting them would steer new ones onto retired paths.
689
+ function registerDeprecatedCreateToken(server, ctx) {
690
+ const { CORS_CONFIG, provisionTokenHandler } = ctx;
680
691
 
681
692
  server.route({
682
693
  method: 'POST',
@@ -719,6 +730,10 @@ async function init(args) {
719
730
  }
720
731
  }
721
732
  });
733
+ }
734
+
735
+ function registerDeprecatedDeleteToken(server, ctx) {
736
+ const { CORS_CONFIG } = ctx;
722
737
 
723
738
  server.route({
724
739
  method: 'DELETE',
@@ -761,6 +776,10 @@ async function init(args) {
761
776
  }
762
777
  }
763
778
  });
779
+ }
780
+
781
+ function registerDeprecatedListAccountTokens(server, ctx) {
782
+ const { CORS_CONFIG } = ctx;
764
783
 
765
784
  server.route({
766
785
  method: 'GET',
@@ -822,4 +841,31 @@ async function init(args) {
822
841
  });
823
842
  }
824
843
 
844
+ async function init(args) {
845
+ const { server, call, CORS_CONFIG } = args;
846
+
847
+ const provisionTokenHandler = createProvisionTokenHandler(call);
848
+ const ctx = { CORS_CONFIG, provisionTokenHandler };
849
+
850
+ registerCreateToken(server, ctx);
851
+ registerDeleteToken(server, ctx);
852
+ registerListTokens(server, ctx);
853
+ registerGetToken(server, ctx);
854
+ registerGetTokenLog(server, ctx);
855
+
856
+ // --------------------------------------------------------------------------------------------
857
+ // Deprecated aliases. v2.79.0 renamed the token endpoints (POST /v1/token -> POST /v1/tokens,
858
+ // DELETE /v1/token/{token} -> DELETE /v1/tokens/{token}) and folded the per-account listing
859
+ // into GET /v1/tokens?account=..., which handed every pre-2.79 integration that provisions,
860
+ // revokes or lists tokens a 404 from a minor upgrade - token-rotation pipelines failing
861
+ // first. The old paths stay registered against the same handlers and schema instances, but are
862
+ // left OUT of the published document and the API reference (plugins.openapi.deprecated - see
863
+ // lib/openapi/build-document.js): they exist for old integrations to keep working, and
864
+ // documenting them would steer new ones onto retired paths.
865
+
866
+ registerDeprecatedCreateToken(server, ctx);
867
+ registerDeprecatedDeleteToken(server, ctx);
868
+ registerDeprecatedListAccountTokens(server, ctx);
869
+ }
870
+
825
871
  module.exports = init;
@@ -19,8 +19,8 @@ const webhookCustomHeadersSchema = settingsSchema.webhooksCustomHeaders
19
19
  .description('Custom HTTP headers added to webhook requests for this route')
20
20
  .label('WebhookRouteCustomHeaders');
21
21
 
22
- async function init(args) {
23
- const { server, CORS_CONFIG } = args;
22
+ function registerListWebhookRoutes(server, args) {
23
+ const { CORS_CONFIG } = args;
24
24
 
25
25
  server.route({
26
26
  method: 'GET',
@@ -120,6 +120,10 @@ async function init(args) {
120
120
  }
121
121
  }
122
122
  });
123
+ }
124
+
125
+ function registerGetWebhookRoute(server, args) {
126
+ const { CORS_CONFIG } = args;
123
127
 
124
128
  server.route({
125
129
  method: 'GET',
@@ -201,4 +205,11 @@ async function init(args) {
201
205
  });
202
206
  }
203
207
 
208
+ async function init(args) {
209
+ const { server } = args;
210
+
211
+ registerListWebhookRoutes(server, args);
212
+ registerGetWebhookRoute(server, args);
213
+ }
214
+
204
215
  module.exports = init;