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
package/lib/arf-detect.js CHANGED
@@ -8,7 +8,7 @@ const { isUnsafeTransformedKey } = require('./utils/unsafe-key');
8
8
 
9
9
  // The attachment types a complaint is read from: the ARF report itself, and the copy of the
10
10
  // original message beside it, which some feedback loops send as text/rfc822-headers. Both arrival
11
- // paths gate and download on these lists, and the switch in arfDetect() parses every type in them
11
+ // paths gate and download on these lists, and arfDetect() parses every type in them
12
12
  const FEEDBACK_REPORT_TYPE = 'message/feedback-report';
13
13
  const ORIGINAL_MESSAGE_TYPES = ['message/rfc822', 'message/rfc822-headers', 'text/rfc822-headers', 'text/rfc822-header'];
14
14
  const COMPLAINT_ATTACHMENT_TYPES = [FEEDBACK_REPORT_TYPE, ...ORIGINAL_MESSAGE_TYPES];
@@ -54,6 +54,169 @@ function stripAngleBrackets(value) {
54
54
  return value.substring(start, end);
55
55
  }
56
56
 
57
+ // Normalizes one field of a feedback report. Returns the [key, value] pair to store, or null for a
58
+ // field that is empty or carries an invalid date
59
+ function normalizeArfField(key, value) {
60
+ if (!key || !value) {
61
+ return null;
62
+ }
63
+
64
+ if (ARF_SINGLE.includes(key)) {
65
+ value = capField(Array.isArray(value) ? value.join(',') : (value || '').toString());
66
+ } else if (!Array.isArray(value)) {
67
+ value = []
68
+ .concat(value || [])
69
+ .map(element => capField((element || '').toString().trim()))
70
+ .filter(element => element);
71
+ } else {
72
+ value = value.map(element => (typeof element === 'string' ? capField(element) : element));
73
+ }
74
+
75
+ if (!value || !value.length) {
76
+ return null;
77
+ }
78
+
79
+ switch (key) {
80
+ case 'original-mail-from':
81
+ value = stripAngleBrackets(value);
82
+ break;
83
+
84
+ case 'arrival-date':
85
+ case 'received-date':
86
+ key = 'arrival-date';
87
+ value = new Date(value);
88
+ if (value.toString() === 'Invalid Date') {
89
+ return null;
90
+ }
91
+ value = value.toISOString();
92
+ break;
93
+ }
94
+
95
+ return [key, value];
96
+ }
97
+
98
+ // Adds recipients to the report's list, each address once: before the ones already listed (the
99
+ // report's own) or after them (those collected from the original-message copies)
100
+ function addRecipients(report, addresses, first) {
101
+ const current = report.arf['original-rcpt-to'] || [];
102
+ report.arf['original-rcpt-to'] = first
103
+ ? addresses.concat(current.filter(addr => !addresses.includes(addr)))
104
+ : current.concat(addresses.filter(addr => !current.includes(addr)));
105
+ }
106
+
107
+ // The fields of a message/feedback-report attachment. Every field overwrites what an earlier
108
+ // attachment stored under the same key, except the recipients, which accumulate whichever order
109
+ // the attachments come in: the report's own come first, then the ones collected before it that it
110
+ // does not list
111
+ function parseFeedbackReport(content, report) {
112
+ const contents = decodeHeaders(content);
113
+
114
+ for (const key of Object.keys(contents)) {
115
+ const field = normalizeArfField(key, contents[key]);
116
+ if (!field) {
117
+ continue;
118
+ }
119
+ const [fieldKey, value] = field;
120
+ if (fieldKey === 'original-rcpt-to') {
121
+ addRecipients(report, value, true);
122
+ } else {
123
+ report.arf[fieldKey] = value;
124
+ }
125
+ }
126
+ }
127
+
128
+ // The addresses of an address header, empty ones dropped
129
+ const headerAddresses = headerLines =>
130
+ headerLines
131
+ .flatMap(addressparser)
132
+ .map(entry => entry.address)
133
+ .filter(addr => addr);
134
+
135
+ const addressList = headerLines => {
136
+ const value = headerAddresses(headerLines);
137
+ return value.length ? value : undefined;
138
+ };
139
+
140
+ // How each copied header of the original message is read from its non-empty lines. Returns
141
+ // undefined for a header that yields nothing. The first attachment to yield a value keeps it
142
+ const HEADER_EXTRACTORS = {
143
+ from: headerLines => {
144
+ const value = addressList(headerLines);
145
+ return value && value.pop();
146
+ },
147
+ to: addressList,
148
+ cc: addressList,
149
+ bcc: addressList,
150
+ subject: headerLines => libmime.decodeWords(headerLines.pop()).trim() || undefined,
151
+ date: headerLines => {
152
+ const value = new Date(headerLines.pop());
153
+ return value.toString() !== 'Invalid Date' ? value.toISOString() : undefined;
154
+ }
155
+ };
156
+
157
+ // The header block of an attachment carrying the original message or its headers
158
+ function readOriginalHeaders(content) {
159
+ let contents = capHeaderBlock(content);
160
+ const headerPos = contents.match(/\r?\n\r?\n/);
161
+
162
+ if (headerPos) {
163
+ contents = contents.substr(0, headerPos.index);
164
+ }
165
+
166
+ return decodeHeaders(contents);
167
+ }
168
+
169
+ // Fills the report from a copy of the original message's headers. Recipients accumulate across
170
+ // attachments; every other value is only used when nothing before it set one
171
+ function parseOriginalHeaders(headers, report) {
172
+ // Hotmail original recipient X-HmXmrOriginalRecipient
173
+ const addresses = addressparser([].concat(headers['x-hmxmroriginalrecipient'] || []).join(', '))
174
+ .map(addr => addr.address)
175
+ .filter(addr => addr);
176
+
177
+ addRecipients(report, addresses, false);
178
+
179
+ let messageId = [].concat(headers['message-id'] || []).pop();
180
+ if (messageId && !report.headers['message-id']) {
181
+ report.headers['message-id'] = messageId;
182
+ }
183
+
184
+ let sourceIp = [].concat(headers['x-sender-ip'] || []).pop();
185
+ if (sourceIp && !report.arf['source-ip']) {
186
+ report.arf['source-ip'] = sourceIp;
187
+ }
188
+
189
+ let originalArrivalTime = [].concat(headers['x-ms-exchange-crosstenant-originalarrivaltime'] || []).pop();
190
+ if (originalArrivalTime && !report.arf['arrival-date']) {
191
+ let originalArrivalTimeDate = new Date(originalArrivalTime);
192
+ if (originalArrivalTimeDate && originalArrivalTimeDate.toString() !== 'Invalid Date') {
193
+ report.arf['arrival-date'] = originalArrivalTimeDate.toISOString();
194
+ }
195
+ }
196
+
197
+ for (let [headerKey, extract] of Object.entries(HEADER_EXTRACTORS)) {
198
+ let headerLines = []
199
+ .concat(headers[headerKey] || [])
200
+ .map(val => val && typeof val === 'string' && val.trim())
201
+ .filter(val => val);
202
+ if (!headerLines.length) {
203
+ continue;
204
+ }
205
+
206
+ let value = extract(headerLines);
207
+ if (value !== undefined) {
208
+ report.headers[headerKey] = report.headers[headerKey] || value;
209
+ }
210
+ }
211
+ }
212
+
213
+ // The last address of a Return-Path header, undefined when it names none
214
+ const lastAddress = header =>
215
+ addressparser([].concat(header || []).join(', '))
216
+ .map(addr => addr.address)
217
+ .filter(addr => addr)
218
+ .pop();
219
+
57
220
  const arfDetect = async messageInfo => {
58
221
  const report = {
59
222
  arf: {},
@@ -63,155 +226,17 @@ const arfDetect = async messageInfo => {
63
226
  let returnPath;
64
227
 
65
228
  for (let attachment of messageInfo.attachments || []) {
66
- switch (attachment.contentType.toLowerCase()) {
67
- case 'message/feedback-report': {
68
- // found a feedback report
69
-
70
- const contents = decodeHeaders(attachment.content);
71
-
72
- Object.keys(contents).forEach(key => {
73
- let value = contents[key];
74
-
75
- if (!key || !value) {
76
- return;
77
- }
78
-
79
- if (ARF_SINGLE.includes(key)) {
80
- value = capField(Array.isArray(contents[key]) ? contents[key].join(',') : (contents[key] || '').toString());
81
- } else if (!Array.isArray(value)) {
82
- value = []
83
- .concat(value || [])
84
- .map(element => capField((element || '').toString().trim()))
85
- .filter(element => element);
86
- } else {
87
- value = value.map(element => (typeof element === 'string' ? capField(element) : element));
88
- }
89
-
90
- if (!value || !value.length) {
91
- return;
92
- }
93
-
94
- switch (key) {
95
- case 'original-mail-from':
96
- value = stripAngleBrackets(value);
97
- break;
98
-
99
- case 'arrival-date':
100
- case 'received-date':
101
- key = 'arrival-date';
102
- value = new Date(value);
103
- if (value.toString() === 'Invalid Date') {
104
- return;
105
- }
106
- value = value.toISOString();
107
- break;
108
- }
109
-
110
- report.arf[key] = value;
111
- });
112
-
113
- break;
229
+ const contentType = attachment.contentType.toLowerCase();
230
+
231
+ if (contentType === FEEDBACK_REPORT_TYPE) {
232
+ // found a feedback report
233
+ parseFeedbackReport(attachment.content, report);
234
+ } else if (ORIGINAL_MESSAGE_TYPES.includes(contentType)) {
235
+ const headers = readOriginalHeaders(attachment.content);
236
+ parseOriginalHeaders(headers, report);
237
+ if (headers['return-path']) {
238
+ returnPath = lastAddress(headers['return-path']);
114
239
  }
115
-
116
- case 'message/rfc822':
117
- case 'message/rfc822-headers':
118
- case 'text/rfc822-headers':
119
- case 'text/rfc822-header': {
120
- let contents = capHeaderBlock(attachment.content);
121
- const headerPos = contents.match(/\r?\n\r?\n/);
122
-
123
- if (headerPos) {
124
- contents = contents.substr(0, headerPos.index);
125
- }
126
-
127
- const headers = decodeHeaders(contents);
128
-
129
- // Hotmail original recipient X-HmXmrOriginalRecipient
130
- const addresses = addressparser([].concat(headers['x-hmxmroriginalrecipient'] || []).join(', '))
131
- .map(addr => addr.address)
132
- .filter(addr => addr && !(report.arf['original-rcpt-to'] && report.arf['original-rcpt-to'].includes(addr)));
133
-
134
- report.arf['original-rcpt-to'] = [].concat(report.arf['original-rcpt-to'] || []).concat(addresses);
135
-
136
- if (headers['return-path']) {
137
- returnPath = addressparser([].concat(headers['return-path'] || []).join(', '))
138
- .map(addr => addr.address)
139
- .filter(addr => addr)
140
- .pop();
141
- }
142
-
143
- let messageId = [].concat(headers['message-id'] || []).pop();
144
- if (messageId && !report.headers['message-id']) {
145
- report.headers['message-id'] = messageId;
146
- }
147
-
148
- let sourceIp = [].concat(headers['x-sender-ip'] || []).pop();
149
- if (sourceIp && !report.arf['source-ip']) {
150
- report.arf['source-ip'] = sourceIp;
151
- }
152
-
153
- let originalArrivalTime = [].concat(headers['x-ms-exchange-crosstenant-originalarrivaltime'] || []).pop();
154
- if (originalArrivalTime && !report.arf['arrival-date']) {
155
- let originalArrivalTimeDate = new Date(originalArrivalTime);
156
- if (originalArrivalTimeDate && originalArrivalTimeDate.toString() !== 'Invalid Date') {
157
- report.arf['arrival-date'] = originalArrivalTimeDate.toISOString();
158
- }
159
- }
160
-
161
- for (let headerKey of ['from', 'to', 'cc', 'bcc', 'subject', 'date']) {
162
- let headerLines = []
163
- .concat(headers[headerKey] || [])
164
- .map(val => val && typeof val === 'string' && val.trim())
165
- .filter(val => val);
166
- if (!headerLines.length) {
167
- continue;
168
- }
169
-
170
- switch (headerKey) {
171
- case 'date':
172
- {
173
- let value = new Date(headerLines.pop());
174
- if (value.toString() !== 'Invalid Date') {
175
- report.headers[headerKey] = report.headers[headerKey] || value.toISOString();
176
- }
177
- }
178
- break;
179
-
180
- case 'subject':
181
- {
182
- let value = libmime.decodeWords(headerLines.pop()).trim();
183
- if (value) {
184
- report.headers[headerKey] = report.headers[headerKey] || value;
185
- }
186
- }
187
- break;
188
-
189
- case 'from':
190
- case 'to':
191
- case 'cc':
192
- case 'bcc':
193
- {
194
- let value = headerLines
195
- .flatMap(addressparser)
196
- .map(entry => entry.address)
197
- .filter(addr => addr);
198
-
199
- if (value && value.length) {
200
- if (headerKey === 'from') {
201
- value = value.pop();
202
- }
203
- report.headers[headerKey] = report.headers[headerKey] || value;
204
- }
205
- }
206
- break;
207
- }
208
- }
209
-
210
- break;
211
- }
212
-
213
- default:
214
- break;
215
240
  }
216
241
  }
217
242
 
@@ -0,0 +1,29 @@
1
+ 'use strict';
2
+
3
+ // Base64 lines are 76 characters long (RFC 2045 section 6.8) and each ends in CRLF
4
+ const BASE64_LINE = 78;
5
+
6
+ /**
7
+ * The size of an IMAP attachment once its transfer encoding is undone, from the size of the
8
+ * encoded body part the BODYSTRUCTURE reports. base64 carries 3 bytes in 4 characters with a line
9
+ * break after every 76, so the file size is computed from it, assuming the standard 76 character lines (base64 written
10
+ * with other line lengths, or none, comes out up to about 2.5% off).
11
+ * Other encodings are left at the encoded size: 7bit, 8bit and binary are the file itself, and
12
+ * quoted-printable can not be undone without the content, so its encoded size is an upper bound.
13
+ *
14
+ * @param {number} encodedSize - Size of the encoded body part in bytes
15
+ * @param {string} [encoding] - Content-Transfer-Encoding of the part
16
+ * @returns {number|undefined} Size of the attachment in bytes
17
+ */
18
+ function decodedAttachmentSize(encodedSize, encoding) {
19
+ if (typeof encodedSize !== 'number' || !Number.isFinite(encodedSize) || encodedSize < 0) {
20
+ return undefined;
21
+ }
22
+ if (String(encoding || '').toLowerCase() !== 'base64') {
23
+ return encodedSize;
24
+ }
25
+ const lineBreaks = Math.floor(encodedSize / BASE64_LINE) * 2;
26
+ return Math.floor(((encodedSize - lineBreaks) * 3) / 4);
27
+ }
28
+
29
+ module.exports = { decodedAttachmentSize };