|
| 1 | +import { createPolicyTestClient } from '@zenstackhq/testtools'; |
| 2 | +import { describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +// https://github.com/zenstackhq/zenstack/issues/2674 |
| 5 | +describe('Regression for issue #2674', () => { |
| 6 | + it('@deny on PK should not break include (HasMany)', async () => { |
| 7 | + const db = await createPolicyTestClient( |
| 8 | + ` |
| 9 | +model User { |
| 10 | + id String @id @default(cuid()) |
| 11 | + role String |
| 12 | + @@allow('all', true) |
| 13 | +} |
| 14 | +
|
| 15 | +model Post { |
| 16 | + id Int @id @default(autoincrement()) @deny('all', auth().role != 'ADMIN') |
| 17 | + title String @unique |
| 18 | + comments Comment[] |
| 19 | + @@allow('all', true) |
| 20 | +} |
| 21 | +
|
| 22 | +model Comment { |
| 23 | + id Int @id @default(autoincrement()) |
| 24 | + content String |
| 25 | + postId Int |
| 26 | + post Post @relation(fields: [postId], references: [id]) |
| 27 | + @@allow('all', true) |
| 28 | +} |
| 29 | + `, |
| 30 | + ); |
| 31 | + |
| 32 | + const admin = { id: 'admin-1', role: 'ADMIN' }; |
| 33 | + const user = { id: 'user-1', role: 'USER' }; |
| 34 | + |
| 35 | + await db.$setAuth(admin).post.create({ |
| 36 | + data: { |
| 37 | + title: 'Test Post', |
| 38 | + comments: { create: [{ content: 'Comment 1' }, { content: 'Comment 2' }] }, |
| 39 | + }, |
| 40 | + }); |
| 41 | + |
| 42 | + // Admin sees everything normally |
| 43 | + const adminResult = await db.$setAuth(admin).post.findUnique({ |
| 44 | + where: { title: 'Test Post' }, |
| 45 | + include: { comments: true }, |
| 46 | + }); |
| 47 | + expect(adminResult?.id).toBeGreaterThan(0); |
| 48 | + expect(adminResult?.comments).toHaveLength(2); |
| 49 | + |
| 50 | + // USER role: id is denied (returns null) but include should still populate comments |
| 51 | + const userResult = await db.$setAuth(user).post.findUnique({ |
| 52 | + where: { title: 'Test Post' }, |
| 53 | + include: { comments: true }, |
| 54 | + }); |
| 55 | + expect(userResult?.id).toBeNull(); |
| 56 | + expect(userResult?.comments).toHaveLength(2); |
| 57 | + }); |
| 58 | + |
| 59 | + it('@deny on FK hides the BelongsTo relation (by design)', async () => { |
| 60 | + const db = await createPolicyTestClient( |
| 61 | + ` |
| 62 | +model User { |
| 63 | + id String @id @default(cuid()) |
| 64 | + role String |
| 65 | + @@allow('all', true) |
| 66 | +} |
| 67 | +
|
| 68 | +model Post { |
| 69 | + id Int @id @default(autoincrement()) |
| 70 | + title String |
| 71 | + comments Comment[] |
| 72 | + @@allow('all', true) |
| 73 | +} |
| 74 | +
|
| 75 | +model Comment { |
| 76 | + id Int @id @default(autoincrement()) |
| 77 | + content String |
| 78 | + postId Int @deny('all', auth().role != 'ADMIN') |
| 79 | + post Post @relation(fields: [postId], references: [id]) |
| 80 | + @@allow('all', true) |
| 81 | +} |
| 82 | + `, |
| 83 | + ); |
| 84 | + |
| 85 | + const admin = { id: 'admin-1', role: 'ADMIN' }; |
| 86 | + const user = { id: 'user-1', role: 'USER' }; |
| 87 | + |
| 88 | + const post = await db.$setAuth(admin).post.create({ |
| 89 | + data: { title: 'Test Post' }, |
| 90 | + }); |
| 91 | + |
| 92 | + const comment = await db.$setAuth(admin).comment.create({ |
| 93 | + data: { content: 'A comment', postId: post.id }, |
| 94 | + }); |
| 95 | + |
| 96 | + // USER role: postId is denied — both the FK value and the relation are hidden (by design) |
| 97 | + const userResult = await db.$setAuth(user).comment.findUnique({ |
| 98 | + where: { id: comment.id }, |
| 99 | + include: { post: true }, |
| 100 | + }); |
| 101 | + expect(userResult?.postId).toBeNull(); |
| 102 | + expect(userResult?.post).toBeNull(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('@deny on both PK and FK should not break include', async () => { |
| 106 | + const db = await createPolicyTestClient( |
| 107 | + ` |
| 108 | +model User { |
| 109 | + id String @id @default(cuid()) |
| 110 | + role String |
| 111 | + @@allow('all', true) |
| 112 | +} |
| 113 | +
|
| 114 | +model Post { |
| 115 | + id Int @id @default(autoincrement()) @deny('all', auth().role != 'ADMIN') |
| 116 | + title String @unique |
| 117 | + comments Comment[] |
| 118 | + @@allow('all', true) |
| 119 | +} |
| 120 | +
|
| 121 | +model Comment { |
| 122 | + id Int @id @default(autoincrement()) |
| 123 | + content String |
| 124 | + postId Int @deny('all', auth().role != 'ADMIN') |
| 125 | + post Post @relation(fields: [postId], references: [id]) |
| 126 | + @@allow('all', true) |
| 127 | +} |
| 128 | + `, |
| 129 | + ); |
| 130 | + |
| 131 | + const admin = { id: 'admin-1', role: 'ADMIN' }; |
| 132 | + const user = { id: 'user-1', role: 'USER' }; |
| 133 | + |
| 134 | + await db.$setAuth(admin).post.create({ |
| 135 | + data: { |
| 136 | + title: 'Test Post', |
| 137 | + comments: { create: [{ content: 'C1' }, { content: 'C2' }] }, |
| 138 | + }, |
| 139 | + }); |
| 140 | + |
| 141 | + // Find by non-denied field (title) so the WHERE is not affected by @deny on id |
| 142 | + const userResult = await db.$setAuth(user).post.findUnique({ |
| 143 | + where: { title: 'Test Post' }, |
| 144 | + include: { comments: true }, |
| 145 | + }); |
| 146 | + expect(userResult?.id).toBeNull(); |
| 147 | + expect(userResult?.comments).toHaveLength(2); |
| 148 | + expect(userResult?.comments[0]?.postId).toBeNull(); |
| 149 | + }); |
| 150 | +}); |
0 commit comments