From d66362f08f58e36f806812e1097de73f6d6d5bc9 Mon Sep 17 00:00:00 2001 From: Alexandre Aubin Date: Mon, 16 Nov 2020 18:11:26 +0100 Subject: [PATCH] Report incorrect syntax for more_set_headers --- package_linter.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/package_linter.py b/package_linter.py index 5791f13..4638409 100755 --- a/package_linter.py +++ b/package_linter.py @@ -631,6 +631,33 @@ class Configurations(TestSuite): "and https://github.com/openresty/headers-more-nginx-module#more_set_headers )" ) + @test() + def misc_nginx_more_set_headers(self): + + app = self.app + + for filename in os.listdir(app.path + "/conf") if os.path.exists(app.path + "/conf") else []: + # Ignore subdirs or filename not containing nginx in the name + if not os.path.isfile(app.path + "/conf/" + filename) or "nginx" not in filename: + continue + + content = open(app.path + "/conf/" + filename).read() + if "location" in content and "more_set_headers" in content: + + lines = content.split("\n") + more_set_headers_lines = [l for l in lines if "more_set_headers" in l] + def right_syntax(line): + return re.search(r"more_set_headers [\"\'][\w-]+\s?: .*[\"\'];", line) + + if any(not right_syntax(line) for line in more_set_headers_lines): + yield Warning( + "It looks like the syntax for the more_set_headers " + "instruction is incorrect in the nginx conf (N.B. " + ": it's different than the add_header syntax!) ... " + "The syntax should look like: " + "more_set_headers \"Header-Name: value\"" + ) + @test() def misc_nginx_path_traversal(self):