Skip to content

Commit de27aaf

Browse files
committed
Add method to find a single directive in scope and update audit logic
1 parent 3d4b64d commit de27aaf

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

gixy/directives/directive.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ def set_parent(self, parent):
3737

3838
@property
3939
def parents(self):
40+
"""Get all parent blocks"""
4041
parent = self.parent
4142
while parent:
4243
yield parent
4344
parent = parent.parent
4445

4546
@property
4647
def variables(self):
48+
"""Get all variables provided by this directive"""
4749
raise NotImplementedError()
4850

4951
def find_directives_in_scope(self, name):
@@ -54,11 +56,21 @@ def find_directives_in_scope(self, name):
5456
yield directive
5557
return None
5658

59+
def find_single_directive_in_scope(self, name):
60+
"""Find a single directive in the current scope"""
61+
for parent in self.parents:
62+
directive = parent.some(name, flat=False)
63+
if directive:
64+
return directive
65+
return None
66+
5767
def __str__(self):
58-
return "{name} {args};".format(name=self.name, args=" ".join(self.args))
68+
return f"{self.name} {' '.join(self.args)};"
5969

6070

6171
class AddHeaderDirective(Directive):
72+
"""The add_header directive is used to add a header to the response"""
73+
6274
nginx_name = "add_header"
6375

6476
def __init__(self, name, args):

gixy/plugins/try_files_is_evil_too.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
"""Module for try_files_is_evil_too plugin."""
2+
13
import gixy
24
from gixy.plugins.plugin import Plugin
35

@@ -18,8 +20,8 @@ class try_files_is_evil_too(Plugin):
1820

1921
def audit(self, directive):
2022
# search for open_file_cache ...; on the same or higher level
21-
open_file_cache = list(directive.find_directives_in_scope("open_file_cache"))
22-
if not open_file_cache or open_file_cache[0].args[0] == "off":
23+
open_file_cache = directive.find_single_directive_in_scope("open_file_cache")
24+
if not open_file_cache or open_file_cache.args[0] == "off":
2325
self.add_issue(
2426
severity=gixy.severity.MEDIUM,
2527
directive=[directive],

0 commit comments

Comments
 (0)