From 59babd174c47a6f2bf6ed15d97588937c4da474b Mon Sep 17 00:00:00 2001 From: Sasha Rakhmatulin Date: Thu, 26 Nov 2026 12:55:45 +0000 Subject: [PATCH] fix(drizzle): reject polymorphic join paths below leaf fields `getFieldByPath` keeps searching the same field list when a segment has no subfields, so the polymorphic join planner resolved `title.tags` to an unrelated top-level `tags` field. A read access rule using such a path then threw a raw `Error` (500) while looking for a value table that doesn't exist. The planner now treats a path whose ancestor is a leaf field as absent, so it fails with a `QueryError` like other unsupported polymorphic join paths. --- .../find/createPolymorphicJoinWherePlan.ts | 34 ++++++++++++++++++- test/joins/collections/AccessJoinArticles.ts | 4 +++ test/joins/int.spec.ts | 17 ++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/packages/drizzle/src/find/createPolymorphicJoinWherePlan.ts b/packages/drizzle/src/find/createPolymorphicJoinWherePlan.ts index 8fd0178c3b5..7665eb96e64 100644 --- a/packages/drizzle/src/find/createPolymorphicJoinWherePlan.ts +++ b/packages/drizzle/src/find/createPolymorphicJoinWherePlan.ts @@ -85,10 +85,17 @@ export const createPolymorphicJoinWherePlan = ({ const scalarQueryValueSignatures = new Set() for (const { collection, collectionConfig, table, tableName } of collectionContexts) { - const fieldAtPath = getFieldByPath({ + // `getFieldByPath` keeps searching the same fields when a segment has no subfields, so it + // resolves `title.tags` to an unrelated top-level `tags`. Treat such a path as absent. + const fieldAtPath = isPathTraversable({ fields: collectionConfig.flattenedFields, path: schemaPath, }) + ? getFieldByPath({ + fields: collectionConfig.flattenedFields, + path: schemaPath, + }) + : null if (schemaPath === 'id') { const idColumn = table['id'] as Column | undefined @@ -240,6 +247,31 @@ const getColumnPath = (schemaPath: string): string => .map((segment) => sanitizePathSegment(segment)) .join('_') +/** + * Reports whether every segment before the leaf is a field that can be descended into. A path + * whose ancestor is a leaf field, such as `title.tags`, is not traversable. + */ +const isPathTraversable = ({ fields, path }: { fields: FlattenedField[]; path: string }) => { + const pathSegments = path.split('.') + + for (let segmentIndex = 1; segmentIndex < pathSegments.length; segmentIndex++) { + const fieldAtAncestorPath = getFieldByPath({ + fields, + path: pathSegments.slice(0, segmentIndex).join('.'), + }) + + if ( + fieldAtAncestorPath && + !('flattenedFields' in fieldAtAncestorPath.field) && + !('blocks' in fieldAtAncestorPath.field) + ) { + return false + } + } + + return true +} + const pathHasSeparateRows = ({ fields, path }: { fields: FlattenedField[]; path: string }) => { const pathSegments = path.split('.') diff --git a/test/joins/collections/AccessJoinArticles.ts b/test/joins/collections/AccessJoinArticles.ts index 62e32bd4943..964a354f623 100644 --- a/test/joins/collections/AccessJoinArticles.ts +++ b/test/joins/collections/AccessJoinArticles.ts @@ -125,6 +125,10 @@ export const AccessJoinArticles: CollectionConfig = { return { 'settings.approved': { exists: false } } } + if (req.context.useLeafSubpathAccessConstraint) { + return { 'title.tags': { equals: 'available' } } + } + return { availability: { not_equals: 'missing' }, tags: { equals: 'available' }, diff --git a/test/joins/int.spec.ts b/test/joins/int.spec.ts index 50cc9c3e71a..49f7c9420da 100644 --- a/test/joins/int.spec.ts +++ b/test/joins/int.spec.ts @@ -3435,6 +3435,23 @@ test.suite('Joins Field', { config: './config.ts', resetBetweenTests: false }, ( ).rejects.toThrow('The following path cannot be queried: settings.approved.exists') }) + test('should reject a sub-path of a field that cannot be descended into', async ({ + payload, + }) => { + const { parent } = await createConstrainedJoinDocuments(payload) + + await expect( + payload.findByID({ + id: parent.id, + collection: accessJoinParentsSlug, + context: { useLeafSubpathAccessConstraint: true }, + depth: 1, + overrideAccess: false, + user, + }), + ).rejects.toThrow('The following path cannot be queried: title.tags') + }) + test('should preserve substring matching in REST join constraints', async ({ payload, restClient,