Skip to content

The open alternative to Salesforce, designed for AI.

Keep row access aliases within Postgres' 63-byte identifier limit (#28411)

Mariee3f5a5a0f2c0
diff --git a/packages/twenty-server/src/engine/core-modules/record-share/utils/__tests__/build-record-share-condition.util.spec.ts b/packages/twenty-server/src/engine/core-modules/record-share/utils/__tests__/build-record-share-condition.util.spec.ts
index 6e86ef59d70..fd96f18bf38 100644
--- a/packages/twenty-server/src/engine/core-modules/record-share/utils/__tests__/build-record-share-condition.util.spec.ts
+++ b/packages/twenty-server/src/engine/core-modules/record-share/utils/__tests__/build-record-share-condition.util.spec.ts
@@ -104,4 +104,25 @@ describe('buildRecordShareCondition', () => {
],
});
});
+
+ it('should keep the record share alias distinct from a 63-byte table alias', () => {
+ const tableAlias =
+ 'customLeafRecord_parentCollectionId_containerId_securityGroupId';
+
+ const { sql } = buildRecordShareCondition({
+ tableAlias,
+ recordShareTableExpression: '"workspace_abc"."recordShare"',
+ objectMetadataId: OBJECT_METADATA_ID,
+ principalIds: PRINCIPAL_IDS,
+ accessLevels: ACCESS_LEVELS,
+ });
+
+ const [, recordShareAlias] = sql.match(/AS "([^"]+)" WHERE/) ?? [];
+
+ expect(Buffer.byteLength(recordShareAlias)).toBeLessThanOrEqual(63);
+ expect(recordShareAlias).not.toBe(tableAlias);
+ expect(sql).toContain(
+ `"${recordShareAlias}"."recordId" = "${tableAlias}"."id"`,
+ );
+ });
});
diff --git a/packages/twenty-server/src/engine/core-modules/record-share/utils/build-record-share-condition.util.ts b/packages/twenty-server/src/engine/core-modules/record-share/utils/build-record-share-condition.util.ts
index 013bdcc66b4..74504cf623b 100644
--- a/packages/twenty-server/src/engine/core-modules/record-share/utils/build-record-share-condition.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/record-share/utils/build-record-share-condition.util.ts
@@ -5,6 +5,7 @@ import { randomBytes } from 'node:crypto';
import { type RecordShareAccessLevel } from 'twenty-shared/types';
import { type ObjectLiteral } from 'typeorm';
+import { buildPostgresSafeAlias } from 'src/engine/twenty-orm/sql/utils/build-postgres-safe-alias.util';
import { escapeIdentifier } from 'src/engine/workspace-manager/workspace-migration/utils/remove-sql-injection.util';
export const buildRecordShareCondition = ({
@@ -27,7 +28,9 @@ export const buildRecordShareCondition = ({
const principalIdsParameterName = `recordSharePrincipalIds_${parameterSuffix}`;
const accessLevelsParameterName = `recordShareAccessLevels_${parameterSuffix}`;
- const recordShareAlias = escapeIdentifier(`${tableAlias}_recordShare`);
+ const recordShareAlias = escapeIdentifier(
+ buildPostgresSafeAlias(`${tableAlias}_recordShare`),
+ );
const recordId = `${escapeIdentifier(tableAlias)}."id"`;
const grantConditions = [
diff --git a/packages/twenty-server/src/engine/core-modules/record-share/utils/build-record-share-gate.util.ts b/packages/twenty-server/src/engine/core-modules/record-share/utils/build-record-share-gate.util.ts
index 7118b26cef2..710fcb36869 100644
--- a/packages/twenty-server/src/engine/core-modules/record-share/utils/build-record-share-gate.util.ts
+++ b/packages/twenty-server/src/engine/core-modules/record-share/utils/build-record-share-gate.util.ts
@@ -17,6 +17,7 @@ import {
type RowAccessPolicyContext,
type RowAccessPolicyTarget,
} from 'src/engine/twenty-orm/types/row-access-policy.type';
+import { buildPostgresSafeAlias } from 'src/engine/twenty-orm/sql/utils/build-postgres-safe-alias.util';
type BuildParentPolicy = (target: RowAccessPolicyTarget) => RowAccessPolicy;
@@ -200,7 +201,9 @@ const buildInheritedReadabilityParentExpression = ({
parent: InheritedReadabilityParent;
}): InheritedReadabilityParentExpression => {
if (parent.kind === 'column') {
- const parentTableAlias = `${tableAlias}_${parent.joinColumnName}`;
+ const parentTableAlias = buildPostgresSafeAlias(
+ `${tableAlias}_${parent.joinColumnName}`,
+ );
return {
kind: 'column',
@@ -216,7 +219,9 @@ const buildInheritedReadabilityParentExpression = ({
};
}
- const childTableAlias = `${tableAlias}_${parent.childFlatObjectMetadata.nameSingular}`;
+ const childTableAlias = buildPostgresSafeAlias(
+ `${tableAlias}_${parent.childFlatObjectMetadata.nameSingular}`,
+ );
return {
kind: 'children',
diff --git a/packages/twenty-server/src/engine/twenty-orm/sql/utils/__tests__/build-postgres-safe-alias.util.spec.ts b/packages/twenty-server/src/engine/twenty-orm/sql/utils/__tests__/build-postgres-safe-alias.util.spec.ts
new file mode 100644
index 00000000000..5f2310b8292
--- /dev/null
+++ b/packages/twenty-server/src/engine/twenty-orm/sql/utils/__tests__/build-postgres-safe-alias.util.spec.ts
@@ -0,0 +1,36 @@
+import { buildPostgresSafeAlias } from 'src/engine/twenty-orm/sql/utils/build-postgres-safe-alias.util';
+
+const OUTER_ALIAS =
+ 'customLeafRecord_parentCollectionId_containerId_securityGroupId';
+
+describe('buildPostgresSafeAlias', () => {
+ it('should keep an alias that fits in 63 bytes', () => {
+ expect(OUTER_ALIAS).toHaveLength(63);
+ expect(buildPostgresSafeAlias(OUTER_ALIAS)).toBe(OUTER_ALIAS);
+ });
+
+ it('should not collapse a nested alias onto its 63-byte parent', () => {
+ const innerAlias = buildPostgresSafeAlias(`${OUTER_ALIAS}_groupMembership`);
+
+ expect(Buffer.byteLength(innerAlias)).toBeLessThanOrEqual(63);
+ expect(innerAlias).not.toBe(OUTER_ALIAS);
+ });
+
+ it('should give distinct aliases to long siblings sharing a prefix', () => {
+ expect(buildPostgresSafeAlias(`${OUTER_ALIAS}_groupMembership`)).not.toBe(
+ buildPostgresSafeAlias(`${OUTER_ALIAS}_groupInvitation`),
+ );
+ });
+
+ it('should be deterministic', () => {
+ expect(buildPostgresSafeAlias(`${OUTER_ALIAS}_groupMembership`)).toBe(
+ buildPostgresSafeAlias(`${OUTER_ALIAS}_groupMembership`),
+ );
+ });
+
+ it('should count bytes rather than characters', () => {
+ const multiByteAlias = 'é'.repeat(40);
+
+ expect(buildPostgresSafeAlias(multiByteAlias)).not.toBe(multiByteAlias);
+ });
+});
diff --git a/packages/twenty-server/src/engine/twenty-orm/sql/utils/build-column-result-alias.util.ts b/packages/twenty-server/src/engine/twenty-orm/sql/utils/build-column-result-alias.util.ts
index e77b44b2021..2fc300b30a3 100644
--- a/packages/twenty-server/src/engine/twenty-orm/sql/utils/build-column-result-alias.util.ts
+++ b/packages/twenty-server/src/engine/twenty-orm/sql/utils/build-column-result-alias.util.ts
@@ -1,22 +1,6 @@
-import { createHash } from 'crypto';
-
-// Postgres truncates identifiers at 63 bytes, so two long "<alias>_<column>" result
-// aliases sharing a prefix would collapse onto the same key in the returned row.
-// Hashing past the limit mirrors what TypeORM does through DriverUtils.buildAlias.
-const POSTGRES_MAX_ALIAS_LENGTH = 63;
+import { buildPostgresSafeAlias } from 'src/engine/twenty-orm/sql/utils/build-postgres-safe-alias.util';
export const buildColumnResultAlias = (
alias: string,
columnName: string,
-): string => {
- const resultAlias = `${alias}_${columnName}`;
-
- if (resultAlias.length <= POSTGRES_MAX_ALIAS_LENGTH) {
- return resultAlias;
- }
-
- return createHash('sha1')
- .update(resultAlias, 'utf8')
- .digest('hex')
- .slice(0, POSTGRES_MAX_ALIAS_LENGTH);
-};
+): string => buildPostgresSafeAlias(`${alias}_${columnName}`);
diff --git a/packages/twenty-server/src/engine/twenty-orm/sql/utils/build-postgres-safe-alias.util.ts b/packages/twenty-server/src/engine/twenty-orm/sql/utils/build-postgres-safe-alias.util.ts
new file mode 100644
index 00000000000..3e7ee315c15
--- /dev/null
+++ b/packages/twenty-server/src/engine/twenty-orm/sql/utils/build-postgres-safe-alias.util.ts
@@ -0,0 +1,15 @@
+import { createHash } from 'crypto';
+
+// Postgres truncates past 63 bytes, which would merge aliases sharing a prefix
+const POSTGRES_MAX_ALIAS_LENGTH = 63;
+
+export const buildPostgresSafeAlias = (alias: string): string => {
+ if (Buffer.byteLength(alias, 'utf8') <= POSTGRES_MAX_ALIAS_LENGTH) {
+ return alias;
+ }
+
+ return createHash('sha256')
+ .update(alias, 'utf8')
+ .digest('hex')
+ .slice(0, POSTGRES_MAX_ALIAS_LENGTH);
+};