emailengine-app 2.79.9 → 2.80.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/.release-please-manifest.json +1 -1
- package/CHANGELOG.md +49 -0
- package/config/default.toml +5 -1
- package/data/google-crawlers.json +1 -1
- package/encrypt.js +43 -0
- package/lib/account/account-state.js +62 -0
- package/lib/account.js +34 -2
- package/lib/api-routes/account-routes.js +20 -0
- package/lib/api-routes/index.js +1 -1
- package/lib/api-routes/settings-routes.js +17 -2
- package/lib/autodetect-imap-settings.js +349 -47
- package/lib/cert-handler.js +62 -0
- package/lib/consts.js +50 -6
- package/lib/db.js +12 -0
- package/lib/egress-fetch.js +21 -9
- package/lib/email-client/base-client.js +234 -66
- package/lib/email-client/credential-errors.js +118 -0
- package/lib/email-client/gmail/gmail-api.js +2 -1
- package/lib/email-client/gmail-client.js +70 -64
- package/lib/email-client/imap/connection-errors.js +52 -0
- package/lib/email-client/imap/listing-diff.js +47 -6
- package/lib/email-client/imap/mailbox.js +457 -379
- package/lib/email-client/imap/subconnection.js +15 -11
- package/lib/email-client/imap/sync-operations.js +46 -59
- package/lib/email-client/imap-client.js +136 -95
- package/lib/email-client/message-builder.js +11 -11
- package/lib/email-client/outlook/graph-api.js +2 -1
- package/lib/email-client/outlook-client.js +219 -106
- package/lib/imap-proxy-auth.js +58 -3
- package/lib/imapproxy/imap-server.js +53 -62
- package/lib/lua/h-del-if-equals.lua +39 -0
- package/lib/lua/h-set-if-equals.lua +39 -0
- package/lib/routes-ui.js +4 -34
- package/lib/schemas.js +32 -0
- package/lib/security-headers.js +15 -0
- package/lib/tls/context.js +376 -0
- package/lib/tls/der.js +223 -0
- package/lib/tls/provision.js +555 -0
- package/lib/tls/self-signed.js +282 -0
- package/lib/tls/status.js +431 -0
- package/lib/tls/store.js +666 -0
- package/lib/tools.js +72 -27
- package/lib/ui-routes/account-routes.js +43 -30
- package/lib/ui-routes/admin-config-routes.js +22 -1
- package/lib/ui-routes/auth-routes.js +16 -1
- package/lib/ui-routes/internals-routes.js +3 -5
- package/lib/ui-routes/network-config-routes.js +54 -45
- package/lib/ui-routes/tls-config-routes.js +581 -0
- package/lib/utils/mailbox-path.js +51 -0
- package/lib/utils/severity.js +31 -0
- package/package.json +4 -4
- package/sbom.json +1 -1
- package/server.js +16 -0
- package/static/css/flyonui.css +1 -1
- package/static/css/src/app.tailwind.css +11 -0
- package/static/js/app.js +34 -0
- package/static/js/public.js +6 -2
- package/static/js/ui.js +8 -25
- package/static/licenses.html +328 -1257
- package/translations/messages.pot +87 -87
- package/views/account/login-redirect.hbs +22 -0
- package/views/config/imap-proxy.hbs +0 -3
- package/views/config/smtp.hbs +0 -3
- package/views/config/tls-upload.hbs +81 -0
- package/views/config/tls.hbs +492 -0
- package/views/partials/side_menu.hbs +1 -0
- package/views/partials/tls_security_card.hbs +25 -53
- package/views/partials/ui/alert-banner.hbs +3 -2
- package/views/partials/ui/badge.hbs +6 -2
- package/views/partials/ui/btn.hbs +5 -2
- package/workers/api.js +93 -134
- package/workers/imap-proxy.js +4 -1
- package/workers/smtp.js +51 -44
- package/views/partials/tls_security_js.hbs +0 -64
- package/views/partials/tls_security_modal.hbs +0 -22
|
@@ -4,6 +4,8 @@ const { ImapFlow } = require('imapflow');
|
|
|
4
4
|
const { backOff } = require('exponential-backoff');
|
|
5
5
|
const { ReconnectBackoff } = require('../reconnect-backoff');
|
|
6
6
|
const { detachImapClient } = require('./detach-client');
|
|
7
|
+
const { connectionUnavailableError } = require('./connection-errors');
|
|
8
|
+
const { samePath } = require('../../utils/mailbox-path');
|
|
7
9
|
const EventEmitter = require('events').EventEmitter;
|
|
8
10
|
|
|
9
11
|
const MAX_WAIT_DELAY = 450; //ms
|
|
@@ -20,7 +22,6 @@ class Subconnection extends EventEmitter {
|
|
|
20
22
|
this.account = opts.account;
|
|
21
23
|
|
|
22
24
|
this.imapClient = null;
|
|
23
|
-
this.getImapConfig = opts.getImapConfig;
|
|
24
25
|
|
|
25
26
|
this.isClosing = false;
|
|
26
27
|
this.isClosed = false;
|
|
@@ -41,10 +42,7 @@ class Subconnection extends EventEmitter {
|
|
|
41
42
|
|
|
42
43
|
checkIMAPConnection() {
|
|
43
44
|
if (!this.isConnected()) {
|
|
44
|
-
|
|
45
|
-
err.code = 'IMAPUnavailable';
|
|
46
|
-
err.statusCode = 503;
|
|
47
|
-
throw err;
|
|
45
|
+
throw connectionUnavailableError();
|
|
48
46
|
}
|
|
49
47
|
}
|
|
50
48
|
|
|
@@ -129,11 +127,13 @@ class Subconnection extends EventEmitter {
|
|
|
129
127
|
let folders = await this.imapClient.run('LIST', '', this.mailbox.path, { listOnly: true });
|
|
130
128
|
// The path goes into the LIST command as the match pattern
|
|
131
129
|
// verbatim, so IMAP wildcards (% and *) in the folder name
|
|
132
|
-
// can match sibling folders. Only an entry
|
|
133
|
-
//
|
|
134
|
-
// (
|
|
135
|
-
//
|
|
136
|
-
|
|
130
|
+
// can match sibling folders. Only an entry naming the same
|
|
131
|
+
// folder proves the monitored mailbox still exists - matched
|
|
132
|
+
// through samePath() because a server is free to answer with
|
|
133
|
+
// a different case for INBOX than the one it listed earlier,
|
|
134
|
+
// and reading that as a missing folder disables the
|
|
135
|
+
// subconnection until the folder is seen again
|
|
136
|
+
if (!folders || !folders.some(folder => samePath(folder.path, this.mailbox.path))) {
|
|
137
137
|
err.mailboxMissing = true;
|
|
138
138
|
}
|
|
139
139
|
} catch (E) {
|
|
@@ -194,7 +194,11 @@ class Subconnection extends EventEmitter {
|
|
|
194
194
|
}
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
-
|
|
197
|
+
// Built as this subconnection, not as the primary connection: a credential failure of its
|
|
198
|
+
// own must not notify or park the account, which is why notify() below is a no-op. The
|
|
199
|
+
// parent used to be reached through an injected zero-argument arrow that silently dropped
|
|
200
|
+
// this context
|
|
201
|
+
let imapConfig = await this.parent.getImapConfig(null, this);
|
|
198
202
|
|
|
199
203
|
let imapClient = new ImapFlow(
|
|
200
204
|
Object.assign({}, imapConfig, {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const { samePath } = require('../../utils/mailbox-path');
|
|
4
|
+
const { connectionClosedError, connectionNotAvailableError } = require('./connection-errors');
|
|
3
5
|
const crypto = require('crypto');
|
|
4
6
|
const { compareExisting, calculateFetchBackoff, readEnvValue, validUidValidity } = require('../../tools');
|
|
5
7
|
const config = require('@zone-eu/wild-config');
|
|
@@ -11,16 +13,6 @@ const FETCH_BATCH_SIZE = Number(readEnvValue('EENGINE_FETCH_BATCH_SIZE') || conf
|
|
|
11
13
|
// Do not check for flag updates using full sync more often than this value (30 minutes)
|
|
12
14
|
const FULL_SYNC_DELAY = 30 * 60 * 1000;
|
|
13
15
|
|
|
14
|
-
// Signals that the sync was interrupted rather than that the command itself failed. The original
|
|
15
|
-
// failure is kept as `cause`, because on this path it is the only record of why the server dropped
|
|
16
|
-
// the connection (NO [OVERQUOTA], * BYE Too many connections, [SERVERBUG]).
|
|
17
|
-
function connectionClosedError(cause) {
|
|
18
|
-
const err = new Error('IMAP connection closed during sync', { cause });
|
|
19
|
-
err.code = 'IMAPConnectionClosing';
|
|
20
|
-
err.statusCode = 503;
|
|
21
|
-
return err;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
16
|
function checkFetchRetryTimeout(retryCount, startTime) {
|
|
25
17
|
if (retryCount >= FETCH_RETRY_MIN_ATTEMPTS && Date.now() - startTime > FETCH_RETRY_MAX_TIME) {
|
|
26
18
|
const err = new Error('FETCH retry time limit exceeded');
|
|
@@ -66,13 +58,15 @@ function isRecentFullSync(storedStatus) {
|
|
|
66
58
|
}
|
|
67
59
|
|
|
68
60
|
/**
|
|
69
|
-
* Checks if UIDVALIDITY has changed (mailbox was recreated)
|
|
61
|
+
* Checks if UIDVALIDITY has changed (mailbox was recreated). getStoredStatus() reports a
|
|
62
|
+
* missing UIDVALIDITY as false, never as an absent key, so the value is tested rather than
|
|
63
|
+
* the presence of the key
|
|
70
64
|
* @param {Object} storedStatus - Stored mailbox status
|
|
71
65
|
* @param {Object} mailboxStatus - Current mailbox status from IMAP
|
|
72
66
|
* @returns {Boolean} True if UIDVALIDITY changed
|
|
73
67
|
*/
|
|
74
68
|
function hasUidValidityChanged(storedStatus, mailboxStatus) {
|
|
75
|
-
return
|
|
69
|
+
return validUidValidity(storedStatus.uidValidity) && mailboxStatus.uidValidity !== storedStatus.uidValidity;
|
|
76
70
|
}
|
|
77
71
|
|
|
78
72
|
/**
|
|
@@ -161,6 +155,17 @@ class SyncOperations {
|
|
|
161
155
|
this.logger = mailbox.logger;
|
|
162
156
|
}
|
|
163
157
|
|
|
158
|
+
/**
|
|
159
|
+
* Ends a sync operation: gives the mailbox lock back (its release reports the task
|
|
160
|
+
* to the connection, see Mailbox.getMailboxLock()) and clears the syncing flags
|
|
161
|
+
* @param {Object} lock - Lock returned by Mailbox.getMailboxLock()
|
|
162
|
+
*/
|
|
163
|
+
releaseSyncLock(lock) {
|
|
164
|
+
lock.release();
|
|
165
|
+
this.connection.syncing = false;
|
|
166
|
+
this.mailbox.syncing = false;
|
|
167
|
+
}
|
|
168
|
+
|
|
164
169
|
/**
|
|
165
170
|
* Performs full synchronization based on indexer type
|
|
166
171
|
* @returns {Promise} Resolves when sync is complete
|
|
@@ -178,18 +183,17 @@ class SyncOperations {
|
|
|
178
183
|
|
|
179
184
|
/**
|
|
180
185
|
* Performs partial synchronization based on indexer type
|
|
181
|
-
* @param {Object} storedStatus - Current stored mailbox status
|
|
182
186
|
* @returns {Promise} Resolves when sync is complete
|
|
183
187
|
*/
|
|
184
|
-
async partialSync(
|
|
188
|
+
async partialSync() {
|
|
185
189
|
const imapIndexer = this.mailbox.imapIndexer;
|
|
186
190
|
|
|
187
191
|
this.logger.trace({ msg: 'Running partial sync', imapIndexer });
|
|
188
192
|
|
|
189
193
|
if (imapIndexer === 'fast') {
|
|
190
|
-
return this.runFastSync(
|
|
194
|
+
return this.runFastSync();
|
|
191
195
|
}
|
|
192
|
-
return this.runPartialSync(
|
|
196
|
+
return this.runPartialSync();
|
|
193
197
|
}
|
|
194
198
|
|
|
195
199
|
/**
|
|
@@ -251,10 +255,7 @@ class SyncOperations {
|
|
|
251
255
|
|
|
252
256
|
let imapClient = this.connection.imapClient;
|
|
253
257
|
if (!imapClient) {
|
|
254
|
-
|
|
255
|
-
err.code = 'IMAPConnectionClosing';
|
|
256
|
-
err.statusCode = 503;
|
|
257
|
-
throw err;
|
|
258
|
+
throw connectionNotAvailableError();
|
|
258
259
|
}
|
|
259
260
|
|
|
260
261
|
let imapIndexer = this.mailbox.imapIndexer;
|
|
@@ -325,32 +326,33 @@ class SyncOperations {
|
|
|
325
326
|
|
|
326
327
|
return indexed;
|
|
327
328
|
} finally {
|
|
328
|
-
|
|
329
|
-
this.connection.syncing = false;
|
|
330
|
-
this.mailbox.syncing = false;
|
|
329
|
+
this.releaseSyncLock(lock);
|
|
331
330
|
}
|
|
332
331
|
}
|
|
333
332
|
|
|
334
333
|
/**
|
|
335
334
|
* Fast sync mode - only tracks new messages, doesn't maintain full message list
|
|
336
335
|
* More efficient for large mailboxes where we only care about new messages
|
|
337
|
-
* @param {Object} storedStatus - Current stored mailbox status
|
|
338
336
|
*/
|
|
339
|
-
async runFastSync(
|
|
340
|
-
storedStatus = storedStatus || (await this.mailbox.getStoredStatus());
|
|
341
|
-
let mailboxStatus = this.mailbox.getMailboxStatus();
|
|
342
|
-
|
|
337
|
+
async runFastSync() {
|
|
343
338
|
let lock = await this.mailbox.getMailboxLock(null, { description: 'Fast sync' });
|
|
344
339
|
this.connection.syncing = true;
|
|
345
340
|
this.mailbox.syncing = true;
|
|
346
341
|
try {
|
|
347
342
|
if (!this.connection.imapClient) {
|
|
348
|
-
|
|
349
|
-
err.code = 'IMAPConnectionClosing';
|
|
350
|
-
err.statusCode = 503;
|
|
351
|
-
throw err;
|
|
343
|
+
throw connectionNotAvailableError();
|
|
352
344
|
}
|
|
353
345
|
|
|
346
|
+
// Both snapshots are read under the lock, which guarantees this folder is the
|
|
347
|
+
// selected one: read before it, the mailbox counters could belong to a folder
|
|
348
|
+
// an API command opened in the meantime, and the stored state could predate a
|
|
349
|
+
// sync of this folder that ran just ahead of this one. Neither is accepted from
|
|
350
|
+
// the caller for that reason - onOpen() reads its own copy before the lock, to
|
|
351
|
+
// pick a strategy from, and a sync ahead of this one in the lock queue leaves
|
|
352
|
+
// that copy behind
|
|
353
|
+
let storedStatus = await this.mailbox.getStoredStatus();
|
|
354
|
+
let mailboxStatus = this.mailbox.getMailboxStatus();
|
|
355
|
+
|
|
354
356
|
let knownUidNext = typeof storedStatus.uidNext === 'number' ? storedStatus.uidNext || 1 : 1;
|
|
355
357
|
|
|
356
358
|
if (knownUidNext && mailboxStatus.messages) {
|
|
@@ -485,32 +487,27 @@ class SyncOperations {
|
|
|
485
487
|
|
|
486
488
|
await this.mailbox.publishSyncedEvents(storedStatus);
|
|
487
489
|
} finally {
|
|
488
|
-
|
|
489
|
-
this.connection.syncing = false;
|
|
490
|
-
this.mailbox.syncing = false;
|
|
490
|
+
this.releaseSyncLock(lock);
|
|
491
491
|
}
|
|
492
492
|
}
|
|
493
493
|
|
|
494
494
|
/**
|
|
495
495
|
* Partial sync - fetches only changed messages using MODSEQ or UID range
|
|
496
496
|
* Used for incremental updates when we know something changed
|
|
497
|
-
* @param {Object} storedStatus - Current stored mailbox status
|
|
498
497
|
*/
|
|
499
|
-
async runPartialSync(
|
|
500
|
-
storedStatus = storedStatus || (await this.mailbox.getStoredStatus());
|
|
501
|
-
let mailboxStatus = this.mailbox.getMailboxStatus();
|
|
502
|
-
|
|
498
|
+
async runPartialSync() {
|
|
503
499
|
let lock = await this.mailbox.getMailboxLock(null, { description: 'Partial sync' });
|
|
504
500
|
this.connection.syncing = true;
|
|
505
501
|
this.mailbox.syncing = true;
|
|
506
502
|
try {
|
|
507
503
|
if (!this.connection.imapClient) {
|
|
508
|
-
|
|
509
|
-
err.code = 'IMAPConnectionClosing';
|
|
510
|
-
err.statusCode = 503;
|
|
511
|
-
throw err;
|
|
504
|
+
throw connectionNotAvailableError();
|
|
512
505
|
}
|
|
513
506
|
|
|
507
|
+
// Read under the lock for the same reason as in runFastSync()
|
|
508
|
+
let storedStatus = await this.mailbox.getStoredStatus();
|
|
509
|
+
let mailboxStatus = this.mailbox.getMailboxStatus();
|
|
510
|
+
|
|
514
511
|
let fields = { uid: true, flags: true, modseq: true, emailId: true, labels: true, internalDate: true };
|
|
515
512
|
let range = '1:*';
|
|
516
513
|
let opts = {
|
|
@@ -536,10 +533,7 @@ class SyncOperations {
|
|
|
536
533
|
let imapClient = this.connection.imapClient;
|
|
537
534
|
if (!imapClient || !imapClient.usable) {
|
|
538
535
|
this.logger.debug({ msg: 'IMAP client not available for partial sync' });
|
|
539
|
-
|
|
540
|
-
err.code = 'IMAPConnectionClosing';
|
|
541
|
-
err.statusCode = 503;
|
|
542
|
-
throw err;
|
|
536
|
+
throw connectionNotAvailableError();
|
|
543
537
|
}
|
|
544
538
|
|
|
545
539
|
try {
|
|
@@ -637,9 +631,7 @@ class SyncOperations {
|
|
|
637
631
|
|
|
638
632
|
await this.mailbox.publishSyncedEvents(storedStatus);
|
|
639
633
|
} finally {
|
|
640
|
-
|
|
641
|
-
this.connection.syncing = false;
|
|
642
|
-
this.mailbox.syncing = false;
|
|
634
|
+
this.releaseSyncLock(lock);
|
|
643
635
|
}
|
|
644
636
|
}
|
|
645
637
|
|
|
@@ -720,10 +712,7 @@ class SyncOperations {
|
|
|
720
712
|
const imapClient = this.connection.imapClient;
|
|
721
713
|
if (!imapClient || !imapClient.usable) {
|
|
722
714
|
this.logger.debug({ msg: 'IMAP client not available for FETCH' });
|
|
723
|
-
|
|
724
|
-
err.code = 'IMAPConnectionClosing';
|
|
725
|
-
err.statusCode = 503;
|
|
726
|
-
throw err;
|
|
715
|
+
throw connectionNotAvailableError();
|
|
727
716
|
}
|
|
728
717
|
|
|
729
718
|
try {
|
|
@@ -904,7 +893,7 @@ class SyncOperations {
|
|
|
904
893
|
// Verify we're still on the correct mailbox after the delay
|
|
905
894
|
// Another operation might have changed the mailbox while we were waiting
|
|
906
895
|
const currentMailbox = this.connection.imapClient?.mailbox;
|
|
907
|
-
if (!currentMailbox || currentMailbox.path
|
|
896
|
+
if (!currentMailbox || !samePath(currentMailbox.path, this.mailbox.path)) {
|
|
908
897
|
this.logger.warn({
|
|
909
898
|
msg: 'Mailbox changed during retry delay, aborting sync',
|
|
910
899
|
expectedPath: this.mailbox.path,
|
|
@@ -969,9 +958,7 @@ class SyncOperations {
|
|
|
969
958
|
|
|
970
959
|
await this.mailbox.publishSyncedEvents(storedStatus);
|
|
971
960
|
} finally {
|
|
972
|
-
this.
|
|
973
|
-
this.mailbox.syncing = false;
|
|
974
|
-
lock.release();
|
|
961
|
+
this.releaseSyncLock(lock);
|
|
975
962
|
}
|
|
976
963
|
}
|
|
977
964
|
}
|