Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blacklists.py: normalize items when comparing for duplicates #13457

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions blacklists.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@ def _write(self, callback):
with open(self._filename, 'w', encoding='utf-8') as f:
yaml.dump(d, f)

def _normalize(self, item):
return item.rstrip()

def _validate(self, item):
ip_regex = regex.compile(r'''
(?(DEFINE)(?P<octet>
Expand Down Expand Up @@ -242,10 +245,11 @@ def add(self, item):
prikey = self.SCHEMA_PRIKEY

def add_callback(d):
item_normalized = self._normalize(item[prikey])
for compare in d['items']:
if compare[prikey] == item[prikey]:
raise ValueError('{0} already in list {1}'.format(
item[prikey], d['items']))
if self._normalize(compare[prikey]) == item_normalized:
raise KeyError('{0} already in list {1}'.format(
compare[prikey], d['items']))
d['items'].append(item)

self._write(add_callback)
Expand Down
12 changes: 7 additions & 5 deletions test/test_blacklists.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ def test_yaml_blacklist():
blacklist.add('1.3.34')
with pytest.raises(ValueError) as e:
blacklist.add({'ip': '1.3.4'})
with pytest.raises(ValueError) as e:
with pytest.raises(KeyError) as e:
blacklist.add({'ip': '1.2.3.4'})
with pytest.raises(ValueError) as e:
with pytest.raises(KeyError) as e:
blacklist.add({'ip': '2.3.4.5'})
with pytest.raises(ValueError) as e:
blacklist.remove({'ip': '34.45.56.67'})
Expand Down Expand Up @@ -195,9 +195,9 @@ def test_yaml_asn():
blacklist.add('123')
with pytest.raises(ValueError) as e:
blacklist.add({'asn': 'invalid'})
with pytest.raises(ValueError) as e:
with pytest.raises(KeyError) as e:
blacklist.add({'asn': '123'})
with pytest.raises(ValueError) as e:
with pytest.raises(KeyError) as e:
blacklist.add({'asn': '234'})
with pytest.raises(ValueError) as e:
blacklist.remove({'asn': '9897'})
Expand Down Expand Up @@ -226,8 +226,10 @@ def test_yaml_nses():
blacklist = Blacklist(('test_nses.yml', YAMLParserNS))
assert 'example.com.' in blacklist.parse()
assert 'EXAMPLE!COM.' not in blacklist.parse()
with pytest.raises(ValueError) as e:
with pytest.raises(KeyError) as e:
blacklist.add({'ns': 'example.com.'})
with pytest.raises(KeyError) as e:
blacklist.add({'ns': 'EXAMPLE.COM.'})
with pytest.raises(ValueError) as e:
blacklist.add({'ns': 'EXAMPLE!COM.'})
assert 'example.net.' not in blacklist.parse()
Expand Down