Skip to content

[Junie]: fix: handle multipart boundary parsing in KTOR 3.0 #4872

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,16 @@ internal fun parseHeaderValue(text: CharArrayBuilder, range: MutableRange) {
}

private fun noColonFound(text: CharSequence, range: MutableRange): Nothing {
throw ParserException("No colon in HTTP header in ${text.substring(range.start, range.end)} in builder: \n$text")
val headerText = text.substring(range.start, range.end)

// Check if this might be a multipart boundary line (starts with -- and might end with --)
if (headerText.startsWith("--") && (headerText.endsWith("--") || headerText.isEmpty())) {
Comment on lines +311 to +312
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

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

Extracting the multipart boundary detection logic into a helper method could improve clarity and reduce the risk of subtle bugs from relying on string pattern comparisons directly.

Suggested change
// Check if this might be a multipart boundary line (starts with -- and might end with --)
if (headerText.startsWith("--") && (headerText.endsWith("--") || headerText.isEmpty())) {
if (isMultipartBoundary(headerText)) {

Copilot uses AI. Check for mistakes.

// This is likely a multipart boundary line, not a header
// We'll throw a specific exception that can be caught and handled by the multipart parser
throw ParserException("Multipart boundary detected: $headerText")
}

throw ParserException("No colon in HTTP header in $headerText in builder: \n$text")
}

private fun characterIsNotAllowed(text: CharSequence, ch: Char): Nothing =
Expand Down
14 changes: 12 additions & 2 deletions ktor-http/ktor-http-cio/common/src/io/ktor/http/cio/Multipart.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,18 @@ private suspend fun parsePartHeadersImpl(input: ByteReadChannel): HttpHeadersMap
val builder = CharArrayBuilder()

try {
return parseHeaders(input, builder)
?: throw EOFException("Failed to parse multipart headers: unexpected end of stream")
try {
return parseHeaders(input, builder)
?: throw EOFException("Failed to parse multipart headers: unexpected end of stream")
} catch (e: ParserException) {
Copy link
Preview

Copilot AI May 21, 2025

Choose a reason for hiding this comment

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

Using exception message prefixes for control flow may be fragile; consider using a dedicated exception type or a flag to clearly indicate a multipart boundary condition.

Copilot uses AI. Check for mistakes.

// Check if this is our special case for multipart boundary
if (e.message?.startsWith("Multipart boundary detected:") == true) {
// This is a boundary line, not a header
// Return empty headers to signal that this is not a valid part
return HttpHeadersMap(builder)
}
throw e
}
} catch (t: Throwable) {
builder.release()
throw t
Expand Down