Skip to content

Commit

Permalink
Merge pull request #31 from fulder/master
Browse files Browse the repository at this point in the history
Fix bug with quoted second key value
  • Loading branch information
peakwinter authored Nov 28, 2018
2 parents 9870812 + 7838900 commit 7b6927e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 7 additions & 6 deletions nginx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 13 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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()

0 comments on commit 7b6927e

Please sign in to comment.