Skip to content
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

[Feature Request] Automatically do a database fetch if the policy rules use fields not provided in the user context object #820

Open
ymc9 opened this issue Nov 10, 2023 · 1 comment
Labels
Milestone

Comments

@ymc9
Copy link
Member

ymc9 commented Nov 10, 2023

Background

One of the traps that ZenStack users frequently fall into is not providing all the fields needed in the user context object when calling enhance. Today, ZenStack doesn't do anything special when dealing with auth() expressions in access policy rules. It just translates it to the user context you provided.

const db = enhance(prisma, { user: { id: userId } });
model Post {
  ...
  @@allow('all', auth().role == 'ADMIN')  // <- this won't work
}

You're responsible for making sure all fields accessed from auth() are available.

It caused two problems:

  1. When you forget to do that, you get unexpected authorization results.
  2. There's no typing guarantee. The auth() call is resolved to the User model (or a model marked @@auth), which causes the illusion that it at least has all non-optional fields of the model. It's not the case if you don't provide those fields.

Proposed Solution

  1. Make enhance() automatically analyze what fields are accessed from auth() and do a database fetch for the missing ones.
  2. This means its signature needs to be changed to return a Promise.
  3. Or, we can make the feature opt-in and introduce an overloaded version of enhance() that enables the auto-fetching and returns a Promise, and keep the original one unchanged (thus no breaking changes).
@ymc9 ymc9 added this to the v1.3.0 milestone Nov 10, 2023
@ymc9
Copy link
Member Author

ymc9 commented Nov 15, 2023

Just realized to do that we need to change enhance to an async API. It'll be a breaking change. Punting to V2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant