Skip to content

Commit 797fb23

Browse files
author
Joshua Rogers
committed
Do not warn on proxy_pass_normalized if request_uri is used.
Also, turn find_directive_in_scope into find_directives_in_scope, allowing it to turn into a list.
1 parent 0678c01 commit 797fb23

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

gixy/directives/directive.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ def parents(self):
4646
def variables(self):
4747
raise NotImplementedError()
4848

49-
def find_directive_in_scope(self, name):
50-
"""Find directive in the current scope"""
49+
def find_directives_in_scope(self, name):
50+
"""Find directives in the current scope"""
5151
for parent in self.parents:
5252
directive = parent.some(name, flat=False)
5353
if directive:
54-
return directive
54+
yield directive
5555
return None
5656

5757
def __str__(self):

gixy/plugins/proxy_pass_normalized.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22
import gixy
3+
import sys
34
from gixy.plugins.plugin import Plugin
45

56
class proxy_pass_normalized(Plugin):
@@ -12,7 +13,7 @@ class proxy_pass_normalized(Plugin):
1213
"""
1314

1415
summary = 'Detect path after host in proxy_pass (potential URL decoding issue)'
15-
severity = gixy.severity.LOW
16+
severity = gixy.severity.MEDIUM
1617
description = ("A slash immediately after the host in proxy_pass leads to the path being decoded and normalized before proxying downstream, leading to unexpected behavior related to encoded slashes.")
1718
help_url = 'https://joshua.hu/proxy-pass-nginx-decoding-normalizing-url-path-dangerous#nginx-proxy_pass'
1819
directives = ['proxy_pass']
@@ -22,23 +23,30 @@ def __init__(self, config):
2223
self.parse_uri_re = re.compile(r'(?P<scheme>[^?#/)]+://)?(?P<host>[^?#/)]+)(?P<path>/.*)?')
2324

2425
def audit(self, directive):
25-
proxy_pass_arg = directive.args[0]
26-
if not proxy_pass_arg:
26+
proxy_pass_args = directive.args
27+
28+
if not proxy_pass_args:
2729
return
2830

29-
parsed = self.parse_uri_re.match(proxy_pass_arg)
31+
parsed = self.parse_uri_re.match(proxy_pass_args[0])
3032

3133
if not parsed:
3234
return
3335

3436
if not parsed.group('path'):
3537
return
3638

39+
40+
for rewrite in directive.find_directives_in_scope("rewrite"):
41+
if hasattr(rewrite, 'pattern') and hasattr(rewrite, 'replace'):
42+
if rewrite.pattern == '^' and rewrite.replace == '$request_uri':
43+
return
44+
3745
self.add_issue(
3846
severity=self.severity,
3947
directive=[directive, directive.parent],
4048
reason=(
41-
"Found a slash (and possibly more) after the hostname in proxy_pass. "
49+
"Found a slash (and possibly more) after the hostname in proxy_pass, without using $request_uri."
4250
"This can lead to path decoding issues."
4351
)
4452
)

gixy/plugins/try_files_is_evil_too.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ class try_files_is_evil_too(Plugin):
1818

1919
def audit(self, directive):
2020
# search for open_file_cache ...; on the same or higher level
21-
open_file_cache = directive.find_directive_in_scope("open_file_cache")
22-
if not open_file_cache or open_file_cache.args[0] == "off":
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":
2323
self.add_issue(
2424
severity=gixy.severity.MEDIUM,
2525
directive=[directive],

0 commit comments

Comments
 (0)