Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Int overflow in BodyParsers.anyContent(maxLength: Option[Long]) #10741

Merged
merged 1 commit into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import play.api.test._

class AnyContentBodyParserSpec extends PlaySpecification {
"The anyContent body parser" should {
def parse(method: String, contentType: Option[String], body: ByteString)(implicit app: Application) = {
def parse(method: String, contentType: Option[String], body: ByteString, maxLength: Option[Long] = None)(
implicit app: Application
) = {
implicit val mat = app.materializer
val parsers = app.injector.instanceOf[PlayBodyParsers]
val request = FakeRequest(method, "/x").withHeaders(contentType.map(CONTENT_TYPE -> _).toSeq: _*)
await(parsers.anyContent(request).run(Source.single(body)))
await(parsers.anyContent(maxLength).apply(request).run(Source.single(body)))
}

"parse text bodies for DELETE requests" in new WithApplication(_.globalApp(false)) {
Expand Down Expand Up @@ -66,5 +68,11 @@ class AnyContentBodyParserSpec extends PlaySpecification {
}
}
}

"accept greater than 2G bytes. not Int overflow" in new WithApplication(_.globalApp(false)) {
parse("POST", Some("text/plain"), ByteString("bar"), maxLength = Some(Int.MaxValue.toLong + 2L)) must beRight(
AnyContentAsText("bar")
)
}
}
}
2 changes: 1 addition & 1 deletion core/play/src/main/scala/play/api/mvc/BodyParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ trait PlayBodyParsers extends BodyParserUtils {
def anyContent(maxLength: Option[Long]): BodyParser[AnyContent] = BodyParser("anyContent") { request =>
import Execution.Implicits.trampoline

def maxLengthOrDefault = maxLength.fold(DefaultMaxTextLength)(_.toInt)
def maxLengthOrDefault = maxLength.getOrElse(DefaultMaxTextLength)
def maxLengthOrDefaultLarge = maxLength.getOrElse(DefaultMaxDiskLength)
Comment on lines +921 to 922
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know the reason there are maxLengthOrDefault and maxLengthOrDefaultLarge both. 🤔
Should we remove either one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like maxLengthOrDefault is the maximum for a body stored in memory, whereas maxLengthOrDefaultLarge is the maximum for a body stored on disk. Perhaps they could be renamed to:

def maxMemoryLength = maxLength.getOrElse(DefaultMaxTextLength)
def maxDiskLength = maxLength.getOrElse(DefaultMaxDiskLength)

val contentType: Option[String] = request.contentType.map(_.toLowerCase(Locale.ENGLISH))
contentType match {
Expand Down