emailengine-app 2.85.0 → 2.87.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 (137) hide show
  1. package/.ncurc.js +24 -6
  2. package/.release-please-manifest.json +1 -1
  3. package/CHANGELOG.md +97 -0
  4. package/DOCKER_DEPLOYMENT.md +2 -1
  5. package/Dockerfile +16 -3
  6. package/ENVIRONMENT.md +139 -0
  7. package/README.md +5 -1
  8. package/config/default.toml +2 -0
  9. package/copy-static-files.sh +4 -2
  10. package/data/google-crawlers.json +1 -1
  11. package/data/public_suffix_list.dat +4 -3
  12. package/lib/account-assignment.js +19 -0
  13. package/lib/account.js +285 -195
  14. package/lib/api-error-codes.js +49 -0
  15. package/lib/api-reference/model.js +6 -1
  16. package/lib/api-routes/account-routes.js +91 -32
  17. package/lib/api-routes/blocklist-routes.js +68 -2
  18. package/lib/api-routes/delivery-test-routes.js +1 -1
  19. package/lib/api-routes/gateway-routes.js +82 -10
  20. package/lib/api-routes/index.js +4 -0
  21. package/lib/api-routes/license-routes.js +2 -2
  22. package/lib/api-routes/mailbox-routes.js +3 -3
  23. package/lib/api-routes/message-routes.js +15 -7
  24. package/lib/api-routes/oauth2-app-routes.js +15 -8
  25. package/lib/api-routes/outbox-routes.js +77 -66
  26. package/lib/api-routes/permission-map.js +55 -6
  27. package/lib/api-routes/scoped-routes.js +74 -0
  28. package/lib/api-routes/settings-routes.js +11 -4
  29. package/lib/api-routes/stats-routes.js +5 -1
  30. package/lib/api-routes/submit-routes.js +25 -5
  31. package/lib/api-routes/template-routes.js +7 -6
  32. package/lib/api-routes/token-routes.js +11 -5
  33. package/lib/api-routes/webhook-delivery-routes.js +262 -0
  34. package/lib/api-routes/webhook-route-routes.js +119 -4
  35. package/lib/calendar-event.js +81 -1
  36. package/lib/consts.js +38 -46
  37. package/lib/db.js +14 -6
  38. package/lib/delivery-error.js +6 -0
  39. package/lib/deprecations.js +270 -0
  40. package/lib/email-client/base-client.js +134 -158
  41. package/lib/email-client/fallback-poll.js +62 -0
  42. package/lib/email-client/gmail-client.js +59 -11
  43. package/lib/email-client/imap/adaptive-idle-store.js +73 -0
  44. package/lib/email-client/imap/adaptive-idle.js +278 -0
  45. package/lib/email-client/imap/connection-errors.js +14 -1
  46. package/lib/email-client/imap/idle-settings.js +31 -5
  47. package/lib/email-client/imap/listing-diff.js +37 -0
  48. package/lib/email-client/imap/mailbox.js +380 -227
  49. package/lib/email-client/imap/subconnection.js +7 -29
  50. package/lib/email-client/imap/sync-operations.js +220 -127
  51. package/lib/email-client/imap-client.js +227 -163
  52. package/lib/email-client/message-builder.js +14 -79
  53. package/lib/email-client/outlook-client.js +378 -338
  54. package/lib/email-client/smtp-pool-manager.js +16 -13
  55. package/lib/email-client/sync-state-fields.js +19 -0
  56. package/lib/encrypt.js +157 -41
  57. package/lib/enum-descriptions.js +3 -1
  58. package/lib/env-setting-overrides.js +288 -0
  59. package/lib/export-worker.js +751 -0
  60. package/lib/export.js +64 -15
  61. package/lib/field-usage.js +1 -1
  62. package/lib/gateway.js +17 -1
  63. package/lib/get-raw-email.js +35 -108
  64. package/lib/health.js +75 -0
  65. package/lib/lock-run.js +119 -0
  66. package/lib/logger-consts.js +48 -0
  67. package/lib/logger.js +1 -1
  68. package/lib/lua/z-head-scores.lua +28 -0
  69. package/lib/network-errors.js +1 -1
  70. package/lib/node-version.js +24 -0
  71. package/lib/notify-attachments.js +20 -1
  72. package/lib/oauth/gmail.js +54 -29
  73. package/lib/oauth2-apps.js +630 -655
  74. package/lib/otp.js +66 -0
  75. package/lib/outbox.js +184 -54
  76. package/lib/parse-message.js +3 -3
  77. package/lib/privileged-settings.js +2 -2
  78. package/lib/queue-browser/definitions.js +11 -0
  79. package/lib/queue-browser/index.js +68 -19
  80. package/lib/queue-job-actions.js +18 -1
  81. package/lib/queue-retention.js +48 -12
  82. package/lib/queue-stats.js +11 -2
  83. package/lib/read-env-value.js +3 -0
  84. package/lib/redis-memory-warnings.js +99 -0
  85. package/lib/redis-reconnect.js +29 -0
  86. package/lib/request-timing.js +23 -0
  87. package/lib/respawn-backoff.js +35 -0
  88. package/lib/rewrite-text-nodes.js +19 -59
  89. package/lib/schemas.js +225 -41
  90. package/lib/settings.js +6 -2
  91. package/lib/smtp-auth.js +19 -1
  92. package/lib/swagger-options.js +2 -2
  93. package/lib/thread-diagnostics.js +18 -1
  94. package/lib/tls/provision.js +6 -3
  95. package/lib/token-audit-log.js +19 -20
  96. package/lib/token-permission-view.js +4 -3
  97. package/lib/tools.js +8 -41
  98. package/lib/ui-routes/admin-config-routes.js +64 -90
  99. package/lib/ui-routes/admin-entities-routes.js +2 -14
  100. package/lib/ui-routes/imap-idle-field.js +1 -1
  101. package/lib/ui-routes/outlook-subscription-view.js +1 -1
  102. package/lib/ui-routes/overridable-fields.js +84 -0
  103. package/lib/ui-routes/queue-routes.js +8 -5
  104. package/lib/ui-routes/settings-page.js +6 -0
  105. package/lib/utils/decode-headers.js +3 -12
  106. package/lib/utils/filter-empty.js +20 -0
  107. package/lib/utils/int-setting.js +48 -0
  108. package/lib/utils/tracking-settings.js +35 -0
  109. package/lib/web-safe-html.js +18 -105
  110. package/lib/webhook-delivery.js +461 -0
  111. package/lib/webhook-failures.js +132 -0
  112. package/lib/webhook-request.js +29 -1
  113. package/lib/webhook-routing.js +30 -1
  114. package/lib/webhook-test.js +88 -0
  115. package/lib/webhooks.js +59 -41
  116. package/lib/worker-rpc-error.js +2 -1
  117. package/package.json +28 -26
  118. package/sbom.json +600 -678
  119. package/server.js +149 -76
  120. package/static/css/flyonui.css +1 -1
  121. package/static/js/ace/ace.js +1 -1
  122. package/static/js/ee-client.js +650 -196
  123. package/static/licenses.html +49 -59
  124. package/translations/messages.pot +26 -26
  125. package/views/config/security.hbs +22 -0
  126. package/views/config/service.hbs +44 -0
  127. package/views/config/webhooks.hbs +36 -0
  128. package/views/layout/app.hbs +9 -6
  129. package/views/legal.hbs +8 -0
  130. package/views/partials/queue_retention_note.hbs +12 -8
  131. package/views/partials/ui/env-managed-alert.hbs +16 -7
  132. package/workers/api.js +112 -37
  133. package/workers/export.js +16 -631
  134. package/workers/imap.js +13 -0
  135. package/workers/smtp.js +9 -11
  136. package/workers/webhooks.js +18 -432
  137. package/lib/email-client/outlook-fallback-poll.js +0 -30
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ // Readers for whole-number settings and environment values. Dependency-free, so pure modules can
4
+ // share them: the overridable settings, the token audit log and the webhook retry policy each had
5
+ // a parser of their own, and they disagreed on blank values, zero and fractions.
6
+
7
+ /**
8
+ * A whole number of at least 1, or undefined for anything else. Fractions are floored.
9
+ * @param {*} value
10
+ * @returns {number|undefined}
11
+ */
12
+ function asPositiveInt(value) {
13
+ const parsed = Math.floor(Number(value));
14
+ return Number.isFinite(parsed) && parsed >= 1 ? parsed : undefined;
15
+ }
16
+
17
+ /**
18
+ * A whole number of at least 0, or undefined for anything else, blank values included (Number('')
19
+ * would read as 0). Fractions are floored.
20
+ * @param {*} value
21
+ * @returns {number|undefined}
22
+ */
23
+ function asNonNegativeInt(value) {
24
+ if (value === '' || value === null || value === undefined) {
25
+ return undefined;
26
+ }
27
+ const parsed = Math.floor(Number(value));
28
+ return Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined;
29
+ }
30
+
31
+ /**
32
+ * A whole number clamped into [min, max], or the default for anything that is not one: a setting
33
+ * that is unset, cleared, or written by an older version without validation
34
+ * @param {*} value
35
+ * @param {number} defaultValue
36
+ * @param {number} min
37
+ * @param {number} max
38
+ * @returns {number}
39
+ */
40
+ function boundedInt(value, defaultValue, min, max) {
41
+ const number = Number(value);
42
+ if (value === null || value === undefined || value === '' || !Number.isInteger(number)) {
43
+ return defaultValue;
44
+ }
45
+ return Math.min(Math.max(number, min), max);
46
+ }
47
+
48
+ module.exports = { asPositiveInt, asNonNegativeInt, boundedInt };
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ // Which kinds of tracking a sent message gets. The message's own flags win, the instance
4
+ // settings fill in what it leaves open, and open and click tracking follow trackSentMessages
5
+ // unless set on their own.
6
+
7
+ const TRACKING_SETTINGS = ['trackSentMessages', 'trackClicks', 'trackOpens'];
8
+
9
+ const isBoolean = value => typeof value === 'boolean';
10
+
11
+ /**
12
+ * Whether the message leaves anything to the instance settings, so the caller can skip the read
13
+ * @param {Object} message - trackingEnabled, trackClicks, trackOpens as the message set them
14
+ * @returns {boolean}
15
+ */
16
+ function needsTrackingSettings(message) {
17
+ return !isBoolean(message.trackingEnabled) || !isBoolean(message.trackClicks) || !isBoolean(message.trackOpens);
18
+ }
19
+
20
+ /**
21
+ * @param {Object} message - trackingEnabled, trackClicks, trackOpens as the message set them
22
+ * @param {Object} [stored] - The TRACKING_SETTINGS values; needed only when needsTrackingSettings()
23
+ * @returns {{trackingEnabled: *, trackClicks: boolean, trackOpens: boolean}}
24
+ */
25
+ function resolveTracking(message, stored = {}) {
26
+ const trackingEnabled = isBoolean(message.trackingEnabled) ? message.trackingEnabled : stored.trackSentMessages || false;
27
+ const pick = (own, setting) => [own, setting].find(isBoolean) ?? trackingEnabled;
28
+ return {
29
+ trackingEnabled,
30
+ trackClicks: pick(message.trackClicks, stored.trackClicks),
31
+ trackOpens: pick(message.trackOpens, stored.trackOpens)
32
+ };
33
+ }
34
+
35
+ module.exports = { TRACKING_SETTINGS, needsTrackingSettings, resolveTracking };
@@ -1,11 +1,12 @@
1
1
  'use strict';
