Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4715ca5

Browse files
committed
Issue #14036: return None when port in urlparse cross 65535
2 parents b95c634 + 2fc5a50 commit 4715ca5

3 files changed

Lines changed: 11 additions & 0 deletions

File tree

Lib/test/test_urlparse.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,11 @@ def test_urlsplit_attributes(self):
524524
self.assertEqual(p.port, 80)
525525
self.assertEqual(p.geturl(), url)
526526

527+
# Verify an illegal port is returned as None
528+
url = b"HTTP://WWW.PYTHON.ORG:65536/doc/#frag"
529+
p = urllib.parse.urlsplit(url)
530+
self.assertEqual(p.port, None)
531+
527532
def test_attributes_bad_port(self):
528533
"""Check handling of non-integer ports."""
529534
p = urllib.parse.urlsplit("http://www.example.net:foo")

Lib/urllib/parse.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ def port(self):
143143
port = self._hostinfo[1]
144144
if port is not None:
145145
port = int(port, 10)
146+
# Return None on an illegal port
147+
if not ( 0 <= port <= 65535):
148+
return None
146149
return port
147150

148151

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ Core and Builtins
4242
Library
4343
-------
4444

45+
- Issue #14036: Add an additional check to validate that port in urlparse does
46+
not go in illegal range and returns None.
47+
4548
- Issue #14862: Add missing names to os.__all__
4649

4750
- Issue #14875: Use float('inf') instead of float('1e66666') in the json module.

0 commit comments

Comments
 (0)