mirror of
https://github.com/certbot/certbot.git
synced 2026-07-28 00:35:50 +02:00
Fixes issue when parsing an Nginx configuration file containing multiline quoted strings
This commit is contained in:
@@ -6,7 +6,7 @@ import string
|
||||
|
||||
from pyparsing import (
|
||||
Literal, White, Word, alphanums, CharsNotIn, Combine, Forward, Group,
|
||||
Optional, OneOrMore, Regex, ZeroOrMore)
|
||||
Optional, OneOrMore, QuotedString, Regex, ZeroOrMore)
|
||||
from pyparsing import stringEnd
|
||||
from pyparsing import restOfLine
|
||||
|
||||
@@ -29,8 +29,8 @@ class RawNginxParser(object):
|
||||
# any chars in single or double quotes
|
||||
# All of these COULD be upgraded to something like
|
||||
# https://stackoverflow.com/a/16130746
|
||||
dquoted = Regex(r'(\".*\")')
|
||||
squoted = Regex(r"(\'.*\')")
|
||||
dquoted = QuotedString('"', multiline=True)
|
||||
squoted = QuotedString("'", multiline=True)
|
||||
nonspecial = Regex(r"[^\{\};,]")
|
||||
varsub = Regex(r"(\$\{\w+\})")
|
||||
# nonspecial nibbles one character at a time, but the other objects take
|
||||
|
||||
@@ -109,6 +109,21 @@ class TestRawNginxParser(unittest.TestCase):
|
||||
['blah', '"hello;world"'],
|
||||
['try_files', '$uri @rewrites']]]]]])
|
||||
|
||||
def test_parse_from_file3(self):
|
||||
with open(util.get_data_filename('multiline_quotes.conf')) as handle:
|
||||
parsed = util.filter_comments(load(handle))
|
||||
self.assertEqual(
|
||||
parsed,
|
||||
[[['http'],
|
||||
[[['server'],
|
||||
[['listen', '*:443'],
|
||||
[['location', '/'],
|
||||
[['body_filter_by_lua',
|
||||
'ngx.ctx.buffered = (ngx.ctx.buffered or "") .. string.sub(ngx.arg[1], 1, 1000)\n'
|
||||
' if ngx.arg[2] then\n'
|
||||
' ngx.var.resp_body = ngx.ctx.buffered\n'
|
||||
' end']]]]]]]])
|
||||
|
||||
def test_abort_on_parse_failure(self):
|
||||
with open(util.get_data_filename('broken.conf')) as handle:
|
||||
self.assertRaises(ParseException, load, handle)
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
# Test nginx configuration file with multiline quoted strings.
|
||||
# Good example of usage for multilined quoted values is when
|
||||
# using Openresty's Lua directives and you wish to keep the
|
||||
# inline Lua code readable.
|
||||
http {
|
||||
server {
|
||||
listen *:443; # because there should be no other port open.
|
||||
|
||||
location / {
|
||||
body_filter_by_lua 'ngx.ctx.buffered = (ngx.ctx.buffered or "") .. string.sub(ngx.arg[1], 1, 1000)
|
||||
if ngx.arg[2] then
|
||||
ngx.var.resp_body = ngx.ctx.buffered
|
||||
end';
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user