Repository navigation
Closed
Description
Description
When before_send modifies values on an event, the changes can leak back into the scope.
userandfingerprintare placed into the event by reference (PLACE_VALUE) rather than being cloned, so any modification corrupts the scope directly.tags,extra, andcontextsusePLACE_CLONED_VALUE, butsentry__value_clone()only performs a shallow clone so modifying a nested object still mutates the scope's original.
When does the problem happen
- During build
- During run-time
- When capturing a hard crash
Steps To Reproduce
#include <stdio.h>
#include <sentry.h>
sentry_value_t before_send(sentry_value_t event, void *hint, void *user_data)
{
sentry_value_t user = sentry_value_get_by_key(event, "user");
char* original = sentry_value_to_json(user);
sentry_value_set_by_key(user, "email", sentry_value_new_string("redacted"));
char* redacted = sentry_value_to_json(user);
fprintf(stderr, "%s ==> %s\n", original, redacted);
sentry_free(original);
sentry_free(redacted);
return event;
}
int main(int argc, char *argv[])
{
sentry_options_t *options = sentry_options_new();
sentry_options_set_dsn(options, SENTRY_DSN);
sentry_options_set_before_send(options, before_send, nullptr);
sentry_init(options);
sentry_set_user(sentry_value_new_user(NULL, "somebody", "[email protected]", NULL));
sentry_capture_event(sentry_value_new_event()); // {"username":"somebody","email":"[email protected]"} ==> {"username":"somebody","email":"redacted"}
sentry_capture_event(sentry_value_new_event()); // {"username":"somebody","email":"redacted"} ==> {"username":"somebody","email":"redacted"}
sentry_close();
}Log output
Expected output:
{"username":"somebody","email":"[email protected]"} ==> {"username":"somebody","email":"redacted"}
{"username":"somebody","email":"[email protected]"} ==> {"username":"somebody","email":"redacted"}
Actual output:
{"username":"somebody","email":"[email protected]"} ==> {"username":"somebody","email":"redacted"}
{"username":"somebody","email":"redacted"} ==> {"username":"somebody","email":"redacted"}
The second before_send receives "redacted" as the original email because the first before_send mutated the scope's user object in place.
Activity
linear commentedon Mar 4, 2026
NATIVE-171
supervacuus commentedon Mar 4, 2026
Tbh, while this is clearly a bug in the context of the hooks, the difference in handling between values like
userorfingerprintand other scope content exists because of expected concurrent modification while serializing.useris always updated atomically as the entire object via the API, whereastagswill be modified by key.When the event is passed to the hooks, we suddenly break this contract, and the internal structure leaks through.
sentry__scope_apply_to_eventdoesn't at all consider the change in semantics when an applied-to event suddenly is entirely mutable. In that case, the right choice is not to change the behavior insentry__scope_apply_to_event, but to actually pass a deep clone of the event to the hooks, right before we invoke them.Since we give users the entire object-graph and they are free to mutate everything, there is no other way to restrict access to shared values. It also adds the cost of the deep clone where needed, rather than having everyone pay for it, and implementers of the hook don't have to scratch their heads about arbitrary mutation boundaries.
jpnurmi commentedon Mar 4, 2026
Yeah, I realized in #1550 that deep clones of large event objects would get costly. One option could be copy-on-write to minimize the cost for non-mutating hooks, and the existing
thing_tindirection for lists and objects should make it fairly doable. What do you think, would COW be an acceptable approach here?supervacuus commentedon Mar 5, 2026
I mean, it is entirely unclear to me how prevalent large event objects are, but yes, every user who registers a hook would have to pay for it, even with only read accesses or even no event access inside the hook, and there is certainly a worst-case effect.
COW could certainly be a replacement for clone, but it has to be designed carefully:
sentry__value_cow_snapshot()that acts analogous tosentry__value_clone()cow_proxy_tor whatever), and that thing points to atargetthing to the shared original (and the proxy would only be applied for mutable thing types, likelistandobject, inline values are returned immediately, and immutable ref values would just beincrefed)so the sequencing would be like this:
This means the cost model shifted from a full recursive deep clone for every hook invocation to a 1-level clone on first access inside the hook. Users who don't inspect the event pay nothing. Users who read still have to pay the proxy clones on first access, but nothing more. Users who mutate also pay for that, as well as for whatever they add. In the worst case, when someone accesses only deeply nested values, they lazily pay the price of a deep clone.
In addition to the new thing-type, you only need a materialize function and a cow resolver. The only functions that must be changed (they need to materialize and resolve) are the getters and setters of the mutable value types and the thing functions (
value_as*andthing_free).Note: We could make copying the children during materialization lazy (making the first access even cheaper), but that would actually add quite a bit of complexity downstream (because every function that counts or iterates must now be aware of incomplete objects and lists). I am not sure if that would be worth it as a first step.
Does that make sense?