Skip to content

Commit

Permalink
ref: Switch the internal representation of project IDs from ints over…
Browse files Browse the repository at this point in the history
… to char*s (#690)

This tries to future-proof project IDs as they aren't guaranteed 
to be numbers forever. Project IDs are now treated as opaque 
strings, and minimal validation is now applied to them to reflect 
that fact.
  • Loading branch information
relaxolotl authored Mar 30, 2022
1 parent 7d2a4b6 commit f6eda12
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
- New API to check whether the application has crashed in the previous run: `sentry_get_crashed_last_run()` and `sentry_clear_crashed_last_run()` ([#685](https://github.com/getsentry/sentry-native/pull/685)).
- Allow overriding the SDK name at build time - set the `SENTRY_SDK_NAME` CMake cache variable.

**Internal**:
- Project IDs are now treated as opaque strings instead of integer values. ([#690](https://github.com/getsentry/sentry-native/pull/690))

**Fixes**:
- Updated CI as well as list of supported platforms to reflect Windows Server 2016, and therefore MSVC 2017 losing active support.

Expand Down
18 changes: 8 additions & 10 deletions src/sentry_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,7 @@ sentry__dsn_new(const char *raw_dsn)
sentry_url_t url;
memset(&url, 0, sizeof(sentry_url_t));
size_t path_len;
char *tmp;
char *end;
char *project_id;

sentry_dsn_t *dsn = SENTRY_MAKE(sentry_dsn_t);
if (!dsn) {
Expand Down Expand Up @@ -255,16 +254,14 @@ sentry__dsn_new(const char *raw_dsn)
path_len--;
}

tmp = strrchr(url.path, '/');
if (!tmp) {
project_id = strrchr(url.path, '/');
if (!project_id || strlen(project_id + 1) == 0) {
goto exit;
}

dsn->project_id = (uint64_t)strtoll(tmp + 1, &end, 10);
if (end != tmp + strlen(tmp)) {
goto exit;
}
*tmp = 0;
dsn->project_id = sentry__string_clone(project_id + 1);
*project_id = 0;

dsn->path = url.path;
url.path = NULL;

Expand Down Expand Up @@ -299,6 +296,7 @@ sentry__dsn_decref(sentry_dsn_t *dsn)
sentry_free(dsn->path);
sentry_free(dsn->public_key);
sentry_free(dsn->secret_key);
sentry_free(dsn->project_id);
sentry_free(dsn);
}
}
Expand Down Expand Up @@ -329,7 +327,7 @@ init_string_builder_for_url(sentry_stringbuilder_t *sb, const sentry_dsn_t *dsn)
sentry__stringbuilder_append_int64(sb, (int64_t)dsn->port);
sentry__stringbuilder_append(sb, dsn->path);
sentry__stringbuilder_append(sb, "/api/");
sentry__stringbuilder_append_int64(sb, (int64_t)dsn->project_id);
sentry__stringbuilder_append(sb, dsn->project_id);
}

char *
Expand Down
2 changes: 1 addition & 1 deletion src/sentry_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ typedef struct sentry_dsn_s {
char *path;
char *secret_key;
char *public_key;
uint64_t project_id;
char *project_id;
int port;
long refcount;
bool is_valid;
Expand Down
30 changes: 16 additions & 14 deletions tests/unit/test_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ SENTRY_TEST(url_parsing_invalid)
SENTRY_TEST(dsn_parsing_complete)
{
sentry_dsn_t *dsn = sentry__dsn_new(
"http://username:password@example.com/foo/bar/42?x=y#z");
"http://username:password@example.com/foo/bar/42%21?x=y#z");
TEST_CHECK(!!dsn);
if (!dsn) {
return;
Expand All @@ -83,10 +83,10 @@ SENTRY_TEST(dsn_parsing_complete)
TEST_CHECK_STRING_EQUAL(dsn->public_key, "username");
TEST_CHECK_STRING_EQUAL(dsn->secret_key, "password");
TEST_CHECK_STRING_EQUAL(dsn->path, "/foo/bar");
TEST_CHECK_INT_EQUAL((int)dsn->project_id, 42);
TEST_CHECK_STRING_EQUAL(dsn->project_id, "42%21");
sentry__dsn_decref(dsn);

dsn = sentry__dsn_new("https://username@example.com/42");
dsn = sentry__dsn_new("https://username@example.com/42%21");
TEST_CHECK(!!dsn);
if (!dsn) {
return;
Expand All @@ -97,22 +97,24 @@ SENTRY_TEST(dsn_parsing_complete)
TEST_CHECK_STRING_EQUAL(dsn->public_key, "username");
TEST_CHECK(!dsn->secret_key);
TEST_CHECK_STRING_EQUAL(dsn->path, "");
TEST_CHECK_INT_EQUAL((int)dsn->project_id, 42);
TEST_CHECK_STRING_EQUAL(dsn->project_id, "42%21");
sentry__dsn_decref(dsn);
}

SENTRY_TEST(dsn_parsing_invalid)
{
sentry_dsn_t *dsn
= sentry__dsn_new("http://username:password@example.com/foo/bar?x=y#z");
dsn = sentry__dsn_new("https://username@example.com/pathone/pathtwo/42%21");
TEST_CHECK(!!dsn);
if (dsn) {
TEST_CHECK(!dsn->is_valid);
sentry__dsn_decref(dsn);
if (!dsn) {
return;
}
TEST_CHECK(dsn->is_valid);
TEST_CHECK_STRING_EQUAL(dsn->path, "/pathone/pathtwo");
TEST_CHECK_STRING_EQUAL(dsn->project_id, "42%21");
sentry__dsn_decref(dsn);
}

dsn = sentry__dsn_new("=https://foo@bar.ingest.sentry.io/"
"1234567");
SENTRY_TEST(dsn_parsing_invalid)
{
sentry_dsn_t *dsn = sentry__dsn_new("=https://foo@bar.ingest.sentry.io/"
"1234567");
TEST_CHECK(!!dsn);
if (dsn) {
TEST_CHECK(!dsn->is_valid);
Expand Down

0 comments on commit f6eda12

Please sign in to comment.