5
5
6
6
import ModSecurity
7
7
8
- logger = logging .getLogger (__name__ )
9
-
10
8
SETTINGS_NAMES = {
11
9
'rule_files' : 'MODSECURITY_RULE_FILES' ,
12
10
'rule_lines' : 'MODSECURITY_RULES' ,
@@ -22,6 +20,8 @@ def __init__(self, get_response):
22
20
23
21
:param callable get_response
24
22
'''
23
+ self .logger = logging .getLogger (__name__ )
24
+
25
25
self .get_response = get_response
26
26
27
27
self .modsecurity = ModSecurity .ModSecurity ()
@@ -44,7 +44,7 @@ def __init__(self, get_response):
44
44
self .load_rules (self .rule_lines )
45
45
46
46
def modsecurity_log_callback (self , data , msg ):
47
- logger .info (msg )
47
+ self . logger .info (msg )
48
48
49
49
@property
50
50
def rules_count (self ):
@@ -54,34 +54,43 @@ def load_rule_files(self, rule_files):
54
54
'''
55
55
Process a list of files (can be a list of globs) and loads into modsecurity
56
56
:param list(str) rule_files
57
+ :rtype: int
58
+ :return the total rules that were loaded
57
59
'''
60
+ before_count = self .rules_count
58
61
import glob
59
62
for pattern in rule_files :
60
63
for rule_file in glob .glob (pattern , recursive = True ):
61
64
rules_count = self .rules .loadFromUri (rule_file )
62
65
if rules_count < 0 :
63
66
msg = '[ModSecurity] Error trying to load rule file %s. %s' % (
64
67
rule_file , self .rules .getParserError ())
65
- logger .warning (msg )
68
+ self . logger .warning (msg )
66
69
else :
67
70
self ._rules_count += rules_count
68
71
72
+ return self .rules_count - before_count
73
+
69
74
def load_rules (self , rules ):
70
75
'''
71
76
Process rules
72
77
:param str: rules
78
+ :rtype: int
79
+ :return the total rules that were loaded
73
80
'''
74
81
if rules is None or not len (rules ) > 0 :
75
- return
82
+ return 0
76
83
77
84
rules_count = self .rules .load (rules )
78
85
if rules_count < 0 :
79
86
msg = '[ModSecurity] Error trying to load rules: %s' % self .rules .getParserError (
80
87
)
81
- logger .warning (msg )
88
+ self . logger .warning (msg )
82
89
else :
83
90
self ._rules_count += rules_count
84
91
92
+ return rules_count
93
+
85
94
def __call__ (self , request ):
86
95
transaction = ModSecurity .Transaction (self .modsecurity , self .rules )
87
96
response = self .process_request (request , transaction )
@@ -168,15 +177,17 @@ def process_intervention(self, transaction):
168
177
:rtype HttpResponse:
169
178
'''
170
179
intervention = ModSecurity .ModSecurityIntervention ()
180
+
181
+ if intervention is None :
182
+ return None
183
+
171
184
if transaction .intervention (intervention ):
172
- if intervention is None :
173
- return None
185
+ if intervention . log is not None :
186
+ self . logger . info ( intervention . log )
174
187
175
188
if not intervention .disruptive :
176
189
return None
177
190
178
- # TODO process intervention logs
179
-
180
191
if intervention .url is not None :
181
192
response = HttpResponseRedirect (intervention .url )
182
193
else :
0 commit comments