The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Restore an isolated enterprise signing key for development (#29266)
Félix Malfait2ca8d6b48c05
diff --git a/packages/twenty-server/.env.test b/packages/twenty-server/.env.testindex 3c3e0ec9570..080a8ed7138 100644--- a/packages/twenty-server/.env.test+++ b/packages/twenty-server/.env.test@@ -12,7 +12,7 @@ IS_MULTIWORKSPACE_ENABLED=trueFRONTEND_URL=http://localhost:3001IS_WORKSPACE_CREATION_LIMITED_TO_SERVER_ADMINS=false-ENTERPRISE_VALIDITY_TOKEN=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkZXYtc3Vic2NyaXB0aW9uLWlkIiwic3RhdHVzIjoidmFsaWQiLCJpYXQiOjE3NzMzMDg4MzMsImV4cCI6NDg5NzUxMTIzM30.qhfrW_SV2Y86fWtWXsALlAVhxmMxylUUIefN0fki10Q2NTGGqFVXZrNn2WacJY37yq3m5y4WgwZw34ua6E0ff_YUXsrlY5OHJWHT9DMqKCRn-JujHJnnYp3VHLncy5CvxH5r9mfPFp-5AWe1pYeR1T63sTiejH3sfDrNE357SB7KVti8LCcnsJxEtXB2tRnvyvdun7A-GKoKYEIam-16ZRKKFs6GaWo8ObHdfm8yBt6uK4DZSGPWb644QyWh9FtDxbzJ0ti54DuHSlErLgIp1NNEsMA0MK7zFY7StRaOdt72rxE1ZHwN7e6HhweTU4ORVUPfYkjDFLB2fF7Pa7Kvdg+ENTERPRISE_VALIDITY_TOKEN=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkZXYtc3Vic2NyaXB0aW9uLWlkIiwic3RhdHVzIjoidmFsaWQiLCJpYXQiOjE3NzMzMDg4MzMsImV4cCI6NDg5NzUxMTIzM30.iG0MMApq-ah-DES-39fuKbj4Rd0WdUuNrvcw0b1Z6S25T9YfiiFcsrGDmeuxjvDsO42lmBxTr0-vdjHp55fXY1z4tSAki_o_K3St7q-LxaO6GL-6iEYoTTDnUNcinhtrc44ck_gXRdIRTF2Dv558mlZs4RZFPTk7cTVOlsz2V3SmYJzC6QohcKXlf3eIm66_NLp4CH2u2NNSjJbvAb4ojjuH3PM_uHqEiFpGrbUdNm-3BU3fpGVd8kPwzWLPhVl5VMt7dUFfxMQGnF84f5PHOKFV7CLcoC_rZYqa9K79vC8wuJPe3_ZFP2D-ReYNZ28Bf6Wq9eLwfVBsSNh6DnZ1wQAUTH_GOOGLE_ENABLED=trueAUTH_GOOGLE_CLIENT_ID=mock-google-client-iddiff --git a/packages/twenty-server/src/engine/core-modules/enterprise/constants/enterprise-public-key.constant.ts b/packages/twenty-server/src/engine/core-modules/enterprise/constants/enterprise-public-key.constant.tsindex f0ec1cb3c8a..0d6e9e21930 100644--- a/packages/twenty-server/src/engine/core-modules/enterprise/constants/enterprise-public-key.constant.ts+++ b/packages/twenty-server/src/engine/core-modules/enterprise/constants/enterprise-public-key.constant.ts@@ -10,7 +10,17 @@ EuZs6PhKiH6l3sXY6kocC0ZV25rFhHgOChVA91BE8a+Wj0MtGGI/UL/b21G3zsr76wIDAQAB-----END PUBLIC KEY-----`;-export const ENTERPRISE_JWT_DEV_PUBLIC_KEY = `-----BEGIN PUBLIC KEY-----+export const ENTERPRISE_JWT_DEV_PUBLIC_KEYS = [+ `-----BEGIN PUBLIC KEY-----+MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqKswkzE4V6LMLvoIB2G3+98EHrkwP789h97lnqBXPhLp4xP4lZurXRcD/ADx6Flyhu1YSVy1XolboUmC8IIgV+wYiZXAc2FVoI11An4RYp5ZREq+lHpgIQ6GpPr1eTWiuB5xbwP2DLq3g+04vL3267+Q9CjEoBCN2QaaH+9B8TUiYMhRl07nCrX3zOtfiJcYgjhqT5YIQtAPnK+dqJhaICd+yLVEbPBvCbURzepddxoJO8o7+PWhM43zpU8QOEF5I4CUu1goZG59nr7musiKQaLi+XYTI35gIrM0BXW4b9ZShz/szEPLpSlBBS31IPl2RU28FlkvJ266X/j3fpmW+wsbV+aQIDAQAB+-----END PUBLIC KEY-----`,+ `-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsHEWc84t399KlYyhRkBOQCXE8/HE/TKvMk35ccoUeZXnBNhuGpWcHuM6+6ekHiToT239hkBy+Bk/Ybd6wVrsVn0Vc0KarRsmeOrJu+sVREL5AUWt0gitpBoNeBzdfgW8dzdyVKDSCUNvXzEOQKkT@@ -18,8 +28,9 @@ Vn0Vc0KarRsmeOrJu+sVREL5AUWt0gitpBoNeBzdfgW8dzdyVKDSCUNvXzEOQKkTDmj1SnUz4yy11EeY48+wTguk9WBFjOsA/1Dnc6jL/MN8zH77xRlIs/iPjBriPhCNnjn0rDVHHdat+3NqKlSbFcQPVzYicxDRXaakJ2IEJhxpEoOFWHJ9bNQKudRqfaoFBQIDAQAB------END PUBLIC KEY-----`;+-----END PUBLIC KEY-----`,+] as const;// signed with ENTERPRISE_JWT_DEV_PRIVATE_KEY (expires 2125)export const ENTERPRISE_DEV_VALIDITY_TOKEN =- 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkZXYtc3Vic2NyaXB0aW9uLWlkIiwic3RhdHVzIjoidmFsaWQiLCJpYXQiOjE3NzMzMDg4MzMsImV4cCI6NDg5NzUxMTIzM30.qhfrW_SV2Y86fWtWXsALlAVhxmMxylUUIefN0fki10Q2NTGGqFVXZrNn2WacJY37yq3m5y4WgwZw34ua6E0ff_YUXsrlY5OHJWHT9DMqKCRn-JujHJnnYp3VHLncy5CvxH5r9mfPFp-5AWe1pYeR1T63sTiejH3sfDrNE357SB7KVti8LCcnsJxEtXB2tRnvyvdun7A-GKoKYEIam-16ZRKKFs6GaWo8ObHdfm8yBt6uK4DZSGPWb644QyWh9FtDxbzJ0ti54DuHSlErLgIp1NNEsMA0MK7zFY7StRaOdt72rxE1ZHwN7e6HhweTU4ORVUPfYkjDFLB2fF7Pa7Kvdg';+ 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkZXYtc3Vic2NyaXB0aW9uLWlkIiwic3RhdHVzIjoidmFsaWQiLCJpYXQiOjE3NzMzMDg4MzMsImV4cCI6NDg5NzUxMTIzM30.iG0MMApq-ah-DES-39fuKbj4Rd0WdUuNrvcw0b1Z6S25T9YfiiFcsrGDmeuxjvDsO42lmBxTr0-vdjHp55fXY1z4tSAki_o_K3St7q-LxaO6GL-6iEYoTTDnUNcinhtrc44ck_gXRdIRTF2Dv558mlZs4RZFPTk7cTVOlsz2V3SmYJzC6QohcKXlf3eIm66_NLp4CH2u2NNSjJbvAb4ojjuH3PM_uHqEiFpGrbUdNm-3BU3fpGVd8kPwzWLPhVl5VMt7dUFfxMQGnF84f5PHOKFV7CLcoC_rZYqa9K79vC8wuJPe3_ZFP2D-ReYNZ28Bf6Wq9eLwfVBsSNh6DnZ1wQ';diff --git a/packages/twenty-server/src/engine/core-modules/enterprise/services/enterprise-plan.service.ts b/packages/twenty-server/src/engine/core-modules/enterprise/services/enterprise-plan.service.tsindex d001a025e8a..f271f019306 100644--- a/packages/twenty-server/src/engine/core-modules/enterprise/services/enterprise-plan.service.ts+++ b/packages/twenty-server/src/engine/core-modules/enterprise/services/enterprise-plan.service.ts@@ -3,8 +3,6 @@import { Injectable, Logger, type OnModuleInit } from '@nestjs/common';import { InjectRepository } from '@nestjs/typeorm';-import * as crypto from 'crypto';-import { isNonEmptyString } from '@sniptt/guards';import { ENTERPRISE_INSTANCE_TYPE } from 'twenty-shared/constants';import { isDefined } from 'twenty-shared/utils';@@ -15,10 +13,6 @@ import {AppTokenEntity,AppTokenType,} from 'src/engine/core-modules/app-token/app-token.entity';-import {- ENTERPRISE_JWT_DEV_PUBLIC_KEY,- ENTERPRISE_JWT_PUBLIC_KEY,-} from 'src/engine/core-modules/enterprise/constants/enterprise-public-key.constant';import {EnterpriseException,EnterpriseExceptionCode,@@ -30,7 +24,7 @@ import {type EnterpriseValidityPayload,} from 'src/engine/core-modules/enterprise/types/organization-key-payload.type';import { isValidityTokenReloadDue } from 'src/engine/core-modules/enterprise/utils/is-validity-token-reload-due.util';-import { NodeEnvironment } from 'src/engine/core-modules/twenty-config/interfaces/node-environment.interface';+import { verifyEnterpriseJwt } from 'src/engine/core-modules/enterprise/utils/verify-enterprise-jwt.util';import {ConfigVariableException,ConfigVariableExceptionCode,@@ -727,65 +721,12 @@ export class EnterprisePlanService implements OnModuleInit {}// Development and tests try both keys so production keys also work locally.- private getPublicKeysToTry(): string[] {- const nodeEnv = this.twentyConfigService.get('NODE_ENV');-- if (- nodeEnv === NodeEnvironment.DEVELOPMENT ||- nodeEnv === NodeEnvironment.TEST- ) {- return [ENTERPRISE_JWT_PUBLIC_KEY, ENTERPRISE_JWT_DEV_PUBLIC_KEY];- }-- return [ENTERPRISE_JWT_PUBLIC_KEY];- }-- private verifyJwt<T extends Record<string, unknown>>(+ private verifyJwt<TPayload extends Record<string, unknown>>(token: string,- ): T | null {- try {- const parts = token.split('.');-- if (parts.length !== 3) {- return null;- }-- const [encodedHeader, encodedPayload, signature] = parts;- const signingInput = `${encodedHeader}.${encodedPayload}`;-- const signatureBuffer = Buffer.from(- signature.replace(/-/g, '+').replace(/_/g, '/') +- '='.repeat((4 - (signature.length % 4)) % 4),- 'base64',- );-- const publicKeys = this.getPublicKeysToTry();-- for (const publicKey of publicKeys) {- const isValid = crypto.verify(- 'sha256',- Buffer.from(signingInput),- {- key: publicKey,- padding: crypto.constants.RSA_PKCS1_PADDING,- },- signatureBuffer,- );-- if (isValid) {- const payloadStr = Buffer.from(- encodedPayload.replace(/-/g, '+').replace(/_/g, '/') +- '='.repeat((4 - (encodedPayload.length % 4)) % 4),- 'base64',- ).toString('utf-8');-- return JSON.parse(payloadStr) as T;- }- }-- return null;- } catch {- return null;- }+ ): TPayload | null {+ return verifyEnterpriseJwt<TPayload>({+ token,+ nodeEnvironment: this.twentyConfigService.get('NODE_ENV'),+ });}}diff --git a/packages/twenty-server/src/engine/core-modules/enterprise/utils/__tests__/verify-enterprise-jwt.util.spec.ts b/packages/twenty-server/src/engine/core-modules/enterprise/utils/__tests__/verify-enterprise-jwt.util.spec.tsnew file mode 100644index 00000000000..1320dd0bd92--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/enterprise/utils/__tests__/verify-enterprise-jwt.util.spec.ts@@ -0,0 +1,44 @@+/* @license Enterprise */++import { ENTERPRISE_DEV_VALIDITY_TOKEN } from 'src/engine/core-modules/enterprise/constants/enterprise-public-key.constant';+import { verifyEnterpriseJwt } from 'src/engine/core-modules/enterprise/utils/verify-enterprise-jwt.util';+import { NodeEnvironment } from 'src/engine/core-modules/twenty-config/interfaces/node-environment.interface';++const PREVIOUS_DEV_VALIDITY_TOKEN =+ 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJkZXYtc3Vic2NyaXB0aW9uLWlkIiwic3RhdHVzIjoidmFsaWQiLCJpYXQiOjE3NzMzMDg4MzMsImV4cCI6NDg5NzUxMTIzM30.qhfrW_SV2Y86fWtWXsALlAVhxmMxylUUIefN0fki10Q2NTGGqFVXZrNn2WacJY37yq3m5y4WgwZw34ua6E0ff_YUXsrlY5OHJWHT9DMqKCRn-JujHJnnYp3VHLncy5CvxH5r9mfPFp-5AWe1pYeR1T63sTiejH3sfDrNE357SB7KVti8LCcnsJxEtXB2tRnvyvdun7A-GKoKYEIam-16ZRKKFs6GaWo8ObHdfm8yBt6uK4DZSGPWb644QyWh9FtDxbzJ0ti54DuHSlErLgIp1NNEsMA0MK7zFY7StRaOdt72rxE1ZHwN7e6HhweTU4ORVUPfYkjDFLB2fF7Pa7Kvdg';++describe.each([+ {+ nodeEnvironment: NodeEnvironment.DEVELOPMENT,+ acceptsDevelopmentTokens: true,+ },+ { nodeEnvironment: NodeEnvironment.TEST, acceptsDevelopmentTokens: true },+ {+ nodeEnvironment: NodeEnvironment.PRODUCTION,+ acceptsDevelopmentTokens: false,+ },+])(+ 'enterprise license verification in $nodeEnvironment',+ ({ nodeEnvironment, acceptsDevelopmentTokens }) => {+ it.each([+ { generation: 'previous', token: PREVIOUS_DEV_VALIDITY_TOKEN },+ { generation: 'current', token: ENTERPRISE_DEV_VALIDITY_TOKEN },+ ])(+ '$generation development token has the expected trust boundary',+ ({ token }) => {+ const payload = verifyEnterpriseJwt({ token, nodeEnvironment });++ if (acceptsDevelopmentTokens) {+ expect(payload).toEqual({+ sub: 'dev-subscription-id',+ status: 'valid',+ iat: 1773308833,+ exp: 4897511233,+ });+ } else {+ expect(payload).toBeNull();+ }+ },+ );+ },+);diff --git a/packages/twenty-server/src/engine/core-modules/enterprise/utils/verify-enterprise-jwt.util.ts b/packages/twenty-server/src/engine/core-modules/enterprise/utils/verify-enterprise-jwt.util.tsnew file mode 100644index 00000000000..3ffe32d32a4--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/enterprise/utils/verify-enterprise-jwt.util.ts@@ -0,0 +1,68 @@+/* @license Enterprise */++import * as crypto from 'crypto';++import {+ ENTERPRISE_JWT_DEV_PUBLIC_KEYS,+ ENTERPRISE_JWT_PUBLIC_KEY,+} from 'src/engine/core-modules/enterprise/constants/enterprise-public-key.constant';+import { NodeEnvironment } from 'src/engine/core-modules/twenty-config/interfaces/node-environment.interface';++type VerifyEnterpriseJwtArgs = {+ token: string;+ nodeEnvironment: NodeEnvironment;+};++export const verifyEnterpriseJwt = <TPayload extends Record<string, unknown>>({+ token,+ nodeEnvironment,+}: VerifyEnterpriseJwtArgs): TPayload | null => {+ try {+ const parts = token.split('.');++ if (parts.length !== 3) {+ return null;+ }++ const [encodedHeader, encodedPayload, signature] = parts;+ const signingInput = `${encodedHeader}.${encodedPayload}`;++ const signatureBuffer = Buffer.from(+ signature.replace(/-/g, '+').replace(/_/g, '/') ++ '='.repeat((4 - (signature.length % 4)) % 4),+ 'base64',+ );++ const publicKeys =+ nodeEnvironment === NodeEnvironment.DEVELOPMENT ||+ nodeEnvironment === NodeEnvironment.TEST+ ? [ENTERPRISE_JWT_PUBLIC_KEY, ...ENTERPRISE_JWT_DEV_PUBLIC_KEYS]+ : [ENTERPRISE_JWT_PUBLIC_KEY];++ for (const publicKey of publicKeys) {+ const isValid = crypto.verify(+ 'sha256',+ Buffer.from(signingInput),+ {+ key: publicKey,+ padding: crypto.constants.RSA_PKCS1_PADDING,+ },+ signatureBuffer,+ );++ if (isValid) {+ const payloadStr = Buffer.from(+ encodedPayload.replace(/-/g, '+').replace(/_/g, '/') ++ '='.repeat((4 - (encodedPayload.length % 4)) % 4),+ 'base64',+ ).toString('utf-8');++ return JSON.parse(payloadStr) as TPayload;+ }+ }++ return null;+ } catch {+ return null;+ }+};