summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.opendev.org>2022-02-18 21:19:51 +0000
committerGerrit Code Review <review@openstack.org>2022-02-18 21:19:51 +0000
commitcad560c52e9f39c69fda696e9095c052b4d23309 (patch)
tree52c2bd643f3e2c5efc8631e4ec81625a18478fa9
parent5c78c8d248ea48953816bc822e026c964b9be4ba (diff)
parentf44395870dcbe08e2f8142ec6d96a0004ba3300f (diff)
downloaddesignate-cad560c52e9f39c69fda696e9095c052b4d23309.tar.gz
Merge "Checks for invalid denylist regex patterns"
-rw-r--r--designate/objects/blacklist.py4
-rw-r--r--designate/objects/fields.py22
-rw-r--r--designate/tests/test_api/test_v2/test_blacklists.py45
-rw-r--r--releasenotes/notes/Fix-to-address-denylist-invalid-patterns-not-being-checked-ec1f1316ccc6cb1d.yaml16
4 files changed, 85 insertions, 2 deletions
diff --git a/designate/objects/blacklist.py b/designate/objects/blacklist.py
index 1a5eb388..a0dd4fcf 100644
--- a/designate/objects/blacklist.py
+++ b/designate/objects/blacklist.py
@@ -20,8 +20,8 @@ from designate.objects import fields
class Blacklist(base.DictObjectMixin, base.PersistentObjectMixin,
base.DesignateObject):
fields = {
- 'pattern': fields.StringFields(maxLength=255),
- 'description': fields.StringFields(maxLength=160, nullable=True),
+ 'pattern': fields.DenylistFields(maxLength=255),
+ 'description': fields.DenylistFields(maxLength=160, nullable=True),
}
STRING_KEYS = [
diff --git a/designate/objects/fields.py b/designate/objects/fields.py
index 8e6b4c2f..3da48088 100644
--- a/designate/objects/fields.py
+++ b/designate/objects/fields.py
@@ -452,3 +452,25 @@ class IPOrHost(IPV4AndV6AddressField):
if not re.match(StringFields.RE_ZONENAME, value):
raise ValueError("%s is not IP address or host name" % value)
return value
+
+
+class DenylistFields(StringFields):
+ def __init__(self, **kwargs):
+ super(DenylistFields, self).__init__(**kwargs)
+
+ def coerce(self, obj, attr, value):
+ value = super(DenylistFields, self).coerce(obj, attr, value)
+
+ if value is None:
+ return self._null(obj, attr)
+
+ # determine the validity if a regex expression filter has been used.
+ msg = ("%s is not a valid regular expression" % value)
+ if not len(value):
+ raise ValueError(msg)
+ try:
+ re.compile(value)
+ except Exception:
+ raise ValueError(msg)
+
+ return value
diff --git a/designate/tests/test_api/test_v2/test_blacklists.py b/designate/tests/test_api/test_v2/test_blacklists.py
index 2dff9b8c..0677b7e1 100644
--- a/designate/tests/test_api/test_v2/test_blacklists.py
+++ b/designate/tests/test_api/test_v2/test_blacklists.py
@@ -165,3 +165,48 @@ class ApiV2BlacklistsTest(ApiV2TestCase):
url = '/blacklists?description=test'
self.policy({'find_blacklists': '@'})
self._assert_exception('bad_request', 400, self.client.get, url)
+
+ def test_create_invalid_denylist_pattern(self):
+ self.policy({'create_blacklist': '@'})
+ body = {
+ 'description': u'This is the description.'
+ }
+
+ url = '/blacklists/'
+
+ # doing each pattern individually so upon error one can trace
+ # back to the exact line number
+ body['pattern'] = ''
+ self._assert_exception(
+ 'invalid_object', 400, self.client.post_json, url, body)
+
+ body['pattern'] = '#(*&^%$%$#@$'
+ self._assert_exception(
+ 'invalid_object', 400, self.client.post_json, url, body)
+
+ body['pattern'] = 'a' * 1000
+ self._assert_exception(
+ 'invalid_object', 400, self.client.post_json, url, body)
+
+ def test_update_invalid_denylist_pattern(self):
+ blacklist = self.create_blacklist(fixture=0)
+ self.policy({'update_blacklist': '@'})
+
+ url = ('/blacklists/%s' % blacklist['id'])
+
+ # doing each pattern individually so upon error one can trace
+ # back to the exact line number
+ body = {'pattern': ''}
+ self._assert_exception(
+ 'invalid_object', 400, self.client.patch_json, url, body,
+ status=400)
+
+ body = {'pattern': '#(*&^%$%$#@$'}
+ self._assert_exception(
+ 'invalid_object', 400, self.client.patch_json, url, body,
+ status=400)
+
+ body = {'pattern': 'a' * 1000}
+ self._assert_exception(
+ 'invalid_object', 400, self.client.patch_json, url, body,
+ status=400)
diff --git a/releasenotes/notes/Fix-to-address-denylist-invalid-patterns-not-being-checked-ec1f1316ccc6cb1d.yaml b/releasenotes/notes/Fix-to-address-denylist-invalid-patterns-not-being-checked-ec1f1316ccc6cb1d.yaml
new file mode 100644
index 00000000..43a3ca94
--- /dev/null
+++ b/releasenotes/notes/Fix-to-address-denylist-invalid-patterns-not-being-checked-ec1f1316ccc6cb1d.yaml
@@ -0,0 +1,16 @@
+---
+fixes:
+ - |
+ Fixes `bug 1934252`_ which ignored invalid denylist patterns. The fix
+ entailed checking the pattern string via regular expression compiler and
+ testing for zero length.
+
+ Previously you could create blacklist/denylist using string that cannot
+ be used either as a regex or as a zone name, for example:
+ patterns = ['', ``'#(*&^%$%$#@$']``
+
+ In addition, the server will return a 400 BadRequest response to an
+ invalid pattern.
+
+ .. _Bug 1934252: https://bugs.launchpad.net/designate/+bug/1934252
+