From 97a19d414561f0da2a0e584f24f04f91229bdf49 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Mon, 23 Feb 2026 17:02:08 +0100 Subject: [PATCH 1/2] 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 --- sentry/src/main/java/io/sentry/Dsn.java | 4 ++-- sentry/src/test/java/io/sentry/DsnTest.kt | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/sentry/src/main/java/io/sentry/Dsn.java b/sentry/src/main/java/io/sentry/Dsn.java index 836e2c55468..31e57c7f5a2 100644 --- a/sentry/src/main/java/io/sentry/Dsn.java +++ b/sentry/src/main/java/io/sentry/Dsn.java @@ -50,8 +50,8 @@ 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(); + 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); diff --git a/sentry/src/test/java/io/sentry/DsnTest.kt b/sentry/src/test/java/io/sentry/DsnTest.kt index eaa129c2073..fee1cf50afe 100644 --- a/sentry/src/test/java/io/sentry/DsnTest.kt +++ b/sentry/src/test/java/io/sentry/DsnTest.kt @@ -89,6 +89,12 @@ 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 `non http protocols are not accepted`() { assertFailsWith { Dsn("ftp://publicKey:secretKey@host/path/id") } From 26c1195a2582b75f3db9e4731c998541cb123228 Mon Sep 17 00:00:00 2001 From: Alexander Dinauer Date: Mon, 23 Feb 2026 17:03:43 +0100 Subject: [PATCH 2/2] docs: Add changelog entry for DSN trimming fix Co-Authored-By: Claude --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b74bc0886e1..770be8996f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Trim DSN string before parsing to avoid `URISyntaxException` caused by trailing whitespace ([#5113](https://github.com/getsentry/sentry-java/pull/5113)) + ## 8.33.0 ### Features