Skip to content

Commit ee71982

Browse files
theluckystrikeclaudeymc9
authored
fix(policy): compare before() enum fields as text on PostgreSQL (#2869)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: ymc9 <[email protected]>
1 parent 675fe20 commit ee71982

2 files changed

Lines changed: 76 additions & 7 deletions

File tree

‎packages/plugins/policy/src/expression-transformer.ts‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -342,14 +342,13 @@ export class ExpressionTransformer<Schema extends SchemaDef> {
342342
const rightFieldDef = this.getFieldDefFromFieldRef(normalizedRight, context);
343343
// Map ZModel operator to SQL operator string
344344
const sqlOp = op === '==' ? '=' : op;
345+
// `before()` enums are text in the VALUES table and pg can't compare text to a native enum
346+
const asText =
347+
this.isBeforeEnumField(normalizedLeft, context) || this.isBeforeEnumField(normalizedRight, context);
348+
const operand = (node: OperationNode) =>
349+
asText ? this.dialect.castText(new ExpressionWrapper(node)) : new ExpressionWrapper(node);
345350
return this.dialect
346-
.buildComparison(
347-
new ExpressionWrapper(left),
348-
leftFieldDef,
349-
sqlOp,
350-
new ExpressionWrapper(right),
351-
rightFieldDef,
352-
)
351+
.buildComparison(operand(left), leftFieldDef, sqlOp, operand(right), rightFieldDef)
353352
.toOperationNode();
354353
}
355354
}
@@ -1215,6 +1214,16 @@ export class ExpressionTransformer<Schema extends SchemaDef> {
12151214
return !!fieldDef?.relation;
12161215
}
12171216

1217+
private isBeforeEnumField(expr: Expression, context: ExpressionTransformerContext) {
1218+
if (!ExpressionUtils.isMember(expr) || !isBeforeInvocation(expr.receiver)) {
1219+
return false;
1220+
}
1221+
// `before()` always refers to the model being updated (`thisType`), even inside
1222+
// collection predicates where `modelOrType` is the related model
1223+
const fieldDef = QueryUtils.getField(this.schema, context.thisType, expr.members[0]!);
1224+
return !!fieldDef && QueryUtils.isEnum(this.schema, fieldDef.type);
1225+
}
1226+
12181227
private getFieldDefFromFieldRef(expr: Expression, context: ExpressionTransformerContext): FieldDef | undefined {
12191228
return this.resolveFieldRef(expr, context)?.fieldDef;
12201229
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { createPolicyTestClient } from '@zenstackhq/testtools';
2+
import { describe, expect, it } from 'vitest';
3+
4+
describe('Regression for issue #2727', () => {
5+
it('compares before() enum fields in post-update rules on PostgreSQL native enums', async () => {
6+
const db = await createPolicyTestClient(
7+
`
8+
enum State {
9+
DRAFT
10+
IN_PROGRESS
11+
DONE
12+
}
13+
14+
model Post {
15+
id Int @id
16+
state State @default(DRAFT)
17+
@@allow('all', true)
18+
@@deny('post-update', before().state != state && before().state == DRAFT && state != IN_PROGRESS)
19+
}
20+
`,
21+
{ provider: 'postgresql', usePrismaPush: true },
22+
);
23+
24+
await db.post.create({ data: { id: 1 } });
25+
await expect(db.post.update({ where: { id: 1 }, data: { state: 'DONE' } })).toBeRejectedByPolicy();
26+
await db.post.update({ where: { id: 1 }, data: { state: 'IN_PROGRESS' } });
27+
});
28+
29+
it('compares before() enum fields inside collection predicates on PostgreSQL native enums', async () => {
30+
const db = await createPolicyTestClient(
31+
`
32+
enum State {
33+
DRAFT
34+
IN_PROGRESS
35+
DONE
36+
}
37+
38+
model Post {
39+
id Int @id
40+
state State @default(DRAFT)
41+
comments Comment[]
42+
@@allow('all', true)
43+
@@deny('post-update', comments?[before().state != this.state] && state == DONE)
44+
}
45+
46+
model Comment {
47+
id Int @id
48+
post Post @relation(fields: [postId], references: [id])
49+
postId Int
50+
@@allow('all', true)
51+
}
52+
`,
53+
{ provider: 'postgresql', usePrismaPush: true },
54+
);
55+
56+
await db.post.create({ data: { id: 1, comments: { create: { id: 1 } } } });
57+
await expect(db.post.update({ where: { id: 1 }, data: { state: 'DONE' } })).toBeRejectedByPolicy();
58+
await db.post.update({ where: { id: 1 }, data: { state: 'IN_PROGRESS' } });
59+
});
60+
});

0 commit comments

Comments
 (0)