emailengine-app 2.89.0 → 2.91.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 (120) hide show
  1. package/.ncurc.js +19 -8
  2. package/.release-please-manifest.json +1 -1
  3. package/CHANGELOG.md +73 -0
  4. package/Dockerfile +7 -3
  5. package/ENVIRONMENT.md +24 -23
  6. package/README.md +1 -1
  7. package/bin/healthcheck.js +1 -1
  8. package/copy-static-files.sh +0 -8
  9. package/data/google-crawlers.json +1 -1
  10. package/encrypt.js +1 -4
  11. package/lib/account/field-codec.js +396 -0
  12. package/lib/account-assignment.js +26 -9
  13. package/lib/account.js +196 -462
  14. package/lib/api-routes/account-routes.js +49 -6
  15. package/lib/api-routes/route-metadata.js +5 -2
  16. package/lib/api-routes/submit-routes.js +15 -2
  17. package/lib/api-token-auth.js +342 -274
  18. package/lib/arf-detect.js +174 -149
  19. package/lib/attachment-size.js +29 -0
  20. package/lib/autodetect-imap-settings.js +482 -296
  21. package/lib/consts.js +5 -5
  22. package/lib/db.js +1 -4
  23. package/lib/email-client/base-client.js +907 -780
  24. package/lib/email-client/gmail-client.js +557 -450
  25. package/lib/email-client/imap/cid-parts.js +61 -0
  26. package/lib/email-client/imap/mailbox.js +376 -422
  27. package/lib/email-client/imap/sync-operations.js +239 -178
  28. package/lib/email-client/imap-client.js +5 -5
  29. package/lib/email-client/message-builder.js +4 -5
  30. package/lib/email-client/new-message-steps.js +168 -2
  31. package/lib/email-client/outlook/graph-message.js +266 -0
  32. package/lib/email-client/outlook/subscription-expiry.js +30 -0
  33. package/lib/email-client/outlook-client.js +652 -808
  34. package/lib/get-raw-email.js +338 -232
  35. package/lib/get-secret.js +1 -4
  36. package/lib/imapproxy/imap-server.js +5 -5
  37. package/lib/license-subscription.js +60 -0
  38. package/lib/load-env.js +48 -0
  39. package/lib/logger-consts.js +37 -1
  40. package/lib/logger.js +1 -4
  41. package/lib/mail-probe.js +157 -0
  42. package/lib/mcp/tools.js +244 -207
  43. package/lib/metric-values.js +76 -0
  44. package/lib/metrics.js +320 -0
  45. package/lib/oauth/gmail.js +39 -120
  46. package/lib/oauth/http-request.js +195 -0
  47. package/lib/oauth/mail-ru.js +33 -60
  48. package/lib/oauth/outlook.js +37 -137
  49. package/lib/oauth/pubsub/google.js +176 -117
  50. package/lib/oauth2-apps.js +440 -463
  51. package/lib/openapi/build-document.js +10 -10
  52. package/lib/read-env-value.js +31 -7
  53. package/lib/redis-backend-warnings.js +70 -0
  54. package/lib/route-descriptors.js +78 -0
  55. package/lib/schemas.js +10 -1
  56. package/lib/sentry-integrations.js +299 -0
  57. package/lib/sentry.js +75 -27
  58. package/lib/sso-routes.js +249 -0
  59. package/lib/sso.js +29 -6
  60. package/lib/tools.js +17 -28
  61. package/lib/ui-routes/account-edit-form.js +198 -0
  62. package/lib/ui-routes/account-page.js +241 -0
  63. package/lib/ui-routes/account-routes.js +46 -349
  64. package/lib/ui-routes/account-setup.js +1 -1
  65. package/lib/ui-routes/auth-routes.js +10 -10
  66. package/lib/ui-routes/provider-signin.js +187 -0
  67. package/lib/ui-routes/route-helpers.js +1 -1
  68. package/lib/ui-routes/unsubscribe-routes.js +2 -2
  69. package/lib/ui-view-context.js +328 -0
  70. package/lib/utils/public-suffix.js +22 -101
  71. package/lib/web-safe-html.js +20 -7
  72. package/lib/worker-metrics.js +82 -0
  73. package/list-generate.js +2 -5
  74. package/package.json +28 -24
  75. package/sbom.json +360 -1030
  76. package/scripts/refresh-test-tokens.js +1 -1
  77. package/server.js +400 -677
  78. package/static/css/flyonui.css +1 -1
  79. package/static/css/public.css +55 -1
  80. package/static/css/src/app.tailwind.css +18 -0
  81. package/static/js/ee-client.js +2619 -645
  82. package/static/js/ui.js +12 -2
  83. package/static/licenses.html +224 -764
  84. package/translations/de.mo +0 -0
  85. package/translations/de.po +429 -390
  86. package/translations/en.mo +0 -0
  87. package/translations/en.po +342 -257
  88. package/translations/es.mo +0 -0
  89. package/translations/es.po +440 -395
  90. package/translations/et.mo +0 -0
  91. package/translations/et.po +425 -375
  92. package/translations/fr.mo +0 -0
  93. package/translations/fr.po +425 -383
  94. package/translations/it.mo +0 -0
  95. package/translations/it.po +441 -394
  96. package/translations/ja.mo +0 -0
  97. package/translations/ja.po +408 -335
  98. package/translations/messages.pot +188 -130
  99. package/translations/nl.mo +0 -0
  100. package/translations/nl.po +419 -382
  101. package/translations/pl.mo +0 -0
  102. package/translations/pl.po +425 -379
  103. package/translations/sv.mo +0 -0
  104. package/translations/sv.po +440 -388
  105. package/translations/tr.mo +0 -0
  106. package/translations/tr.po +441 -387
  107. package/views/account-identity-error.hbs +1 -1
  108. package/views/account-workspace-error.hbs +2 -2
  109. package/views/accounts/browse.hbs +19 -1
  110. package/views/accounts/register/imap-server.hbs +22 -7
  111. package/views/accounts/register/imap.hbs +3 -0
  112. package/views/accounts/register/index.hbs +1 -27
  113. package/views/accounts/register/provider-signin.hbs +58 -0
  114. package/views/oauth-scope-error.hbs +1 -1
  115. package/views/partials/provider_signin_button.hbs +30 -0
  116. package/views/queues/index.hbs +1 -1
  117. package/views/redirect.hbs +1 -1
  118. package/views/unsubscribe.hbs +6 -6
  119. package/workers/api.js +20 -451
  120. package/data/public_suffix_list.dat +0 -16698
