Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**Features**:

- Auto-populate `event.user.id` with a persistent per-installation UUID when no explicit user ID is set. ([#1661](https://github.com/getsentry/sentry-native/pull/1661))
- Add `sentry_attachment_set_type` and `SENTRY_ATTACHMENT_TYPE_*` macros for standard Sentry attachment types. ([#1700](https://github.com/getsentry/sentry-native/pull/1700))

## 0.14.0

Expand Down
18 changes: 18 additions & 0 deletions include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -2719,6 +2719,24 @@ SENTRY_API sentry_attachment_t *sentry_scope_attach_bytesw_n(
const wchar_t *filename, size_t filename_len);
#endif

#define SENTRY_ATTACHMENT_TYPE_GENERIC "event.attachment"
#define SENTRY_ATTACHMENT_TYPE_MINIDUMP "event.minidump"
#define SENTRY_ATTACHMENT_TYPE_VIEW_HIERARCHY "event.view_hierarchy"

/**
* Sets the attachment type.
*
* Well-known attachment types are exposed as `SENTRY_ATTACHMENT_TYPE_*`
* macros. Pass `NULL` or an empty string to clear the attachment type.
*
* See:
* https://develop.sentry.dev/sdk/telemetry/attachments/#attachment-types
*/
SENTRY_API void sentry_attachment_set_type(
sentry_attachment_t *attachment, const char *type);
SENTRY_API void sentry_attachment_set_type_n(
sentry_attachment_t *attachment, const char *type, size_t type_len);

/**
* Sets the content type of attachment.
*/
Expand Down
43 changes: 30 additions & 13 deletions src/backends/native/sentry_crash_daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ __asan_default_options(void)
*/
static bool
write_attachment_to_envelope(int fd, const char *file_path,
const char *filename, const char *content_type)
const char *filename, const char *attachment_type, const char *content_type)
{
#if defined(SENTRY_PLATFORM_UNIX)
int attach_fd = open(file_path, O_RDONLY);
Expand Down Expand Up @@ -117,16 +117,20 @@ write_attachment_to_envelope(int fd, const char *file_path,
if (content_type) {
header_written = snprintf(header, sizeof(header),
"{\"type\":\"attachment\",\"length\":%lld,"
"\"attachment_type\":\"event.attachment\","
"\"attachment_type\":\"%s\","
"\"content_type\":\"%s\","
"\"filename\":\"%s\"}\n",
file_size, content_type, filename ? filename : "attachment");
file_size,
attachment_type ? attachment_type : SENTRY_ATTACHMENT_TYPE_GENERIC,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty attachment_type written for generic attachments in crash daemon

High Severity

When generic attachments lack an attachment_type key in the JSON manifest, sentry_value_as_string() returns "" (empty string, not NULL). The check attachment_type ? attachment_type : SENTRY_ATTACHMENT_TYPE_GENERIC only guards against NULL, so "" passes through, producing "attachment_type":"" in the envelope header instead of "attachment_type":"event.attachment". The same file correctly handles this at line 275 using (attachment_type && *attachment_type), but write_attachment_to_envelope does not dereference to check for empty string.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit e4abe3d. Configure here.

content_type, filename ? filename : "attachment");
} else {
header_written = snprintf(header, sizeof(header),
"{\"type\":\"attachment\",\"length\":%lld,"
"\"attachment_type\":\"event.attachment\","
"\"attachment_type\":\"%s\","
"\"filename\":\"%s\"}\n",
file_size, filename ? filename : "attachment");
file_size,
attachment_type ? attachment_type : SENTRY_ATTACHMENT_TYPE_GENERIC,
filename ? filename : "attachment");
}

if (header_written < 0 || header_written >= (int)sizeof(header)) {
Expand Down Expand Up @@ -203,7 +207,6 @@ attachment_is_placeholder(const sentry_options_t *options, const char *path)
if (!attachment.path) {
return false;
}
attachment.type = ATTACHMENT;
bool is_placeholder
= sentry__attachment_is_placeholder(&attachment, options);
sentry__path_free(attachment.path);
Expand Down Expand Up @@ -251,6 +254,8 @@ add_attachment_refs(sentry_envelope_t *envelope,
= sentry_value_as_string(sentry_value_get_by_key(info, "path"));
const char *filename
= sentry_value_as_string(sentry_value_get_by_key(info, "filename"));
const char *attachment_type = sentry_value_as_string(
sentry_value_get_by_key(info, "attachment_type"));
const char *content_type = sentry_value_as_string(
sentry_value_get_by_key(info, "content_type"));
if (!path || !*path || !filename || !*filename) {
Expand All @@ -266,7 +271,9 @@ add_attachment_refs(sentry_envelope_t *envelope,
sentry__path_free(attachment.filename);
continue;
}
attachment.type = ATTACHMENT;
attachment.type
= (char *)((attachment_type && *attachment_type) ? attachment_type
: NULL);
attachment.content_type
= (char *)((content_type && *content_type) ? content_type : NULL);
if (!sentry__attachment_is_placeholder(&attachment, options)) {
Expand Down Expand Up @@ -2471,20 +2478,25 @@ write_envelope_with_native_stacktrace(const sentry_options_t *options,
= sentry_value_get_by_key(attach_info, "path");
sentry_value_t filename_val
= sentry_value_get_by_key(attach_info, "filename");
sentry_value_t attachment_type_val
= sentry_value_get_by_key(
attach_info, "attachment_type");
sentry_value_t content_type_val
= sentry_value_get_by_key(
attach_info, "content_type");

const char *path = sentry_value_as_string(path_val);
const char *filename
= sentry_value_as_string(filename_val);
const char *attachment_type
= sentry_value_as_string(attachment_type_val);
const char *content_type
= sentry_value_as_string(content_type_val);

if (path && filename
&& !attachment_is_placeholder(options, path)) {
write_attachment_to_envelope(
fd, path, filename, content_type);
write_attachment_to_envelope(fd, path, filename,
attachment_type, content_type);
}
}
sentry_value_decref(attach_list);
Expand All @@ -2499,7 +2511,7 @@ write_envelope_with_native_stacktrace(const sentry_options_t *options,
= sentry__path_join_str(run_folder, "screenshot.png");
if (screenshot_path) {
write_attachment_to_envelope(
fd, screenshot_path->path, "screenshot.png", "image/png");
fd, screenshot_path->path, "screenshot.png", NULL, "image/png");
sentry__path_free(screenshot_path);
}
}
Expand Down Expand Up @@ -2707,20 +2719,25 @@ write_envelope_with_minidump(const sentry_options_t *options,
= sentry_value_get_by_key(attach_info, "path");
sentry_value_t filename_val
= sentry_value_get_by_key(attach_info, "filename");
sentry_value_t attachment_type_val
= sentry_value_get_by_key(
attach_info, "attachment_type");
sentry_value_t content_type_val
= sentry_value_get_by_key(
attach_info, "content_type");

const char *path = sentry_value_as_string(path_val);
const char *filename
= sentry_value_as_string(filename_val);
const char *attachment_type
= sentry_value_as_string(attachment_type_val);
const char *content_type
= sentry_value_as_string(content_type_val);

if (path && filename
&& !attachment_is_placeholder(options, path)) {
write_attachment_to_envelope(
fd, path, filename, content_type);
write_attachment_to_envelope(fd, path, filename,
attachment_type, content_type);
}
}
sentry_value_decref(attach_list);
Expand All @@ -2735,7 +2752,7 @@ write_envelope_with_minidump(const sentry_options_t *options,
= sentry__path_join_str(run_folder, "screenshot.png");
if (screenshot_path) {
write_attachment_to_envelope(
fd, screenshot_path->path, "screenshot.png", "image/png");
fd, screenshot_path->path, "screenshot.png", NULL, "image/png");
sentry__path_free(screenshot_path);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/backends/sentry_backend_breakpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,14 @@ breakpad_backend_callback(const google_breakpad::MinidumpDescriptor &descriptor,
envelope, dump_path, "attachment");
if (item) {
sentry__envelope_item_set_header(item, "attachment_type",
sentry_value_new_string("event.minidump"));
sentry_value_new_string(SENTRY_ATTACHMENT_TYPE_MINIDUMP));

sentry__envelope_item_set_header(item, "filename",
sentry_value_new_string(sentry__path_filename(dump_path)));
} else if (options->enable_large_attachments) {
sentry_attachment_t tmp = {};
tmp.path = dump_path;
tmp.type = MINIDUMP;
tmp.type = (char *)SENTRY_ATTACHMENT_TYPE_MINIDUMP;
if (!sentry__cache_attachment_ref(
envelope, &tmp, options->run->cache_path, nullptr)) {
SENTRY_SIGNAL_SAFE_LOG(
Expand Down
8 changes: 4 additions & 4 deletions src/backends/sentry_backend_crashpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,8 +517,8 @@ report_to_envelope(const crashpad::CrashReportDatabase::Report &report,
} else if (strcmp(filename, "__sentry-breadcrumb2") == 0) {
breadcrumbs2 = read_msgpack_file(path);
} else {
sentry__attachments_add_path(&attachments,
sentry__path_clone(path), ATTACHMENT, nullptr);
sentry__attachments_add_path(
&attachments, sentry__path_clone(path), nullptr, nullptr);
}
}
sentry__pathiter_free(iter);
Expand All @@ -537,8 +537,8 @@ report_to_envelope(const crashpad::CrashReportDatabase::Report &report,
sentry_value_set_by_key(event, "breadcrumbs",
sentry__value_merge_breadcrumbs(
breadcrumbs1, breadcrumbs2, options->max_breadcrumbs));
sentry__attachments_add_path(
&attachments, minidump_path, MINIDUMP, nullptr);
sentry__attachments_add_path(&attachments, minidump_path,
SENTRY_ATTACHMENT_TYPE_MINIDUMP, nullptr);

if (sentry__envelope_add_event(envelope, event)) {
sentry__envelope_add_attachments(envelope, attachments, options);
Expand Down
4 changes: 4 additions & 0 deletions src/backends/sentry_backend_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,10 @@ native_backend_write_attachments(const sentry_path_t *event_path)
it->filename ? it->filename : it->path);
sentry_value_set_by_key(
attach_info, "filename", sentry_value_new_string(filename));
if (it->type && *it->type) {
sentry_value_set_by_key(attach_info, "attachment_type",
sentry_value_new_string(it->type));
}
if (it->content_type) {
sentry_value_set_by_key(attach_info, "content_type",
sentry_value_new_string(it->content_type));
Expand Down
38 changes: 31 additions & 7 deletions src/sentry_attachment.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@

#include <string.h>

void
sentry_attachment_set_type(sentry_attachment_t *attachment, const char *type)
{
sentry_attachment_set_type_n(
attachment, type, sentry__guarded_strlen(type));
}

void
sentry_attachment_set_type_n(
sentry_attachment_t *attachment, const char *type, size_t type_len)
{
if (!attachment) {
return;
}

sentry_free(attachment->type);
attachment->type
= type && type_len > 0 ? sentry__string_clone_n(type, type_len) : NULL;
}

void
sentry_attachment_set_content_type(
sentry_attachment_t *attachment, const char *content_type)
Expand Down Expand Up @@ -117,6 +137,7 @@ sentry__attachment_free(sentry_attachment_t *attachment)
sentry__path_free(attachment->path);
sentry__path_free(attachment->filename);
sentry_free(attachment->buf);
sentry_free(attachment->type);
sentry_free(attachment->content_type);
sentry_free(attachment);
}
Expand All @@ -141,7 +162,6 @@ sentry__attachment_is_placeholder(
const sentry_attachment_t *att, const sentry_options_t *options)
{
return options && options->enable_large_attachments && att
&& att->type == ATTACHMENT
&& sentry__attachment_get_size(att) >= SENTRY_LARGE_ATTACHMENT_SIZE;
}

Expand Down Expand Up @@ -170,15 +190,20 @@ attachment_eq(const sentry_attachment_t *a, const sentry_attachment_t *b)
if (a == b) {
return true;
}
if (!a || !b || a->buf || b->buf || a->type != b->type) {
if (!a || !b || a->buf || b->buf) {
return false;
}
const char *a_type = a->type ? a->type : "";
const char *b_type = b->type ? b->type : "";
if (!sentry__string_eq(a_type, b_type)) {
return false;
}
return sentry__path_eq(a->path, b->path);
}

sentry_attachment_t *
sentry__attachments_add(sentry_attachment_t **attachments_ptr,
sentry_attachment_t *attachment, sentry_attachment_type_t attachment_type,
sentry_attachment_t *attachment, const char *attachment_type,
const char *content_type)
{
if (!attachment) {
Expand All @@ -196,8 +221,8 @@ sentry__attachments_add(sentry_attachment_t **attachments_ptr,
SENTRY_INFOF("added large attachment \"%s\" (%zu MiB)",
sentry__attachment_get_filename(attachment), size / (1024 * 1024));
}
attachment->type = attachment_type;
attachment->content_type = sentry__string_clone(content_type);
sentry_attachment_set_type(attachment, attachment_type);
sentry_attachment_set_content_type(attachment, content_type);

sentry_attachment_t **next_ptr = attachments_ptr;

Expand All @@ -216,8 +241,7 @@ sentry__attachments_add(sentry_attachment_t **attachments_ptr,

sentry_attachment_t *
sentry__attachments_add_path(sentry_attachment_t **attachments_ptr,
sentry_path_t *path, sentry_attachment_type_t attachment_type,
const char *content_type)
sentry_path_t *path, const char *attachment_type, const char *content_type)
{
sentry_attachment_t *attachment = sentry__attachment_from_path(path);
return sentry__attachments_add(
Expand Down
15 changes: 3 additions & 12 deletions src/sentry_attachment.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@
#define SENTRY_LARGE_ATTACHMENT_SIZE (100 * 1024 * 1024) // 100 MiB
#define SENTRY_MAX_ATTACHMENT_SIZE (1024 * 1024 * 1024) // 1 GiB

/**
* The attachment_type.
*/
typedef enum {
ATTACHMENT,
MINIDUMP,
VIEW_HIERARCHY,
} sentry_attachment_type_t;

/**
* This is a linked list of all the attachments registered via
* `sentry_options_add_attachment`.
Expand All @@ -38,7 +29,7 @@ struct sentry_attachment_s {

// Common fields for both attachment types
sentry_path_t *filename; // Attachment name in envelope (can be NULL)
sentry_attachment_type_t type;
char *type;
char *content_type;
sentry_attachment_t *next; // Linked list pointer
};
Expand Down Expand Up @@ -89,14 +80,14 @@ void sentry__attachments_free(sentry_attachment_t *attachments);
*/
sentry_attachment_t *sentry__attachments_add(
sentry_attachment_t **attachments_ptr, sentry_attachment_t *attachment,
sentry_attachment_type_t attachment_type, const char *content_type);
const char *attachment_type, const char *content_type);

/**
* Adds a file attachment to the attachments list at `attachments_ptr`.
*/
sentry_attachment_t *sentry__attachments_add_path(
sentry_attachment_t **attachments_ptr, sentry_path_t *path,
sentry_attachment_type_t attachment_type, const char *content_type);
const char *attachment_type, const char *content_type);

/**
* Removes an attachment from the attachments list at `attachments_ptr`.
Expand Down
6 changes: 3 additions & 3 deletions src/sentry_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -1769,7 +1769,7 @@ sentry_capture_minidump_n(const char *path, size_t path_len)
>= SENTRY_LARGE_ATTACHMENT_SIZE) {
sentry_attachment_t tmp = { 0 };
tmp.path = dump_path;
tmp.type = MINIDUMP;
tmp.type = (char *)SENTRY_ATTACHMENT_TYPE_MINIDUMP;
if (!sentry__cache_attachment_ref(
envelope, &tmp, options->run->cache_path, NULL)) {
SENTRY_WARN("failed to cache minidump attachment-ref");
Expand Down Expand Up @@ -1798,7 +1798,7 @@ sentry_capture_minidump_n(const char *path, size_t path_len)
sentry_envelope_free(envelope);
} else {
sentry__envelope_item_set_header(item, "attachment_type",
sentry_value_new_string("event.minidump"));
sentry_value_new_string(SENTRY_ATTACHMENT_TYPE_MINIDUMP));

sentry__envelope_item_set_header(item, "filename",
sentry_value_new_string(sentry__path_filename(dump_path)));
Expand Down Expand Up @@ -1831,7 +1831,7 @@ add_attachment(sentry_attachment_t *attachment)
}
SENTRY_WITH_SCOPE_MUT (scope) {
attachment = sentry__attachments_add(
&scope->attachments, attachment, ATTACHMENT, NULL);
&scope->attachments, attachment, NULL, NULL);
}
return attachment;
}
Expand Down
2 changes: 1 addition & 1 deletion src/sentry_database.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ static sentry_path_t *
build_sibling_path(const sentry_path_t *cache_path, const char *uuid_str,
const sentry_attachment_t *att)
{
if (att->type == MINIDUMP) {
if (att->type && strcmp(att->type, SENTRY_ATTACHMENT_TYPE_MINIDUMP) == 0) {
char buf[41];
snprintf(buf, sizeof(buf), "%s.dmp", uuid_str);
return sentry__path_unique(cache_path, buf);
Expand Down
Loading
Loading