Skip to content

Commit fb6653e

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 fea0ceb commit fb6653e

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
@@ -1384,17 +1384,37 @@ def reset(self):
13841384
for substate in self.substates.values():
13851385
substate.reset()
13861386

1387+
@classmethod
1388+
@functools.lru_cache
1389+
def _is_client_storage(cls, prop_name_or_field: str | ModelField) -> bool:
1390+
"""Check if the var is a client storage var.
1391+
1392+
Args:
1393+
prop_name_or_field: The name of the var or the field itself.
1394+
1395+
Returns:
1396+
Whether the var is a client storage var.
1397+
"""
1398+
if isinstance(prop_name_or_field, str):
1399+
field = cls.get_fields().get(prop_name_or_field)
1400+
else:
1401+
field = prop_name_or_field
1402+
return field is not None and (
1403+
isinstance(field.default, ClientStorageBase)
1404+
or (
1405+
isinstance(field.type_, type)
1406+
and issubclass(field.type_, ClientStorageBase)
1407+
)
1408+
)
1409+
13871410
def _reset_client_storage(self):
13881411
"""Reset client storage base vars to their default values."""
13891412
# Client-side storage is reset during hydrate so that clearing cookies
13901413
# on the browser also resets the values on the backend.
13911414
fields = self.get_fields()
13921415
for prop_name in self.base_vars:
13931416
field = fields[prop_name]
1394-
if isinstance(field.default, ClientStorageBase) or (
1395-
isinstance(field.type_, type)
1396-
and issubclass(field.type_, ClientStorageBase)
1397-
):
1417+
if self._is_client_storage(field):
13981418
setattr(self, prop_name, copy.deepcopy(field.default))
13991419

14001420
# Recursively reset the substate client storage.
@@ -2405,8 +2425,9 @@ async def update_vars_internal(self, vars: dict[str, Any]) -> None:
24052425
for var, value in vars.items():
24062426
state_name, _, var_name = var.rpartition(".")
24072427
var_state_cls = State.get_class_substate(state_name)
2408-
var_state = await self.get_state(var_state_cls)
2409-
setattr(var_state, var_name, value)
2428+
if var_state_cls._is_client_storage(var_name):
2429+
var_state = await self.get_state(var_state_cls)
2430+
setattr(var_state, var_name, value)
24102431

24112432

24122433
class OnLoadInternalState(State):

0 commit comments

Comments
 (0)