Raise a TypeError when invalid input passed to check_whitelist_blacklist

This commit is contained in:
Erik Johnson 2018-04-27 08:15:15 -05:00
parent 2ae510ff2b
commit bcccaf2621
No known key found for this signature in database
GPG Key ID: 5E5583C437808F3F
2 changed files with 24 additions and 0 deletions

View File

@ -364,6 +364,12 @@ def check_whitelist_blacklist(value, whitelist=None, blacklist=None):
if blacklist is not None:
if isinstance(blacklist, six.string_types):
blacklist = [blacklist]
if not hasattr(blacklist, '__iter__'):
raise TypeError(
'Expecting iterable blacklist, but got {0} ({1})'.format(
type(blacklist).__name__, blacklist
)
)
for expr in blacklist:
if expr_match(value, expr):
return False
@ -371,6 +377,12 @@ def check_whitelist_blacklist(value, whitelist=None, blacklist=None):
if whitelist is not None:
if isinstance(whitelist, six.string_types):
whitelist = [whitelist]
if not hasattr(whitelist, '__iter__'):
raise TypeError(
'Expecting iterable whitelist, but got {0} ({1})'.format(
type(whitelist).__name__, whitelist
)
)
for expr in whitelist:
if expr_match(value, expr):
return True

View File

@ -331,3 +331,15 @@ class StringutilsTestCase(TestCase):
blacklist=set(blacklist),
)
)
# Test with invalid type for whitelist/blacklist
self.assertRaises(
TypeError,
salt.utils.stringutils.check_whitelist_blacklist,
'foo', whitelist=123
)
self.assertRaises(
TypeError,
salt.utils.stringutils.check_whitelist_blacklist,
'foo', blacklist=123
)