2
2
 
3
- const { analyze } = require('@postalsys/email-content');
3
+ const { analyze, canonicalisePlainText } = require('@postalsys/email-content');
4
4
  // Resolved at call time rather than destructured here: the package exposes mimeHtml through a
5
5
  // lazy getter, and touching it loads jsdom (tens of MB of heap) into every process that merely
6
6
  // requires this module, the main process included.
7
7
  const textTools = require('@postalsys/email-text-tools');
8
- const { getBoolean, readEnvValue, attributeValueRegex } = require('./tools');
8
+ const { attributeValueRegex } = require('./tools');
9
+ const { getOverridableSettings } = require('./env-setting-overrides');
9
10
  const { COLLAPSE_CLASS, COLLAPSE_TOGGLE_CLASS } = require('./consts');
10
11
  const logger = require('./logger');
11
12
  const he = require('he');
@@ -53,54 +54,16 @@ const MIN_COLLAPSE_CHARS = 120;
53
54
  const MAX_ANALYZE_CHARS = 256 * 1024;
54
55
 
55
56
  // Opt-out for operators who need the previous output shape, or who do not want to pay for
56
- // extraction on every notified message.
57
- const DISABLE_THREAD_COLLAPSE = getBoolean(readEnvValue('EENGINE_DISABLE_THREAD_COLLAPSE'));
57
+ // extraction on every notified message: the disableThreadCollapse setting, or
58
+ // EENGINE_DISABLE_THREAD_COLLAPSE, which wins when set (lib/env-setting-overrides.js). Read per
59
+ // message through that module's short cache, so a change applies without a restart.
58
60
 
