Commits

Virgil Dupras committed ced27d8

Handle invalid filters in conf.

Comments (0)

Files changed (3)

core/model/filter.py

 from collections import namedtuple
 import re
 
+from .visit import Hit
+
 FilterItem = namedtuple('FilterItem', 'type regexp')
 
+ACCEPTED_TYPES = set(Hit._fields)
+
 class FilterList:
     def __init__(self):
         self._filters = []
     
     def add_filter(self, type_, value):
-        item = FilterItem(type_, re.compile(value))
+        if type_ not in ACCEPTED_TYPES:
+            return
+        try:
+            item = FilterItem(type_, re.compile(value))
+        except re.error:
+            return
         self._filters.append(item)
     
     def hit_matches(self, hit):

gui/main_window.py

             conf = yaml.load(fp)
         filters = conf.get('filters', [])
         for filter_item in filters:
-            type_ = filter_item['type']
-            value = filter_item['value']
+            try:
+                type_ = filter_item['type']
+                value = filter_item['value']
+            except KeyError:
+                continue
             self.filters.add_filter(type_, value)
     
     def load_log(self, logpath):

tests/filter_test.py

     ]}
     app.load_conf(conf)
     app.load_log('simple.log')
-    assert app.vtable[0].last_url != '/updates/dupeguru.aiu'
+    assert app.vtable[0].last_url != '/updates/dupeguru.aiu'
+
+@with_app(TestApp)
+def test_invalid_filters(app):
+    # invalid filter types don't cause crashes
+    conf = {'filters': [
+        {'type': 'foobar', 'value': 'hello'}, # invalid type
+        {'invalid': '', 'keys': None}, # invalid keys
+        {'type': 'url', 'value': '['}, # invalid regex
+    ]}
+    app.load_conf(conf) # no crash
+    app.load_log('simple.log') # no crash
+    eq_(app.vtable[0].last_url, '/updates/dupeguru.aiu')