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

KTOR-8015 CIO: Do not accept CR as line delimiter in requests #4668

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

osipxd
Copy link
Member

@osipxd osipxd commented Feb 12, 2025

Subsystem
Server (CIO)

Motivation
KTOR-8015 Server accepts \r without a following \n as a valid line terminator in chunked transfer encoding

Solution
Add a parameter lineEnding to readUTF8Line to make it possible to ensure that a proper line ending is used.

@osipxd osipxd self-assigned this Feb 12, 2025
@@ -395,6 +396,32 @@ public suspend fun ByteReadChannel.discard(max: Long = Long.MAX_VALUE): Long {
private const val CR: Byte = '\r'.code.toByte()
private const val LF: Byte = '\n'.code.toByte()

@JvmInline
public value class LineEndingMode private constructor(private val mode: Int) {
Copy link
Member Author

@osipxd osipxd Feb 12, 2025

Choose a reason for hiding this comment

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

I could fix the problem only for chunked body, but it seems we should also fix line endings in request/response parsing.

Comment on lines 492 to 496
private fun LineEndingMode.assertIncludes(other: LineEndingMode) {
if (other !in this) {
throw EOFException("Unexpected line ending $other, while expected $this")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think EOF is technically correct since it's malformed data but the input hasn't ended, maybe just an IOException would do.

Could there be scenarios here where you'd simply want to ignore the partial line ending?

Copy link
Member Author

@osipxd osipxd Feb 13, 2025

Choose a reason for hiding this comment

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

I don't think EOF is technically correct

Changed to IOException

Could there be scenarios here where you'd simply want to ignore the partial line ending?

That is how Netty works. It just ignores everything until the LF line ending. It makes a server more tolerant to malformed requests. The question is, do we want to make it tolerant?

Copy link
Member

Choose a reason for hiding this comment

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

I think it's better to throw an exception as Jetty and Tomcat do it. Also, a user says that the behavior of Google Load Balancer (in Netty it's the same: parse input as one request with two chunks) is incorrect for them

@osipxd osipxd force-pushed the osipxd/kmp-engine-test-base branch 3 times, most recently from 8a50989 to 4231ba6 Compare February 13, 2025 10:56
@osipxd osipxd changed the title KTOR-8015 CIO: Do not accept CR as line delimiter in chunked requests KTOR-8015 CIO: Do not accept CR as line delimiter in requests Feb 13, 2025
@osipxd osipxd force-pushed the osipxd/kmp-engine-test-base branch 3 times, most recently from 9e166eb to 148dc2c Compare February 13, 2025 12:05
}

@OptIn(InternalAPI::class, InternalIoApi::class)
public suspend fun ByteReadChannel.readUTF8LineTo(
Copy link
Member Author

Choose a reason for hiding this comment

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

I will add KDoc comments if this solution with the new API is acceptable

@osipxd osipxd requested a review from marychatte February 13, 2025 12:08
@osipxd osipxd force-pushed the osipxd/kmp-engine-test-base branch from 148dc2c to ef09f08 Compare February 13, 2025 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants