← Back to team overview

yahoo-eng-team team mailing list archive

[Bug 1452804] [NEW] _validate_mac_address do not check if it's input is None

 

Public bug reported:

In neutron/api/v2/attributes.py I found a TODO near line 170:

def _validate_no_whitespace(data):
    """Validates that input has no whitespace."""
    if re.search(r'\s', data):
        msg = _("'%s' contains whitespace") % data
        LOG.debug(msg)
        raise n_exc.InvalidInput(error_message=msg)
    return data


def _validate_mac_address(data, valid_values=None):
    try:
        valid_mac = netaddr.valid_mac(_validate_no_whitespace(data))
    except Exception:
        valid_mac = False
    # TODO(arosen): The code in this file should be refactored
    # so it catches the correct exceptions. _validate_no_whitespace
    # raises AttributeError if data is None.
    if not valid_mac:
        msg = _("'%s' is not a valid MAC address") % data
        LOG.debug(msg)
        return msg

_validate_mac_address will be called in neutron/api/v2/attributes.py :

'type:mac_address': _validate_mac_address


if data is None in _validate_no_whitespace, re.search(r'\s', data) will raise an Exception and we will get TypeError instead of n_exc.InvalidInput:

>>> import re
>>> re.search(r'\s', None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/re.py", line 142, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or buffer

The traceback msg can confuse the caller. So I thinks this better to
warp TypeError into  n_exc.InvalidInput with a clear error msg to the
caller.

** Affects: neutron
     Importance: Undecided
         Status: New

-- 
You received this bug notification because you are a member of Yahoo!
Engineering Team, which is subscribed to neutron.
https://bugs.launchpad.net/bugs/1452804

Title:
  _validate_mac_address do not check if it's input is None

Status in OpenStack Neutron (virtual network service):
  New

Bug description:
  In neutron/api/v2/attributes.py I found a TODO near line 170:

  def _validate_no_whitespace(data):
      """Validates that input has no whitespace."""
      if re.search(r'\s', data):
          msg = _("'%s' contains whitespace") % data
          LOG.debug(msg)
          raise n_exc.InvalidInput(error_message=msg)
      return data

  
  def _validate_mac_address(data, valid_values=None):
      try:
          valid_mac = netaddr.valid_mac(_validate_no_whitespace(data))
      except Exception:
          valid_mac = False
      # TODO(arosen): The code in this file should be refactored
      # so it catches the correct exceptions. _validate_no_whitespace
      # raises AttributeError if data is None.
      if not valid_mac:
          msg = _("'%s' is not a valid MAC address") % data
          LOG.debug(msg)
          return msg

  _validate_mac_address will be called in neutron/api/v2/attributes.py :

  'type:mac_address': _validate_mac_address

  
  if data is None in _validate_no_whitespace, re.search(r'\s', data) will raise an Exception and we will get TypeError instead of n_exc.InvalidInput:

  >>> import re
  >>> re.search(r'\s', None)
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/lib64/python2.7/re.py", line 142, in search
      return _compile(pattern, flags).search(string)
  TypeError: expected string or buffer

  The traceback msg can confuse the caller. So I thinks this better to
  warp TypeError into  n_exc.InvalidInput with a clear error msg to the
  caller.

To manage notifications about this bug go to:
https://bugs.launchpad.net/neutron/+bug/1452804/+subscriptions


Follow ups

References