Skip to content

The open alternative to Salesforce, designed for AI.

Stop enabling IMAP4rev2 on IMAP connections (#26843)

neo773528c78e259c7
diff --git a/packages/twenty-server/src/engine/core-modules/imap-smtp-caldav-connection/services/imap-smtp-caldav-connection.service.ts b/packages/twenty-server/src/engine/core-modules/imap-smtp-caldav-connection/services/imap-smtp-caldav-connection.service.ts
index 2f40a97a72f..fb57e492f83 100644
--- a/packages/twenty-server/src/engine/core-modules/imap-smtp-caldav-connection/services/imap-smtp-caldav-connection.service.ts
+++ b/packages/twenty-server/src/engine/core-modules/imap-smtp-caldav-connection/services/imap-smtp-caldav-connection.service.ts
@@ -50,6 +50,7 @@ export class ImapSmtpCaldavService {
pass: params.password,
},
logger: false,
+ disableIMAP4rev2: true,
tls: {
rejectUnauthorized: false,
},
diff --git a/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/imap/providers/imap-client.provider.ts b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/imap/providers/imap-client.provider.ts
index 0f88066b110..5d74490c5b5 100644
--- a/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/imap/providers/imap-client.provider.ts
+++ b/packages/twenty-server/src/modules/messaging/message-import-manager/drivers/imap/providers/imap-client.provider.ts
@@ -103,6 +103,7 @@ export class ImapClientProvider {
pass: imapParams.password,
},
logger: false,
+ disableIMAP4rev2: true,
tls: {
rejectUnauthorized: false,
},
diff --git a/packages/twenty-server/test/integration/imap/suites/imap-rev2-empty-search.integration-spec.ts b/packages/twenty-server/test/integration/imap/suites/imap-rev2-empty-search.integration-spec.ts
new file mode 100644
index 00000000000..b32bea04079
--- /dev/null
+++ b/packages/twenty-server/test/integration/imap/suites/imap-rev2-empty-search.integration-spec.ts
@@ -0,0 +1,133 @@
+import { randomUUID } from 'node:crypto';
+
+import { isNonEmptyString } from '@sniptt/guards';
+import { ImapFlow } from 'imapflow';
+
+import { EmailConnectionSecurity } from 'src/engine/core-modules/imap-smtp-caldav-connection/enums/email-connection-security.enum';
+import { MessageChannelEntity } from 'src/engine/metadata-modules/message-channel/entities/message-channel.entity';
+
+import { deleteConnectedAccount } from 'test/integration/metadata/suites/connected-account/utils/delete-connected-account.util';
+import { saveImapSmtpCaldavAccount } from 'test/integration/metadata/suites/connected-account/utils/save-imap-smtp-caldav-account.util';
+import { updateConfigVariable } from 'test/integration/twenty-config/utils/update-config-variable.util';
+import { appendMessageOverImap } from 'test/integration/utils/append-message-over-imap.util';
+import { findImportedMessageSubjects } from 'test/integration/utils/find-imported-records.util';
+import { getCoreRepository } from 'test/integration/utils/get-core-repository.util';
+import { runMessageChannelSync } from 'test/integration/utils/run-message-channel-sync.util';
+import {
+ type DovecotServer,
+ startDovecotContainer,
+} from 'test/integration/utils/start-dovecot-container.util';
+import {
+ type ImapProxy,
+ startImapProxyEmptyingRev2Search,
+} from 'test/integration/utils/start-imap-proxy-emptying-rev2-search.util';
+
+const PASSWORD = 'dovecot-password';
+const HANDLE = `imap-rev2-empty-search-${randomUUID()}@acme.test`;
+
+describe('IMAP server returning empty searches in IMAP4rev2 mode (integration)', () => {
+ let dovecot: DovecotServer;
+ let proxy: ImapProxy;
+ let connectedAccountId: string;
+ let messageChannelId: string;
+
+ const deliverMessage = (subject: string) =>
+ appendMessageOverImap({
+ host: dovecot.host,
+ port: dovecot.imapPort,
+ username: HANDLE,
+ password: PASSWORD,
+ folder: 'INBOX',
+ from: `sender-${randomUUID()}@external.test`,
+ to: HANDLE,
+ subject,
+ });
+
+ beforeAll(async () => {
+ await updateConfigVariable({
+ input: { key: 'OUTBOUND_HTTP_ALLOWED_INTERNAL_HOSTS', value: ['*'] },
+ });
+
+ dovecot = await startDovecotContainer({ password: PASSWORD });
+
+ proxy = await startImapProxyEmptyingRev2Search({
+ targetHost: dovecot.host,
+ targetPort: dovecot.imapPort,
+ });
+
+ const { data } = await saveImapSmtpCaldavAccount({
+ input: {
+ handle: HANDLE,
+ connectionParameters: {
+ IMAP: {
+ host: proxy.host,
+ port: proxy.port,
+ username: HANDLE,
+ password: PASSWORD,
+ connectionSecurity: EmailConnectionSecurity.NONE,
+ },
+ },
+ },
+ expectToFail: false,
+ });
+
+ connectedAccountId = data.connectedAccountId;
+
+ messageChannelId = (
+ await getCoreRepository<MessageChannelEntity>(
+ MessageChannelEntity,
+ ).findOneByOrFail({ connectedAccountId })
+ ).id;
+ }, 300000);
+
+ afterAll(async () => {
+ await updateConfigVariable({
+ input: { key: 'OUTBOUND_HTTP_ALLOWED_INTERNAL_HOSTS', value: [] },
+ }).catch(() => undefined);
+
+ if (isNonEmptyString(connectedAccountId)) {
+ await deleteConnectedAccount({
+ id: connectedAccountId,
+ expectToFail: false,
+ }).catch(() => undefined);
+ }
+
+ await proxy?.stop().catch(() => undefined);
+ await dovecot?.stop().catch(() => undefined);
+ });
+
+ it('answers searches with no results once IMAP4rev2 is enabled', async () => {
+ await deliverMessage(`IMAP rev2 proxy check ${randomUUID()}`);
+
+ const client = new ImapFlow({
+ host: proxy.host,
+ port: proxy.port,
+ secure: false,
+ auth: { user: HANDLE, pass: PASSWORD },
+ logger: false,
+ });
+
+ await client.connect();
+
+ try {
+ const lock = await client.getMailboxLock('INBOX');
+
+ try {
+ expect(await client.search({ all: true }, { uid: true })).toEqual([]);
+ } finally {
+ lock.release();
+ }
+ } finally {
+ await client.logout();
+ }
+ }, 300000);
+
+ it('imports new mail even though IMAP4rev2 searches come back empty', async () => {
+ const subject = `IMAP rev2 empty search ${randomUUID()}`;
+
+ await deliverMessage(subject);
+ await runMessageChannelSync(messageChannelId);
+
+ expect(await findImportedMessageSubjects([subject])).toEqual([subject]);
+ }, 300000);
+});
diff --git a/packages/twenty-server/test/integration/utils/start-imap-proxy-emptying-rev2-search.util.ts b/packages/twenty-server/test/integration/utils/start-imap-proxy-emptying-rev2-search.util.ts
new file mode 100644
index 00000000000..4a9ae7f00d7
--- /dev/null
+++ b/packages/twenty-server/test/integration/utils/start-imap-proxy-emptying-rev2-search.util.ts
@@ -0,0 +1,113 @@
+import { connect, createServer, type Socket } from 'node:net';
+
+import { isDefined } from 'twenty-shared/utils';
+
+const ENABLE_IMAP4REV2_COMMAND = /\bENABLE\b.*\bIMAP4rev2\b/i;
+const SEARCH_COMMAND = /^(\S+) (UID )?SEARCH\b/i;
+const SEARCH_RESPONSE = /^\* E?SEARCH\b/i;
+const COMPRESS_CAPABILITY = / COMPRESS=DEFLATE\b/g;
+
+type SearchCommand = {
+ tag: string;
+ isUid: boolean;
+};
+
+export type ImapProxy = {
+ host: string;
+ port: number;
+ stop: () => Promise<void>;
+};
+
+const emptyEsearchResponse = ({ tag, isUid }: SearchCommand) =>
+ `* ESEARCH (TAG "${tag}")${isUid ? ' UID' : ''}`;
+
+export const startImapProxyEmptyingRev2Search = async ({
+ targetHost,
+ targetPort,
+}: {
+ targetHost: string;
+ targetPort: number;
+}): Promise<ImapProxy> => {
+ const sockets = new Set<Socket>();
+
+ const server = createServer((client) => {
+ const upstream = connect(targetPort, targetHost);
+ let isRev2Enabled = false;
+ let lastSearchCommand: SearchCommand | undefined;
+ let pendingClientText = '';
+ let pendingServerText = '';
+
+ sockets.add(client);
+ sockets.add(upstream);
+
+ client.on('data', (chunk) => {
+ const lines = (pendingClientText + chunk.toString('latin1')).split(
+ '\r\n',
+ );
+
+ pendingClientText = lines.pop() ?? '';
+
+ for (const line of lines) {
+ if (ENABLE_IMAP4REV2_COMMAND.test(line)) {
+ isRev2Enabled = true;
+ }
+
+ const searchCommand = line.match(SEARCH_COMMAND);
+
+ if (isDefined(searchCommand)) {
+ lastSearchCommand = {
+ tag: searchCommand[1],
+ isUid: isDefined(searchCommand[2]),
+ };
+ }
+ }
+
+ upstream.write(chunk);
+ });
+
+ upstream.on('data', (chunk) => {
+ const lines = (pendingServerText + chunk.toString('latin1')).split(
+ '\r\n',
+ );
+
+ pendingServerText = lines.pop() ?? '';
+
+ const forwarded = lines
+ .map((line) => line.replace(COMPRESS_CAPABILITY, ''))
+ .map((line) =>
+ isRev2Enabled &&
+ isDefined(lastSearchCommand) &&
+ SEARCH_RESPONSE.test(line)
+ ? emptyEsearchResponse(lastSearchCommand)
+ : line,
+ )
+ .map((line) => `${line}\r\n`)
+ .join('');
+
+ client.write(forwarded, 'latin1');
+ });
+
+ client.on('close', () => upstream.destroy());
+ upstream.on('close', () => client.destroy());
+ client.on('error', () => upstream.destroy());
+ upstream.on('error', () => client.destroy());
+ });
+
+ await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
+
+ const address = server.address();
+
+ if (!isDefined(address) || typeof address === 'string') {
+ throw new Error('IMAP proxy is not listening on a TCP port');
+ }
+
+ return {
+ host: '127.0.0.1',
+ port: address.port,
+ stop: () =>
+ new Promise<void>((resolve) => {
+ sockets.forEach((socket) => socket.destroy());
+ server.close(() => resolve());
+ }),
+ };
+};