The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
fix(server): preserve OAuth-only caller reach in application targeting (#27120)
Weikod43ca20f0845
diff --git a/packages/twenty-server/src/engine/core-modules/application/application.resolver.ts b/packages/twenty-server/src/engine/core-modules/application/application.resolver.tsindex b8ce80e226e..1f408c28381 100644--- a/packages/twenty-server/src/engine/core-modules/application/application.resolver.ts+++ b/packages/twenty-server/src/engine/core-modules/application/application.resolver.ts@@ -16,6 +16,7 @@ import {import { ApplicationDTO } from 'src/engine/core-modules/application/dtos/application.dto';import { type FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';import { buildPublicAssetLogoUrl } from 'src/engine/core-modules/application/utils/build-public-asset-logo-url.util';+import { canCallerReachApplication } from 'src/engine/core-modules/application/utils/can-caller-reach-application.util';import { ForbiddenError } from 'src/engine/core-modules/graphql/utils/graphql-errors.util';import { SdkClientChecksumsDTO } from 'src/engine/core-modules/sdk-client/dtos/sdk-client-checksums.dto';import { getInstalledSdkMetadataModule } from 'src/engine/core-modules/sdk-client/utils/get-installed-sdk-metadata-module.util';@@ -122,8 +123,10 @@ export class ApplicationResolver {callingApplication: FlatApplication | undefined,): ApplicationVariableEntity[] | undefined {if (- isDefined(callingApplication) &&- callingApplication.id !== application.id+ !canCallerReachApplication({+ callingApplication,+ applicationId: application.id,+ })) {throw new ForbiddenError(new ApplicationException(diff --git a/packages/twenty-server/src/engine/core-modules/application/utils/__tests__/can-caller-reach-application.util.spec.ts b/packages/twenty-server/src/engine/core-modules/application/utils/__tests__/can-caller-reach-application.util.spec.tsnew file mode 100644index 00000000000..b00014b13f7--- /dev/null+++ b/packages/twenty-server/src/engine/core-modules/application/utils/__tests__/can-caller-reach-application.util.spec.ts@@ -0,0 +1,55 @@+import { ApplicationRegistrationSourceType } from 'src/engine/core-modules/application/application-registration/enums/application-registration-source-type.enum';+import { canCallerReachApplication } from 'src/engine/core-modules/application/utils/can-caller-reach-application.util';++const CALLING_APPLICATION_ID = 'b1a4c0d2-6e1f-4a6b-9c1d-0f2e3a4b5c6d';++const OTHER_APPLICATION_ID = 'e5f6a7b8-c9d0-4e1f-a2b3-c4d5e6f7a8b9';++describe('canCallerReachApplication', () => {+ describe.each([+ ApplicationRegistrationSourceType.LOCAL,+ ApplicationRegistrationSourceType.NPM,+ ApplicationRegistrationSourceType.TARBALL,+ ])('with a %s application caller', (sourceType) => {+ const callingApplication = { id: CALLING_APPLICATION_ID, sourceType };++ it('should reach its own application', () => {+ expect(+ canCallerReachApplication({+ callingApplication,+ applicationId: CALLING_APPLICATION_ID,+ }),+ ).toBe(true);+ });++ it('should not reach another application', () => {+ expect(+ canCallerReachApplication({+ callingApplication,+ applicationId: OTHER_APPLICATION_ID,+ }),+ ).toBe(false);+ });+ });++ it('should let an OAuth-only application caller reach another application', () => {+ expect(+ canCallerReachApplication({+ callingApplication: {+ id: CALLING_APPLICATION_ID,+ sourceType: ApplicationRegistrationSourceType.OAUTH_ONLY,+ },+ applicationId: OTHER_APPLICATION_ID,+ }),+ ).toBe(true);+ });++ it('should let a caller without an application reach any application', () => {+ expect(+ canCallerReachApplication({+ callingApplication: undefined,+ applicationId: OTHER_APPLICATION_ID,+ }),+ ).toBe(true);+ });+});diff --git a/packages/twenty-server/src/engine/core-modules/application/utils/__tests__/resolve-target-application-or-throw.util.spec.ts b/packages/twenty-server/src/engine/core-modules/application/utils/__tests__/resolve-target-application-or-throw.util.spec.tsindex 6ad8ac1a77b..444ccf8f050 100644--- a/packages/twenty-server/src/engine/core-modules/application/utils/__tests__/resolve-target-application-or-throw.util.spec.ts+++ b/packages/twenty-server/src/engine/core-modules/application/utils/__tests__/resolve-target-application-or-throw.util.spec.ts@@ -1,3 +1,4 @@+import { ApplicationRegistrationSourceType } from 'src/engine/core-modules/application/application-registration/enums/application-registration-source-type.enum';import { ApplicationExceptionCode } from 'src/engine/core-modules/application/application.exception';import { resolveTargetApplicationOrThrow } from 'src/engine/core-modules/application/utils/resolve-target-application-or-throw.util';@@ -24,11 +25,17 @@ const expectInvalidInput = (resolve: () => unknown) => {};describe('resolveTargetApplicationOrThrow', () => {- describe('with an application caller', () => {+ describe.each([+ ApplicationRegistrationSourceType.LOCAL,+ ApplicationRegistrationSourceType.NPM,+ ApplicationRegistrationSourceType.TARBALL,+ ])('with a %s application caller', (sourceType) => {+ const callingApplication = { ...CALLING_APPLICATION, sourceType };+it('should target the calling application when no argument is given', () => {expect(resolveTargetApplicationOrThrow({- callingApplication: CALLING_APPLICATION,+ callingApplication,}),).toEqual({ targetApplicationId: CALLING_APPLICATION.id });});@@ -36,13 +43,13 @@ describe('resolveTargetApplicationOrThrow', () => {it('should accept the calling application named by id or universal identifier', () => {expect(resolveTargetApplicationOrThrow({- callingApplication: CALLING_APPLICATION,+ callingApplication,applicationId: CALLING_APPLICATION.id,}),).toEqual({ targetApplicationId: CALLING_APPLICATION.id });expect(resolveTargetApplicationOrThrow({- callingApplication: CALLING_APPLICATION,+ callingApplication,applicationUniversalIdentifier:CALLING_APPLICATION.universalIdentifier,}),@@ -52,7 +59,7 @@ describe('resolveTargetApplicationOrThrow', () => {it('should refuse another application named by id', () => {expectForbidden(() =>resolveTargetApplicationOrThrow({- callingApplication: CALLING_APPLICATION,+ callingApplication,applicationId: OTHER_APPLICATION.id,}),);@@ -61,7 +68,7 @@ describe('resolveTargetApplicationOrThrow', () => {it('should refuse another application named by universal identifier', () => {expectForbidden(() =>resolveTargetApplicationOrThrow({- callingApplication: CALLING_APPLICATION,+ callingApplication,applicationUniversalIdentifier: OTHER_APPLICATION.universalIdentifier,}),);@@ -70,7 +77,7 @@ describe('resolveTargetApplicationOrThrow', () => {it('should refuse another application even when its own id is also given', () => {expectForbidden(() =>resolveTargetApplicationOrThrow({- callingApplication: CALLING_APPLICATION,+ callingApplication,applicationId: CALLING_APPLICATION.id,applicationUniversalIdentifier: OTHER_APPLICATION.universalIdentifier,}),@@ -78,6 +85,50 @@ describe('resolveTargetApplicationOrThrow', () => {});});+ describe('with an OAuth-only application caller', () => {+ const callingApplication = {+ ...CALLING_APPLICATION,+ sourceType: ApplicationRegistrationSourceType.OAUTH_ONLY,+ };++ it('should accept another application by id', () => {+ expect(+ resolveTargetApplicationOrThrow({+ callingApplication,+ applicationId: OTHER_APPLICATION.id,+ }),+ ).toEqual({ targetApplicationId: OTHER_APPLICATION.id });+ });++ it('should accept another application by universal identifier', () => {+ expect(+ resolveTargetApplicationOrThrow({+ callingApplication,+ applicationUniversalIdentifier: OTHER_APPLICATION.universalIdentifier,+ }),+ ).toEqual({+ targetApplicationUniversalIdentifier:+ OTHER_APPLICATION.universalIdentifier,+ });+ });++ it('should require an explicit target instead of selecting the OAuth client', () => {+ expectInvalidInput(() =>+ resolveTargetApplicationOrThrow({ callingApplication }),+ );+ });++ it('should refuse two identifiers', () => {+ expectInvalidInput(() =>+ resolveTargetApplicationOrThrow({+ callingApplication,+ applicationId: OTHER_APPLICATION.id,+ applicationUniversalIdentifier: OTHER_APPLICATION.universalIdentifier,+ }),+ );+ });+ });+describe('without an application caller', () => {it('should keep the one identifier given', () => {expect(diff --git a/packages/twenty-server/src/engine/core-modules/application/utils/resolve-target-application-or-throw.util.ts b/packages/twenty-server/src/engine/core-modules/application/utils/resolve-target-application-or-throw.util.tsindex bded90f043e..00d66b6cf99 100644--- a/packages/twenty-server/src/engine/core-modules/application/utils/resolve-target-application-or-throw.util.ts+++ b/packages/twenty-server/src/engine/core-modules/application/utils/resolve-target-application-or-throw.util.ts@@ -5,6 +5,7 @@ import {ApplicationExceptionCode,} from 'src/engine/core-modules/application/application.exception';import { type FlatApplication } from 'src/engine/core-modules/application/types/flat-application.type';+import { getScopedCallingApplication } from 'src/engine/core-modules/application/utils/get-scoped-calling-application.util';type TargetApplication =| {@@ -22,13 +23,16 @@ export const resolveTargetApplicationOrThrow = ({applicationUniversalIdentifier,}: {callingApplication:- | Pick<FlatApplication, 'id' | 'universalIdentifier'>+ | Pick<FlatApplication, 'id' | 'universalIdentifier' | 'sourceType'>| null| undefined;applicationId?: string | null;applicationUniversalIdentifier?: string | null;}): TargetApplication => {- if (!isDefined(callingApplication)) {+ const scopedCallingApplication =+ getScopedCallingApplication(callingApplication);++ if (!isDefined(scopedCallingApplication)) {if (isDefined(applicationId) && isDefined(applicationUniversalIdentifier)) {throw new ApplicationException('applicationId and applicationUniversalIdentifier cannot both be given',@@ -53,10 +57,11 @@ export const resolveTargetApplicationOrThrow = ({}const namesAnotherApplication =- (isDefined(applicationId) && applicationId !== callingApplication.id) ||+ (isDefined(applicationId) &&+ applicationId !== scopedCallingApplication.id) ||(isDefined(applicationUniversalIdentifier) &&applicationUniversalIdentifier !==- callingApplication.universalIdentifier);+ scopedCallingApplication.universalIdentifier);if (namesAnotherApplication) {throw new ApplicationException(@@ -65,5 +70,5 @@ export const resolveTargetApplicationOrThrow = ({);}- return { targetApplicationId: callingApplication.id };+ return { targetApplicationId: scopedCallingApplication.id };};