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

Content length verifier negative check #361

Merged
merged 1 commit into from Nov 25, 2022

Conversation

rnro
Copy link
Contributor

@rnro rnro commented Nov 24, 2022

ContentLengthVerifier init throws on negative values

Motivation:

We discovered that this code path can result in an arithmetic overflow in the case where the content-length header has INT_MIN value. Since negative content-lengths don't have any meaning anyway we may as well throw if we encounter one so the decoding can be aborted.

Modifications:

  • ContentLengthVerifier init throws a ContentLengthHeaderNegative error when the decoded value is < 0.
  • ContentLengthVerifier init throws a ContentLengthHeaderMalformedValue error when the value cannot be decoded, e.g. it is out of bounds for Int or is an invalid string.

Result:

We will no longer panic for INT_MIN content-lengths and will fail decoding if one of various invalid values is encountered.

@swift-server-bot
Copy link

Can one of the admins verify this patch?

8 similar comments
@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@swift-server-bot
Copy link

Can one of the admins verify this patch?

@rnro rnro force-pushed the ContentLengthVerifierNegativeCheck branch 2 times, most recently from dbd8c37 to d734101 Compare November 24, 2022 17:21
Motivation:

We discovered that this code path can result in an arithmetic overflow
in the case where the content-length header has INT_MIN value. Since
negative content-lengths don't have any meaning anyway we may as well
throw if we encounter one so the decoding can be aborted.

Modifications:

* ContentLengthVerifier init throws a `ContentLengthHeaderNegative` error
when the decoded value is < 0.
* ContentLengthVerifier init throws a `ContentLengthHeaderMalformedValue` error
when the value cannot be decoded, e.g. it is out of bounds for Int or is
an invalid string.

Result:

We will no longer panic for INT_MIN content-lengths and will fail
decoding if one of various invalid values is encountered.
@rnro rnro marked this pull request as ready for review November 25, 2022 10:00
@rnro rnro force-pushed the ContentLengthVerifierNegativeCheck branch from d734101 to ea77144 Compare November 25, 2022 10:24
@Lukasa Lukasa merged commit a6d0d37 into apple:main Nov 25, 2022
Comment on lines 61 to +68
self.expectedContentLength = Int(first, radix: 10)

guard let expectedLength = self.expectedContentLength else {
throw NIOHTTP2Errors.contentLengthHeaderMalformedValue()
}
if expectedLength < 0 {
throw NIOHTTP2Errors.contentLengthHeaderNegative()
}
Copy link
Member

Choose a reason for hiding this comment

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

Note: more of a felling then an actual rule I follow

I usually only assign after the value is fully validated. It doesn't really matter but it signals to me at least that once it is assigned I'm done with it.

Suggested change
self.expectedContentLength = Int(first, radix: 10)
guard let expectedLength = self.expectedContentLength else {
throw NIOHTTP2Errors.contentLengthHeaderMalformedValue()
}
if expectedLength < 0 {
throw NIOHTTP2Errors.contentLengthHeaderNegative()
}
guard let expectedLength = Int(first, radix: 10) else {
throw NIOHTTP2Errors.contentLengthHeaderMalformedValue()
}
if expectedLength < 0 {
throw NIOHTTP2Errors.contentLengthHeaderNegative()
}
self.expectedContentLength = expectedLength

@glbrntt glbrntt added the 🔼 needs-minor-version-bump For PRs that when merged cause a bump of the minor version, ie. 1.x.0 -> 1.(x+1).0 label Feb 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔼 needs-minor-version-bump For PRs that when merged cause a bump of the minor version, ie. 1.x.0 -> 1.(x+1).0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants