Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add defensive checks against data being funny #4633

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions reflex/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1563,10 +1563,36 @@ async def on_event(self, sid, data):
Args:
sid: The Socket.IO session id.
data: The event data.

Raises:
EventDeserializationError: If the event data is not a dictionary.
"""
fields = data
# Get the event.
event = Event(**{k: v for k, v in fields.items() if k in _EVENT_FIELDS})

if isinstance(fields, str):
console.warn(
"Received event data as a string. This generally should not happen and may indicate a bug."
f" Event data: {fields}"
)
try:
fields = json.loads(fields)
except json.JSONDecodeError as ex:
raise exceptions.EventDeserializationError(
f"Failed to deserialize event data: {fields}."
) from ex

if not isinstance(fields, dict):
raise exceptions.EventDeserializationError(
f"Event data must be a dictionary, but received {fields} of type {type(fields)}."
)

try:
# Get the event.
event = Event(**{k: v for k, v in fields.items() if k in _EVENT_FIELDS})
except (TypeError, ValueError) as ex:
raise exceptions.EventDeserializationError(
f"Failed to deserialize event data: {fields}."
) from ex

self.token_to_sid[event.token] = sid
self.sid_to_token[sid] = event.token
Expand Down
4 changes: 4 additions & 0 deletions reflex/utils/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class SystemPackageMissingError(ReflexError):
"""Raised when a system package is missing."""


class EventDeserializationError(ReflexError, ValueError):
"""Raised when an event cannot be deserialized."""


def raise_system_package_missing_error(package: str) -> NoReturn:
"""Raise a SystemPackageMissingError.

Expand Down
Loading