Skip to content

Commit e620357

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 98171a5 commit e620357

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

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

14181438
# Recursively reset the substate client storage.
@@ -2393,8 +2413,9 @@ async def update_vars_internal(self, vars: dict[str, Any]) -> None:
23932413
for var, value in vars.items():
23942414
state_name, _, var_name = var.rpartition(".")
23952415
var_state_cls = State.get_class_substate(state_name)
2396-
var_state = await self.get_state(var_state_cls)
2397-
setattr(var_state, var_name, value)
2416+
if var_state_cls._is_client_storage(var_name):
2417+
var_state = await self.get_state(var_state_cls)
2418+
setattr(var_state, var_name, value)
23982419

23992420

24002421
class OnLoadInternalState(State):

0 commit comments

Comments
 (0)