59
- /**
60
- * Base styles for messages that only have a plain text body.
61
- *
62
- * mimeHtml() applies its own copy of these whenever it converts text to HTML itself. Once a
63
- * collapse marker is involved the conversion happens here instead, so the styling has to travel
64
- * with it, otherwise a plain text reply would lose its quote colouring the moment it became
65
- * collapsible. juice inlines the block and the sanitizer then drops the tag.
66
- *
67
- * Kept byte for byte identical to INLINE_STYLE_BLOCK in @postalsys/email-text-tools, so the two
68
- * can be diffed when that package restyles quoted text.
69
- */
70
- const PLAINTEXT_STYLE_BLOCK = `<style>
71
-
72
- body, td, th, p {
73
- font-family: sans-serif;
74
- font-size: 12px;
75
- }
76
-
77
- blockquote {
78
- border-left-width: 2px;
79
- border-left-style: solid;
80
-
81
- border-left-color: darkblue;
82
- color: darkblue;
83
-
84
- margin: 1rem 0;
85
- padding-left: 1rem;
86
- }
87
-
88
- blockquote blockquote{
89
- border-left-color: royalblue;
90
- color: royalblue;
91
- }
92
-
93
- blockquote blockquote blockquote{
94
- border-left-color: dodgerblue;
95
- color: dodgerblue;
96
- }
97
-
98
- blockquote blockquote blockquote blockquote{
99
- border-left-color: darkblue;
100
- color: darkblue;
101
- }
102
-
103
- </style>`;
61
+ // Base styles for messages that only have a plain text body. mimeHtml() applies the same block
62
+ // whenever it converts text to HTML itself; once a collapse marker is involved the conversion happens
63
+ // here instead, so the styling has to travel with it, or a plain text reply would lose its quote
64
+ // colouring the moment it became collapsible. juice inlines the block and the sanitizer drops the tag.
65
+ // Taken from the package rather than kept as a copy, which had already drifted from it.
66
+ const PLAINTEXT_STYLE_BLOCK = textTools.INLINE_STYLE_BLOCK;
104
67
 
