Skip to content

Commit 71abe16

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 12e7b91 commit 71abe16

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

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

14191439
# Recursively reset the substate client storage.
@@ -2360,8 +2380,9 @@ async def update_vars_internal(self, vars: dict[str, Any]) -> None:
23602380
for var, value in vars.items():
23612381
state_name, _, var_name = var.rpartition(".")
23622382
var_state_cls = State.get_class_substate(state_name)
2363-
var_state = await self.get_state(var_state_cls)
2364-
setattr(var_state, var_name, value)
2383+
if var_state_cls._is_client_storage(var_name):
2384+
var_state = await self.get_state(var_state_cls)
2385+
setattr(var_state, var_name, value)
23652386

23662387

23672388
class OnLoadInternalState(State):

0 commit comments

Comments
 (0)