Skip to content
Merged
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 @@ -31,6 +31,7 @@
### Fixes

- Fix crash when unregistering `SystemEventsBroadcastReceiver` with try-catch block. ([#5106](https://github.com/getsentry/sentry-java/pull/5106))
- Trim DSN string before parsing to avoid `URISyntaxException` caused by trailing whitespace ([#5113](https://github.com/getsentry/sentry-java/pull/5113))

### Dependencies

Expand Down
7 changes: 5 additions & 2 deletions sentry/src/main/java/io/sentry/Dsn.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ URI getSentryUri() {

Dsn(@Nullable String dsn) throws IllegalArgumentException {
try {
Objects.requireNonNull(dsn, "The DSN is required.");
final URI uri = new URI(dsn).normalize();
final String dsnString = Objects.requireNonNull(dsn, "The DSN is required.").trim();
if (dsnString.isEmpty()) {
throw new IllegalArgumentException("The DSN is empty.");
}
final URI uri = new URI(dsnString).normalize();
final String scheme = uri.getScheme();
if (!("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme))) {
throw new IllegalArgumentException("Invalid DSN scheme: " + scheme);
Expand Down
2 changes: 1 addition & 1 deletion sentry/src/main/java/io/sentry/SentryOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ Dsn retrieveParsedDsn() throws IllegalArgumentException {
* @param dsn the DSN
*/
public void setDsn(final @Nullable String dsn) {
this.dsn = dsn;
this.dsn = dsn != null ? dsn.trim() : null;
this.parsedDsn.resetValue();

dsnHash = StringUtils.calculateStringHash(this.dsn, logger);
Expand Down
18 changes: 18 additions & 0 deletions sentry/src/test/java/io/sentry/DsnTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ class DsnTest {
assertEquals("http://host/api/id", dsn.sentryUri.toURL().toString())
}

@Test
fun `dsn parsed with leading and trailing whitespace`() {
val dsn = Dsn(" https://key@host/id ")
assertEquals("https://host/api/id", dsn.sentryUri.toURL().toString())
}

@Test
fun `when dsn is empty, throws exception`() {
val ex = assertFailsWith<IllegalArgumentException> { Dsn("") }
assertEquals("java.lang.IllegalArgumentException: The DSN is empty.", ex.message)
}

@Test
fun `when dsn is only whitespace, throws exception`() {
val ex = assertFailsWith<IllegalArgumentException> { Dsn(" ") }
assertEquals("java.lang.IllegalArgumentException: The DSN is empty.", ex.message)
}

@Test
fun `non http protocols are not accepted`() {
assertFailsWith<IllegalArgumentException> { Dsn("ftp://publicKey:secretKey@host/path/id") }
Expand Down
18 changes: 18 additions & 0 deletions sentry/src/test/java/io/sentry/SentryOptionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,24 @@ class SentryOptionsTest {
assertFalse(cacheDirPathWithoutDsn.contains(hash.toString()))
}

@Test
fun `when setting dsn with whitespace, it is trimmed and produces the same cache dir path`() {
val dsn = "http://key@localhost/proj"
val options1 =
SentryOptions().apply {
setDsn(dsn)
cacheDirPath = "${File.separator}test"
}
val options2 =
SentryOptions().apply {
setDsn(" $dsn ")
cacheDirPath = "${File.separator}test"
}

assertEquals(dsn, options2.dsn)
assertEquals(options1.cacheDirPath, options2.cacheDirPath)
}

@Test
fun `when options are initialized, idleTimeout is 3000`() {
assertEquals(3000L, SentryOptions().idleTimeout)
Expand Down
Loading