split_host_and_port: reject ports above 65535 with HTTPInputError#3658
Open
HrachShah wants to merge 1 commit into
Open
split_host_and_port: reject ports above 65535 with HTTPInputError#3658HrachShah wants to merge 1 commit into
HrachShah wants to merge 1 commit into
Conversation
The current regex accepts any number of digits, so 'example.com:70000' parses without complaint and surfaces as an out-of-range port deeper in the stack. Add an explicit range check that raises HTTPInputError for any port above 65535, matching what the rest of the option/address parsers in the project already do for invalid host:port inputs.
Member
|
LGTM except for the lint failure |
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.
What
tornado.httputil.split_host_and_portparseshost:portstrings via aregex that accepts any number of digits in the port slot, so
split_host_and_port('example.com:70000')silently returns('example.com', 70000). That out-of-range port then surfaces in therequest handling stack as a confusing
OSErrorfrom socket creation or agarbled HTTP request instead of a clean validation error.
Fix
Add an explicit range check that raises
HTTPInputErrorfor any portabove 65535, matching what the rest of the option/address parsers in the
project do for invalid host:port inputs.
Negative ports are already filtered out by the regex (
\d+only matchesdigits), so the only runtime check needed is the upper bound.
Tests
Added
SplitHostAndPortTestintornado/test/httputil_test.py:test_host_only—example.com->('example.com', None)test_host_and_port—example.com:8080->('example.com', 8080)test_port_above_max_raises—example.com:70000->HTTPInputErrortest_port_at_upper_bound—example.com:65535-> acceptedtest_port_at_lower_bound—example.com:0-> acceptedAll 61
httputil_testtests pass. The newtest_port_above_max_raisesfails on the pre-fix code (returns the out-of-range port without raising).