From 856203bcac972e573a42b39376bec7cff6d93b2e Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 11 Feb 2026 14:02:00 +0100 Subject: [PATCH] ref: parse event_id from raw envelopes Co-Authored-By: Claude Opus 4.5 --- src/sentry_envelope.c | 10 ++++++- src/sentry_envelope.h | 4 +-- tests/unit/test_envelopes.c | 59 +++++++++++++++++++++++++++++++++++++ tests/unit/tests.inc | 1 + 4 files changed, 71 insertions(+), 3 deletions(-) diff --git a/src/sentry_envelope.c b/src/sentry_envelope.c index d5ffd96dae..e6edc98448 100644 --- a/src/sentry_envelope.c +++ b/src/sentry_envelope.c @@ -230,7 +230,15 @@ sentry_uuid_t sentry__envelope_get_event_id(const sentry_envelope_t *envelope) { if (envelope->is_raw) { - return sentry_uuid_nil(); + const char *payload = envelope->contents.raw.payload; + size_t payload_len = envelope->contents.raw.payload_len; + const char *newline = memchr(payload, '\n', payload_len); + size_t header_len = newline ? (size_t)(newline - payload) : payload_len; + sentry_value_t header = sentry__value_from_json(payload, header_len); + sentry_uuid_t event_id = sentry_uuid_from_string(sentry_value_as_string( + sentry_value_get_by_key(header, "event_id"))); + sentry_value_decref(header); + return event_id; } return sentry_uuid_from_string(sentry_value_as_string( sentry_value_get_by_key(envelope->contents.items.headers, "event_id"))); diff --git a/src/sentry_envelope.h b/src/sentry_envelope.h index 56e3423055..02054eb424 100644 --- a/src/sentry_envelope.h +++ b/src/sentry_envelope.h @@ -27,8 +27,8 @@ sentry_envelope_t *sentry__envelope_from_path(const sentry_path_t *path); /** * This returns the UUID of the event associated with this envelope. - * If there is no event inside this envelope, or the envelope was previously - * loaded from disk, the empty nil UUID will be returned. + * If there is no event inside this envelope, the empty nil UUID will be + * returned. */ sentry_uuid_t sentry__envelope_get_event_id(const sentry_envelope_t *envelope); diff --git a/tests/unit/test_envelopes.c b/tests/unit/test_envelopes.c index 5acfea790d..2f7c646a18 100644 --- a/tests/unit/test_envelopes.c +++ b/tests/unit/test_envelopes.c @@ -377,6 +377,65 @@ SENTRY_TEST(write_raw_envelope_to_file) sentry_close(); } +SENTRY_TEST(raw_envelope_event_id) +{ + sentry_envelope_t *envelope = create_test_envelope(); + const char *test_file_str = SENTRY_TEST_PATH_PREFIX "sentry_test_envelope"; + sentry_path_t *test_file_path = sentry__path_from_str(test_file_str); + TEST_CHECK_INT_EQUAL( + sentry_envelope_write_to_file(envelope, test_file_str), 0); + + sentry_envelope_t *raw_envelope + = sentry__envelope_from_path(test_file_path); + TEST_CHECK(!!raw_envelope); + + sentry_uuid_t event_id = sentry__envelope_get_event_id(raw_envelope); + char event_id_str[37]; + sentry_uuid_as_string(&event_id, event_id_str); + TEST_CHECK_STRING_EQUAL( + event_id_str, "c993afb6-b4ac-48a6-b61b-2558e601d65d"); + + sentry__path_remove(test_file_path); + sentry__path_free(test_file_path); + sentry_envelope_free(envelope); + sentry_envelope_free(raw_envelope); + + // missing event_id + const char header_no_event_id[] + = "{\"dsn\":\"https://foo@sentry.invalid/42\"}\n{}"; + test_file_path = sentry__path_from_str(test_file_str); + TEST_CHECK_INT_EQUAL( + sentry__path_write_buffer( + test_file_path, header_no_event_id, sizeof(header_no_event_id) - 1), + 0); + raw_envelope = sentry__envelope_from_path(test_file_path); + TEST_CHECK(!!raw_envelope); + event_id = sentry__envelope_get_event_id(raw_envelope); + TEST_CHECK(sentry_uuid_is_nil(&event_id)); + sentry__path_remove(test_file_path); + sentry__path_free(test_file_path); + sentry_envelope_free(raw_envelope); + + // missing newline + const char header_no_newline[] + = "{\"event_id\":\"c993afb6-b4ac-48a6-b61b-2558e601d65d\"}"; + test_file_path = sentry__path_from_str(test_file_str); + TEST_CHECK_INT_EQUAL(sentry__path_write_buffer(test_file_path, + header_no_newline, sizeof(header_no_newline) - 1), + 0); + raw_envelope = sentry__envelope_from_path(test_file_path); + TEST_CHECK(!!raw_envelope); + event_id = sentry__envelope_get_event_id(raw_envelope); + sentry_uuid_as_string(&event_id, event_id_str); + TEST_CHECK_STRING_EQUAL( + event_id_str, "c993afb6-b4ac-48a6-b61b-2558e601d65d"); + sentry__path_remove(test_file_path); + sentry__path_free(test_file_path); + sentry_envelope_free(raw_envelope); + + sentry_close(); +} + SENTRY_TEST(read_envelope_from_file) { const char *test_file_str = SENTRY_TEST_PATH_PREFIX "sentry_test_envelope"; diff --git a/tests/unit/tests.inc b/tests/unit/tests.inc index 0a5cb2e9d5..cb9e24eb8a 100644 --- a/tests/unit/tests.inc +++ b/tests/unit/tests.inc @@ -146,6 +146,7 @@ XX(procmaps_parser) XX(propagation_context_init) XX(query_consent_requirement) XX(rate_limit_parsing) +XX(raw_envelope_event_id) XX(read_envelope_from_file) XX(read_write_envelope_to_file_null) XX(read_write_envelope_to_invalid_path)