Skip to content

Commit

Permalink
Fix crash when auth tokens contain new line character
Browse files Browse the repository at this point in the history
When a new line character (0x0a) exists in the authorization tokens, the
 ddi-client/third-party application using the ddi-api will keep crashing
 while trying to send the HTTPS request again.
 This commit catches and logs this exception to prevent fatal crashes.

UF-888
Signed-off-by: Saeed Rezaee <saeed.rezaee@kynetics.it>
  • Loading branch information
SaeedRe committed Jun 10, 2024
1 parent f6f638b commit a3badc6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import java.io.IOException
import java.util.Objects
import okhttp3.Interceptor
import okhttp3.Response
import org.slf4j.LoggerFactory

/**
* @author Daniele Sergio
Expand Down Expand Up @@ -41,7 +42,11 @@ class HawkbitAuthenticationRequestInterceptor(private val authentications: List<
do {
response?.close()
val authentication = authentications[authenticationUse]
builder.header(authentication.header, authentication.headerValue)
runCatching {
builder.header(authentication.header, authentication.headerValue)
}.onFailure {
LOG.error("Error in setting the ${authentication.type.type} header", it)
}
response = chain.proceed(builder.build())
if (response.code != 401) {
break
Expand All @@ -51,4 +56,8 @@ class HawkbitAuthenticationRequestInterceptor(private val authentications: List<

return response!!
}

companion object {
val LOG = LoggerFactory.getLogger(HawkbitAuthenticationRequestInterceptor::class.java)!!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,46 @@ class DdiClientHttpRequestsTest : AbstractHaraMessageTest() {
startSubTestTest(true)
}

@Test(enabled = true, priority = 8, timeOut = 60_000)
fun useInvalidTokenWithForbiddenCharactersTest() = runBlocking {
enableTargetTokenInServer(true)
enableGatewayTokenInServer(true)
client = createClient(gatewayToken = "")

`test #6-1= request should fail, when there is invalid character in both auth tokens`()
`test #6-2= request should succeed, when there is an invalid character in target token with valid gateway token`()
}

private suspend fun `test #6-1= request should fail, when there is invalid character in both auth tokens`() {
logCurrentFunctionName()

val invalidToken = "\nInvalidGatewayToken"
client = createClient(targetToken = invalidToken, gatewayToken = invalidToken)

expectPollingOnlyMessage()
expectedServerResponses.apply {
add(emptyTokenErrorMessage())
add(emptyTokenErrorMessage())
}

startSubTestTest()
}

private suspend fun `test #6-2= request should succeed, when there is an invalid character in target token with valid gateway token`() {
logCurrentFunctionName()

val invalidToken = "\nInvalidGatewayToken"
client = createClient(targetToken = invalidToken, gatewayToken = gatewayToken)

expectPollingAndIdleMessages()
expectedServerResponses.apply {
add(emptyTokenErrorMessage())
add(gatewayTokenMessage(HttpURLConnection.HTTP_OK))
}

startSubTestTest()
}

private suspend fun startSubTestTest(lastTest: Boolean = false) {
client?.startAsync()
startWatchingExpectedMessages(lastTest)
Expand Down Expand Up @@ -392,6 +432,9 @@ class DdiClientHttpRequestsTest : AbstractHaraMessageTest() {
).headerValue
)

private fun emptyTokenErrorMessage() =
OkHttpMessage(HttpURLConnection.HTTP_UNAUTHORIZED, null)

data class OkHttpMessage(val code: Int, val authHeader: String?) :
ExpectedMessage()

Expand Down

0 comments on commit a3badc6

Please sign in to comment.