Skip to content

Commit 4347a4b

Browse files
authored
Update backend-only var and auth docs w.r.t. app security (#1348)
* Update backend-only var and auth docs w.r.t. app security * Update docs/authentication/authentication_overview.md
1 parent a59b831 commit 4347a4b

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1+
```python exec
2+
from pcweb.pages.docs import vars
3+
```
4+
15
# Authentication Overview
26

37
Many apps require authentication to manage users. There are a few different ways to accomplish this in Reflex:
48

5-
We have solutions here:
9+
We have solutions that currently exist outside of the core framework:
610

7-
1. Local Auth: Uses your own database: https://github.com/reflex-dev/reflex-examples/tree/main/twitter
8-
2. Google Auth: Uses sign in with Google: https://reflex.dev/blog/2023-10-25-implementing-sign-in-with-google/
11+
1. Local Auth: Uses your own database: https://github.com/masenf/reflex-local-auth
12+
2. Google Auth: Uses sign in with Google: https://github.com/masenf/reflex-google-auth
913
3. Captcha: Generates tests that humans can pass but automated systems cannot: https://github.com/masenf/reflex-google-recaptcha-v2
1014
4. Magic Link Auth: A passwordless login method that sends a unique, one-time-use URL to a user's email: https://github.com/masenf/reflex-magic-link-auth
1115
5. Clerk Auth: A community member wrapped this component and hooked it up in this app: https://github.com/TimChild/reflex-clerk-api
1216

17+
## Guidance for Implementing Authentication
18+
19+
- Store sensitive user tokens and information in [backend-only vars]({vars.base_vars.path}#backend-only-vars).
20+
- Validate user session and permissions for each event handler that performs an authenticated action and all computed vars or loader events that access private data.
21+
- All content that is statically rendered in the frontend (for example, data hardcoded or loaded at compile time in the UI) will be publicly available, even if the page redirects to a login or uses `rx.cond` to hide content.
22+
- Only data that originates from state can be truly private and protected.
23+
- When using cookies or local storage, a signed JWT can detect and invalidate any local tampering.
24+
1325
More auth documentation on the way. Check back soon!

docs/vars/base_vars.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,29 @@ def ticker_example():
7777

7878
## Backend-only Vars
7979

80-
Any Var in a state class that starts with an underscore is considered backend
81-
only and will not be synchronized with the frontend. Data associated with a
82-
specific session that is not directly rendered on the frontend should be stored
83-
in a backend-only var to reduce network traffic and improve performance.
80+
Any Var in a state class that starts with an underscore (`_`) is considered backend
81+
only and will **not be synchronized with the frontend**. Data associated with a
82+
specific session that is _not directly rendered on the frontend should be stored
83+
in a backend-only var_ to reduce network traffic and improve performance.
8484

8585
They have the advantage that they don't need to be JSON serializable, however
86-
they must still be cloudpickle-able to be used with redis in prod mode. They are
87-
not directly renderable on the frontend, and may be used to store sensitive
88-
values that should not be sent to the client.
86+
they must still be pickle-able to be used with redis in prod mode. They are
87+
not directly renderable on the frontend, and **may be used to store sensitive
88+
values that should not be sent to the client**.
89+
90+
```md alert warning
91+
# Protect auth data and sensitive state in backend-only vars.
92+
93+
Regular vars and computed vars should **only** be used for rendering the state
94+
of your app in the frontend. Having any type of permissions or authenticated state based on
95+
a regular var presents a security risk as you may assume these have shared control
96+
with the frontend (client) due to default setter methods.
97+
98+
For improved security, `state_auto_setters=False` may be set in `rxconfig.py`
99+
to prevent the automatic generation of setters for regular vars, however, the
100+
client will still be able to locally modify the contents of frontend vars as
101+
they are presented in the UI.
102+
```
89103

90104
For example, a backend-only var is used to store a large data structure which is
91105
then paged to the frontend using cached vars.

0 commit comments

Comments
 (0)