-
-
Notifications
You must be signed in to change notification settings - Fork 122
feat: permissions checker #966
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
feat: permissions checker #966
Conversation
Co-authored-by: ymc9 <[email protected]> Co-authored-by: Yiming <[email protected]>
…e AST cloning from base models more robust (zenstackhq#957)
In function permissionsChecker(args: any, user?: AuthUser) {
return {
post: {
read: {
OR: [
{
field: args.authorId,
operator: "===", // or comparator ?
value: user?.id,
},
{
field: args.author?.id,
operator: "===",
value: user?.id,
},
{
field: args.author?.name,
operator: "===",
value: user?.name,
},
],
},
delete: {
AND: [
// {
// 'author': { // or nested like this ?
// 'name': {
// 'operator': '===',
// 'value': user.name
// }
// }
// },
// 'AND': [
// {
// 'score': {
// 'operator': '<',
// 'value': 10
// }
// },
// {
// 'role': {
// 'operator': 'in',
// 'value': ['ADMIN', 'REVIEWER']
// }
// }
// ]
],
},
// note for myself
create: { AND: [] }, // return true
update: { OR: [] }, // return false
},
user: {
create: true,
update: false,
read: true,
delete: false,
},
};
}
const policy: PolicyDef = {
guard: {...},
validation: {...},
permissions: permissionsChecker,
};
export default policy; |
Imagined implementation details / steps:
|
Thinking again about I should instead return an object with inline conditions, like this : function permissionsChecker(args: any, user?: AuthUser) {
return {
post: {
read: args.authorId === user?.id || args.author?.id === user?.id || args.author?.name === user?.name,
delete: true,
// ...
},
user: {
// ...
},
};
} I don't know why I love making things more complicated than they need to be ^^ To handle undefined args, does anyone have an elegant solution? I believe I have no choice but to create a wrapper for each one: define(args.authorId) === user?.id || define(args.author?.id) === user?.id || define(args.author?.name) === user?.name |
Looking forward for this feature. At the moment do we have a workaround for this feature? |
For now, the only solution I have is to manually check the property on the authenticated user/resources. This is problematic as it is not synchronized with our zmodel schemas. I have planned to dedicate tomorrow to establishing the basics of this, I will share my progress in any case. |
What I have in mind to build the permissions themselves:
// `@@deny` / `@@allow` → deny first
!(denyCondition || denyCondition) && (allowCondition || allowCondition) Transformation rules:
user?.id === 1
args?.status === "public"
user?.id === undefined
user?.id === (args?.author?.id || args?.authorId) || user?.email === args?.author?.email
define(args?.authorId) === user?.id || define(args?.author?.id) === user?.id
// enhancedDb.space.check("read", { where: {id: 1} })
args?.id === define(user?.spaceId)
// enhancedDb.space.check("read", { where: {name: "best-space"} })
args?.name === define(user?.space?.name)
|
Hi @Azzerty23 , thanks for continuing to work on it. My apologies for the late response. I've been thinking about this feature for a while, and I think there are several questions to be answered before getting into details. 1. What's the semantic of "check"?Let's say if we call
This effectively means whether the "read" policy has any self-contradiction, like:
Note that this can also involve relations like:
If we accept this semantic, other CRUD operations should be consistent:
2. Should we allow more fine-grained checks?Just checking for CRUD is very coarse, and I believe there will be many scenarios where one wants to check for a smaller scope by providing "partial conditions". Like:
To support that, we can simply merge the user-provided conditions with policy rules and check if it's satisfiable. 3. How to implement the check?With the interpretation above, permission checking seems to become "constraint solving". The policy rules and user-provided conditions together form a set of constraints to satisfy. If we can find a model value (a solution) that satisfies the conditions, we can return Implementing such a solver can be quite tricky. We should probably use an existing constraint solver library (like Microsoft Z3) to get the job done. In a nut shell it let's you do things like: const x = Int.const('x');
const solver = new Solver();
solver.add(And(x.ge(0), x.le(9)));
console.log(await solver.check()); If so we can just encode the policy expressions into constraint solver's input and let it do the magic. Over all things, I'm most unsure about #2. To be honest I don't have very good ideas about how the API will be used in real-world applications. Maybe we should create a channel/thread to gather from people who need it? |
Thank you @ymc9 I had not imagined such a use case where we would add constraints/conditions to the permission checker. In my understanding, all 'constraints' were supposed to be present in the zmodel schema, and we would check their equality against actual values/available values at runtime. But you are absolutely right; let me take a closer look at this constraint solver library. |
To address each of your points:
Let me create the thread in Discord. EDIT: please share your feedbacks here: https://discord.com/channels/1035538056146595961/1090570544186933258/threads/1203297724921946162 |
I compared kiwi and Z3 and decided to proceed with Z3 because the syntax is simpler, and there are many missing features in Kiwi (such as the OR condition). Here are my experiments with Z3: https://github.com/Azzerty23/z3-test/blob/main/src/solver.ts. I faced challenges before finding the solution to handle the process that is not automatically terminated. However, I now have a better idea how to implement the feature. Hopefully, I will find time in a week or two to continue working on it and complete the task. |
Important Auto Review SkippedDraft detected. Please check the settings in the CodeRabbit UI or the To trigger a single review, invoke the Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
39aad38
to
e1497c0
Compare
Hi @Azzerty23 , I went through Z3 documentation and thought about how to model constraints with it too. I've captured my thoughts in this repo: https://github.com/ymc9/z3-exp I think it overlaps with your draft implementation in many ways, but with some differences too. Hope it helps. There might be things we need to discuss in more details. |
No, Z3 handles string and array constraints natively :o I used the playground as a reference and dove headfirst into the high-level API, knowing it had limitations, but without delving into them in detail... Thank you for taking the time to explain everything and for looking at my implementation. I'll study it more carefully tonight. |
Draft for Permissions checker / issue #242