|
| 1 | +import { PolicyPlugin } from '@zenstackhq/plugin-policy'; |
| 2 | +import type { ClientContract } from '@zenstackhq/orm'; |
| 3 | +import type { SchemaDef } from '@zenstackhq/orm/schema'; |
| 4 | +import { createTestClient } from '@zenstackhq/testtools'; |
| 5 | +import { sql } from 'kysely'; |
| 6 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 7 | + |
| 8 | +const schema = ` |
| 9 | +model User { |
| 10 | + id String @id |
| 11 | + role String |
| 12 | + secrets Secret[] |
| 13 | +
|
| 14 | + @@allow('all', true) |
| 15 | +} |
| 16 | +
|
| 17 | +model Secret { |
| 18 | + id String @id |
| 19 | + value String |
| 20 | + ownerId String |
| 21 | + owner User @relation(fields: [ownerId], references: [id]) |
| 22 | +
|
| 23 | + @@allow('read', auth() != null && auth().role == 'admin') |
| 24 | + @@allow('create', auth() != null && auth().role == 'admin') |
| 25 | +} |
| 26 | +`; |
| 27 | + |
| 28 | +describe('Policy raw SQL tests', () => { |
| 29 | + const clients: ClientContract<SchemaDef>[] = []; |
| 30 | + |
| 31 | + afterEach(async () => { |
| 32 | + await Promise.all(clients.splice(0).map((client) => client.$disconnect())); |
| 33 | + }); |
| 34 | + |
| 35 | + function ref(client: ClientContract<SchemaDef>, col: string) { |
| 36 | + return client.$schema.provider.type === 'mysql' ? sql.raw(`\`${col}\``) : sql.raw(`"${col}"`); |
| 37 | + } |
| 38 | + |
| 39 | + async function createPolicyClient(options?: { dangerouslyAllowRawSql?: boolean; dbName: string }) { |
| 40 | + const unsafeClient = await createTestClient(schema, { |
| 41 | + dbName: options?.dbName, |
| 42 | + plugins: [new PolicyPlugin({ dangerouslyAllowRawSql: options?.dangerouslyAllowRawSql })], |
| 43 | + }); |
| 44 | + clients.push(unsafeClient); |
| 45 | + |
| 46 | + const rawClient = unsafeClient.$unuseAll(); |
| 47 | + const adminClient = unsafeClient.$setAuth({ id: 'admin', role: 'admin' }); |
| 48 | + |
| 49 | + await rawClient.user.create({ |
| 50 | + data: { |
| 51 | + id: 'admin', |
| 52 | + role: 'admin', |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + return { adminClient }; |
| 57 | + } |
| 58 | + |
| 59 | + it('keeps rejecting raw SQL by default', async () => { |
| 60 | + const { adminClient } = await createPolicyClient({ dbName: 'policy_raw_sql_default' }); |
| 61 | + |
| 62 | + await expect( |
| 63 | + adminClient.$transaction(async (tx) => { |
| 64 | + await tx.secret.create({ |
| 65 | + data: { |
| 66 | + id: 'secret-default', |
| 67 | + ownerId: 'admin', |
| 68 | + value: 'still-guarded', |
| 69 | + }, |
| 70 | + }); |
| 71 | + |
| 72 | + await tx.$queryRaw<{ value: string }[]>` |
| 73 | + SELECT ${ref(tx, 'Secret')}.${ref(tx, 'value')} |
| 74 | + FROM ${ref(tx, 'Secret')} |
| 75 | + WHERE ${ref(tx, 'Secret')}.${ref(tx, 'id')} = ${'secret-default'} |
| 76 | + `; |
| 77 | + }), |
| 78 | + ).rejects.toThrow('non-CRUD queries are not allowed'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('allows raw SQL inside a transaction when dangerous raw SQL is enabled', async () => { |
| 82 | + const { adminClient } = await createPolicyClient({ |
| 83 | + dangerouslyAllowRawSql: true, |
| 84 | + dbName: 'policy_raw_sql_dangerous', |
| 85 | + }); |
| 86 | + |
| 87 | + await adminClient.$transaction(async (tx) => { |
| 88 | + await tx.secret.create({ |
| 89 | + data: { |
| 90 | + id: 'secret-1', |
| 91 | + ownerId: 'admin', |
| 92 | + value: 'top-secret', |
| 93 | + }, |
| 94 | + }); |
| 95 | + |
| 96 | + const rows = await tx.$queryRaw<{ value: string }[]>` |
| 97 | + SELECT ${ref(tx, 'Secret')}.${ref(tx, 'value')} |
| 98 | + FROM ${ref(tx, 'Secret')} |
| 99 | + WHERE ${ref(tx, 'Secret')}.${ref(tx, 'id')} = ${'secret-1'} |
| 100 | + `; |
| 101 | + |
| 102 | + expect(rows).toEqual([{ value: 'top-secret' }]); |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments