Skip to content

Commit 200fa0e

Browse files
masenfadhami3310
authored andcommitted
Restrict update_vars_internal to browser storage vars
Only allow reflex API event `update_vars_internal` to update vars associated with client storage values.
1 parent 7a74550 commit 200fa0e

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

reflex/state.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,17 +1404,37 @@ def reset(self):
14041404
for substate in self.substates.values():
14051405
substate.reset()
14061406

1407+
@classmethod
1408+
@functools.lru_cache
1409+
def _is_client_storage(cls, prop_name_or_field: str | ModelField) -> bool:
1410+
"""Check if the var is a client storage var.
1411+
1412+
Args:
1413+
prop_name_or_field: The name of the var or the field itself.
1414+
1415+
Returns:
1416+
Whether the var is a client storage var.
1417+
"""
1418+
if isinstance(prop_name_or_field, str):
1419+
field = cls.get_fields().get(prop_name_or_field)
1420+
else:
1421+
field = prop_name_or_field
1422+
return field is not None and (
1423+
isinstance(field.default, ClientStorageBase)
1424+
or (
1425+
isinstance(field.type_, type)
1426+
and issubclass(field.type_, ClientStorageBase)
1427+
)
1428+
)
1429+
14071430
def _reset_client_storage(self):
14081431
"""Reset client storage base vars to their default values."""
14091432
# Client-side storage is reset during hydrate so that clearing cookies
14101433
# on the browser also resets the values on the backend.
14111434
fields = self.get_fields()
14121435
for prop_name in self.base_vars:
14131436
field = fields[prop_name]
1414-
if isinstance(field.default, ClientStorageBase) or (
1415-
isinstance(field.type_, type)
1416-
and issubclass(field.type_, ClientStorageBase)
1417-
):
1437+
if self._is_client_storage(field):
14181438
setattr(self, prop_name, copy.deepcopy(field.default))
14191439

14201440
# Recursively reset the substate client storage.
@@ -2361,8 +2381,9 @@ async def update_vars_internal(self, vars: dict[str, Any]) -> None:
23612381
for var, value in vars.items():
23622382
state_name, _, var_name = var.rpartition(".")
23632383
var_state_cls = State.get_class_substate(state_name)
2364-
var_state = await self.get_state(var_state_cls)
2365-
setattr(var_state, var_name, value)
2384+
if var_state_cls._is_client_storage(var_name):
2385+
var_state = await self.get_state(var_state_cls)
2386+
setattr(var_state, var_name, value)
23662387

23672388

23682389
class OnLoadInternalState(State):

0 commit comments

Comments
 (0)