Skip to content

Commit 62c624d

Browse files
authored
fix: check attribute function shouldn't delegate "postUpdate" rules (#1663)
1 parent 8589b79 commit 62c624d

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

packages/schema/src/plugins/enhancer/policy/expression-writer.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -815,8 +815,15 @@ export class ExpressionWriter {
815815
}
816816

817817
this.block(() => {
818-
const targetGuardFunc = getQueryGuardFunctionName(targetModel, undefined, false, operation);
819-
this.writer.write(`${fieldRef.target.$refText}: ${targetGuardFunc}(context, db)`);
818+
if (operation === 'postUpdate') {
819+
// 'postUpdate' policies are not delegated to relations, just use constant `false` here
820+
// e.g.:
821+
// @@allow('all', check(author)) should not delegate "postUpdate" to author
822+
this.writer.write(`${fieldRef.target.$refText}: ${FALSE}`);
823+
} else {
824+
const targetGuardFunc = getQueryGuardFunctionName(targetModel, undefined, false, operation);
825+
this.writer.write(`${fieldRef.target.$refText}: ${targetGuardFunc}(context, db)`);
826+
}
820827
});
821828
}
822829
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
describe('issue 1642', () => {
3+
it('regression', async () => {
4+
const { prisma, enhance } = await loadSchema(
5+
`
6+
model User {
7+
id Int @id
8+
name String
9+
posts Post[]
10+
11+
@@allow('read', true)
12+
@@allow('all', auth().id == 1)
13+
}
14+
15+
model Post {
16+
id Int @id
17+
title String
18+
description String
19+
author User @relation(fields: [authorId], references: [id])
20+
authorId Int
21+
22+
// delegate all access policies to the author:
23+
@@allow('all', check(author))
24+
25+
@@allow('update', future().title == 'hello')
26+
}
27+
`
28+
);
29+
30+
await prisma.user.create({ data: { id: 1, name: 'User1' } });
31+
await prisma.post.create({ data: { id: 1, title: 'hello', description: 'desc1', authorId: 1 } });
32+
33+
const db = enhance({ id: 2 });
34+
await expect(
35+
db.post.update({ where: { id: 1 }, data: { title: 'world', description: 'desc2' } })
36+
).toBeRejectedByPolicy();
37+
38+
await expect(db.post.update({ where: { id: 1 }, data: { description: 'desc2' } })).toResolveTruthy();
39+
});
40+
});

0 commit comments

Comments
 (0)