1
0
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:
Ceesjan Luiten
2015-07-10 22:54:54 +02:00
parent 10e993331c
commit 86c8fb1410
4 changed files with 35 additions and 2 deletions

View File

@@ -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

View File

@@ -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'],

View File

@@ -0,0 +1,12 @@
# A faulty configuration file
pid logs/nginx.pid;
events {
worker_connections 1024;
}
include foo.conf;
@@@

View File

@@ -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))