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

(cherry picked from commit 5513dcf)
  • Loading branch information
Stexxe authored and e5l committed Apr 5, 2024
1 parent 0c0e0a3 commit 182386a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Expand Up @@ -151,7 +151,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
Expand Up @@ -744,4 +744,29 @@ abstract class ContentTestSuite<TEngine : ApplicationEngine, TConfiguration : Ap
assertEquals("Hello", bodyAsText())
}
}

@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/"
}
}

0 comments on commit 182386a

Please sign in to comment.