emailengine-app 2.88.0 → 2.90.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 (79) hide show
  1. package/.ncurc.js +36 -45
  2. package/.release-please-manifest.json +1 -1
  3. package/CHANGELOG.md +58 -0
  4. package/Dockerfile +5 -1
  5. package/bin/healthcheck.js +1 -1
  6. package/copy-static-files.sh +0 -8
  7. package/data/google-crawlers.json +1 -1
  8. package/encrypt.js +1 -4
  9. package/lib/account/field-codec.js +396 -0
  10. package/lib/account.js +158 -453
  11. package/lib/api-routes/account-routes.js +42 -6
  12. package/lib/api-routes/route-metadata.js +5 -2
  13. package/lib/api-routes/submit-routes.js +15 -2
  14. package/lib/api-token-auth.js +342 -274
  15. package/lib/arf-detect.js +174 -149
  16. package/lib/attachment-size.js +29 -0
  17. package/lib/autodetect-imap-settings.js +83 -234
  18. package/lib/consts.js +1 -5
  19. package/lib/db.js +1 -4
  20. package/lib/email-client/base-client.js +907 -780
  21. package/lib/email-client/gmail-client.js +557 -450
  22. package/lib/email-client/imap/cid-parts.js +61 -0
  23. package/lib/email-client/imap/mailbox.js +376 -422
  24. package/lib/email-client/imap/sync-operations.js +239 -178
  25. package/lib/email-client/new-message-steps.js +168 -2
  26. package/lib/email-client/outlook/graph-message.js +266 -0
  27. package/lib/email-client/outlook/subscription-expiry.js +30 -0
  28. package/lib/email-client/outlook-client.js +652 -808
  29. package/lib/get-raw-email.js +338 -232
  30. package/lib/get-secret.js +1 -4
  31. package/lib/license-subscription.js +77 -11
  32. package/lib/load-env.js +48 -0
  33. package/lib/logger.js +1 -4
  34. package/lib/mcp/tools.js +244 -207
  35. package/lib/metric-values.js +76 -0
  36. package/lib/metrics.js +320 -0
  37. package/lib/node-version.js +99 -12
  38. package/lib/oauth/gmail.js +39 -120
  39. package/lib/oauth/http-request.js +195 -0
  40. package/lib/oauth/mail-ru.js +24 -58
  41. package/lib/oauth/outlook.js +37 -137
  42. package/lib/oauth/pubsub/google.js +176 -117
  43. package/lib/oauth2-apps.js +440 -463
  44. package/lib/openapi/build-document.js +10 -10
  45. package/lib/read-env-value.js +1 -1
  46. package/lib/route-descriptors.js +78 -0
  47. package/lib/schemas.js +10 -1
  48. package/lib/sentry-integrations.js +299 -0
  49. package/lib/sentry.js +75 -27
  50. package/lib/settings.js +4 -1
  51. package/lib/sso-routes.js +249 -0
  52. package/lib/sso.js +29 -6
  53. package/lib/tools.js +5 -4
  54. package/lib/ui-routes/account-edit-form.js +198 -0
  55. package/lib/ui-routes/account-page.js +241 -0
  56. package/lib/ui-routes/account-routes.js +12 -318
  57. package/lib/ui-routes/auth-routes.js +10 -10
  58. package/lib/ui-routes/dashboard-routes.js +4 -0
  59. package/lib/ui-view-context.js +328 -0
  60. package/lib/utils/public-suffix.js +22 -101
  61. package/lib/web-safe-html.js +70 -13
  62. package/lib/worker-metrics.js +82 -0
  63. package/list-generate.js +57 -23
  64. package/package.json +34 -33
  65. package/sbom.json +380 -1612
  66. package/scripts/refresh-test-tokens.js +1 -1
  67. package/server.js +351 -707
  68. package/static/css/flyonui.css +1 -1
  69. package/static/css/src/app.tailwind.css +18 -0
  70. package/static/js/ee-client.js +2619 -645
  71. package/static/js/ui.js +12 -2
  72. package/static/licenses.html +1055 -969
  73. package/translations/messages.pot +71 -60
  74. package/views/accounts/browse.hbs +19 -1
  75. package/views/dashboard.hbs +9 -0
  76. package/views/upgrade.hbs +25 -0
  77. package/workers/api.js +20 -451
  78. package/data/public_suffix_list.dat +0 -16597
  79. package/lib/net-tos-guard.js +0 -42
@@ -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 };