Skip to content

Commit 249373e

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 5518d92 commit 249373e

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
@@ -1400,17 +1400,37 @@ def reset(self):
14001400
for substate in self.substates.values():
14011401
substate.reset()
14021402

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

14161436
# Recursively reset the substate client storage.
@@ -2391,8 +2411,9 @@ async def update_vars_internal(self, vars: dict[str, Any]) -> None:
23912411
for var, value in vars.items():
23922412
state_name, _, var_name = var.rpartition(".")
23932413
var_state_cls = State.get_class_substate(state_name)
2394-
var_state = await self.get_state(var_state_cls)
2395-
setattr(var_state, var_name, value)
2414+
if var_state_cls._is_client_storage(var_name):
2415+
var_state = await self.get_state(var_state_cls)
2416+
setattr(var_state, var_name, value)
23962417

23972418

23982419
class OnLoadInternalState(State):

0 commit comments

Comments
 (0)