require three ASCII digits for the response status code - #9731
Open
basavaraj-sm05 wants to merge 2 commits into
Open
require three ASCII digits for the response status code#9731basavaraj-sm05 wants to merge 2 commits into
basavaraj-sm05 wants to merge 2 commits into
Conversation
JakeWharton
reviewed
Aug 27, 2026
Comment on lines
+80
to
+84
| val codeString = statusLine.substring(codeStart, codeStart + 3) | ||
| if (!codeString.all { it in '0'..'9' }) { | ||
| throw ProtocolException("Unexpected status line: $statusLine") | ||
| } | ||
| val code = codeString.toInt() |
Collaborator
There was a problem hiding this comment.
This makes multiple passes and allocations. If we're going to modify this code, we should also improve its performance by not creating the substring and operating directly on chars/doing the trivial math ourselves.
Contributor
Author
There was a problem hiding this comment.
Done. It now walks the three chars once, checking each is in '0'..'9' and accumulating the code in the same loop, so the substring and the extra pass are both gone.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
StatusLine.parse pulls the three response-code characters out of the status line and hands them to toIntOrNull() to get the numeric code. That delegate does more than read ASCII digits: it accepts a leading + or - sign, and because it resolves each character through Character.digit it also accepts any Unicode decimal digit, so an Arabic-Indic "٢٠٠" comes back as 200 and "+99" as 99. RFC 9112 defines status-code as exactly three ASCII DIGIT, and the value here comes straight off the wire, whether from an HTTP/1 status line, an HTTP/2 :status pseudo-header (Http2ExchangeCodec builds "HTTP/1.1 $value" from it), or a cached response, so a hostile or non-conforming server can hand okhttp a code that a conforming proxy or log sitting in front of it reads differently. A "-12" even parses to a negative code that only trips a check much later when the Response is built. I noticed it while adding cases to the existing nonThreeDigitCode test, which only covered letters and wrong lengths. Restricting the three characters to '0'..'9' before converting keeps every valid code working and rejects the rest at the parser.