The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(accounts): trim whitespace in IMAP/SMTP/CalDAV server and username fields (#29091) (#29163)
Mahmoud Ghazy08a788cdc0a9
diff --git a/packages/twenty-front/src/modules/settings/accounts/validation-schemas/__tests__/connectionImapSmtpCalDav.test.ts b/packages/twenty-front/src/modules/settings/accounts/validation-schemas/__tests__/connectionImapSmtpCalDav.test.tsnew file mode 100644index 00000000000..e47a3000465--- /dev/null+++ b/packages/twenty-front/src/modules/settings/accounts/validation-schemas/__tests__/connectionImapSmtpCalDav.test.ts@@ -0,0 +1,73 @@+import {+ getConnectionImapSmtpCalDavSchema,+ getConnectionImapSmtpCalDavUpdateSchema,+} from '@/settings/accounts/validation-schemas/connectionImapSmtpCalDav';++describe('connectionImapSmtpCalDav', () => {+ describe('getConnectionImapSmtpCalDavSchema', () => {+ it('should trim leading and trailing whitespace from host and username', () => {+ const schema = getConnectionImapSmtpCalDavSchema();++ const input = {+ name: ' Work Account ',+ handle: '[email protected]',+ IMAP: {+ host: ' imap.example.com ',+ port: 993,+ username: ' [email protected] ',+ password: 'password123',+ connectionSecurity: 'SSL_TLS' as const,+ },+ };++ const result = schema.parse(input);++ expect(result.name).toBe('Work Account');+ expect(result.IMAP?.host).toBe('imap.example.com');+ expect(result.IMAP?.username).toBe('[email protected]');+ expect(result.IMAP?.password).toBe('password123');+ });++ it('should preserve whitespace inside passwords', () => {+ const schema = getConnectionImapSmtpCalDavSchema();++ const input = {+ name: 'Work Account',+ handle: '[email protected]',+ SMTP: {+ host: 'smtp.example.com',+ port: 587,+ password: ' pass word ',+ connectionSecurity: 'STARTTLS' as const,+ },+ };++ const result = schema.parse(input);++ expect(result.SMTP?.password).toBe(' pass word ');+ });+ });++ describe('getConnectionImapSmtpCalDavUpdateSchema', () => {+ it('should trim leading and trailing whitespace on update', () => {+ const schema = getConnectionImapSmtpCalDavUpdateSchema();++ const input = {+ name: ' Updated Account ',+ handle: '[email protected]',+ CALDAV: {+ host: ' caldav.example.com ',+ port: 443,+ username: ' caldav-user ',+ connectionSecurity: 'SSL_TLS' as const,+ },+ };++ const result = schema.parse(input);++ expect(result.name).toBe('Updated Account');+ expect(result.CALDAV?.host).toBe('caldav.example.com');+ expect(result.CALDAV?.username).toBe('caldav-user');+ });+ });+});diff --git a/packages/twenty-front/src/modules/settings/accounts/validation-schemas/connectionImapSmtpCalDav.ts b/packages/twenty-front/src/modules/settings/accounts/validation-schemas/connectionImapSmtpCalDav.tsindex 0f8f727e00f..a02944d042a 100644--- a/packages/twenty-front/src/modules/settings/accounts/validation-schemas/connectionImapSmtpCalDav.ts+++ b/packages/twenty-front/src/modules/settings/accounts/validation-schemas/connectionImapSmtpCalDav.ts@@ -11,9 +11,9 @@ import {const getConnectionParametersSchema = () =>z.object({- host: z.string().default(''),+ host: z.string().trim().default(''),port: z.int().nullable().default(null),- username: z.string().optional(),+ username: z.string().trim().optional(),password: z.string().default(''),connectionSecurity: z.enum(['NONE', 'STARTTLS', 'SSL_TLS'])diff --git a/packages/twenty-server/src/engine/core-modules/imap-smtp-caldav-connection/schemas/__tests__/connection-parameters.schema.spec.ts b/packages/twenty-server/src/engine/core-modules/imap-smtp-caldav-connection/schemas/__tests__/connection-parameters.schema.spec.tsnew file mode 100644index 00000000000..1c9eac00cdd--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/imap-smtp-caldav-connection/schemas/__tests__/connection-parameters.schema.spec.ts@@ -0,0 +1,62 @@+import { EmailConnectionSecurity } from 'src/engine/core-modules/imap-smtp-caldav-connection/enums/email-connection-security.enum';+import { connectionParametersUpdateSchema } from 'src/engine/core-modules/imap-smtp-caldav-connection/schemas/connection-parameters-update.schema';+import { connectionParametersSchema } from 'src/engine/core-modules/imap-smtp-caldav-connection/schemas/connection-parameters.schema';++describe('connectionParametersSchema', () => {+ it('should trim leading and trailing whitespace from host and username', () => {+ const input = {+ host: ' imap.example.com ',+ port: 993,+ username: ' [email protected] ',+ password: 'password123',+ connectionSecurity: EmailConnectionSecurity.SSL_TLS,+ };++ const result = connectionParametersSchema.parse(input);++ expect(result.host).toBe('imap.example.com');+ expect(result.username).toBe('[email protected]');+ expect(result.password).toBe('password123');+ });++ it('should preserve whitespace in password', () => {+ const input = {+ host: 'imap.example.com',+ port: 993,+ password: ' pass word with spaces ',+ connectionSecurity: EmailConnectionSecurity.SSL_TLS,+ };++ const result = connectionParametersSchema.parse(input);++ expect(result.password).toBe(' pass word with spaces ');+ });++ it('should reject whitespace-only host', () => {+ const input = {+ host: ' ',+ port: 993,+ password: 'password123',+ connectionSecurity: EmailConnectionSecurity.SSL_TLS,+ };++ expect(() => connectionParametersSchema.parse(input)).toThrow();+ });+});++describe('connectionParametersUpdateSchema', () => {+ it('should trim leading and trailing whitespace from host and username on update', () => {+ const input = {+ host: ' smtp.example.com ',+ port: 587,+ username: ' smtp-user ',+ connectionSecurity: EmailConnectionSecurity.STARTTLS,+ };++ const result = connectionParametersUpdateSchema.parse(input);++ expect(result.host).toBe('smtp.example.com');+ expect(result.username).toBe('smtp-user');+ expect(result.password).toBeUndefined();+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/imap-smtp-caldav-connection/schemas/connection-parameters.schema.ts b/packages/twenty-server/src/engine/core-modules/imap-smtp-caldav-connection/schemas/connection-parameters.schema.tsindex 0bee7e33ee1..c545d0d9057 100644--- a/packages/twenty-server/src/engine/core-modules/imap-smtp-caldav-connection/schemas/connection-parameters.schema.ts+++ b/packages/twenty-server/src/engine/core-modules/imap-smtp-caldav-connection/schemas/connection-parameters.schema.ts@@ -3,9 +3,9 @@ import { plaintextStringSchema } from 'src/engine/core-modules/secret-encryptionimport { z } from 'zod';export const connectionParametersSchema = z.object({- host: z.string().min(1, 'Host is required'),+ host: z.string().trim().min(1, 'Host is required'),port: z.int().positive('Port must be a positive number'),- username: z.string().optional(),+ username: z.string().trim().optional(),password: plaintextStringSchema.min(1, 'Password is required'),connectionSecurity: z.nativeEnum(EmailConnectionSecurity)diff --git a/packages/twenty-server/src/engine/core-modules/secure-http-client/utils/__tests__/resolve-and-validate-hostname.util.spec.ts b/packages/twenty-server/src/engine/core-modules/secure-http-client/utils/__tests__/resolve-and-validate-hostname.util.spec.tsindex e825876a1ca..e31e163afa6 100644--- a/packages/twenty-server/src/engine/core-modules/secure-http-client/utils/__tests__/resolve-and-validate-hostname.util.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/secure-http-client/utils/__tests__/resolve-and-validate-hostname.util.spec.ts@@ -16,6 +16,32 @@ describe('resolveAndValidateHostname', () => {expect(mockDnsLookup).toHaveBeenCalledWith('imap.fastmail.com');});+ it('should trim leading and trailing whitespace from plain hostname before resolving', async () => {+ mockDnsLookup.mockResolvedValue({ address: '93.184.216.34', family: 4 });++ await resolveAndValidateHostname(+ ' imap.fastmail.com ',+ [],+ mockDnsLookup,+ );++ expect(mockDnsLookup).toHaveBeenCalledTimes(1);+ expect(mockDnsLookup).toHaveBeenCalledWith('imap.fastmail.com');+ });++ it('should trim leading and trailing whitespace from a full URL before resolving', async () => {+ mockDnsLookup.mockResolvedValue({ address: '93.184.216.34', family: 4 });++ await resolveAndValidateHostname(+ ' https://caldav.example.com:8443/dav/principals ',+ [],+ mockDnsLookup,+ );++ expect(mockDnsLookup).toHaveBeenCalledTimes(1);+ expect(mockDnsLookup).toHaveBeenCalledWith('caldav.example.com');+ });+it('should extract hostname from a full URL before resolving', async () => {mockDnsLookup.mockResolvedValue({ address: '93.184.216.34', family: 4 });diff --git a/packages/twenty-server/src/engine/core-modules/secure-http-client/utils/resolve-and-validate-hostname.util.ts b/packages/twenty-server/src/engine/core-modules/secure-http-client/utils/resolve-and-validate-hostname.util.tsindex 7c1b43c976a..5dad349117c 100644--- a/packages/twenty-server/src/engine/core-modules/secure-http-client/utils/resolve-and-validate-hostname.util.ts+++ b/packages/twenty-server/src/engine/core-modules/secure-http-client/utils/resolve-and-validate-hostname.util.ts@@ -7,14 +7,15 @@ export const resolveAndValidateHostname = async (allowedInternalHosts: string[] = [],dnsLookup: typeof dns.lookup = dns.lookup,): Promise<string> => {+ const trimmedHostnameOrUrl = hostnameOrUrl.trim();let hostname: string;try {- const url = new URL(hostnameOrUrl);+ const url = new URL(trimmedHostnameOrUrl);hostname = url.hostname;} catch {- hostname = hostnameOrUrl;+ hostname = trimmedHostnameOrUrl;}const isBlockedIp = getIsBlockedIp(hostname, allowedInternalHosts);