Skip to content

Commit

Permalink
Fix crash when HTTP connection error message contains formatting symb…
Browse files Browse the repository at this point in the history
…ols (#3002)
  • Loading branch information
markushi authored Oct 23, 2023
1 parent 02e9e80 commit 3548754
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Fix crash when HTTP connection error message contains formatting symbols ([#3002](https://github.com/getsentry/sentry-java/pull/3002))

## 6.32.0

### Features
Expand Down
6 changes: 4 additions & 2 deletions sentry/src/main/java/io/sentry/transport/HttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ HttpURLConnection open() throws IOException {
options.getLogger().log(ERROR, "Request failed, API returned %s", responseCode);
// double check because call is expensive
if (options.isDebug()) {
String errorMessage = getErrorMessageFromStream(connection);
options.getLogger().log(ERROR, errorMessage);
final @NotNull String errorMessage = getErrorMessageFromStream(connection);
// the error message may contain anything (including formatting symbols), so provide it as
// an argument itself
options.getLogger().log(ERROR, "%s", errorMessage);
}

return TransportResult.error(responseCode);
Expand Down
33 changes: 32 additions & 1 deletion sentry/src/test/java/io/sentry/transport/HttpConnectionTest.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.sentry.transport

import io.sentry.ILogger
import io.sentry.ISerializer
import io.sentry.RequestDetails
import io.sentry.SentryEnvelope
import io.sentry.SentryEvent
import io.sentry.SentryLevel
import io.sentry.SentryOptions
import io.sentry.SentryOptions.Proxy
import io.sentry.Session
Expand All @@ -19,6 +21,7 @@ import java.io.IOException
import java.net.InetSocketAddress
import java.net.Proxy.Type
import java.net.URL
import java.nio.charset.Charset
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.HttpsURLConnection
import javax.net.ssl.SSLSocketFactory
Expand All @@ -40,6 +43,7 @@ class HttpConnectionTest {
var sslSocketFactory: SSLSocketFactory? = null
var hostnameVerifier: HostnameVerifier? = null
val requestDetails = mock<RequestDetails>()
val options = SentryOptions()

init {
whenever(connection.outputStream).thenReturn(mock())
Expand All @@ -54,7 +58,6 @@ class HttpConnectionTest {
}

fun getSUT(): HttpConnection {
val options = SentryOptions()
options.setSerializer(serializer)
options.proxy = proxy
options.sslSocketFactory = sslSocketFactory
Expand Down Expand Up @@ -187,6 +190,34 @@ class HttpConnectionTest {
verify(fixture.connection, never()).hostnameVerifier = any()
}

@Test
fun `When connection error message contains formatting symbols, does not crash the logger`() {
fixture.options.isDebug = true
fixture.options.setLogger(object : ILogger {
override fun log(level: SentryLevel, message: String, vararg args: Any?) =
println(String.format(message, args))

override fun log(level: SentryLevel, message: String, throwable: Throwable?) =
println(message)

override fun log(
level: SentryLevel,
throwable: Throwable?,
message: String,
vararg args: Any?
) = println(String.format(message))

override fun isEnabled(level: SentryLevel?): Boolean = true
})

// when error message contains funky formatting symbols
whenever(fixture.connection.errorStream).thenReturn("Something is off %d, %s, %s\n".byteInputStream(Charset.forName("UTF-8")))
val transport = fixture.getSUT()

// it should not crash
transport.send(createEnvelope())
}

@Test
fun `When Proxy host and port are given, set to connection`() {
fixture.proxy = Proxy("proxy.example.com", "8090")
Expand Down

0 comments on commit 3548754

Please sign in to comment.