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.
- package/.ncurc.js +19 -8
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +73 -0
- package/Dockerfile +7 -3
- package/ENVIRONMENT.md +24 -23
- package/README.md +1 -1
- package/bin/healthcheck.js +1 -1
- package/copy-static-files.sh +0 -8
- package/data/google-crawlers.json +1 -1
- package/encrypt.js +1 -4
- package/lib/account/field-codec.js +396 -0
- package/lib/account-assignment.js +26 -9
- package/lib/account.js +196 -462
- package/lib/api-routes/account-routes.js +49 -6
- package/lib/api-routes/route-metadata.js +5 -2
- package/lib/api-routes/submit-routes.js +15 -2
- package/lib/api-token-auth.js +342 -274
- package/lib/arf-detect.js +174 -149
- package/lib/attachment-size.js +29 -0
- package/lib/autodetect-imap-settings.js +482 -296
- package/lib/consts.js +5 -5
- package/lib/db.js +1 -4
- package/lib/email-client/base-client.js +907 -780
- package/lib/email-client/gmail-client.js +557 -450
- package/lib/email-client/imap/cid-parts.js +61 -0
- package/lib/email-client/imap/mailbox.js +376 -422
- package/lib/email-client/imap/sync-operations.js +239 -178
- package/lib/email-client/imap-client.js +5 -5
- package/lib/email-client/message-builder.js +4 -5
- package/lib/email-client/new-message-steps.js +168 -2
- package/lib/email-client/outlook/graph-message.js +266 -0
- package/lib/email-client/outlook/subscription-expiry.js +30 -0
- package/lib/email-client/outlook-client.js +652 -808
- package/lib/get-raw-email.js +338 -232
- package/lib/get-secret.js +1 -4
- package/lib/imapproxy/imap-server.js +5 -5
- package/lib/license-subscription.js +60 -0
- package/lib/load-env.js +48 -0
- package/lib/logger-consts.js +37 -1
- package/lib/logger.js +1 -4
- package/lib/mail-probe.js +157 -0
- package/lib/mcp/tools.js +244 -207
- package/lib/metric-values.js +76 -0
- package/lib/metrics.js +320 -0
- package/lib/oauth/gmail.js +39 -120
- package/lib/oauth/http-request.js +195 -0
- package/lib/oauth/mail-ru.js +33 -60
- package/lib/oauth/outlook.js +37 -137
- package/lib/oauth/pubsub/google.js +176 -117
- package/lib/oauth2-apps.js +440 -463
- package/lib/openapi/build-document.js +10 -10
- package/lib/read-env-value.js +31 -7
- package/lib/redis-backend-warnings.js +70 -0
- package/lib/route-descriptors.js +78 -0
- package/lib/schemas.js +10 -1
- package/lib/sentry-integrations.js +299 -0
- package/lib/sentry.js +75 -27
- package/lib/sso-routes.js +249 -0
- package/lib/sso.js +29 -6
- package/lib/tools.js +17 -28
- package/lib/ui-routes/account-edit-form.js +198 -0
- package/lib/ui-routes/account-page.js +241 -0
- package/lib/ui-routes/account-routes.js +46 -349
- package/lib/ui-routes/account-setup.js +1 -1
- package/lib/ui-routes/auth-routes.js +10 -10
- package/lib/ui-routes/provider-signin.js +187 -0
- package/lib/ui-routes/route-helpers.js +1 -1
- package/lib/ui-routes/unsubscribe-routes.js +2 -2
- package/lib/ui-view-context.js +328 -0
- package/lib/utils/public-suffix.js +22 -101
- package/lib/web-safe-html.js +20 -7
- package/lib/worker-metrics.js +82 -0
- package/list-generate.js +2 -5
- package/package.json +28 -24
- package/sbom.json +360 -1030
- package/scripts/refresh-test-tokens.js +1 -1
- package/server.js +400 -677
- package/static/css/flyonui.css +1 -1
- package/static/css/public.css +55 -1
- package/static/css/src/app.tailwind.css +18 -0
- package/static/js/ee-client.js +2619 -645
- package/static/js/ui.js +12 -2
- package/static/licenses.html +224 -764
- package/translations/de.mo +0 -0
- package/translations/de.po +429 -390
- package/translations/en.mo +0 -0
- package/translations/en.po +342 -257
- package/translations/es.mo +0 -0
- package/translations/es.po +440 -395
- package/translations/et.mo +0 -0
- package/translations/et.po +425 -375
- package/translations/fr.mo +0 -0
- package/translations/fr.po +425 -383
- package/translations/it.mo +0 -0
- package/translations/it.po +441 -394
- package/translations/ja.mo +0 -0
- package/translations/ja.po +408 -335
- package/translations/messages.pot +188 -130
- package/translations/nl.mo +0 -0
- package/translations/nl.po +419 -382
- package/translations/pl.mo +0 -0
- package/translations/pl.po +425 -379
- package/translations/sv.mo +0 -0
- package/translations/sv.po +440 -388
- package/translations/tr.mo +0 -0
- package/translations/tr.po +441 -387
- package/views/account-identity-error.hbs +1 -1
- package/views/account-workspace-error.hbs +2 -2
- package/views/accounts/browse.hbs +19 -1
- package/views/accounts/register/imap-server.hbs +22 -7
- package/views/accounts/register/imap.hbs +3 -0
- package/views/accounts/register/index.hbs +1 -27
- package/views/accounts/register/provider-signin.hbs +58 -0
- package/views/oauth-scope-error.hbs +1 -1
- package/views/partials/provider_signin_button.hbs +30 -0
- package/views/queues/index.hbs +1 -1
- package/views/redirect.hbs +1 -1
- package/views/unsubscribe.hbs +6 -6
- package/workers/api.js +20 -451
- package/data/public_suffix_list.dat +0 -16698
package/lib/account.js
CHANGED
|
@@ -18,7 +18,7 @@ const {
|
|
|
18
18
|
const { MessageChannel } = require('worker_threads');
|
|
19
19
|
const { MessagePortReadable } = require('./message-port-stream');
|
|
20
20
|
const { isDeepStrictEqual } = require('util');
|
|
21
|
-
const { encrypt, decrypt, decryptAsync, decryptField
|
|
21
|
+
const { encrypt, decrypt, decryptAsync, decryptField } = require('./encrypt');
|
|
22
22
|
const { oauth2Apps, isApiBasedApp } = require('./oauth2-apps');
|
|
23
23
|
const settings = require('./settings');
|
|
24
24
|
const redisScanDelete = require('./redis-scan-delete');
|
|
@@ -29,7 +29,16 @@ const tokens = require('./tokens');
|
|
|
29
29
|
const { Export } = require('./export');
|
|
30
30
|
const { publishRevocation } = require('./revocation-events');
|
|
31
31
|
const { randomId } = require('./utils/random-id');
|
|
32
|
-
const {
|
|
32
|
+
const {
|
|
33
|
+
REDIS_PREFIX,
|
|
34
|
+
AUTH_FAILURE_DISABLED_FIELD,
|
|
35
|
+
AUTH_FAILURE_DISABLED_LEGACY_DESCRIPTION,
|
|
36
|
+
OAUTH_TOKEN_MIN_VALIDITY,
|
|
37
|
+
DISPATCH_RETRY_DELAY
|
|
38
|
+
} = require('./consts');
|
|
39
|
+
|
|
40
|
+
// Worker commands notifyWorker() sends a second time when the first dispatch fails
|
|
41
|
+
const RETRIED_WORKER_COMMANDS = new Set(['update', 'reconnect']);
|
|
33
42
|
const { webSafeTextResponse, applyWebSafeHtmlOptions } = require('./web-safe-html');
|
|
34
43
|
const { checkAccountScopes: checkScopes } = require('./oauth/scope-checker');
|
|
35
44
|
const { ACCOUNT_STATES, ERROR_STATES, calculateEffectiveState, validateAccountState, getDisplayState, formatLastError } = require('./account/account-state');
|
|
@@ -37,8 +46,10 @@ const { decodeStoredMailboxEntry } = require('./email-client/imap/listing-diff')
|
|
|
37
46
|
const { SYNC_STATE_FIELDS } = require('./email-client/sync-state-fields');
|
|
38
47
|
const { runLocked, refuseUnavailableLock, getSharedLock } = require('./lock-run');
|
|
39
48
|
const { readImapData } = require('./account/imap-data');
|
|
49
|
+
const { FIELD_DECODERS, FIELD_ENCODERS, decodePlain, encodePlain } = require('./account/field-codec');
|
|
40
50
|
const { isTransientCredentialError, credentialErrorStatus } = require('./email-client/credential-errors');
|
|
41
51
|
const { getAccountHttpRoute } = require('./utils/network');
|
|
52
|
+
const { isUnsafeKey } = require('./utils/unsafe-key');
|
|
42
53
|
|
|
43
54
|
// Every Account takes its locks from the one shared instance (lib/lock-run.js getSharedLock()): each
|
|
44
55
|
// ioredfour Lock opens a subscriber connection, and a Lock per Account once meant ~30k connections
|
|
@@ -100,6 +111,9 @@ function credentialFields(accountData) {
|
|
|
100
111
|
return fields;
|
|
101
112
|
}
|
|
102
113
|
|
|
114
|
+
// Per-account message counters, stored as `stats:count:<type>:<counter>` fields of the account hash
|
|
115
|
+
const STATS_COUNT_KEY = /^stats:count:([^:]+):([^:]+)/;
|
|
116
|
+
|
|
103
117
|
// Sync positions the Gmail API and Graph clients keep in the account hash, cleared by a flush
|
|
104
118
|
const FLUSHED_SYNC_FIELDS = Object.values(SYNC_STATE_FIELDS).flat();
|
|
105
119
|
|
|
@@ -180,6 +194,47 @@ function isOperatorDisabled(accountData) {
|
|
|
180
194
|
return isImapDisabled(accountData) && !authFailureDisabledAt(accountData);
|
|
181
195
|
}
|
|
182
196
|
|
|
197
|
+
// The API error for a worker listing that came back as an error object
|
|
198
|
+
function listingError(mailboxListing) {
|
|
199
|
+
let error = Boom.boomify(new Error(mailboxListing.error), { statusCode: mailboxListing.statusCode || 500 });
|
|
200
|
+
if (mailboxListing.code) {
|
|
201
|
+
error.output.payload.code = mailboxListing.code;
|
|
202
|
+
}
|
|
203
|
+
return error;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// The 503 for an account whose listing cannot be served yet
|
|
207
|
+
function notReadyError(message, code, state) {
|
|
208
|
+
let error = Boom.boomify(new Error(message), { statusCode: 503 });
|
|
209
|
+
if (state) {
|
|
210
|
+
error.output.payload.state = state;
|
|
211
|
+
}
|
|
212
|
+
error.output.payload.code = code;
|
|
213
|
+
return error;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// A live LIST result keyed the way the stored listing is keyed, its redundant status paths
|
|
217
|
+
// removed. Undefined when there is no live result to merge.
|
|
218
|
+
function indexLiveListing(mailboxListing) {
|
|
219
|
+
if (!mailboxListing) {
|
|
220
|
+
return undefined;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
let mailboxListingMap = new Map();
|
|
224
|
+
for (let entry of mailboxListing) {
|
|
225
|
+
if (entry.status) {
|
|
226
|
+
delete entry.status.path;
|
|
227
|
+
}
|
|
228
|
+
// Keyed the way the stored listing is keyed, because that is what the lookup
|
|
229
|
+
// below uses. A server that spells the inbox "Inbox" reports it that way in
|
|
230
|
+
// every LIST response while the stored hash holds the case-folded "INBOX", so
|
|
231
|
+
// an unnormalized key silently dropped the live counters for the one folder
|
|
232
|
+
// every account has.
|
|
233
|
+
mailboxListingMap.set(normalizePath(entry.path), entry);
|
|
234
|
+
}
|
|
235
|
+
return mailboxListingMap;
|
|
236
|
+
}
|
|
237
|
+
|
|
183
238
|
class Account {
|
|
184
239
|
constructor(options) {
|
|
185
240
|
this.redis = options.redis;
|
|
@@ -190,6 +245,7 @@ class Account {
|
|
|
190
245
|
this.timeout = options.timeout ? Number(options.timeout) : 0;
|
|
191
246
|
|
|
192
247
|
this.call = options.call; // async method to request data from parent
|
|
248
|
+
this.dispatchRetryDelay = options.dispatchRetryDelay ?? DISPATCH_RETRY_DELAY;
|
|
193
249
|
|
|
194
250
|
this.logger = options.logger || logger;
|
|
195
251
|
}
|
|
@@ -550,194 +606,28 @@ class Account {
|
|
|
550
606
|
|
|
551
607
|
const counters = {};
|
|
552
608
|
|
|
553
|
-
Object.keys(accountData)
|
|
609
|
+
for (const key of Object.keys(accountData)) {
|
|
554
610
|
if (key.startsWith('_')) {
|
|
555
|
-
|
|
611
|
+
continue;
|
|
556
612
|
}
|
|
557
613
|
|
|
558
|
-
|
|
614
|
+
const countMatch = key.match(STATS_COUNT_KEY);
|
|
559
615
|
if (countMatch) {
|
|
560
616
|
const [, type, counter] = countMatch;
|
|
561
|
-
|
|
562
|
-
|
|
617
|
+
// The names come from hash field names, so a field like `stats:count:__proto__:x`
|
|
618
|
+
// must not reach Object.prototype, nor `toString` an inherited method
|
|
619
|
+
if (isUnsafeKey(type) || isUnsafeKey(counter)) {
|
|
620
|
+
continue;
|
|
563
621
|
}
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
counters[type][counter] = 0;
|
|
622
|
+
if (!Object.hasOwn(counters, type)) {
|
|
623
|
+
counters[type] = {};
|
|
567
624
|
}
|
|
568
|
-
|
|
569
625
|
counters[type][counter] = Number(accountData[key]);
|
|
570
|
-
|
|
626
|
+
continue;
|
|
571
627
|
}
|
|
572
628
|
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
case 'lastWatch':
|
|
576
|
-
// Date object
|
|
577
|
-
if (accountData[key]) {
|
|
578
|
-
if (accountData[key] === 'null') {
|
|
579
|
-
result[key] = null;
|
|
580
|
-
} else {
|
|
581
|
-
let date = new Date(accountData[key]);
|
|
582
|
-
if (date.toString() !== 'Invalid Date') {
|
|
583
|
-
result[key] = date;
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
break;
|
|
588
|
-
|
|
589
|
-
// boolean values
|
|
590
|
-
case 'copy':
|
|
591
|
-
case 'logs':
|
|
592
|
-
if (accountData[key] && accountData[key] !== 'null') {
|
|
593
|
-
result[key] = accountData[key] === 'true' ? true : false;
|
|
594
|
-
}
|
|
595
|
-
break;
|
|
596
|
-
|
|
597
|
-
case 'expectedEmail':
|
|
598
|
-
// '' is the cleared marker written by serializeAccountData - report it as unset.
|
|
599
|
-
if (accountData[key]) {
|
|
600
|
-
result[key] = accountData[key];
|
|
601
|
-
}
|
|
602
|
-
break;
|
|
603
|
-
|
|
604
|
-
case 'path':
|
|
605
|
-
if (!accountData[key] || !accountData[key].length) {
|
|
606
|
-
break;
|
|
607
|
-
}
|
|
608
|
-
if (accountData[key].length > 1 && (/^"|"$/.test(accountData[key]) || /^\[|\]$/.test(accountData[key]))) {
|
|
609
|
-
// seems like JSON array or string
|
|
610
|
-
try {
|
|
611
|
-
let value = JSON.parse(accountData[key]);
|
|
612
|
-
if (value === null) {
|
|
613
|
-
break;
|
|
614
|
-
}
|
|
615
|
-
result[key] = value;
|
|
616
|
-
} catch (err) {
|
|
617
|
-
this.logUnserializeFailure('Failed to parse input from Redis', key, accountData[key], accountData.account, err);
|
|
618
|
-
}
|
|
619
|
-
} else {
|
|
620
|
-
// regular string
|
|
621
|
-
result[key] = accountData[key];
|
|
622
|
-
}
|
|
623
|
-
break;
|
|
624
|
-
|
|
625
|
-
case 'webhooks':
|
|
626
|
-
case 'proxy': {
|
|
627
|
-
let value = skipDecrypt ? accountData[key] : this.decryptAccountField(key, accountData[key], accountData.account);
|
|
628
|
-
if (typeof value !== 'undefined') {
|
|
629
|
-
result[key] = value;
|
|
630
|
-
}
|
|
631
|
-
break;
|
|
632
|
-
}
|
|
633
|
-
|
|
634
|
-
case 'webhooksCustomHeaders': {
|
|
635
|
-
let stored = accountData[key];
|
|
636
|
-
if (skipDecrypt) {
|
|
637
|
-
if (this.secret && parseEncryptedData(stored).format !== 'cleartext') {
|
|
638
|
-
// not read by any caller that skips decryption
|
|
639
|
-
break;
|
|
640
|
-
}
|
|
641
|
-
} else {
|
|
642
|
-
stored = this.decryptAccountField(key, stored, accountData.account);
|
|
643
|
-
if (typeof stored === 'undefined') {
|
|
644
|
-
break;
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
try {
|
|
648
|
-
let value = JSON.parse(stored);
|
|
649
|
-
if (value === null) {
|
|
650
|
-
break;
|
|
651
|
-
}
|
|
652
|
-
result[key] = value;
|
|
653
|
-
} catch (err) {
|
|
654
|
-
this.logUnserializeFailure('Failed to parse input from Redis', key, stored, accountData.account, err);
|
|
655
|
-
}
|
|
656
|
-
break;
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
// generic JSON values
|
|
660
|
-
case 'subconnections':
|
|
661
|
-
case 'watchResponse':
|
|
662
|
-
case 'watchFailure':
|
|
663
|
-
try {
|
|
664
|
-
let value = JSON.parse(accountData[key]);
|
|
665
|
-
if (value === null) {
|
|
666
|
-
break;
|
|
667
|
-
}
|
|
668
|
-
result[key] = value;
|
|
669
|
-
} catch (err) {
|
|
670
|
-
this.logUnserializeFailure('Failed to parse input from Redis', key, accountData[key], accountData.account, err);
|
|
671
|
-
}
|
|
672
|
-
break;
|
|
673
|
-
|
|
674
|
-
case 'outlookSubscription':
|
|
675
|
-
try {
|
|
676
|
-
let value = JSON.parse(accountData[key]);
|
|
677
|
-
if (value === null) {
|
|
678
|
-
break;
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
if (value.expirationDateTime) {
|
|
682
|
-
let expirationDateTime = new Date(value.expirationDateTime);
|
|
683
|
-
if (expirationDateTime.toString() === 'Invalid Date') {
|
|
684
|
-
expirationDateTime = null;
|
|
685
|
-
}
|
|
686
|
-
value.expirationDateTime = expirationDateTime;
|
|
687
|
-
}
|
|
688
|
-
|
|
689
|
-
result[key] = value;
|
|
690
|
-
} catch (err) {
|
|
691
|
-
this.logUnserializeFailure('Failed to parse input from Redis', key, accountData[key], accountData.account, err);
|
|
692
|
-
}
|
|
693
|
-
break;
|
|
694
|
-
|
|
695
|
-
case 'imap':
|
|
696
|
-
case 'smtp':
|
|
697
|
-
case 'imapServerInfo':
|
|
698
|
-
case 'smtpServerEhlo':
|
|
699
|
-
case 'oauth2':
|
|
700
|
-
case 'lastErrorState':
|
|
701
|
-
case 'syncError':
|
|
702
|
-
case 'smtpStatus':
|
|
703
|
-
case 'webhookErrorFlag':
|
|
704
|
-
try {
|
|
705
|
-
let value = JSON.parse(accountData[key]);
|
|
706
|
-
if (value === null) {
|
|
707
|
-
break;
|
|
708
|
-
}
|
|
709
|
-
result[key] = value;
|
|
710
|
-
for (let subKey of ['created', 'expires', 'generated']) {
|
|
711
|
-
if (result[key][subKey]) {
|
|
712
|
-
let dateVal = /^[0-9]+$/.test(result[key][subKey]) ? Number(result[key][subKey]) : result[key][subKey];
|
|
713
|
-
let date = new Date(dateVal);
|
|
714
|
-
if (date.toString() !== 'Invalid Date') {
|
|
715
|
-
result[key][subKey] = date;
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
for (let subKey of Object.keys(result[key])) {
|
|
720
|
-
if (result[key][subKey] === null) {
|
|
721
|
-
delete result[key][subKey];
|
|
722
|
-
}
|
|
723
|
-
}
|
|
724
|
-
} catch (err) {
|
|
725
|
-
this.logUnserializeFailure('Failed to parse input from Redis', key, accountData[key], accountData.account, err);
|
|
726
|
-
}
|
|
727
|
-
break;
|
|
728
|
-
|
|
729
|
-
// number values
|
|
730
|
-
case 'connections':
|
|
731
|
-
case 'listRegistry':
|
|
732
|
-
case 'runIndex':
|
|
733
|
-
result[key] = Number(accountData[key]) || 0;
|
|
734
|
-
break;
|
|
735
|
-
|
|
736
|
-
default:
|
|
737
|
-
result[key] = accountData[key];
|
|
738
|
-
break;
|
|
739
|
-
}
|
|
740
|
-
});
|
|
629
|
+
(FIELD_DECODERS.get(key) || decodePlain)(this, key, accountData, result, skipDecrypt);
|
|
630
|
+
}
|
|
741
631
|
|
|
742
632
|
// decrypt secrets
|
|
743
633
|
if (this.secret && !skipDecrypt && !deferCredentials) {
|
|
@@ -819,187 +709,13 @@ class Account {
|
|
|
819
709
|
serializeAccountData(accountData) {
|
|
820
710
|
let result = {};
|
|
821
711
|
|
|
822
|
-
Object.keys(accountData)
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
switch (key) {
|
|
828
|
-
case 'notifyFrom':
|
|
829
|
-
case 'lastWatch':
|
|
830
|
-
// Date object
|
|
831
|
-
if (accountData[key] === 'now') {
|
|
832
|
-
result[key] = new Date().toISOString();
|
|
833
|
-
} else if (
|
|
834
|
-
accountData[key] &&
|
|
835
|
-
typeof accountData[key] === 'object' &&
|
|
836
|
-
typeof accountData[key].toISOString === 'function' &&
|
|
837
|
-
accountData[key].toString() !== 'Invalid Date'
|
|
838
|
-
) {
|
|
839
|
-
result[key] = accountData[key].toISOString();
|
|
840
|
-
} else if (typeof accountData[key] === 'string') {
|
|
841
|
-
let date = new Date(accountData[key]);
|
|
842
|
-
if (date.toString() !== 'Invalid Date') {
|
|
843
|
-
result[key] = date.toISOString();
|
|
844
|
-
}
|
|
845
|
-
} else if (accountData[key] === null) {
|
|
846
|
-
result[key] = 'null';
|
|
847
|
-
}
|
|
848
|
-
break;
|
|
849
|
-
|
|
850
|
-
case 'path':
|
|
851
|
-
if (
|
|
852
|
-
accountData[key] === null ||
|
|
853
|
-
accountData[key] === '*' ||
|
|
854
|
-
(Array.isArray(accountData[key]) && (accountData[key].length === 0 || (accountData[key].length === 1 && accountData[key][0] === '*')))
|
|
855
|
-
) {
|
|
856
|
-
result[key] = '';
|
|
857
|
-
} else {
|
|
858
|
-
try {
|
|
859
|
-
result[key] = JSON.stringify(accountData[key]);
|
|
860
|
-
} catch (err) {
|
|
861
|
-
this.logger.error({ msg: 'Failed to stringify input for Redis', key, account: this.account, err });
|
|
862
|
-
}
|
|
863
|
-
}
|
|
864
|
-
break;
|
|
865
|
-
|
|
866
|
-
case 'webhooksCustomHeaders':
|
|
867
|
-
try {
|
|
868
|
-
result[key] = this.encryptAccountField(key, JSON.stringify(accountData[key]));
|
|
869
|
-
} catch (err) {
|
|
870
|
-
this.logger.error({ msg: 'Failed to stringify input for Redis', key, account: this.account, err });
|
|
871
|
-
}
|
|
872
|
-
break;
|
|
873
|
-
|
|
874
|
-
case 'proxy':
|
|
875
|
-
if (typeof accountData[key] !== 'undefined' && accountData[key] !== null && typeof accountData[key].toString === 'function') {
|
|
876
|
-
result[key] = this.encryptAccountField(key, accountData[key].toString());
|
|
877
|
-
}
|
|
878
|
-
break;
|
|
879
|
-
|
|
880
|
-
// generic JSON values
|
|
881
|
-
case 'subconnections':
|
|
882
|
-
case 'watchResponse':
|
|
883
|
-
case 'watchFailure':
|
|
884
|
-
try {
|
|
885
|
-
let value = JSON.stringify(accountData[key]);
|
|
886
|
-
|
|
887
|
-
result[key] = value;
|
|
888
|
-
} catch (err) {
|
|
889
|
-
this.logger.error({ msg: 'Failed to stringify input for Redis', key, account: this.account, err });
|
|
890
|
-
}
|
|
891
|
-
|
|
892
|
-
break;
|
|
893
|
-
|
|
894
|
-
case 'outlookSubscription':
|
|
895
|
-
try {
|
|
896
|
-
let objValue = Object.assign({}, accountData[key]);
|
|
897
|
-
|
|
898
|
-
if (['number', 'string'].includes(typeof objValue.expirationDateTime)) {
|
|
899
|
-
objValue.expirationDateTime = new Date(objValue.expirationDateTime);
|
|
900
|
-
}
|
|
901
|
-
|
|
902
|
-
if (
|
|
903
|
-
objValue.expirationDateTime &&
|
|
904
|
-
typeof objValue.expirationDateTime === 'object' &&
|
|
905
|
-
typeof objValue.expirationDateTime.toISOString === 'function' &&
|
|
906
|
-
objValue.expirationDateTime.toString() !== 'Invalid Date'
|
|
907
|
-
) {
|
|
908
|
-
objValue.expirationDateTime = objValue.expirationDateTime.toISOString();
|
|
909
|
-
} else if (objValue.expirationDateTime) {
|
|
910
|
-
objValue.expirationDateTime = null;
|
|
911
|
-
}
|
|
912
|
-
|
|
913
|
-
let value = JSON.stringify(objValue);
|
|
914
|
-
|
|
915
|
-
result[key] = value;
|
|
916
|
-
} catch (err) {
|
|
917
|
-
this.logger.error({ msg: 'Failed to stringify input for Redis', key, account: this.account, err });
|
|
918
|
-
}
|
|
919
|
-
|
|
920
|
-
break;
|
|
921
|
-
|
|
922
|
-
case 'imap':
|
|
923
|
-
case 'smtp':
|
|
924
|
-
case 'oauth2':
|
|
925
|
-
try {
|
|
926
|
-
// make a deep copy for manipulation
|
|
927
|
-
let connectData = JSON.parse(JSON.stringify(accountData[key]));
|
|
928
|
-
|
|
929
|
-
// if possible encrypt passwords
|
|
930
|
-
if (this.secret && connectData.auth) {
|
|
931
|
-
for (let key of ['pass', 'accessToken', 'refreshToken']) {
|
|
932
|
-
if (key in connectData.auth) {
|
|
933
|
-
try {
|
|
934
|
-
connectData.auth[key] = encrypt(connectData.auth[key], this.secret);
|
|
935
|
-
} catch (err) {
|
|
936
|
-
this.logger.error({ msg: 'Failed to encrypt value', key, account: this.account, err });
|
|
937
|
-
}
|
|
938
|
-
} else if (key in connectData) {
|
|
939
|
-
try {
|
|
940
|
-
connectData[key] = encrypt(connectData[key], this.secret);
|
|
941
|
-
} catch (err) {
|
|
942
|
-
this.logger.error({ msg: 'Failed to encrypt value', key, account: this.account, err });
|
|
943
|
-
}
|
|
944
|
-
}
|
|
945
|
-
}
|
|
946
|
-
}
|
|
947
|
-
|
|
948
|
-
for (let subKey of ['created', 'expires', 'generated']) {
|
|
949
|
-
if (
|
|
950
|
-
accountData[key][subKey] &&
|
|
951
|
-
typeof accountData[key][subKey] === 'object' &&
|
|
952
|
-
accountData[key][subKey].toString() !== 'Invalid Date'
|
|
953
|
-
) {
|
|
954
|
-
connectData[subKey] = accountData[key][subKey].toISOString();
|
|
955
|
-
}
|
|
956
|
-
}
|
|
957
|
-
|
|
958
|
-
result[key] = JSON.stringify(connectData);
|
|
959
|
-
} catch (err) {
|
|
960
|
-
this.logger.error({ msg: 'Failed to stringify input for Redis', key, account: this.account, err });
|
|
961
|
-
}
|
|
962
|
-
break;
|
|
963
|
-
|
|
964
|
-
case 'webhooks':
|
|
965
|
-
if (typeof accountData[key] !== 'undefined' && accountData[key] !== null && typeof accountData[key].toString === 'function') {
|
|
966
|
-
let value = accountData[key].toString();
|
|
967
|
-
if (!value) {
|
|
968
|
-
// clear potential error flag
|
|
969
|
-
result.webhookErrorFlag = '{}';
|
|
970
|
-
}
|
|
971
|
-
result[key] = this.encryptAccountField(key, value);
|
|
972
|
-
}
|
|
973
|
-
break;
|
|
974
|
-
|
|
975
|
-
// boolean values
|
|
976
|
-
case 'copy':
|
|
977
|
-
case 'logs':
|
|
978
|
-
if (typeof accountData[key] === 'boolean') {
|
|
979
|
-
result[key] = accountData[key].toString();
|
|
980
|
-
} else if (accountData[key] === null) {
|
|
981
|
-
result[key] = '';
|
|
982
|
-
}
|
|
983
|
-
break;
|
|
984
|
-
|
|
985
|
-
case 'expectedEmail':
|
|
986
|
-
// Clearable, so it cannot ride the default branch: that drops nulls, and hmset only
|
|
987
|
-
// ever adds keys, which would leave an identity pin settable but never removable.
|
|
988
|
-
// Follows the same convention as `copy` above - null serializes to the empty marker
|
|
989
|
-
// and unserialize treats it as unset - rather than adding a per-field delete to
|
|
990
|
-
// persistUpdate.
|
|
991
|
-
if (typeof accountData[key] === 'string' || accountData[key] === null) {
|
|
992
|
-
result[key] = accountData[key] || '';
|
|
993
|
-
}
|
|
994
|
-
break;
|
|
995
|
-
|
|
996
|
-
default:
|
|
997
|
-
if (typeof accountData[key] !== 'undefined' && accountData[key] !== null && typeof accountData[key].toString === 'function') {
|
|
998
|
-
result[key] = accountData[key].toString();
|
|
999
|
-
}
|
|
1000
|
-
break;
|
|
712
|
+
for (const key of Object.keys(accountData)) {
|
|
713
|
+
// A key holding undefined was not given: hmset would store it as '', which reads back as
|
|
714
|
+
// "every folder" for `path` and as unparseable JSON for the JSON fields
|
|
715
|
+
if (!key.startsWith('_') && accountData[key] !== undefined) {
|
|
716
|
+
(FIELD_ENCODERS.get(key) || encodePlain)(this, key, accountData, result);
|
|
1001
717
|
}
|
|
1002
|
-
}
|
|
718
|
+
}
|
|
1003
719
|
|
|
1004
720
|
return result;
|
|
1005
721
|
}
|
|
@@ -1198,8 +914,14 @@ class Account {
|
|
|
1198
914
|
* Sends a command to the worker that owns the account after a change was already written.
|
|
1199
915
|
* The write stands either way, so a dispatch that fails (an RPC timeout, no worker yet) is
|
|
1200
916
|
* logged rather than thrown: an error response for a saved change made a retried create report
|
|
1201
|
-
* the account as existing and a retried delete answer 404.
|
|
1202
|
-
*
|
|
917
|
+
* the account as existing and a retried delete answer 404.
|
|
918
|
+
*
|
|
919
|
+
* A failed `update` or `reconnect` is sent once more after DISPATCH_RETRY_DELAY, in the
|
|
920
|
+
* background, since a short main-thread stall would otherwise leave the worker on the old
|
|
921
|
+
* settings until its next reconnect; both are safe to repeat. `new` and `delete` are not
|
|
922
|
+
* retried: a lost `new` is picked up by the main thread's orphaned-account sweep, which also
|
|
923
|
+
* announces it, while a repeated one could announce the account twice, and a `delete` sent late
|
|
924
|
+
* could tear down an account re-created under the same id in the meantime.
|
|
1203
925
|
*
|
|
1204
926
|
* @returns {Promise<Boolean>} Whether the worker acknowledged the command
|
|
1205
927
|
*/
|
|
@@ -1210,18 +932,31 @@ class Account {
|
|
|
1210
932
|
return false;
|
|
1211
933
|
}
|
|
1212
934
|
try {
|
|
1213
|
-
await this.
|
|
1214
|
-
cmd,
|
|
1215
|
-
account: this.account,
|
|
1216
|
-
timeout: this.timeout
|
|
1217
|
-
});
|
|
935
|
+
await this.dispatchToWorker(cmd);
|
|
1218
936
|
return true;
|
|
1219
937
|
} catch (err) {
|
|
1220
|
-
|
|
938
|
+
const retry = RETRIED_WORKER_COMMANDS.has(cmd);
|
|
939
|
+
this.logger.error({ msg: 'Failed to notify the worker after a saved account change', account: this.account, cmd, retry, err });
|
|
940
|
+
if (retry) {
|
|
941
|
+
setTimeout(() => {
|
|
942
|
+
this.dispatchToWorker(cmd).then(
|
|
943
|
+
() => this.logger.info({ msg: 'Notified the worker on retry', account: this.account, cmd }),
|
|
944
|
+
err => this.logger.error({ msg: 'Failed to notify the worker on retry', account: this.account, cmd, err })
|
|
945
|
+
);
|
|
946
|
+
}, this.dispatchRetryDelay).unref();
|
|
947
|
+
}
|
|
1221
948
|
return false;
|
|
1222
949
|
}
|
|
1223
950
|
}
|
|
1224
951
|
|
|
952
|
+
async dispatchToWorker(cmd) {
|
|
953
|
+
await this.call({
|
|
954
|
+
cmd,
|
|
955
|
+
account: this.account,
|
|
956
|
+
timeout: this.timeout
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
|
|
1225
960
|
/**
|
|
1226
961
|
* The read-modify-write half of update(): loads the stored account, merges partial credential
|
|
1227
962
|
* objects into it and writes the result back. Runs under the update lock.
|
|
@@ -1972,119 +1707,34 @@ class Account {
|
|
|
1972
1707
|
async getMailboxListing(query) {
|
|
1973
1708
|
let accountData = await this.loadAccountData(this.account, false);
|
|
1974
1709
|
|
|
1975
|
-
let mailboxListing;
|
|
1976
1710
|
if (await this.isApiClient(accountData)) {
|
|
1977
|
-
mailboxListing = await this.listMailboxes(query);
|
|
1711
|
+
let mailboxListing = await this.listMailboxes(query);
|
|
1978
1712
|
|
|
1979
1713
|
if (mailboxListing && mailboxListing.error) {
|
|
1980
|
-
|
|
1981
|
-
if (mailboxListing.code) {
|
|
1982
|
-
error.output.payload.code = mailboxListing.code;
|
|
1983
|
-
}
|
|
1984
|
-
throw error;
|
|
1714
|
+
throw listingError(mailboxListing);
|
|
1985
1715
|
}
|
|
1986
1716
|
|
|
1987
1717
|
// just pass through, do nothing
|
|
1988
1718
|
return mailboxListing;
|
|
1989
|
-
} else if (accountData.state === ACCOUNT_STATES.CONNECTED || query.counters) {
|
|
1990
|
-
// run LIST
|
|
1991
|
-
mailboxListing = await this.listMailboxes(query);
|
|
1992
|
-
if (mailboxListing && mailboxListing.error) {
|
|
1993
|
-
let error = Boom.boomify(new Error(mailboxListing.error), { statusCode: mailboxListing.statusCode || 500 });
|
|
1994
|
-
if (mailboxListing.code) {
|
|
1995
|
-
error.output.payload.code = mailboxListing.code;
|
|
1996
|
-
}
|
|
1997
|
-
throw error;
|
|
1998
|
-
}
|
|
1999
|
-
} else if (accountData.state === ACCOUNT_STATES.UNSET) {
|
|
2000
|
-
// account has not been set up yet
|
|
2001
|
-
let error = Boom.boomify(new Error('Syncing is disabled for the requested account'), { statusCode: 503 });
|
|
2002
|
-
if (accountData.state) {
|
|
2003
|
-
error.output.payload.state = accountData.state;
|
|
2004
|
-
}
|
|
2005
|
-
error.output.payload.code = 'NotSyncing';
|
|
2006
|
-
throw error;
|
|
2007
|
-
} else if (accountData.state === ACCOUNT_STATES.INIT || !(await this.redis.exists(this.getMailboxListKey()))) {
|
|
2008
|
-
// account has not been set up yet
|
|
2009
|
-
let error = Boom.boomify(new Error('Requested account is not yet initialized'), { statusCode: 503 });
|
|
2010
|
-
if (accountData.state) {
|
|
2011
|
-
error.output.payload.state = accountData.state;
|
|
2012
|
-
}
|
|
2013
|
-
error.output.payload.code = 'NotYetConnected';
|
|
2014
|
-
throw error;
|
|
2015
1719
|
}
|
|
2016
1720
|
|
|
1721
|
+
let mailboxListing = await this.fetchLiveListing(accountData, query);
|
|
1722
|
+
|
|
2017
1723
|
let mailboxes = [];
|
|
2018
1724
|
let storedListing = await this.redis.hgetallBuffer(this.getMailboxListKey());
|
|
2019
1725
|
|
|
2020
|
-
let mailboxListingMap;
|
|
2021
|
-
if (mailboxListing) {
|
|
2022
|
-
mailboxListingMap = new Map();
|
|
2023
|
-
for (let entry of mailboxListing) {
|
|
2024
|
-
if (entry.status) {
|
|
2025
|
-
delete entry.status.path;
|
|
2026
|
-
}
|
|
2027
|
-
// Keyed the way the stored listing is keyed, because that is what the lookup
|
|
2028
|
-
// below uses. A server that spells the inbox "Inbox" reports it that way in
|
|
2029
|
-
// every LIST response while the stored hash holds the case-folded "INBOX", so
|
|
2030
|
-
// an unnormalized key silently dropped the live counters for the one folder
|
|
2031
|
-
// every account has.
|
|
2032
|
-
mailboxListingMap.set(normalizePath(entry.path), entry);
|
|
2033
|
-
}
|
|
2034
|
-
}
|
|
1726
|
+
let mailboxListingMap = indexLiveListing(mailboxListing);
|
|
2035
1727
|
|
|
2036
1728
|
let paths = Object.keys(storedListing || {});
|
|
2037
|
-
|
|
2038
|
-
// Batch into a single round-trip to avoid per-mailbox latency with many folders
|
|
2039
|
-
let pipeline = this.redis.pipeline();
|
|
2040
|
-
for (let path of paths) {
|
|
2041
|
-
pipeline.hgetall(getMailboxStatusKey(this.account, path));
|
|
2042
|
-
}
|
|
2043
|
-
let pipelineResults = await pipeline.exec();
|
|
1729
|
+
let pipelineResults = await this.readMailboxStatuses(paths);
|
|
2044
1730
|
|
|
2045
1731
|
for (let i = 0; i < paths.length; i++) {
|
|
2046
1732
|
let path = paths[i];
|
|
2047
1733
|
try {
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
// also where the corruption is escalated to the error tracker - once, with every
|
|
2052
|
-
// affected folder, instead of once per request from here. Until that pass runs
|
|
2053
|
-
// the folder is simply missing from a 200 response, which a client reconciling
|
|
2054
|
-
// its own state against this endpoint reads as a deletion.
|
|
2055
|
-
let decoded = decodeStoredMailboxEntry(storedListing[path]);
|
|
2056
|
-
if (!decoded) {
|
|
2057
|
-
this.logger.warn({ msg: 'Skipping corrupt stored mailbox listing entry', path, account: this.account });
|
|
2058
|
-
continue;
|
|
1734
|
+
let mailbox = this.mergeMailboxEntry(path, storedListing[path], mailboxListingMap, pipelineResults[i]);
|
|
1735
|
+
if (mailbox) {
|
|
1736
|
+
mailboxes.push(mailbox);
|
|
2059
1737
|
}
|
|
2060
|
-
|
|
2061
|
-
if (decoded.path && decoded.delimiter && decoded.path.indexOf(decoded.delimiter) >= 0) {
|
|
2062
|
-
decoded.parentPath = decoded.path.substring(0, decoded.path.lastIndexOf(decoded.delimiter));
|
|
2063
|
-
}
|
|
2064
|
-
|
|
2065
|
-
let listedMailboxInfo = mailboxListingMap ? mailboxListingMap.get(path) : undefined;
|
|
2066
|
-
|
|
2067
|
-
let mailboxInfo = {};
|
|
2068
|
-
let [pipelineErr, data] = pipelineResults[i] || [];
|
|
2069
|
-
if (!pipelineErr && data && Object.keys(data).length) {
|
|
2070
|
-
mailboxInfo = {
|
|
2071
|
-
path: data.path || path,
|
|
2072
|
-
messages: data.messages && !isNaN(data.messages) ? Number(data.messages) : false,
|
|
2073
|
-
uidNext: data.uidNext && !isNaN(data.uidNext) ? Number(data.uidNext) : false
|
|
2074
|
-
};
|
|
2075
|
-
}
|
|
2076
|
-
|
|
2077
|
-
mailboxes.push(
|
|
2078
|
-
Object.assign(
|
|
2079
|
-
decoded,
|
|
2080
|
-
mailboxInfo,
|
|
2081
|
-
listedMailboxInfo && listedMailboxInfo.status
|
|
2082
|
-
? {
|
|
2083
|
-
status: listedMailboxInfo.status
|
|
2084
|
-
}
|
|
2085
|
-
: {}
|
|
2086
|
-
)
|
|
2087
|
-
);
|
|
2088
1738
|
} catch (err) {
|
|
2089
1739
|
// should not happen
|
|
2090
1740
|
if (logger.notifyError) {
|
|
@@ -2102,6 +1752,90 @@ class Account {
|
|
|
2102
1752
|
return mailboxes;
|
|
2103
1753
|
}
|
|
2104
1754
|
|
|
1755
|
+
/**
|
|
1756
|
+
* Runs a live LIST for an IMAP account that is connected or asked for counters, and refuses
|
|
1757
|
+
* an account that has nothing stored to list yet. Resolves to the live listing, or undefined
|
|
1758
|
+
* when the stored listing is served on its own.
|
|
1759
|
+
*/
|
|
1760
|
+
async fetchLiveListing(accountData, query) {
|
|
1761
|
+
if (accountData.state === ACCOUNT_STATES.CONNECTED || query.counters) {
|
|
1762
|
+
// run LIST
|
|
1763
|
+
let mailboxListing = await this.listMailboxes(query);
|
|
1764
|
+
if (mailboxListing && mailboxListing.error) {
|
|
1765
|
+
throw listingError(mailboxListing);
|
|
1766
|
+
}
|
|
1767
|
+
return mailboxListing;
|
|
1768
|
+
}
|
|
1769
|
+
|
|
1770
|
+
if (accountData.state === ACCOUNT_STATES.UNSET) {
|
|
1771
|
+
// account has not been set up yet
|
|
1772
|
+
throw notReadyError('Syncing is disabled for the requested account', 'NotSyncing', accountData.state);
|
|
1773
|
+
}
|
|
1774
|
+
|
|
1775
|
+
if (accountData.state === ACCOUNT_STATES.INIT || !(await this.redis.exists(this.getMailboxListKey()))) {
|
|
1776
|
+
// account has not been set up yet
|
|
1777
|
+
throw notReadyError('Requested account is not yet initialized', 'NotYetConnected', accountData.state);
|
|
1778
|
+
}
|
|
1779
|
+
|
|
1780
|
+
return undefined;
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
/**
|
|
1784
|
+
* The status hash of every listed folder, in one round trip rather than one per folder
|
|
1785
|
+
*/
|
|
1786
|
+
async readMailboxStatuses(paths) {
|
|
1787
|
+
let pipeline = this.redis.pipeline();
|
|
1788
|
+
for (let path of paths) {
|
|
1789
|
+
pipeline.hgetall(getMailboxStatusKey(this.account, path));
|
|
1790
|
+
}
|
|
1791
|
+
return await pipeline.exec();
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
/**
|
|
1795
|
+
* One folder of the listing: the stored entry, its status counters and the live LIST status.
|
|
1796
|
+
* Null for a corrupt stored entry.
|
|
1797
|
+
*/
|
|
1798
|
+
mergeMailboxEntry(path, storedEntry, mailboxListingMap, pipelineResult) {
|
|
1799
|
+
// Returns false for a corrupt value. Skipping the folder (rather than pushing a
|
|
1800
|
+
// boxed primitive into the response) keeps the rest of the listing usable; the
|
|
1801
|
+
// IMAP sync purges the entry on its next pass (see getCurrentListing), which is
|
|
1802
|
+
// also where the corruption is escalated to the error tracker - once, with every
|
|
1803
|
+
// affected folder, instead of once per request from here. Until that pass runs
|
|
1804
|
+
// the folder is simply missing from a 200 response, which a client reconciling
|
|
1805
|
+
// its own state against this endpoint reads as a deletion.
|
|
1806
|
+
let decoded = decodeStoredMailboxEntry(storedEntry);
|
|
1807
|
+
if (!decoded) {
|
|
1808
|
+
this.logger.warn({ msg: 'Skipping corrupt stored mailbox listing entry', path, account: this.account });
|
|
1809
|
+
return null;
|
|
1810
|
+
}
|
|
1811
|
+
|
|
1812
|
+
if (decoded.path && decoded.delimiter && decoded.path.indexOf(decoded.delimiter) >= 0) {
|
|
1813
|
+
decoded.parentPath = decoded.path.substring(0, decoded.path.lastIndexOf(decoded.delimiter));
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
let listedMailboxInfo = mailboxListingMap ? mailboxListingMap.get(path) : undefined;
|
|
1817
|
+
|
|
1818
|
+
let mailboxInfo = {};
|
|
1819
|
+
let [pipelineErr, data] = pipelineResult || [];
|
|
1820
|
+
if (!pipelineErr && data && Object.keys(data).length) {
|
|
1821
|
+
mailboxInfo = {
|
|
1822
|
+
path: data.path || path,
|
|
1823
|
+
messages: data.messages && !isNaN(data.messages) ? Number(data.messages) : false,
|
|
1824
|
+
uidNext: data.uidNext && !isNaN(data.uidNext) ? Number(data.uidNext) : false
|
|
1825
|
+
};
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
return Object.assign(
|
|
1829
|
+
decoded,
|
|
1830
|
+
mailboxInfo,
|
|
1831
|
+
listedMailboxInfo && listedMailboxInfo.status
|
|
1832
|
+
? {
|
|
1833
|
+
status: listedMailboxInfo.status
|
|
1834
|
+
}
|
|
1835
|
+
: {}
|
|
1836
|
+
);
|
|
1837
|
+
}
|
|
1838
|
+
|
|
2105
1839
|
// Worker backends return false for entities they can not find (e.g. an unknown mailbox path
|
|
2106
1840
|
// or message ID on IMAP). Convert such results into a 404 error for the API.
|
|
2107
1841
|
assertFound(result, message, code) {
|