The open alternative to Salesforce, designed for AI.
TypeScript crmcrm-systemcustomergood-first-issuegraphqlhacktoberfestjavascriptmarketingmonoreponestjsopen-sourcepostgresqlreactreactjssalestypescriptweb
Restrict workflow run step updates to form values on readable runs (#28095)
Thomas Trompette3462deaa5692
diff --git a/packages/twenty-server/src/engine/core-modules/workflow/resolvers/workflow-version-step.resolver.ts b/packages/twenty-server/src/engine/core-modules/workflow/resolvers/workflow-version-step.resolver.tsindex b126f790c39..18556b8bd40 100644--- a/packages/twenty-server/src/engine/core-modules/workflow/resolvers/workflow-version-step.resolver.ts+++ b/packages/twenty-server/src/engine/core-modules/workflow/resolvers/workflow-version-step.resolver.ts@@ -88,11 +88,26 @@ export class WorkflowVersionStepResolver {}@Mutation(() => WorkflowActionDTO)+ @UseGuards(+ AuthPrincipalGuard({+ userSession: {+ standard: true,+ impersonated: true,+ playground: true,+ workspaceAgnostic: false,+ },+ apiKey: false,+ oauthClient: { withUser: true, withoutUser: false },+ application: false,+ }),+ )async updateWorkflowRunStep(@AuthWorkspace() { id: workspaceId }: WorkspaceEntity,@Args('input'){ workflowRunId, step }: UpdateWorkflowRunStepInput,): Promise<WorkflowActionDTO> {+ await this.assertWorkflowRunIsReadableByRequesterOrThrow(workflowRunId);+const workflowRun =await this.workflowRunWorkspaceService.getWorkflowRunOrFail({workflowRunId,@@ -101,7 +116,7 @@ export class WorkflowVersionStepResolver {if (!canUpdateWorkflowRunStep({ workflowRun, step })) {throw new WorkflowVersionStepException(- 'Only the values of a form step can change on a workflow run bound to an application',+ 'Only the values of a form step can change on a workflow run',WorkflowVersionStepExceptionCode.INVALID_REQUEST,{userFriendlyMessage: msg`Only the values of this form can be changed.`,@@ -130,16 +145,7 @@ export class WorkflowVersionStepResolver {);}- if (- !(await this.workflowRunWorkspaceService.isWorkflowRunReadableByRequester(- workflowRunId,- ))- ) {- throw new WorkflowVersionStepException(- 'Workflow run not found',- WorkflowVersionStepExceptionCode.NOT_FOUND,- );- }+ await this.assertWorkflowRunIsReadableByRequesterOrThrow(workflowRunId);await this.workflowRunnerWorkspaceService.submitFormStep({workspaceId,@@ -167,4 +173,19 @@ export class WorkflowVersionStepResolver {{ workspaceId: workspace.id },);}++ private async assertWorkflowRunIsReadableByRequesterOrThrow(+ workflowRunId: string,+ ): Promise<void> {+ if (+ !(await this.workflowRunWorkspaceService.isWorkflowRunReadableByRequester(+ workflowRunId,+ ))+ ) {+ throw new WorkflowVersionStepException(+ 'Workflow run not found',+ WorkflowVersionStepExceptionCode.NOT_FOUND,+ );+ }+ }}diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/utils/__tests__/can-update-workflow-run-step.util.spec.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/utils/__tests__/can-update-workflow-run-step.util.spec.tsindex e73dbe1a33a..d4b73b75519 100644--- a/packages/twenty-server/src/modules/workflow/workflow-runner/utils/__tests__/can-update-workflow-run-step.util.spec.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/utils/__tests__/can-update-workflow-run-step.util.spec.ts@@ -48,13 +48,12 @@ const RECORD_STEP: WorkflowCreateRecordAction = {},};-const buildWorkflowRun = (applicationId?: string) => ({- createdBy: { context: { applicationId } },+const WORKFLOW_RUN = {state: { flow: { steps: [FORM_STEP, RECORD_STEP] } },-});+};describe('canUpdateWorkflowRunStep', () => {- it('keeps the steps of a run no application bounds editable', () => {+ it('refuses replacing a record step with another action', () => {const deleteStep: WorkflowDeleteRecordAction = {...RECORD_STEP,type: WorkflowActionType.DELETE_RECORD,@@ -66,16 +65,42 @@ describe('canUpdateWorkflowRunStep', () => {expect(canUpdateWorkflowRunStep({- workflowRun: buildWorkflowRun(),+ workflowRun: WORKFLOW_RUN,step: deleteStep,}),+ ).toBe(false);+ });++ it('lets the values of a form step be filled', () => {+ expect(+ canUpdateWorkflowRunStep({+ workflowRun: WORKFLOW_RUN,+ step: {+ ...FORM_STEP,+ settings: {+ ...FORM_STEP.settings,+ input: [{ ...FORM_STEP.settings.input[0], value: 'Acme' }],+ },+ },+ }),).toBe(true);});- it('lets the values of a form step of an application-bound run be filled', () => {+ it('lets the values of a form step be filled when the stored step still carries a legacy retry flag', () => {+ const legacyFormStep = {+ ...FORM_STEP,+ settings: {+ ...FORM_STEP.settings,+ errorHandlingOptions: {+ ...ERROR_HANDLING_OPTIONS,+ retryOnFailure: { value: false },+ },+ },+ } as unknown as WorkflowFormAction;+expect(canUpdateWorkflowRunStep({- workflowRun: buildWorkflowRun('installed-app-id'),+ workflowRun: { state: { flow: { steps: [legacyFormStep] } } },step: {...FORM_STEP,settings: {@@ -87,7 +112,7 @@ describe('canUpdateWorkflowRunStep', () => {).toBe(true);});- it('refuses a change to the fields of a form step of an application-bound run', () => {+ it('refuses a change to the fields of a form step', () => {const textFieldTurnedIntoRecordPicker: WorkflowFormAction = {...FORM_STEP,settings: {@@ -105,7 +130,7 @@ describe('canUpdateWorkflowRunStep', () => {expect(canUpdateWorkflowRunStep({- workflowRun: buildWorkflowRun('installed-app-id'),+ workflowRun: WORKFLOW_RUN,step: textFieldTurnedIntoRecordPicker,}),).toBe(false);@@ -127,6 +152,10 @@ describe('canUpdateWorkflowRunStep', () => {...FORM_STEP,nextStepIds: [],};+ const renamedFormStep: WorkflowFormAction = {+ ...FORM_STEP,+ name: 'Renamed',+ };it.each<{ change: string; step: WorkflowAction }>([{ change: 'a code step replacing the form', step: codeStepReplacingForm },@@ -135,10 +164,11 @@ describe('canUpdateWorkflowRunStep', () => {step: formStepReplacingRecord,},{ change: 'a rerouted form step', step: reroutedFormStep },- ])('refuses $change on an application-bound run', ({ step }) => {+ { change: 'a renamed form step', step: renamedFormStep },+ ])('refuses $change', ({ step }) => {expect(canUpdateWorkflowRunStep({- workflowRun: buildWorkflowRun('installed-app-id'),+ workflowRun: WORKFLOW_RUN,step,}),).toBe(false);diff --git a/packages/twenty-server/src/modules/workflow/workflow-runner/utils/can-update-workflow-run-step.util.ts b/packages/twenty-server/src/modules/workflow/workflow-runner/utils/can-update-workflow-run-step.util.tsindex 79ad91db75d..eb024cf3c98 100644--- a/packages/twenty-server/src/modules/workflow/workflow-runner/utils/can-update-workflow-run-step.util.ts+++ b/packages/twenty-server/src/modules/workflow/workflow-runner/utils/can-update-workflow-run-step.util.ts@@ -1,6 +1,4 @@import isEqual from 'lodash.isequal';-import { type ActorMetadata } from 'twenty-shared/types';-import { isDefined } from 'twenty-shared/utils';import { WorkflowActionType } from 'twenty-shared/workflow';import {@@ -13,15 +11,10 @@ export const canUpdateWorkflowRunStep = ({step,}: {workflowRun: {- createdBy: Pick<ActorMetadata, 'context'>;state: { flow?: { steps?: WorkflowAction[] } } | null;};step: WorkflowAction;}): boolean => {- if (!isDefined(workflowRun.createdBy.context?.applicationId)) {- return true;- }-const existingStep = workflowRun.state?.flow?.steps?.find(({ id }) => id === step.id,);@@ -34,13 +27,20 @@ export const canUpdateWorkflowRunStep = ({return false;}- return isEqual(withoutFormValues(existingStep), withoutFormValues(step));+ return (+ existingStep.name === step.name &&+ isEqual(existingStep.nextStepIds, step.nextStepIds) &&+ isEqual(+ getFormFieldDefinitions(existingStep),+ getFormFieldDefinitions(step),+ )+ );};-const withoutFormValues = (step: WorkflowFormAction) => ({- ...step,- settings: {- ...step.settings,- input: step.settings.input.map((field) => ({ ...field, value: undefined })),- },-});+const getFormFieldDefinitions = (step: WorkflowFormAction) =>+ step.settings.input.map(({ id, name, type, settings }) => ({+ id,+ name,+ type,+ settings,+ }));diff --git a/packages/twenty-server/test/integration/endpoint-permissions/__snapshots__/endpoint-permission-declarations.integration-spec.ts.snap b/packages/twenty-server/test/integration/endpoint-permissions/__snapshots__/endpoint-permission-declarations.integration-spec.ts.snapindex 949cd9153ab..fb3967cc075 100644--- a/packages/twenty-server/test/integration/endpoint-permissions/__snapshots__/endpoint-permission-declarations.integration-spec.ts.snap+++ b/packages/twenty-server/test/integration/endpoint-permissions/__snapshots__/endpoint-permission-declarations.integration-spec.ts.snap@@ -659,7 +659,7 @@ exports[`endpoint permission declarations should match the AuthPrincipalGuard an"WorkflowTriggerResolver.stopWorkflowRun {"applicationTarget":null,"authPrincipalGuards":[{"apiKey":true,"application":{"withAgent":false,"withUser":true,"withoutUser":true},"oauthClient":{"withUser":true,"withoutUser":true},"userSession":{"impersonated":true,"playground":true,"standard":true,"workspaceAgnostic":false}}]}","WorkflowVersionStepResolver.submitFormStep {"applicationTarget":null,"authPrincipalGuards":[{"apiKey":false,"application":{"withAgent":false,"withUser":true,"withoutUser":false},"oauthClient":{"withUser":true,"withoutUser":false},"userSession":{"impersonated":true,"playground":true,"standard":true,"workspaceAgnostic":false}}]}","WorkflowVersionStepResolver.testHttpRequest {"applicationTarget":null,"authPrincipalGuards":[{"apiKey":false,"application":{"withAgent":false,"withUser":true,"withoutUser":false},"oauthClient":{"withUser":true,"withoutUser":false},"userSession":{"impersonated":true,"playground":true,"standard":true,"workspaceAgnostic":false}}]}",- "WorkflowVersionStepResolver.updateWorkflowRunStep {"applicationTarget":null,"authPrincipalGuards":[{"apiKey":false,"application":{"withAgent":false,"withUser":true,"withoutUser":false},"oauthClient":{"withUser":true,"withoutUser":false},"userSession":{"impersonated":true,"playground":true,"standard":true,"workspaceAgnostic":false}}]}",+ "WorkflowVersionStepResolver.updateWorkflowRunStep {"applicationTarget":null,"authPrincipalGuards":[{"apiKey":false,"application":{"withAgent":false,"withUser":true,"withoutUser":false},"oauthClient":{"withUser":true,"withoutUser":false},"userSession":{"impersonated":true,"playground":true,"standard":true,"workspaceAgnostic":false}},{"apiKey":false,"application":{"withAgent":false,"withUser":false,"withoutUser":false},"oauthClient":{"withUser":true,"withoutUser":false},"userSession":{"impersonated":true,"playground":true,"standard":true,"workspaceAgnostic":false}}]}","WorkflowVersionStepResolver.workflowStepConnectedAccountHandle {"applicationTarget":null,"authPrincipalGuards":[{"apiKey":false,"application":{"withAgent":false,"withUser":true,"withoutUser":false},"oauthClient":{"withUser":true,"withoutUser":false},"userSession":{"impersonated":true,"playground":true,"standard":true,"workspaceAgnostic":false}}]}","WorkspaceInvitationResolver.deleteWorkspaceInvitation {"applicationTarget":null,"authPrincipalGuards":[{"apiKey":true,"application":{"withAgent":false,"withUser":true,"withoutUser":true},"oauthClient":{"withUser":true,"withoutUser":true},"userSession":{"impersonated":true,"playground":true,"standard":true,"workspaceAgnostic":false}},{"apiKey":true,"application":{"withAgent":false,"withUser":false,"withoutUser":false},"oauthClient":{"withUser":true,"withoutUser":true},"userSession":{"impersonated":true,"playground":true,"standard":true,"workspaceAgnostic":false}}]}","WorkspaceInvitationResolver.findWorkspaceInvitations {"applicationTarget":null,"authPrincipalGuards":[{"apiKey":true,"application":{"withAgent":false,"withUser":true,"withoutUser":true},"oauthClient":{"withUser":true,"withoutUser":true},"userSession":{"impersonated":true,"playground":true,"standard":true,"workspaceAgnostic":false}}]}",diff --git a/packages/twenty-server/test/integration/graphql/suites/workflow/application-workflow-execution-permissions.integration-spec.ts b/packages/twenty-server/test/integration/graphql/suites/workflow/application-workflow-execution-permissions.integration-spec.tsindex 33618af5991..99b5eacf7fa 100644--- a/packages/twenty-server/test/integration/graphql/suites/workflow/application-workflow-execution-permissions.integration-spec.ts+++ b/packages/twenty-server/test/integration/graphql/suites/workflow/application-workflow-execution-permissions.integration-spec.ts@@ -551,7 +551,7 @@ describe('application workflow execution permissions', () => {).toBe(0);}, 120000);- it('only lets the values of a form step change on an application workflow run', async () => {+ it('only lets the values of a form step change on an application workflow run, and not through the application', async () => {const workflowRunId = await runWorkflow(FORM_WORKFLOW);const [formStep] = FORM_WORKFLOW.steps;@@ -591,6 +591,16 @@ describe('application workflow execution permissions', () => {});expect(filledStep.body.errors).toBeUndefined();++ const stepFilledThroughApplication = await graphqlAs(+ await buildJaneTokenThroughApplication(APP_ID),+ UPDATE_WORKFLOW_RUN_STEP,+ { input: { workflowRunId, step: runFormStep } },+ );++ expect(+ stepFilledThroughApplication.body.errors?.[0]?.extensions?.code,+ ).toBe('FORBIDDEN');}, 120000);it('reads the records picked in a form with the permissions of the run, keeping a refused form open', async () => {