Skip to content

Raise InvalidURL for out-of-range ports - #3883

Open
swastiktawate wants to merge 2 commits into
encode:masterfrom
swastiktawate:fix-invalid-port-range
Open

swastiktawate wants to merge 2 commits into
encode:masterfrom
swastiktawate:fix-invalid-port-range

Conversation

@swastiktawate

Copy link
Copy Markdown

URL() accepted out-of-range ports such as 99999, 65536, and -1, because normalize_port() only checked that int(port) works. The bad value reached the socket layer, where on Linux it wraps modulo 65536, so a request to port 99999 can reach a server on port 34463.

This change raises InvalidURL in normalize_port() when the port is below 0 or above 65535, mirroring the existing error for non-numeric ports. Port 0 is left accepted, since some parsers accept it and it is a separate decision.

Tests: added test_url_invalid_port_range in tests/models/test_url.py covering 65536, 99999, and -1. tests/models/test_url.py: 93 passed.

Fixes #3875. The bug was reported in discussion #3875 and independently reproduced there by Mickael Saidi.

normalize_port() accepted any integer port, so values such as 99999, 65536, and -1 reached the socket layer. Raise InvalidURL when the port is below 0 or above 65535, and add tests covering 65536, 99999, and -1. See discussion encode#3875.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant