diff --git a/nginx.py b/nginx.py index aba8ad3..087e6d2 100755 --- a/nginx.py +++ b/nginx.py @@ -500,13 +500,14 @@ def loads(data, conf=True): index += m.end() continue - s1 = r'("[^"]+"|\'[^\']+\'|[^\s;]+)' - s2 = r'("[^"]*"|\'[^\']*\'|[^\s;]*)' - s3 = r'(\s*[^;]*?)' - key = r'^\s*{}\s*{}{};'.format(s1, s2, s3) - m = re.compile(key, re.S).search(data[index:]) + double = r'\s*"[^"]*"' + single = r'\s*\'[^\']*\'' + normal = r'\s*[^;\s]*' + s1 = r'{}|{}|{}'.format(double, single, normal) + s = r'^\s*({})\s*((?:{})+);'.format(s1, s1) + m = re.compile(s, re.S).search(data[index:]) if m: - k = Key(m.group(1), m.group(2) + m.group(3)) + k = Key(m.group(1), m.group(2)) if lopen and isinstance(lopen[0], (Container, Server)): lopen[0].add(k) else: diff --git a/tests.py b/tests.py index 489cf73..c72035d 100755 --- a/tests.py +++ b/tests.py @@ -162,6 +162,11 @@ } """ +TESTBLOCK_CASE_9 = """ +location test9 { + add_header X-XSS-Protection "1;mode-block"; +} +""" class TestPythonNginx(unittest.TestCase): def test_basic_load(self): @@ -259,6 +264,14 @@ def test_limit_expect(self): self.assertEqual(first_key.name, "deny") self.assertEqual(first_key.value, "all") + def test_semicolon_in_second_key_value(self): + inp_data = nginx.loads(TESTBLOCK_CASE_9) + self.assertEqual(len(inp_data.filter("Location")), 1) + location_children = inp_data.filter("Location")[0].children + self.assertEqual(len(location_children), 1) + self.assertEqual(location_children[0].name, "add_header") + self.assertEqual(location_children[0].value, 'X-XSS-Protection "1;mode-block"') + if __name__ == '__main__': unittest.main()