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

Better Pattern for Protected State Vars #8

Open
kroo opened this issue Jul 15, 2024 · 0 comments
Open

Better Pattern for Protected State Vars #8

kroo opened this issue Jul 15, 2024 · 0 comments

Comments

@kroo
Copy link
Owner

kroo commented Jul 15, 2024

Investigate the following two methods for ensuring state vars are not leaked to unauthenticated users:

  1. Inherit from a "protected state" class, which does not allow any of the fields to be returned unless the user is logged in.
class State(clerk.ClerkProtectedState):
    field1: int
    field2: str
  1. Annotate a state class or var with a decorator, perhaps named auth_required, or protect, which allows you to specify 'permission' or 'role', similar to the react component:
@protect(permission="org:invoices:create")
class State(rx.State):
    field1: int
    field2: str


class State2(rx.State):
    field1: Protected[int, Role("org:billing")]
    field2: Protected[str, Permission("org:invoices:create")]
    field3: Protected[bool]

    @rx.var
    @protect(role="org:billing")
    def field4(self) -> str:
        return self._field2

Approach 1 is simple, encourages the substate behavior described above, and makes accessing the signed in user information straight forward... Approach 2 feels pretty magical, and allows for much finer-grained permissions, but has the dangerous default of allowing information to leak if you don't wrap one of the fields...

(Discord thread here)

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

No branches or pull requests

1 participant