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: Ignore everything after closing boundary when parsing multi form data #2862

Merged
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion zio-http/jvm/src/test/scala/zio/http/FormSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ object FormSpec extends ZIOHttpSpec {
form2 == form,
)
},
test("encoding with custom paramaters [charset]") {
test("encoding with custom parameters [charset]") {

val form = Form(
FormField.textField(
Expand Down Expand Up @@ -144,6 +144,30 @@ object FormSpec extends ZIOHttpSpec {
}

},
test("decoding no lf at the end") {
val body = Chunk.fromArray(
s"""|--(((AaB03x)))${CR}
|Content-Disposition: form-data; name="hocon-data"${CR}
|Content-Type: text/plain${CR}
|${CR}
|foos: []${CR}
|--(((AaB03x)))${CR}
|Content-Disposition: form-data; name="json-data"${CR}
|Content-Type: text/plain${CR}
|${CR}
|{ "bars": [] }${CR}
|--(((AaB03x)))--""".stripMargin.getBytes(),
)

val form = Form(
FormField.textField("hocon-data", "foos: []", MediaType.text.`plain`),
FormField.textField("json-data", """{ "bars": [] }""", MediaType.text.`plain`),
)

Form
.fromMultipartBytes(body)
.map(form2 => assertTrue(form2 == form))
},
)

val multiFormStreamingSuite: Spec[Any, Throwable] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ private[http] object FormState {

final class FormStateBuffer(boundary: Boundary) extends FormState { self =>

private val tree0: ChunkBuilder[FormAST] = ChunkBuilder.make[FormAST]()
private val buffer: ChunkBuilder.Byte = new ChunkBuilder.Byte
private val tree0: ChunkBuilder[FormAST] = ChunkBuilder.make[FormAST]()
private val buffer: ChunkBuilder[Byte] = new ChunkBuilder.Byte
private var bufferSize: Int = 0
seakayone marked this conversation as resolved.
Show resolved Hide resolved
private val boundaryMatches: Array[Boolean] = new Array[Boolean](boundary.closingBoundaryBytes.size)

private var lastByte: OptionalByte = OptionalByte.None
private var isBufferEmpty = true
Expand All @@ -54,8 +56,20 @@ private[http] object FormState {

val crlf = byte == '\n' && !lastByte.isEmpty && lastByte.get == '\r'

var boundaryDetected = false;
val posInLine = bufferSize + (if (this.lastByte.isEmpty) 0 else 1)
if (posInLine < boundary.closingBoundaryBytes.size) {
seakayone marked this conversation as resolved.
Show resolved Hide resolved
boundaryMatches.update(posInLine, boundary.closingBoundaryBytes(posInLine) == byte)
}

if (posInLine == boundary.closingBoundaryBytes.size - 1) {
boundaryDetected = boundaryMatches.forall(_ == true)
}

def flush(ast: FormAST): Unit = {
buffer.clear()
bufferSize = 0
boundaryMatches.map(_ => false)
seakayone marked this conversation as resolved.
Show resolved Hide resolved
lastByte = OptionalByte.None
if (crlf && isBufferEmpty && (phase eq Phase.Part1)) phase0 = Phase.Part2
if (ast.isContent && dropContents) () else addToTree(ast)
Expand All @@ -72,7 +86,12 @@ private[http] object FormState {
case ClosingBoundary(_) => BoundaryClosed(tree)
}

} else if (crlf && (phase eq Phase.Part2)) {
} else if ((crlf || boundaryDetected) && (phase eq Phase.Part2)) {
if (boundaryDetected) {
val hyphen = "-".getBytes().head
buffer += hyphen
buffer += hyphen
seakayone marked this conversation as resolved.
Show resolved Hide resolved
}
val ast = FormAST.makePart2(buffer.result(), boundary)

ast match {
Expand All @@ -87,6 +106,7 @@ private[http] object FormState {
if (!lastByte.isEmpty) {
if (isBufferEmpty) isBufferEmpty = false
buffer += lastByte.get
bufferSize = bufferSize + 1
seakayone marked this conversation as resolved.
Show resolved Hide resolved
}
lastByte = OptionalByte.Some(byte)
self
Expand All @@ -102,6 +122,8 @@ private[http] object FormState {
def reset(): Unit = {
tree0.clear()
buffer.clear()
bufferSize = 0
boundaryMatches.map(_ => false)
seakayone marked this conversation as resolved.
Show resolved Hide resolved
isBufferEmpty = true
dropContents = false
phase0 = Phase.Part1
Expand Down
Loading