Issue #14036: return None when port in urlparse cross 65535
authorSenthil Kumaran <senthil@uthcode.com>
Thu, 24 May 2012 13:54:34 +0000 (21:54 +0800)
committerSenthil Kumaran <senthil@uthcode.com>
Thu, 24 May 2012 13:54:34 +0000 (21:54 +0800)
Lib/test/test_urlparse.py
Lib/urlparse.py
Misc/NEWS

index e484aafbc8d43444cd60596656a2203220520e88..3f316d385c359da6cc871c633cc2aaefab5eb7c2 100644 (file)
@@ -437,6 +437,11 @@ class UrlParseTestCase(unittest.TestCase):
         self.assertEqual(p.port, 80)
         self.assertEqual(p.geturl(), url)
 
+        # Verify an illegal port of value greater than 65535 is set as None
+        url = "http://www.python.org:65536"
+        p = urlparse.urlsplit(url)
+        self.assertEqual(p.port, None)
+
     def test_issue14072(self):
         p1 = urlparse.urlsplit('tel:+31-641044153')
         self.assertEqual(p1.scheme, 'tel')
index 4c57725ce35fc1a0d6edae873a44669be169d18c..8a207565030567ea6507a617787541a7b10c416d 100644 (file)
@@ -97,9 +97,11 @@ class ResultMixin(object):
         netloc = self.netloc.split('@')[-1].split(']')[-1]
         if ':' in netloc:
             port = netloc.split(':')[1]
-            return int(port, 10)
-        else:
-            return None
+            port = int(port, 10)
+            # verify legal port
+            if (0 <= port <= 65535):
+                return port
+        return None
 
 from collections import namedtuple
 
index e091706aec2adcd762582b84cc4cafb6391ac033..943f9f8840c25ffd611b68d631e5fb66977a9e6c 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -64,6 +64,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #14036: Add an additional check to validate that port in urlparse does
+  not go in illegal range and returns None.
+
 - Issue #14888: Fix misbehaviour of the _md5 module when called on data
   larger than 2**32 bytes.