@@ -0,0 +1,266 @@
1
+ 'use strict';
2
+
3
+ // How a Microsoft Graph message is asked for and read back, shared by the single fetch
4
+ // (OutlookClient.getMessage()) and the batched one the export uses (getMessages()). Both carried
5
+ // their own copy of the field list and the header parsing.
6
+
7
+ const { pushHeaderValue } = require('../../utils/header-map');
8
+
9
+ // Every field a formatted message is built from. internetMessageHeaders is not included by
10
+ // default, so the full list has to be named
11
+ const GRAPH_MESSAGE_SELECT_FULL = [
12
+ 'id',
13
+ 'conversationId',
14
+ 'receivedDateTime',
15
+ 'isRead',
16
+ 'isDraft',
17
+ 'flag',
18
+ 'body',
19
+ 'subject',
20
+ 'from',
21
+ 'replyTo',
22
+ 'sender',
23
+ 'toRecipients',
24
+ 'ccRecipients',
25
+ 'bccRecipients',
26
+ 'internetMessageId',
27
+ 'bodyPreview',
28
+ 'internetMessageHeaders',
29
+ 'parentFolderId',
30
+ 'categories'
31
+ ];
32
+
33
+ // The same list as a $select value
34
+ const GRAPH_MESSAGE_SELECT = GRAPH_MESSAGE_SELECT_FULL.join(',');
35
+
36
+ // The attachment list of a message, without the content
37
+ const GRAPH_ATTACHMENT_EXPAND = 'attachments($select=id,name,contentType,size,isInline,microsoft.graph.fileAttachment/contentId)';
38
+
39
+ // The fields of a listing entry, by detail level: the ids and the date for minimalFields, the
40
+ // flags and the sender for metadataOnly, and everything a listed message shows otherwise
41
+ const GRAPH_LIST_SELECT_MINIMAL = ['id', 'conversationId', 'receivedDateTime'];
42
+ const GRAPH_LIST_SELECT_METADATA = [
43
+ 'id',
44
+ 'conversationId',
45
+ 'receivedDateTime',
46
+ 'isRead',
47
+ 'isDraft',
48
+ 'flag',
49
+ 'body',
50
+ 'subject',
51
+ 'from',
52
+ 'replyTo',
53
+ 'sender',
54
+ 'internetMessageId'
55
+ ];
56
+ const GRAPH_LIST_SELECT_FULL = [
57
+ 'id',
58
+ 'conversationId',
59
+ 'receivedDateTime',
60
+ 'isRead',
61
+ 'isDraft',
62
+ 'flag',
63
+ 'body',
64
+ 'subject',
65
+ 'from',
66
+ 'replyTo',
67
+ 'sender',
68
+ 'toRecipients',
69
+ 'ccRecipients',
70
+ 'bccRecipients',
71
+ 'internetMessageId',
72
+ 'bodyPreview',
73
+ 'categories'
74
+ ];
75
+
76
+ /**
77
+ * The $select and $expand values of a message listing. Only the full set expands the attachment
78
+ * list. A listing across every folder also selects parentFolderId, which is how each message
79
+ * finds its path
80
+ * @param {Object} options - Listing options
81
+ * @param {boolean} [options.minimalFields] - Ids and date only
82
+ * @param {boolean} [options.metadataOnly] - No recipients, no attachments
83
+ * @param {boolean} crossFolder - Whether the listing spans every folder
84
+ * @returns {{$select: string, $expand: (string|undefined)}}
85
+ */
86
+ function buildListSelect(options, crossFolder) {
87
+ let selectFields;
88
+ let expandFields;
89
+
90
+ if (options.minimalFields) {
91
+ selectFields = GRAPH_LIST_SELECT_MINIMAL.slice();
92
+ } else if (options.metadataOnly) {
93
+ selectFields = GRAPH_LIST_SELECT_METADATA.slice();
94
+ } else {
95
+ selectFields = GRAPH_LIST_SELECT_FULL.slice();
96
+ expandFields = GRAPH_ATTACHMENT_EXPAND;
97
+ }
98
+
99
+ if (crossFolder) {
100
+ selectFields.push('parentFolderId');
101
+ }
102
+
103
+ return { $select: selectFields.join(','), $expand: expandFields };
104
+ }
105
+
106
+ /**
107
+ * Whether Graph refused a listing as too complex ("InefficientFilter"), which a categories/any()
108
+ * filter combined with $orderBy can be
109
+ * @param {Error} err - The failed request
110
+ * @returns {boolean}
111
+ */
112
+ function isInefficientFilterError(err) {
113
+ let graphCode = err?.oauthRequest?.response?.error?.code || '';
114
+ let graphMessage = err?.oauthRequest?.response?.error?.message || '';
115
+ return /inefficientfilter/i.test(graphCode) || /restriction or sort order is too complex/i.test(graphMessage);
116
+ }
117
+
118
+ // A batch request that failed with one of these is sent again
119
+ const BATCH_MAX_RETRIES = 3;
120
+ const BATCH_RETRY_BASE_DELAY = 5000;
121
+
122
+ /**
123
+ * The Prefer header value that asks Graph for a body format. For '*' or nothing Graph returns its default
124
+ * @param {string} [textType] - plain, html or *
125
+ * @returns {string} Empty when no preference is expressed
126
+ */
127
+ function bodyPreferHeader(textType) {
128
+ if (textType === 'plain') {
129
+ return 'outlook.body-content-type="text"';
130
+ }
131
+ if (textType === 'html') {
132
+ return 'outlook.body-content-type="html"';
133
+ }
134
+ return '';
135
+ }
136
+
137
+ /**
138
+ * Turns internetMessageHeaders into a `headers` map of lowercase names to value lists, and
139
+ * sets `inReplyTo` from the In-Reply-To header. Nothing is set when Graph sent no headers
140
+ * @param {Object} messageData - Graph message, updated in place
141
+ */
142
+ function parseGraphHeaders(messageData) {
143
+ if (!messageData.internetMessageHeaders) {
144
+ return;
145
+ }
146
+
147
+ let headers = {};
148
+ for (let header of messageData.internetMessageHeaders) {
149
+ let { name, value } = header;
150
+ name = (name || '').toString().trim().toLowerCase();
151
+ value = (value || '').toString().trim();
152
+ if (!pushHeaderValue(headers, name, value)) {
153
+ continue;
154
+ }
155
+ if (name === 'in-reply-to') {
156
+ messageData.inReplyTo = value;
157
+ }
158
+ }
159
+ messageData.headers = headers;
160
+ }
161
+
162
+ /**
163
+ * The error a failed single message fetch is reported with: 404 is an unknown message, 400
164
+ * carries what Graph said
165
+ * @param {Error} err - Error from the Graph request
166
+ * @returns {Error|null} The error to throw, or null when the failure is to be thrown as is
167
+ */
168
+ function mapGetMessageError(err) {
169
+ switch (err.oauthRequest?.status) {
170
+ case 404: {
171
+ let error = new Error('Unknown message');
172
+ error.info = {
173
+ response: `Message does not exist`
174
+ };
175
+ error.code = 'NotFound';
176
+ error.statusCode = 404;
177
+ return error;
178
+ }
179
+
180
+ case 400: {
181
+ let error = new Error('Invalid request');
182
+ error.info = {
183
+ response: err.oauthRequest?.response?.error?.message || `Invalid request`
184
+ };
185
+ error.code = err.oauthRequest?.response?.error?.code || 'InvalidRequest';
186
+ error.statusCode = 400;
187
+ return error;
188
+ }
189
+ }
190
+ return null;
191
+ }
192
+
193
+ /**
194
+ * The requests of one /$batch call, one GET per message
195
+ * @param {string[]} emailIds - Message ids of the batch
196
+ * @param {string} oauth2UserPath - Graph user path ("me" or "users/<id>")
197
+ * @param {string} preferHeader - Prefer header value, empty for none
198
+ * @param {Function} nextRequestId - Returns the next request id, unique across the batches of one call
199
+ * @returns {{requests: Object[], messageMap: Map<string, string>}} The requests, and the message id of each request id
200
+ */
201
+ function buildMessageBatchRequests(emailIds, oauth2UserPath, preferHeader, nextRequestId) {
202
+ const messageMap = new Map();
203
+
204
+ const requests = emailIds.map(emailId => {
205
+ const reqId = nextRequestId();
206
+ messageMap.set(reqId, emailId);
207
+
208
+ const request = {
209
+ id: reqId,
210
+ method: 'GET',
211
+ url: `/${oauth2UserPath}/messages/${emailId}?$select=${GRAPH_MESSAGE_SELECT}&$expand=${GRAPH_ATTACHMENT_EXPAND}`
212
+ };
213
+
214
+ // Add Prefer header for body content type if specified
215
+ if (preferHeader) {
216
+ request.headers = { Prefer: preferHeader };
217
+ }
218
+
219
+ return request;
220
+ });
221
+
222
+ return { requests, messageMap };
223
+ }
224
+
225
+ /**
226
+ * Whether a failed batch call is worth sending again: throttled (429) or a server error (5xx)
227
+ * @param {Error} err - Error from the Graph request
228
+ * @returns {boolean}
229
+ */
230
+ function isRetryableBatchError(err) {
231
+ return err.oauthRequest?.status === 429 || (err.oauthRequest?.status >= 500 && err.oauthRequest?.status < 600);
232
+ }
233
+
234
+ /**
235
+ * Reports every request of a batch that got no response, after the ones that did
236
+ * @param {Map<string, string>} messageMap - Message id of each request id
237
+ * @param {Set<string>} respondedIds - Request ids a response was received for
238
+ * @param {Object[]} results - Results, appended to
239
+ */
240
+ function reconcileMissingBatchResponses(messageMap, respondedIds, results) {
241
+ for (const [reqId, emailId] of messageMap) {
242
+ if (!respondedIds.has(reqId)) {
243
+ results.push({
244
+ messageId: emailId,
245
+ data: null,
246
+ error: { message: 'No response received from batch API', code: 'EMISSING_RESPONSE' }
247
+ });
248
+ }
249
+ }
250
+ }
251
+
252
+ module.exports = {
253
+ GRAPH_MESSAGE_SELECT_FULL,
254
+ GRAPH_MESSAGE_SELECT,
255
+ GRAPH_ATTACHMENT_EXPAND,
256
+ BATCH_MAX_RETRIES,
257
+ BATCH_RETRY_BASE_DELAY,
258
+ bodyPreferHeader,
259
+ buildListSelect,
260
+ isInefficientFilterError,
261
+ parseGraphHeaders,
262
+ mapGetMessageError,
263
+ buildMessageBatchRequests,
264
+ isRetryableBatchError,
265
+ reconcileMissingBatchResponses
266
+ };
@@ -0,0 +1,30 @@
1
+ 'use strict';
2
+
3
+ // What a stored MS Graph change subscription needs, by how long it has left. Kept pure so
4
+ // test/outlook-subscription-expiry-test.js can pin the boundaries without a client.
5
+
6
+ const { OUTLOOK_CLOCK_SKEW_BUFFER, OUTLOOK_EXPIRATION_RENEW_TIME } = require('../../consts');
7
+
8
+ /**
9
+ * Classifies a stored subscription by its expiry.
10
+ *
11
+ * @param {Date|null} expirationDate - the parsed expiry, null when absent or unparseable
12
+ * @param {number} now - current time in ms
13
+ * @returns {'expired'|'renew'|'valid'} `expired` once inside the clock-skew buffer (Graph may
14
+ * already consider it gone), `renew` inside the renewal window, `valid` otherwise. An expiry
15
+ * that could not be parsed counts as valid: nothing says it needs anything.
16
+ */
17
+ function classifyExistingSubscription(expirationDate, now) {
18
+ if (!expirationDate) {
19
+ return 'valid';
20
+ }
21
+ if (expirationDate.getTime() < now + OUTLOOK_CLOCK_SKEW_BUFFER) {
22
+ return 'expired';
23
+ }
24
+ if (expirationDate.getTime() < now + OUTLOOK_EXPIRATION_RENEW_TIME) {
25
+ return 'renew';
26
+ }
27
+ return 'valid';
28
+ }
29
+
30
+ module.exports = { classifyExistingSubscription };