mirror of
https://github.com/certbot/certbot.git
synced 2026-01-23 07:20:55 +03:00
Do not silently truncate nginx config files
This commit is contained in:
@@ -4,7 +4,7 @@ import string
|
||||
from pyparsing import (
|
||||
Literal, White, Word, alphanums, CharsNotIn, Forward, Group,
|
||||
Optional, OneOrMore, Regex, ZeroOrMore, pythonStyleComment)
|
||||
|
||||
from pyparsing import stringEnd
|
||||
|
||||
class RawNginxParser(object):
|
||||
# pylint: disable=expression-not-assigned
|
||||
@@ -35,7 +35,7 @@ class RawNginxParser(object):
|
||||
+ Group(ZeroOrMore(Group(assignment) | block))
|
||||
+ right_bracket)
|
||||
|
||||
script = OneOrMore(Group(assignment) | block).ignore(pythonStyleComment)
|
||||
script = (OneOrMore(Group(assignment) | block) + stringEnd).ignore(pythonStyleComment)
|
||||
|
||||
def __init__(self, source):
|
||||
self.source = source
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
import operator
|
||||
import unittest
|
||||
|
||||
from pyparsing import ParseException
|
||||
|
||||
from letsencrypt_nginx.nginxparser import (
|
||||
RawNginxParser, load, dumps, dump)
|
||||
from letsencrypt_nginx.tests import util
|
||||
@@ -104,6 +106,10 @@ class TestRawNginxParser(unittest.TestCase):
|
||||
['blah', '"hello;world"'],
|
||||
['try_files', '$uri @rewrites']]]]]])
|
||||
|
||||
def test_abort_on_parse_failure(self):
|
||||
with open(util.get_data_filename('broken.conf')) as handle:
|
||||
self.assertRaises(ParseException, load, handle)
|
||||
|
||||
def test_dump_as_file(self):
|
||||
parsed = load(open(util.get_data_filename('nginx.conf')))
|
||||
parsed[-1][-1].append([['server'],
|
||||
|
||||
12
letsencrypt-nginx/letsencrypt_nginx/tests/testdata/etc_nginx/broken.conf
vendored
Normal file
12
letsencrypt-nginx/letsencrypt_nginx/tests/testdata/etc_nginx/broken.conf
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
# A faulty configuration file
|
||||
|
||||
pid logs/nginx.pid;
|
||||
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
include foo.conf;
|
||||
|
||||
@@@
|
||||
@@ -59,3 +59,18 @@ def get_nginx_configurator(
|
||||
version=version)
|
||||
config.prepare()
|
||||
return config
|
||||
|
||||
|
||||
def filter_comments(tree):
|
||||
"""Filter comment nodes from parsed configurations."""
|
||||
|
||||
def traverse(tree):
|
||||
"""Generator dropping comment nodes"""
|
||||
for key, values in tree:
|
||||
if isinstance(key, list):
|
||||
yield [key, filter_comments(values)]
|
||||
else:
|
||||
if key != '#':
|
||||
yield [key, values]
|
||||
|
||||
return list(traverse(tree))
|
||||
|
||||
Reference in New Issue
Block a user