The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
perf(server): memoize credentialed CORS origins on their config inputs (#27154)
Charles Bocheta4722c3f3d77
diff --git a/packages/twenty-server/src/engine/core-modules/user-session/utils/__tests__/resolve-allowed-credentialed-origins.util.spec.ts b/packages/twenty-server/src/engine/core-modules/user-session/utils/__tests__/resolve-allowed-credentialed-origins.util.spec.tsnew file mode 100644index 00000000000..be344efd860--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/user-session/utils/__tests__/resolve-allowed-credentialed-origins.util.spec.ts@@ -0,0 +1,99 @@+import { ConfigVariables } from 'src/engine/core-modules/twenty-config/config-variables';+import { NodeEnvironment } from 'src/engine/core-modules/twenty-config/interfaces/node-environment.interface';+import { type TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';+import { resolveAllowedCredentialedOrigins } from 'src/engine/core-modules/user-session/utils/resolve-allowed-credentialed-origins.util';++type AllowedOriginsConfigVariables = Pick<+ ConfigVariables,+ 'SERVER_URL' | 'FRONTEND_URL' | 'AUTH_COOKIE_ALLOWED_ORIGINS' | 'NODE_ENV'+>;++const PRODUCTION_CONFIG_VARIABLES: AllowedOriginsConfigVariables = {+ SERVER_URL: 'https://api.twenty.com',+ FRONTEND_URL: 'https://app.twenty.com',+ AUTH_COOKIE_ALLOWED_ORIGINS: '',+ NODE_ENV: NodeEnvironment.PRODUCTION,+};++const buildTwentyConfigService = (+ allowedOriginsConfigVariables: AllowedOriginsConfigVariables,+): Pick<TwentyConfigService, 'get'> => {+ const configVariables = Object.assign(+ new ConfigVariables(),+ allowedOriginsConfigVariables,+ );++ return {+ get: (key) => configVariables[key],+ };+};++describe('resolveAllowedCredentialedOrigins', () => {+ it('should allow the server, frontend and explicit origins', () => {+ const allowedOrigins = resolveAllowedCredentialedOrigins(+ buildTwentyConfigService({+ ...PRODUCTION_CONFIG_VARIABLES,+ SERVER_URL: 'https://api.twenty.com/graphql',+ FRONTEND_URL: 'https://App.twenty.com',+ AUTH_COOKIE_ALLOWED_ORIGINS: ' https://other.example , file:///tmp',+ }),+ );++ expect([...allowedOrigins]).toEqual([+ 'https://api.twenty.com',+ 'https://app.twenty.com',+ 'https://other.example',+ ]);+ });++ it('should skip derived loopback origins in production only', () => {+ const loopbackConfigVariables: AllowedOriginsConfigVariables = {+ SERVER_URL: 'http://localhost:3000',+ FRONTEND_URL: 'http://localhost:3001',+ AUTH_COOKIE_ALLOWED_ORIGINS: 'http://localhost:3001',+ NODE_ENV: NodeEnvironment.PRODUCTION,+ };++ const productionAllowedOrigins = resolveAllowedCredentialedOrigins(+ buildTwentyConfigService(loopbackConfigVariables),+ );+ const developmentAllowedOrigins = resolveAllowedCredentialedOrigins(+ buildTwentyConfigService({+ ...loopbackConfigVariables,+ NODE_ENV: NodeEnvironment.DEVELOPMENT,+ }),+ );++ expect([...productionAllowedOrigins]).toEqual(['http://localhost:3001']);+ expect([...developmentAllowedOrigins]).toEqual([+ 'http://localhost:3000',+ 'http://localhost:3001',+ ]);+ });++ it('should reuse the resolved origins while the config is unchanged', () => {+ const twentyConfigService = buildTwentyConfigService(+ PRODUCTION_CONFIG_VARIABLES,+ );++ expect(resolveAllowedCredentialedOrigins(twentyConfigService)).toBe(+ resolveAllowedCredentialedOrigins(twentyConfigService),+ );+ });++ it('should resolve the origins again when the config changes', () => {+ const allowedOriginsBeforeChange = resolveAllowedCredentialedOrigins(+ buildTwentyConfigService(PRODUCTION_CONFIG_VARIABLES),+ );+ const allowedOriginsAfterChange = resolveAllowedCredentialedOrigins(+ buildTwentyConfigService({+ ...PRODUCTION_CONFIG_VARIABLES,+ AUTH_COOKIE_ALLOWED_ORIGINS: 'https://other.example',+ }),+ );++ expect(allowedOriginsAfterChange).not.toBe(allowedOriginsBeforeChange);+ expect(allowedOriginsBeforeChange.has('https://other.example')).toBe(false);+ expect(allowedOriginsAfterChange.has('https://other.example')).toBe(true);+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/user-session/utils/resolve-allowed-credentialed-origins.util.ts b/packages/twenty-server/src/engine/core-modules/user-session/utils/resolve-allowed-credentialed-origins.util.tsindex 821e03e35c6..8af09e7b813 100644--- a/packages/twenty-server/src/engine/core-modules/user-session/utils/resolve-allowed-credentialed-origins.util.ts+++ b/packages/twenty-server/src/engine/core-modules/user-session/utils/resolve-allowed-credentialed-origins.util.ts@@ -1,4 +1,5 @@import { isNonEmptyString } from '@sniptt/guards';+import { isDefined } from 'twenty-shared/utils';import { NodeEnvironment } from 'src/engine/core-modules/twenty-config/interfaces/node-environment.interface';import { type TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';@@ -51,24 +52,44 @@ const isLoopbackOrigin = (origin: string): boolean => {}};-export const resolveAllowedCredentialedOrigins = (- twentyConfigService: TwentyConfigService,-): Set<string> => {+type AllowedOriginsSettings = {+ serverUrl: string;+ frontendUrl: string;+ authCookieAllowedOrigins: string;+ nodeEnvironment: NodeEnvironment;+};++let cachedAllowedOriginsSettings: AllowedOriginsSettings | undefined;+let cachedComputedAllowedOrigins: ReadonlySet<string> | undefined;++const isSameAllowedOriginsSettings = (+ allowedOriginsSettings: AllowedOriginsSettings,+ otherAllowedOriginsSettings: AllowedOriginsSettings,+): boolean =>+ allowedOriginsSettings.serverUrl === otherAllowedOriginsSettings.serverUrl &&+ allowedOriginsSettings.frontendUrl ===+ otherAllowedOriginsSettings.frontendUrl &&+ allowedOriginsSettings.authCookieAllowedOrigins ===+ otherAllowedOriginsSettings.authCookieAllowedOrigins &&+ allowedOriginsSettings.nodeEnvironment ===+ otherAllowedOriginsSettings.nodeEnvironment;++const computeAllowedCredentialedOrigins = ({+ serverUrl,+ frontendUrl,+ authCookieAllowedOrigins,+ nodeEnvironment,+}: AllowedOriginsSettings): ReadonlySet<string> => {const allowedOrigins = new Set<string>();- const derivedUrls = [- twentyConfigService.get('SERVER_URL'),- twentyConfigService.get('FRONTEND_URL'),- ];+ const derivedUrls = [serverUrl, frontendUrl];- const explicitUrls = twentyConfigService- .get('AUTH_COOKIE_ALLOWED_ORIGINS')+ const explicitUrls = authCookieAllowedOrigins.split(',').map((allowedOrigin) => allowedOrigin.trim());// SERVER_URL defaults to http://localhost:3000, which would hand any local page on that port a credentialed origin.- const isProduction =- twentyConfigService.get('NODE_ENV') === NodeEnvironment.PRODUCTION;+ const isProduction = nodeEnvironment === NodeEnvironment.PRODUCTION;for (const candidateUrl of [...derivedUrls, ...explicitUrls]) {if (!isNonEmptyString(candidateUrl)) {@@ -94,3 +115,36 @@ export const resolveAllowedCredentialedOrigins = (return allowedOrigins;};++export const resolveAllowedCredentialedOrigins = (+ twentyConfigService: Pick<TwentyConfigService, 'get'>,+): ReadonlySet<string> => {+ const allowedOriginsSettings: AllowedOriginsSettings = {+ serverUrl: twentyConfigService.get('SERVER_URL'),+ frontendUrl: twentyConfigService.get('FRONTEND_URL'),+ authCookieAllowedOrigins: twentyConfigService.get(+ 'AUTH_COOKIE_ALLOWED_ORIGINS',+ ),+ nodeEnvironment: twentyConfigService.get('NODE_ENV'),+ };++ if (+ isDefined(cachedAllowedOriginsSettings) &&+ isDefined(cachedComputedAllowedOrigins) &&+ isSameAllowedOriginsSettings(+ cachedAllowedOriginsSettings,+ allowedOriginsSettings,+ )+ ) {+ return cachedComputedAllowedOrigins;+ }++ const allowedOrigins = computeAllowedCredentialedOrigins(+ allowedOriginsSettings,+ );++ cachedAllowedOriginsSettings = allowedOriginsSettings;+ cachedComputedAllowedOrigins = allowedOrigins;++ return allowedOrigins;+};