Skip to content

Serialize local vars early on #2117

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

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 5 additions & 1 deletion sentry_sdk/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,11 @@ def _prepare_event(
# Postprocess the event here so that annotated types do
# generally not surface in before_send
if event is not None:
event = serialize(event, request_bodies=self.options.get("request_bodies"))
event = serialize(
event,
request_bodies=self.options.get("request_bodies"),
ignore_local_vars=True,
)

before_send = self.options["before_send"]
if (
Expand Down
4 changes: 3 additions & 1 deletion sentry_sdk/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ def serialize(event, **kwargs):
meta_stack = [] # type: List[Dict[str, Any]]

keep_request_bodies = kwargs.pop("request_bodies", None) == "always" # type: bool
ignore_local_vars = kwargs.pop("ignore_local_vars", False)

def _annotate(**meta):
# type: (**Any) -> None
Expand Down Expand Up @@ -380,7 +381,8 @@ def _serialize_node_impl(
return rv_list

if should_repr_strings:
obj = safe_repr(obj)
if not ignore_local_vars:
obj = safe_repr(obj)
else:
if isinstance(obj, bytes) or isinstance(obj, bytearray):
obj = obj.decode("utf-8", "replace")
Expand Down
6 changes: 5 additions & 1 deletion sentry_sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,11 @@ def serialize_frame(
)

if include_local_variables:
rv["vars"] = frame.f_locals
# repr local vars here already to avoid changing stuff
# by reference later (e.g. when scrubbing PII)
from sentry_sdk.serializer import serialize

rv["vars"] = serialize(frame.f_locals, should_repr_strings=True)

return rv

Expand Down
8 changes: 8 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,11 @@ def test_include_source_context_when_serializing_frame(include_source_context):
assert include_source_context ^ ("pre_context" in result) ^ True
assert include_source_context ^ ("context_line" in result) ^ True
assert include_source_context ^ ("post_context" in result) ^ True


def test_local_vars_serialized_when_serializing_frame():
local_var = lambda: "cats" # noqa: F841
frame = sys._getframe()
result = serialize_frame(frame)

assert all(isinstance(val, str) for val in result["vars"].values())