Skip to content

The open alternative to Salesforce, designed for AI.

fix(merge): reject non-UUID ids and a missing conflict priority index (#29183)

Etienneca3d998442f7
diff --git a/packages/twenty-server/src/engine/api/common/common-query-runners/common-merge-many-query-runner.service.ts b/packages/twenty-server/src/engine/api/common/common-query-runners/common-merge-many-query-runner.service.ts
index 6dc812c435b..0a97e7955c5 100644
--- a/packages/twenty-server/src/engine/api/common/common-query-runners/common-merge-many-query-runner.service.ts
+++ b/packages/twenty-server/src/engine/api/common/common-query-runners/common-merge-many-query-runner.service.ts
@@ -1,6 +1,7 @@
import { Injectable } from '@nestjs/common';
import { msg } from '@lingui/core/macro';
+import { isArray } from '@sniptt/guards';
import chunk from 'lodash.chunk';
import {
MUTATION_MAX_MERGE_RECORDS,
@@ -38,6 +39,7 @@ import { getAllSelectableColumnNames } from 'src/engine/api/utils/get-all-select
import { buildColumnsToReturn } from 'src/engine/api/graphql/graphql-query-runner/utils/build-columns-to-return';
import { hasRecordFieldValue } from 'src/engine/api/graphql/graphql-query-runner/utils/has-record-field-value.util';
import { mergeFieldValues } from 'src/engine/api/graphql/graphql-query-runner/utils/merge-field-values.util';
+import { assertIsValidUuid } from 'src/engine/api/graphql/workspace-query-runner/utils/assert-is-valid-uuid.util';
import { WorkspaceAuthContext } from 'src/engine/core-modules/auth/types/workspace-auth-context.type';
import { MetricsKeys } from 'src/engine/core-modules/metrics/types/metrics-keys.type';
import { computeMorphOrRelationFieldJoinColumnName } from 'src/engine/metadata-modules/field-metadata/utils/compute-morph-or-relation-field-join-column-name.util';
@@ -729,7 +731,7 @@ export class CommonMergeManyQueryRunnerService extends CommonBaseQueryRunnerServ
const { ids, conflictPriorityIndex } = args;
- if (!ids || ids.length < 2) {
+ if (!isArray(ids) || ids.length < 2) {
throw new CommonQueryRunnerException(
'At least 2 record IDs are required for merge',
CommonQueryRunnerExceptionCode.INVALID_QUERY_INPUT,
@@ -749,7 +751,13 @@ export class CommonMergeManyQueryRunnerService extends CommonBaseQueryRunnerServ
);
}
- if (conflictPriorityIndex < 0 || conflictPriorityIndex >= ids.length) {
+ ids.forEach(assertIsValidUuid);
+
+ if (
+ !Number.isInteger(conflictPriorityIndex) ||
+ conflictPriorityIndex < 0 ||
+ conflictPriorityIndex >= ids.length
+ ) {
throw new CommonQueryRunnerException(
`Invalid conflict priority '${conflictPriorityIndex}'. Valid options for ${ids.length} records: 0-${ids.length - 1}`,
CommonQueryRunnerExceptionCode.INVALID_QUERY_INPUT,
diff --git a/packages/twenty-server/test/integration/rest/suites/rest-api-core-merge.integration-spec.ts b/packages/twenty-server/test/integration/rest/suites/rest-api-core-merge.integration-spec.ts
index 1fa6650eef6..6f17068fee7 100644
--- a/packages/twenty-server/test/integration/rest/suites/rest-api-core-merge.integration-spec.ts
+++ b/packages/twenty-server/test/integration/rest/suites/rest-api-core-merge.integration-spec.ts
@@ -41,6 +41,32 @@ describe('Core REST API Merge endpoint', () => {
}).expect(404);
});
+ it('should reject a merge without a conflict priority index', async () => {
+ await makeRestApiRequest({
+ method: 'patch',
+ path: '/people/merge',
+ body: { ids: [TEST_PERSON_1_ID, TEST_PERSON_2_ID] },
+ })
+ .expect(400)
+ .expect((res) => {
+ expect(res.body.messages[0]).toBe(
+ "Invalid conflict priority 'undefined'. Valid options for 2 records: 0-1",
+ );
+ });
+ });
+
+ it('should reject a merge with a record id that is not a UUID', async () => {
+ await makeRestApiRequest({
+ method: 'patch',
+ path: '/people/merge',
+ body: { ids: [TEST_PERSON_1_ID, 'abc'], conflictPriorityIndex: 0 },
+ })
+ .expect(400)
+ .expect((res) => {
+ expect(res.body.messages[0]).toBe('Value "abc" is not a valid UUID');
+ });
+ });
+
it('should not delete records on a dry run merge', async () => {
await makeRestApiRequest({
method: 'patch',