forked from zenstackhq/zenstack
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: short-circuit post-read check when policy rules don't depend on …
…model fields (zenstackhq#376)
- Loading branch information
Showing
19 changed files
with
190 additions
and
77 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,9 +1,10 @@ | ||
import { Expression, isInvocationExpr } from '@zenstackhq/language/ast'; | ||
import { isInvocationExpr } from '@zenstackhq/language/ast'; | ||
import { AstNode } from 'langium/lib/syntax-tree'; | ||
import { isFromStdlib } from '../../language-server/utils'; | ||
|
||
/** | ||
* Returns if the given expression is a "future()" method call. | ||
*/ | ||
export function isFutureExpr(expr: Expression) { | ||
return !!(isInvocationExpr(expr) && expr.function.ref?.name === 'future' && isFromStdlib(expr.function.ref)); | ||
export function isFutureExpr(node: AstNode) { | ||
return !!(isInvocationExpr(node) && node.function.ref?.name === 'future' && isFromStdlib(node.function.ref)); | ||
} |
This file contains 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 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,71 @@ | ||
import { loadSchema } from '@zenstackhq/testtools'; | ||
|
||
describe('Policy plugin tests', () => { | ||
let origDir: string; | ||
|
||
beforeEach(() => { | ||
origDir = process.cwd(); | ||
}); | ||
|
||
afterEach(() => { | ||
process.chdir(origDir); | ||
}); | ||
|
||
it('short-circuit', async () => { | ||
const model = ` | ||
model User { | ||
id String @id @default(cuid()) | ||
value Int | ||
} | ||
model M { | ||
id String @id @default(cuid()) | ||
value Int | ||
@@allow('read', auth() != null) | ||
@@allow('create', auth().value > 0) | ||
@@allow('update', auth() != null) | ||
@@deny('update', auth().value == null || auth().value <= 0) | ||
} | ||
`; | ||
|
||
const { policy } = await loadSchema(model); | ||
|
||
expect(policy.guard.m.read({ user: undefined })).toEqual(false); | ||
expect(policy.guard.m.read({ user: { id: '1' } })).toEqual(true); | ||
|
||
expect(policy.guard.m.create({ user: undefined })).toEqual(false); | ||
expect(policy.guard.m.create({ user: { id: '1' } })).toEqual(false); | ||
expect(policy.guard.m.create({ user: { id: '1', value: 0 } })).toEqual(false); | ||
expect(policy.guard.m.create({ user: { id: '1', value: 1 } })).toEqual(true); | ||
|
||
expect(policy.guard.m.update({ user: undefined })).toEqual(false); | ||
expect(policy.guard.m.update({ user: { id: '1' } })).toEqual(false); | ||
expect(policy.guard.m.update({ user: { id: '1', value: 0 } })).toEqual(false); | ||
expect(policy.guard.m.update({ user: { id: '1', value: 1 } })).toEqual(true); | ||
}); | ||
|
||
it('no short-circuit', async () => { | ||
const model = ` | ||
model User { | ||
id String @id @default(cuid()) | ||
value Int | ||
} | ||
model M { | ||
id String @id @default(cuid()) | ||
value Int | ||
@@allow('read', auth() != null && value > 0) | ||
} | ||
`; | ||
|
||
const { policy } = await loadSchema(model); | ||
|
||
expect(policy.guard.m.read({ user: undefined })).toEqual( | ||
expect.objectContaining({ AND: [{ zenstack_guard: false }, { value: { gt: 0 } }] }) | ||
); | ||
expect(policy.guard.m.read({ user: { id: '1' } })).toEqual( | ||
expect.objectContaining({ AND: [{ zenstack_guard: true }, { value: { gt: 0 } }] }) | ||
); | ||
}); | ||
}); |
Oops, something went wrong.