Skip to content

Commit 3548754

Browse files
authored
Fix crash when HTTP connection error message contains formatting symbols (#3002)
1 parent 02e9e80 commit 3548754

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Fix crash when HTTP connection error message contains formatting symbols ([#3002](https://github.com/getsentry/sentry-java/pull/3002))
8+
39
## 6.32.0
410

511
### Features

sentry/src/main/java/io/sentry/transport/HttpConnection.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,10 @@ HttpURLConnection open() throws IOException {
182182
options.getLogger().log(ERROR, "Request failed, API returned %s", responseCode);
183183
// double check because call is expensive
184184
if (options.isDebug()) {
185-
String errorMessage = getErrorMessageFromStream(connection);
186-
options.getLogger().log(ERROR, errorMessage);
185+
final @NotNull String errorMessage = getErrorMessageFromStream(connection);
186+
// the error message may contain anything (including formatting symbols), so provide it as
187+
// an argument itself
188+
options.getLogger().log(ERROR, "%s", errorMessage);
187189
}
188190

189191
return TransportResult.error(responseCode);

sentry/src/test/java/io/sentry/transport/HttpConnectionTest.kt

+32-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package io.sentry.transport
22

3+
import io.sentry.ILogger
34
import io.sentry.ISerializer
45
import io.sentry.RequestDetails
56
import io.sentry.SentryEnvelope
67
import io.sentry.SentryEvent
8+
import io.sentry.SentryLevel
79
import io.sentry.SentryOptions
810
import io.sentry.SentryOptions.Proxy
911
import io.sentry.Session
@@ -19,6 +21,7 @@ import java.io.IOException
1921
import java.net.InetSocketAddress
2022
import java.net.Proxy.Type
2123
import java.net.URL
24+
import java.nio.charset.Charset
2225
import javax.net.ssl.HostnameVerifier
2326
import javax.net.ssl.HttpsURLConnection
2427
import javax.net.ssl.SSLSocketFactory
@@ -40,6 +43,7 @@ class HttpConnectionTest {
4043
var sslSocketFactory: SSLSocketFactory? = null
4144
var hostnameVerifier: HostnameVerifier? = null
4245
val requestDetails = mock<RequestDetails>()
46+
val options = SentryOptions()
4347

4448
init {
4549
whenever(connection.outputStream).thenReturn(mock())
@@ -54,7 +58,6 @@ class HttpConnectionTest {
5458
}
5559

5660
fun getSUT(): HttpConnection {
57-
val options = SentryOptions()
5861
options.setSerializer(serializer)
5962
options.proxy = proxy
6063
options.sslSocketFactory = sslSocketFactory
@@ -187,6 +190,34 @@ class HttpConnectionTest {
187190
verify(fixture.connection, never()).hostnameVerifier = any()
188191
}
189192

193+
@Test
194+
fun `When connection error message contains formatting symbols, does not crash the logger`() {
195+
fixture.options.isDebug = true
196+
fixture.options.setLogger(object : ILogger {
197+
override fun log(level: SentryLevel, message: String, vararg args: Any?) =
198+
println(String.format(message, args))
199+
200+
override fun log(level: SentryLevel, message: String, throwable: Throwable?) =
201+
println(message)
202+
203+
override fun log(
204+
level: SentryLevel,
205+
throwable: Throwable?,
206+
message: String,
207+
vararg args: Any?
208+
) = println(String.format(message))
209+
210+
override fun isEnabled(level: SentryLevel?): Boolean = true
211+
})
212+
213+
// when error message contains funky formatting symbols
214+
whenever(fixture.connection.errorStream).thenReturn("Something is off %d, %s, %s\n".byteInputStream(Charset.forName("UTF-8")))
215+
val transport = fixture.getSUT()
216+
217+
// it should not crash
218+
transport.send(createEnvelope())
219+
}
220+
190221
@Test
191222
fun `When Proxy host and port are given, set to connection`() {
192223
fixture.proxy = Proxy("proxy.example.com", "8090")

0 commit comments

Comments
 (0)