Skip to content

Commit ea6c580

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 f34dda2 commit ea6c580

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
@@ -1412,17 +1412,37 @@ def reset(self):
14121412
for substate in self.substates.values():
14131413
substate.reset()
14141414

1415+
@classmethod
1416+
@functools.lru_cache
1417+
def _is_client_storage(cls, prop_name_or_field: str | ModelField) -> bool:
1418+
"""Check if the var is a client storage var.
1419+
1420+
Args:
1421+
prop_name_or_field: The name of the var or the field itself.
1422+
1423+
Returns:
1424+
Whether the var is a client storage var.
1425+
"""
1426+
if isinstance(prop_name_or_field, str):
1427+
field = cls.get_fields().get(prop_name_or_field)
1428+
else:
1429+
field = prop_name_or_field
1430+
return field is not None and (
1431+
isinstance(field.default, ClientStorageBase)
1432+
or (
1433+
isinstance(field.type_, type)
1434+
and issubclass(field.type_, ClientStorageBase)
1435+
)
1436+
)
1437+
14151438
def _reset_client_storage(self):
14161439
"""Reset client storage base vars to their default values."""
14171440
# Client-side storage is reset during hydrate so that clearing cookies
14181441
# on the browser also resets the values on the backend.
14191442
fields = self.get_fields()
14201443
for prop_name in self.base_vars:
14211444
field = fields[prop_name]
1422-
if isinstance(field.default, ClientStorageBase) or (
1423-
isinstance(field.type_, type)
1424-
and issubclass(field.type_, ClientStorageBase)
1425-
):
1445+
if self._is_client_storage(field):
14261446
setattr(self, prop_name, copy.deepcopy(field.default))
14271447

14281448
# Recursively reset the substate client storage.
@@ -2330,8 +2350,9 @@ async def update_vars_internal(self, vars: dict[str, Any]) -> None:
23302350
for var, value in vars.items():
23312351
state_name, _, var_name = var.rpartition(".")
23322352
var_state_cls = State.get_class_substate(state_name)
2333-
var_state = await self.get_state(var_state_cls)
2334-
setattr(var_state, var_name, value)
2353+
if var_state_cls._is_client_storage(var_name):
2354+
var_state = await self.get_state(var_state_cls)
2355+
setattr(var_state, var_name, value)
23352356

23362357

23372358
class OnLoadInternalState(State):

0 commit comments

Comments
 (0)