Skip to content

Commit 97a19d4

Browse files
adinauerclaude
andcommitted
fix: Trim DSN string before URI parsing
Trailing or leading whitespace in the DSN string (commonly introduced by copy-paste) causes a URISyntaxException that crashes the application on startup. Trim the DSN before passing it to the URI constructor. Fixes GH-5087 Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6ea4329 commit 97a19d4

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

sentry/src/main/java/io/sentry/Dsn.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ URI getSentryUri() {
5050

5151
Dsn(@Nullable String dsn) throws IllegalArgumentException {
5252
try {
53-
Objects.requireNonNull(dsn, "The DSN is required.");
54-
final URI uri = new URI(dsn).normalize();
53+
final String dsnString = Objects.requireNonNull(dsn, "The DSN is required.").trim();
54+
final URI uri = new URI(dsnString).normalize();
5555
final String scheme = uri.getScheme();
5656
if (!("http".equalsIgnoreCase(scheme) || "https".equalsIgnoreCase(scheme))) {
5757
throw new IllegalArgumentException("Invalid DSN scheme: " + scheme);

sentry/src/test/java/io/sentry/DsnTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@ class DsnTest {
8989
assertEquals("http://host/api/id", dsn.sentryUri.toURL().toString())
9090
}
9191

92+
@Test
93+
fun `dsn parsed with leading and trailing whitespace`() {
94+
val dsn = Dsn(" https://key@host/id ")
95+
assertEquals("https://host/api/id", dsn.sentryUri.toURL().toString())
96+
}
97+
9298
@Test
9399
fun `non http protocols are not accepted`() {
94100
assertFailsWith<IllegalArgumentException> { Dsn("ftp://publicKey:secretKey@host/path/id") }

0 commit comments

Comments
 (0)