105
68
  // Table internals can not host a wrapper element. A <details> opened between table cells is
106
69
  // foster-parented out of the table by the HTML parser, which would leave the content behind and
@@ -136,36 +99,6 @@ function formatSender(from) {
136
99
  return address || name || null;
137
100
  }
138
101
 
139
- /**
140
- * Normalizes text the same way the extractor does, so the offsets it reports index this string.
141
- *
142
- * Mirrors canonicaliseText() in @postalsys/email-content: BOM removal, LF line endings, NUL
143
- * removal and NFC normalization. Anything else would shift the offsets.
144
- *
145
- * There is no HTML counterpart on purpose. HTML offsets come from source ranges over the string
146
- * exactly as it was handed to the extractor, so HTML must not be normalized before slicing it.
147
- *
148
- * @param {string} text - Decoded plain text body
149
- * @returns {string} Canonical text
150
- */
151
- function canonicaliseText(text) {
152
- let canonical = text;
153
-
154
- if (canonical.charCodeAt(0) === 0xfeff) {
155
- canonical = canonical.slice(1);
156
- }
157
-
158
- canonical = canonical.replace(/\r\n?/g, '\n').replace(/\0/g, '');
159
-
160
- try {
161
- canonical = canonical.normalize('NFC');
162
- } catch (_err) {
163
- // Extremely long or malformed input can defeat NFC. Unnormalized text still slices.
164
- }
165
-
166
- return canonical;
167
- }
168
-
169
102
  /**
170
103
  * Finds the offset where the quoted tail of a message starts.
171
104
  *
@@ -375,7 +308,7 @@ async function collapseThreadHistory(messageData) {
375
308
  // Text offsets index the extractor's canonical form, so the same normalization has to be
376
309
  // applied before slicing. It is done here rather than up front because the HTML path,
377
310
  // which is the common one, has no use for it.
378
- collapsed = collapseText(canonicaliseText(text), cut.offset);
311
+ collapsed = collapseText(canonicalisePlainText(text), cut.offset);
379
312
  }
380
313
 
381
314
  return collapsed ? { html: collapsed } : null;
@@ -415,24 +348,6 @@ function stripReservedClasses(html) {
415
348
  });
416
349
  }
417
350
 
418
- /**
419
- * Last-resort rendering when the sanitizer itself fails.
420
- *
421
- * mimeHtml() throws RangeError on hostile nesting (a few thousand nested elements or quote
422
- * levels). The message is still delivered and still has to be readable, so it is shown as escaped
423
- * text rather than dropped. Never the unconverted HTML: that has not been sanitized.
424
- *
425
- * @param {Object} source - `{ html, text }` as handed to mimeHtml()
426
- * @returns {string} Escaped text in a wrapper
427
- */
428
- function escapedTextFallback(source) {
429
- let text = source.text || '';
430
- if (!text && source.html) {
431
- text = he.decode(source.html.toString().replace(/<[^>]*>/g, ' '));
432
- }
433
- return `<div style="overflow: auto; white-space: pre-wrap;">${he.escape(text.toString())}</div>`;
434
- }
435
-
436
351
  /**
437
352
  * Converts the bodies of a message object into sanitized, web-safe HTML.
438
353
  *
@@ -447,7 +362,8 @@ async function messageWebSafeHtml(messageData) {
447
362
 
448
363
  let source = { html, text: messageData.text.plain };
449
364
 
450
- if (!DISABLE_THREAD_COLLAPSE) {
365
+ const { disableThreadCollapse } = await getOverridableSettings('disableThreadCollapse');
366
+ if (!disableThreadCollapse) {
451
367
  try {
452
368
  let collapsed = await collapseThreadHistory(messageData);
453
369
  if (collapsed) {
@@ -459,12 +375,9 @@ async function messageWebSafeHtml(messageData) {
459
375
  }
460
376
  }
461
377
 
462
- try {
463
- return textTools.mimeHtml(source);
464
- } catch (err) {
465
- logger.warn({ msg: 'Failed to convert message to web-safe HTML, rendering it as text', err });
466
- return escapedTextFallback({ html: messageData.text.html, text: messageData.text.plain });
467
- }
378
+ // mimeHtml() does not throw: on hostile input (thousands of nested elements or quote levels)
379
+ // it falls back to escaped text itself, which is what the catch that used to be here did
380
+ return textTools.mimeHtml(source);
468
381
  }
469
382
 
470
383
  /**