Skip to content

Commit

Permalink
BUG: lib/message: check also extra field names
Browse files Browse the repository at this point in the history
if they match the regular expression for key names

fixes #1807
  • Loading branch information
Sebastian Wagner authored and waldbauer-certat committed Sep 9, 2021
1 parent f975bd3 commit 24f390b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ CHANGELOG
- Refactor upgrade functions global configuration handling removing the old-style defaults configuration (PR#2058 by Sebastian Wagner).
- Pass version history as parameter to upgrade functions (PR#2058 by Sebastian Wagner).
- `intelmq.lib.message`:
- Fix and pre-compile the regular expression for harmonization key names (PR#2059 by Sebastian Wagner, fixes #1807).
- Fix and pre-compile the regular expression for harmonization key names and also check keys in the `extra.` namespace (PR#2059 by Sebastian Wagner, fixes #1807).

### Development

Expand Down
4 changes: 3 additions & 1 deletion intelmq/lib/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,10 @@ def __is_valid_key(self, key: str):
class_name, subitem = self.__get_type_config(key)
except KeyError:
return False
if key in self.harmonization_config or key == '__type' or subitem:
if key in self.harmonization_config or key == '__type':
return True
if subitem:
return HARMONIZATION_KEY_FORMAT.match(key)
return False

def __is_valid_value(self, key: str, value: str):
Expand Down
6 changes: 6 additions & 0 deletions intelmq/tests/lib/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,12 @@ def test_invalid_harm_key(self):
with self.assertRaises(exceptions.InvalidKey):
message.Event(harmonization={'event': {'foo.bar.': {}}})

def test_invalid_extra_key_name(self):
""" Test if error is raised if an extra field name is invalid. """
event = message.Event(harmonization=HARM)
with self.assertRaises(exceptions.InvalidKey):
event.add('extra.foo-', 'bar')


class TestReport(unittest.TestCase):
"""
Expand Down

0 comments on commit 24f390b

Please sign in to comment.