]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Validate that netaddr does not receive a string with whitespace
authorGary Kotton <gkotton@redhat.com>
Tue, 7 May 2013 09:58:31 +0000 (09:58 +0000)
committerGary Kotton <gkotton@redhat.com>
Tue, 7 May 2013 11:52:12 +0000 (11:52 +0000)
Fixes bug 1177277

Change-Id: Ibecaeed3e5918b06e74d8fa68cbe443bcbada7fc

quantum/api/v2/attributes.py
quantum/tests/unit/test_attributes.py

index ea38e5f54441c5d9e0c52cb96ea510d4f01f21cb..55f37def6df48cb0bc3e16faa8a14bbe4c0cc709 100644 (file)
@@ -96,9 +96,18 @@ def _validate_range(data, valid_values=None):
         return msg
 
 
+def _validate_no_whitespace(data):
+    """Validates that input has no whitespace."""
+    if len(data.split()) > 1:
+        msg = _("'%s' contains whitespace") % data
+        LOG.debug(msg)
+        raise q_exc.InvalidInput(error_message=msg)
+    return data
+
+
 def _validate_mac_address(data, valid_values=None):
     try:
-        netaddr.EUI(data)
+        netaddr.EUI(_validate_no_whitespace(data))
     except Exception:
         msg = _("'%s' is not a valid MAC address") % data
         LOG.debug(msg)
@@ -107,7 +116,7 @@ def _validate_mac_address(data, valid_values=None):
 
 def _validate_ip_address(data, valid_values=None):
     try:
-        netaddr.IPAddress(data)
+        netaddr.IPAddress(_validate_no_whitespace(data))
     except Exception:
         msg = _("'%s' is not a valid IP address") % data
         LOG.debug(msg)
@@ -227,7 +236,7 @@ def _validate_ip_address_or_none(data, valid_values=None):
 
 def _validate_subnet(data, valid_values=None):
     try:
-        netaddr.IPNetwork(data)
+        netaddr.IPNetwork(_validate_no_whitespace(data))
         if len(data.split('/')) == 2:
             return
     except Exception:
index f3415aefcdb7664c8e1cf4dbaac140139dfde987..085142c901bfd052d48514b8881094bd1832042d 100644 (file)
@@ -92,6 +92,19 @@ class TestAttributes(base.BaseTestCase):
         msg = attributes._validate_string("123456789", None)
         self.assertIsNone(msg)
 
+    def test_validate_no_whitespace(self):
+        data = 'no_white_space'
+        result = attributes._validate_no_whitespace(data)
+        self.assertEqual(result, data)
+
+        self.assertRaises(q_exc.InvalidInput,
+                          attributes._validate_no_whitespace,
+                          'i have whitespace')
+
+        self.assertRaises(q_exc.InvalidInput,
+                          attributes._validate_no_whitespace,
+                          'i\thave\twhitespace')
+
     def test_validate_range(self):
         msg = attributes._validate_range(1, [1, 9])
         self.assertIsNone(msg)
@@ -135,6 +148,18 @@ class TestAttributes(base.BaseTestCase):
         msg = attributes._validate_ip_address(ip_addr)
         self.assertEqual(msg, "'%s' is not a valid IP address" % ip_addr)
 
+        ip_addr = '1.1.1.1 has whitespace'
+        msg = attributes._validate_ip_address(ip_addr)
+        self.assertEqual(msg, "'%s' is not a valid IP address" % ip_addr)
+
+        ip_addr = '111.1.1.1\twhitespace'
+        msg = attributes._validate_ip_address(ip_addr)
+        self.assertEqual(msg, "'%s' is not a valid IP address" % ip_addr)
+
+        ip_addr = '111.1.1.1\nwhitespace'
+        msg = attributes._validate_ip_address(ip_addr)
+        self.assertEqual(msg, "'%s' is not a valid IP address" % ip_addr)
+
     def test_validate_ip_pools(self):
         pools = [[{'end': '10.0.0.254'}],
                  [{'start': '10.0.0.254'}],