fix(policy): resolve this.relation.field against @@allow model, unnest array columns in subqueries#2734
Open
hechang27-sprt wants to merge 2 commits into
Conversation
…t array columns in subqueries Fix zenstackhq#1: In _member() isThis branch, pass modelOrType: context.thisType to transformRelationAccess() so that this.relation.field correctly resolves relation fields against the @@Allow model (thisType) rather than the collection predicate's model (modelOrType). Fix zenstackhq#2: In _member() when the innermost field of a subquery selection is an array type AND the provider is PostgreSQL, wrap the column with unnest() so that ANY(subquery) receives scalar rows instead of array rows. Other providers (SQLite, MySQL) leave the column as-is since they store arrays differently (JSON) and would need different handling.
…field in operator Two PG-specific tests covering the fixes: - Fix zenstackhq#1: this.relation.field resolves against @@Allow model in collection predicates (e.g. auth().permissions?[p, p.clearance >= this.author.level]) - Fix zenstackhq#2: this.scalarField in this.relation.arrayField generates unnest() for PG subqueries (e.g. this.id in this.group.visibleDocIds)
Contributor
|
Caution Review failedAn error occurred during the review process. Please try again later. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Contributor
|
Caution Failed to replace (edit) comment. This is likely due to insufficient permissions or the comment being deleted. Error details |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2733.
Problem
Two bugs in the policy plugin expression transformer:
this.relation.fieldresolves against the wrong model insideauth().collection?[...]predicates. The expression transformer usescontext.thisTypeto resolve the field definition but passesrestContext(withmodelOrTypestill pointing to the collection model) totransformRelationAccess. This causesField "X" not found in model "<collection>"crashes.x in this.relation.arrayFieldgenerates broken SQL on PostgreSQL. The expression compiles to= ANY((SELECT arrayCol FROM ...))where the subquery returns array-typed rows (integer[]). PostgreSQLANY(subquery)requires scalar rows, producingoperator does not exist: integer = integer[].Fix
Fix 1: Resolve
this.relation.fieldagainst the@@allowmodelPass
modelOrType: context.thisTypetotransformRelationAccesswhen the receiver isthis, so relation fields resolve against the model bearing the policy rather than the collection model.Fix 2: Unnest array columns in subqueries on PostgreSQL
When a relation-chain subquery selects an array-typed column (e.g.
this.group.visibleDocIdswherevisibleDocIdsisInt[]), wrap the column withunnest()soANY(subquery)receives scalar rows. Guarded to PostgreSQL only — other providers store arrays as JSON and need separate handling.Verification
Two new PG-specific tests added to
auth-access.test.ts:this.author.levelinauth().permissions?[p, ...]this.id in this.group.visibleDocIdsAll existing policy tests continue to pass on both PostgreSQL and SQLite.