Skip to content

Commit d3812d8

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 73c8dd9 commit d3812d8

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
@@ -1397,17 +1397,37 @@ def reset(self):
13971397
for substate in self.substates.values():
13981398
substate.reset()
13991399

1400+
@classmethod
1401+
@functools.lru_cache
1402+
def _is_client_storage(cls, prop_name_or_field: str | ModelField) -> bool:
1403+
"""Check if the var is a client storage var.
1404+
1405+
Args:
1406+
prop_name_or_field: The name of the var or the field itself.
1407+
1408+
Returns:
1409+
Whether the var is a client storage var.
1410+
"""
1411+
if isinstance(prop_name_or_field, str):
1412+
field = cls.get_fields().get(prop_name_or_field)
1413+
else:
1414+
field = prop_name_or_field
1415+
return field is not None and (
1416+
isinstance(field.default, ClientStorageBase)
1417+
or (
1418+
isinstance(field.type_, type)
1419+
and issubclass(field.type_, ClientStorageBase)
1420+
)
1421+
)
1422+
14001423
def _reset_client_storage(self):
14011424
"""Reset client storage base vars to their default values."""
14021425
# Client-side storage is reset during hydrate so that clearing cookies
14031426
# on the browser also resets the values on the backend.
14041427
fields = self.get_fields()
14051428
for prop_name in self.base_vars:
14061429
field = fields[prop_name]
1407-
if isinstance(field.default, ClientStorageBase) or (
1408-
isinstance(field.type_, type)
1409-
and issubclass(field.type_, ClientStorageBase)
1410-
):
1430+
if self._is_client_storage(field):
14111431
setattr(self, prop_name, copy.deepcopy(field.default))
14121432

14131433
# Recursively reset the substate client storage.
@@ -2331,8 +2351,9 @@ async def update_vars_internal(self, vars: dict[str, Any]) -> None:
23312351
for var, value in vars.items():
23322352
state_name, _, var_name = var.rpartition(".")
23332353
var_state_cls = State.get_class_substate(state_name)
2334-
var_state = await self.get_state(var_state_cls)
2335-
setattr(var_state, var_name, value)
2354+
if var_state_cls._is_client_storage(var_name):
2355+
var_state = await self.get_state(var_state_cls)
2356+
setattr(var_state, var_name, value)
23362357

23372358

23382359
class OnLoadInternalState(State):

0 commit comments

Comments
 (0)