Skip to content

Commit

Permalink
KTOR-6851 Parsing Content-Length to Long instead of Int to prevent Nu… (
Browse files Browse the repository at this point in the history
#4008)

* KTOR-6851 Parse Content-Length to Long instead of Int to prevent NumberFormatException for lengths more than Int.MAX_VALUE
  • Loading branch information
Stexxe committed Mar 28, 2024
1 parent 9c78494 commit 5513dcf
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public class CIOApplicationEngine(
}

private fun hasBody(request: CIOApplicationRequest): Boolean {
val contentLength = request.headers[HttpHeaders.ContentLength]?.toInt()
val contentLength = request.headers[HttpHeaders.ContentLength]?.toLong()
val transferEncoding = request.headers[HttpHeaders.TransferEncoding]
return transferEncoding != null || (contentLength != null && contentLength > 0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,27 @@ abstract class ContentTestSuite<TEngine : ApplicationEngine, TConfiguration : Ap
}
}

@Test
fun testReceivingBodyWithContentLengthMoreThanMaxInt() {
createAndStartServer {
post("/") {
call.receiveChannel()
call.respondText { "OK" }
}
}

withUrl(
"/",
{
method = HttpMethod.Post
headers.append("Content-Length", (Int.MAX_VALUE.toLong() + 1).toString())
}
) {
assertEquals(200, status.value)
assertEquals("OK", bodyAsText())
}
}

companion object {
const val classesDir: String = "build/classes/"
}
Expand Down

0 comments on commit 5513dcf

Please sign in to comment.