-
-
Notifications
You must be signed in to change notification settings - Fork 134
fix(policy): allow dangerous raw SQL opt-in #2502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+128
−2
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
41a503f
fix(policy): allow raw sql opt-in
pkudinov 48cc5a9
test(policy): move raw sql regression to e2e
pkudinov cd78922
chore: restore untouched policy package files
pkudinov cc4504b
refactor(policy): move raw sql allowance into handler
pkudinov f3b8a6b
refactor(policy): extract plugin options type
pkudinov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| export type PolicyPluginOptions = { | ||
| /** | ||
| * Dangerously bypasses access-policy enforcement for raw SQL queries. | ||
| * Raw queries remain in the current transaction, but the policy plugin will | ||
| * not inspect or reject them. | ||
| */ | ||
| dangerouslyAllowRawSql?: boolean; | ||
| }; |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { PolicyPlugin } from '@zenstackhq/plugin-policy'; | ||
| import type { ClientContract } from '@zenstackhq/orm'; | ||
| import type { SchemaDef } from '@zenstackhq/orm/schema'; | ||
| import { createTestClient } from '@zenstackhq/testtools'; | ||
| import { sql } from 'kysely'; | ||
| import { afterEach, describe, expect, it } from 'vitest'; | ||
|
|
||
| const schema = ` | ||
| model User { | ||
| id String @id | ||
| role String | ||
| secrets Secret[] | ||
|
|
||
| @@allow('all', true) | ||
| } | ||
|
|
||
| model Secret { | ||
| id String @id | ||
| value String | ||
| ownerId String | ||
| owner User @relation(fields: [ownerId], references: [id]) | ||
|
|
||
| @@allow('read', auth() != null && auth().role == 'admin') | ||
| @@allow('create', auth() != null && auth().role == 'admin') | ||
| } | ||
| `; | ||
|
|
||
| describe('Policy raw SQL tests', () => { | ||
| const clients: ClientContract<SchemaDef>[] = []; | ||
|
|
||
| afterEach(async () => { | ||
| await Promise.all(clients.splice(0).map((client) => client.$disconnect())); | ||
| }); | ||
|
|
||
| function ref(client: ClientContract<SchemaDef>, col: string) { | ||
| return client.$schema.provider.type === 'mysql' ? sql.raw(`\`${col}\``) : sql.raw(`"${col}"`); | ||
| } | ||
|
|
||
| async function createPolicyClient(options?: { dangerouslyAllowRawSql?: boolean; dbName: string }) { | ||
| const unsafeClient = await createTestClient(schema, { | ||
| dbName: options?.dbName, | ||
| plugins: [new PolicyPlugin({ dangerouslyAllowRawSql: options?.dangerouslyAllowRawSql })], | ||
| }); | ||
| clients.push(unsafeClient); | ||
|
|
||
| const rawClient = unsafeClient.$unuseAll(); | ||
| const adminClient = unsafeClient.$setAuth({ id: 'admin', role: 'admin' }); | ||
|
|
||
| await rawClient.user.create({ | ||
| data: { | ||
| id: 'admin', | ||
| role: 'admin', | ||
| }, | ||
| }); | ||
|
|
||
| return { adminClient }; | ||
| } | ||
|
|
||
| it('keeps rejecting raw SQL by default', async () => { | ||
| const { adminClient } = await createPolicyClient({ dbName: 'policy_raw_sql_default' }); | ||
|
|
||
| await expect( | ||
| adminClient.$transaction(async (tx) => { | ||
| await tx.secret.create({ | ||
| data: { | ||
| id: 'secret-default', | ||
| ownerId: 'admin', | ||
| value: 'still-guarded', | ||
| }, | ||
| }); | ||
|
|
||
| await tx.$queryRaw<{ value: string }[]>` | ||
| SELECT ${ref(tx, 'Secret')}.${ref(tx, 'value')} | ||
| FROM ${ref(tx, 'Secret')} | ||
| WHERE ${ref(tx, 'Secret')}.${ref(tx, 'id')} = ${'secret-default'} | ||
| `; | ||
| }), | ||
| ).rejects.toThrow('non-CRUD queries are not allowed'); | ||
| }); | ||
|
|
||
| it('allows raw SQL inside a transaction when dangerous raw SQL is enabled', async () => { | ||
| const { adminClient } = await createPolicyClient({ | ||
| dangerouslyAllowRawSql: true, | ||
| dbName: 'policy_raw_sql_dangerous', | ||
| }); | ||
|
|
||
| await adminClient.$transaction(async (tx) => { | ||
| await tx.secret.create({ | ||
| data: { | ||
| id: 'secret-1', | ||
| ownerId: 'admin', | ||
| value: 'top-secret', | ||
| }, | ||
| }); | ||
|
|
||
| const rows = await tx.$queryRaw<{ value: string }[]>` | ||
| SELECT ${ref(tx, 'Secret')}.${ref(tx, 'value')} | ||
| FROM ${ref(tx, 'Secret')} | ||
| WHERE ${ref(tx, 'Secret')}.${ref(tx, 'id')} = ${'secret-1'} | ||
| `; | ||
|
|
||
| expect(rows).toEqual([{ value: 'top-secret' }]); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.