emailengine-app 2.89.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.
- package/.ncurc.js +19 -8
- package/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +44 -0
- package/Dockerfile +5 -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.js +158 -453
- package/lib/api-routes/account-routes.js +42 -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 +83 -234
- package/lib/consts.js +1 -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/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/license-subscription.js +60 -0
- package/lib/load-env.js +48 -0
- package/lib/logger.js +1 -4
- 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 +24 -58
- 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 +1 -1
- 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/ui-routes/account-edit-form.js +198 -0
- package/lib/ui-routes/account-page.js +241 -0
- package/lib/ui-routes/account-routes.js +12 -318
- package/lib/ui-routes/auth-routes.js +10 -10
- 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 +23 -19
- package/sbom.json +267 -908
- package/scripts/refresh-test-tokens.js +1 -1
- package/server.js +320 -676
- package/static/css/flyonui.css +1 -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 +219 -737
- package/translations/messages.pot +64 -53
- package/views/accounts/browse.hbs +19 -1
- 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');
|
|
@@ -37,8 +37,10 @@ const { decodeStoredMailboxEntry } = require('./email-client/imap/listing-diff')
|
|
|
37
37
|
const { SYNC_STATE_FIELDS } = require('./email-client/sync-state-fields');
|
|
38
38
|
const { runLocked, refuseUnavailableLock, getSharedLock } = require('./lock-run');
|
|
39
39
|
const { readImapData } = require('./account/imap-data');
|
|
40
|
+
const { FIELD_DECODERS, FIELD_ENCODERS, decodePlain, encodePlain } = require('./account/field-codec');
|
|
40
41
|
const { isTransientCredentialError, credentialErrorStatus } = require('./email-client/credential-errors');
|
|
41
42
|
const { getAccountHttpRoute } = require('./utils/network');
|
|
43
|
+
const { isUnsafeKey } = require('./utils/unsafe-key');
|
|
42
44
|
|
|
43
45
|
// Every Account takes its locks from the one shared instance (lib/lock-run.js getSharedLock()): each
|
|
44
46
|
// ioredfour Lock opens a subscriber connection, and a Lock per Account once meant ~30k connections
|
|
@@ -100,6 +102,9 @@ function credentialFields(accountData) {
|
|
|
100
102
|
return fields;
|
|
101
103
|
}
|
|
102
104
|
|
|
105
|
+
// Per-account message counters, stored as `stats:count:<type>:<counter>` fields of the account hash
|
|
106
|
+
const STATS_COUNT_KEY = /^stats:count:([^:]+):([^:]+)/;
|
|
107
|
+
|
|
103
108
|
// Sync positions the Gmail API and Graph clients keep in the account hash, cleared by a flush
|
|
104
109
|
const FLUSHED_SYNC_FIELDS = Object.values(SYNC_STATE_FIELDS).flat();
|
|
105
110
|
|
|
@@ -180,6 +185,47 @@ function isOperatorDisabled(accountData) {
|
|
|
180
185
|
return isImapDisabled(accountData) && !authFailureDisabledAt(accountData);
|
|
181
186
|
}
|
|
182
187
|
|
|
188
|
+
// The API error for a worker listing that came back as an error object
|
|
189
|
+
function listingError(mailboxListing) {
|
|
190
|
+
let error = Boom.boomify(new Error(mailboxListing.error), { statusCode: mailboxListing.statusCode || 500 });
|
|
191
|
+
if (mailboxListing.code) {
|
|
192
|
+
error.output.payload.code = mailboxListing.code;
|
|
193
|
+
}
|
|
194
|
+
return error;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// The 503 for an account whose listing cannot be served yet
|
|
198
|
+
function notReadyError(message, code, state) {
|
|
199
|
+
let error = Boom.boomify(new Error(message), { statusCode: 503 });
|
|
200
|
+
if (state) {
|
|
201
|
+
error.output.payload.state = state;
|
|
202
|
+
}
|
|
203
|
+
error.output.payload.code = code;
|
|
204
|
+
return error;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// A live LIST result keyed the way the stored listing is keyed, its redundant status paths
|
|
208
|
+
// removed. Undefined when there is no live result to merge.
|
|
209
|
+
function indexLiveListing(mailboxListing) {
|
|
210
|
+
if (!mailboxListing) {
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
let mailboxListingMap = new Map();
|
|
215
|
+
for (let entry of mailboxListing) {
|
|
216
|
+
if (entry.status) {
|
|
217
|
+
delete entry.status.path;
|
|
218
|
+
}
|
|
219
|
+
// Keyed the way the stored listing is keyed, because that is what the lookup
|
|
220
|
+
// below uses. A server that spells the inbox "Inbox" reports it that way in
|
|
221
|
+
// every LIST response while the stored hash holds the case-folded "INBOX", so
|
|
222
|
+
// an unnormalized key silently dropped the live counters for the one folder
|
|
223
|
+
// every account has.
|
|
224
|
+
mailboxListingMap.set(normalizePath(entry.path), entry);
|
|
225
|
+
}
|
|
226
|
+
return mailboxListingMap;
|
|
227
|
+
}
|
|
228
|
+
|
|
183
229
|
class Account {
|
|
184
230
|
constructor(options) {
|
|
185
231
|
this.redis = options.redis;
|
|
@@ -550,194 +596,28 @@ class Account {
|
|
|
550
596
|
|
|
551
597
|
const counters = {};
|
|
552
598
|
|
|
553
|
-
Object.keys(accountData)
|
|
599
|
+
for (const key of Object.keys(accountData)) {
|
|
554
600
|
if (key.startsWith('_')) {
|
|
555
|
-
|
|
601
|
+
continue;
|
|
556
602
|
}
|
|
557
603
|
|
|
558
|
-
|
|
604
|
+
const countMatch = key.match(STATS_COUNT_KEY);
|
|
559
605
|
if (countMatch) {
|
|
560
606
|
const [, type, counter] = countMatch;
|
|
561
|
-
|
|
562
|
-
|
|
607
|
+
// The names come from hash field names, so a field like `stats:count:__proto__:x`
|
|
608
|
+
// must not reach Object.prototype, nor `toString` an inherited method
|
|
609
|
+
if (isUnsafeKey(type) || isUnsafeKey(counter)) {
|
|
610
|
+
continue;
|
|
563
611
|
}
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
counters[type][counter] = 0;
|
|
612
|
+
if (!Object.hasOwn(counters, type)) {
|
|
613
|
+
counters[type] = {};
|
|
567
614
|
}
|
|
568
|
-
|
|
569
615
|
counters[type][counter] = Number(accountData[key]);
|
|
570
|
-
|
|
616
|
+
continue;
|
|
571
617
|
}
|
|
572
618
|
|
|
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
|
-
});
|
|
619
|
+
(FIELD_DECODERS.get(key) || decodePlain)(this, key, accountData, result, skipDecrypt);
|
|
620
|
+
}
|
|
741
621
|
|
|
742
622
|
// decrypt secrets
|
|
743
623
|
if (this.secret && !skipDecrypt && !deferCredentials) {
|
|
@@ -819,187 +699,13 @@ class Account {
|
|
|
819
699
|
serializeAccountData(accountData) {
|
|
820
700
|
let result = {};
|
|
821
701
|
|
|
822
|
-
Object.keys(accountData)
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
702
|
+
for (const key of Object.keys(accountData)) {
|
|
703
|
+
// A key holding undefined was not given: hmset would store it as '', which reads back as
|
|
704
|
+
// "every folder" for `path` and as unparseable JSON for the JSON fields
|
|
705
|
+
if (!key.startsWith('_') && accountData[key] !== undefined) {
|
|
706
|
+
(FIELD_ENCODERS.get(key) || encodePlain)(this, key, accountData, result);
|
|
826
707
|
}
|
|
827
|
-
|
|
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;
|
|
1001
|
-
}
|
|
1002
|
-
});
|
|
708
|
+
}
|
|
1003
709
|
|
|
1004
710
|
return result;
|
|
1005
711
|
}
|
|
@@ -1972,119 +1678,34 @@ class Account {
|
|
|
1972
1678
|
async getMailboxListing(query) {
|
|
1973
1679
|
let accountData = await this.loadAccountData(this.account, false);
|
|
1974
1680
|
|
|
1975
|
-
let mailboxListing;
|
|
1976
1681
|
if (await this.isApiClient(accountData)) {
|
|
1977
|
-
mailboxListing = await this.listMailboxes(query);
|
|
1682
|
+
let mailboxListing = await this.listMailboxes(query);
|
|
1978
1683
|
|
|
1979
1684
|
if (mailboxListing && mailboxListing.error) {
|
|
1980
|
-
|
|
1981
|
-
if (mailboxListing.code) {
|
|
1982
|
-
error.output.payload.code = mailboxListing.code;
|
|
1983
|
-
}
|
|
1984
|
-
throw error;
|
|
1685
|
+
throw listingError(mailboxListing);
|
|
1985
1686
|
}
|
|
1986
1687
|
|
|
1987
1688
|
// just pass through, do nothing
|
|
1988
1689
|
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
1690
|
}
|
|
2016
1691
|
|
|
1692
|
+
let mailboxListing = await this.fetchLiveListing(accountData, query);
|
|
1693
|
+
|
|
2017
1694
|
let mailboxes = [];
|
|
2018
1695
|
let storedListing = await this.redis.hgetallBuffer(this.getMailboxListKey());
|
|
2019
1696
|
|
|
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
|
-
}
|
|
1697
|
+
let mailboxListingMap = indexLiveListing(mailboxListing);
|
|
2035
1698
|
|
|
2036
1699
|
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();
|
|
1700
|
+
let pipelineResults = await this.readMailboxStatuses(paths);
|
|
2044
1701
|
|
|
2045
1702
|
for (let i = 0; i < paths.length; i++) {
|
|
2046
1703
|
let path = paths[i];
|
|
2047
1704
|
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;
|
|
2059
|
-
}
|
|
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
|
-
};
|
|
1705
|
+
let mailbox = this.mergeMailboxEntry(path, storedListing[path], mailboxListingMap, pipelineResults[i]);
|
|
1706
|
+
if (mailbox) {
|
|
1707
|
+
mailboxes.push(mailbox);
|
|
2075
1708
|
}
|
|
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
1709
|
} catch (err) {
|
|
2089
1710
|
// should not happen
|
|
2090
1711
|
if (logger.notifyError) {
|
|
@@ -2102,6 +1723,90 @@ class Account {
|
|
|
2102
1723
|
return mailboxes;
|
|
2103
1724
|
}
|
|
2104
1725
|
|
|
1726
|
+
/**
|
|
1727
|
+
* Runs a live LIST for an IMAP account that is connected or asked for counters, and refuses
|
|
1728
|
+
* an account that has nothing stored to list yet. Resolves to the live listing, or undefined
|
|
1729
|
+
* when the stored listing is served on its own.
|
|
1730
|
+
*/
|
|
1731
|
+
async fetchLiveListing(accountData, query) {
|
|
1732
|
+
if (accountData.state === ACCOUNT_STATES.CONNECTED || query.counters) {
|
|
1733
|
+
// run LIST
|
|
1734
|
+
let mailboxListing = await this.listMailboxes(query);
|
|
1735
|
+
if (mailboxListing && mailboxListing.error) {
|
|
1736
|
+
throw listingError(mailboxListing);
|
|
1737
|
+
}
|
|
1738
|
+
return mailboxListing;
|
|
1739
|
+
}
|
|
1740
|
+
|
|
1741
|
+
if (accountData.state === ACCOUNT_STATES.UNSET) {
|
|
1742
|
+
// account has not been set up yet
|
|
1743
|
+
throw notReadyError('Syncing is disabled for the requested account', 'NotSyncing', accountData.state);
|
|
1744
|
+
}
|
|
1745
|
+
|
|
1746
|
+
if (accountData.state === ACCOUNT_STATES.INIT || !(await this.redis.exists(this.getMailboxListKey()))) {
|
|
1747
|
+
// account has not been set up yet
|
|
1748
|
+
throw notReadyError('Requested account is not yet initialized', 'NotYetConnected', accountData.state);
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
return undefined;
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
/**
|
|
1755
|
+
* The status hash of every listed folder, in one round trip rather than one per folder
|
|
1756
|
+
*/
|
|
1757
|
+
async readMailboxStatuses(paths) {
|
|
1758
|
+
let pipeline = this.redis.pipeline();
|
|
1759
|
+
for (let path of paths) {
|
|
1760
|
+
pipeline.hgetall(getMailboxStatusKey(this.account, path));
|
|
1761
|
+
}
|
|
1762
|
+
return await pipeline.exec();
|
|
1763
|
+
}
|
|
1764
|
+
|
|
1765
|
+
/**
|
|
1766
|
+
* One folder of the listing: the stored entry, its status counters and the live LIST status.
|
|
1767
|
+
* Null for a corrupt stored entry.
|
|
1768
|
+
*/
|
|
1769
|
+
mergeMailboxEntry(path, storedEntry, mailboxListingMap, pipelineResult) {
|
|
1770
|
+
// Returns false for a corrupt value. Skipping the folder (rather than pushing a
|
|
1771
|
+
// boxed primitive into the response) keeps the rest of the listing usable; the
|
|
1772
|
+
// IMAP sync purges the entry on its next pass (see getCurrentListing), which is
|
|
1773
|
+
// also where the corruption is escalated to the error tracker - once, with every
|
|
1774
|
+
// affected folder, instead of once per request from here. Until that pass runs
|
|
1775
|
+
// the folder is simply missing from a 200 response, which a client reconciling
|
|
1776
|
+
// its own state against this endpoint reads as a deletion.
|
|
1777
|
+
let decoded = decodeStoredMailboxEntry(storedEntry);
|
|
1778
|
+
if (!decoded) {
|
|
1779
|
+
this.logger.warn({ msg: 'Skipping corrupt stored mailbox listing entry', path, account: this.account });
|
|
1780
|
+
return null;
|
|
1781
|
+
}
|
|
1782
|
+
|
|
1783
|
+
if (decoded.path && decoded.delimiter && decoded.path.indexOf(decoded.delimiter) >= 0) {
|
|
1784
|
+
decoded.parentPath = decoded.path.substring(0, decoded.path.lastIndexOf(decoded.delimiter));
|
|
1785
|
+
}
|
|
1786
|
+
|
|
1787
|
+
let listedMailboxInfo = mailboxListingMap ? mailboxListingMap.get(path) : undefined;
|
|
1788
|
+
|
|
1789
|
+
let mailboxInfo = {};
|
|
1790
|
+
let [pipelineErr, data] = pipelineResult || [];
|
|
1791
|
+
if (!pipelineErr && data && Object.keys(data).length) {
|
|
1792
|
+
mailboxInfo = {
|
|
1793
|
+
path: data.path || path,
|
|
1794
|
+
messages: data.messages && !isNaN(data.messages) ? Number(data.messages) : false,
|
|
1795
|
+
uidNext: data.uidNext && !isNaN(data.uidNext) ? Number(data.uidNext) : false
|
|
1796
|
+
};
|
|
1797
|
+
}
|
|
1798
|
+
|
|
1799
|
+
return Object.assign(
|
|
1800
|
+
decoded,
|
|
1801
|
+
mailboxInfo,
|
|
1802
|
+
listedMailboxInfo && listedMailboxInfo.status
|
|
1803
|
+
? {
|
|
1804
|
+
status: listedMailboxInfo.status
|
|
1805
|
+
}
|
|
1806
|
+
: {}
|
|
1807
|
+
);
|
|
1808
|
+
}
|
|
1809
|
+
|
|
2105
1810
|
// Worker backends return false for entities they can not find (e.g. an unknown mailbox path
|
|
2106
1811
|
// or message ID on IMAP). Convert such results into a 404 error for the API.
|
|
2107
1812
|
assertFound(result, message, code) {
|