@@ -29,6 +29,13 @@ class Processor
2929 */
3030 private $ whitelistRules = array ();
3131
32+ /**
33+ * The legacy whitelist rules to process.
34+ *
35+ * @var array
36+ */
37+ private $ whitelistRulesLegacy = array ();
38+
3239 /**
3340 * Firewall datasets which can be interacted with by the firewall rules.
3441 *
@@ -82,24 +89,29 @@ class Processor
8289 *
8390 * @param ExtensionInterface $extension
8491 * @param array $firewallRules
85- * @param array $firewallRulesLegacy
8692 * @param array $whitelistRules
8793 * @param array $options
94+ * @param array $datasets
95+ * @param array $firewallRulesLegacy
96+ * @param array $whitelistRulesLegacy
97+ * @return void
8898 */
8999 public function __construct (
90100 ExtensionInterface $ extension ,
91101 $ firewallRules = array (),
92- $ firewallRulesLegacy = array (),
93102 $ whitelistRules = array (),
94103 $ options = array (),
95- $ datasets = array ()
104+ $ datasets = array (),
105+ $ firewallRulesLegacy = array (),
106+ $ whitelistRulesLegacy = array ()
96107 ) {
97108 $ this ->extension = $ extension ;
98109 $ this ->firewallRules = $ firewallRules ;
99- $ this ->firewallRulesLegacy = $ firewallRulesLegacy ;
100110 $ this ->whitelistRules = $ whitelistRules ;
101111 $ this ->options = array_merge ($ this ->options , $ options );
102112 $ this ->dataset = $ datasets ;
113+ $ this ->firewallRulesLegacy = $ firewallRulesLegacy ;
114+ $ this ->whitelistRulesLegacy = $ whitelistRulesLegacy ;
103115
104116 $ this ->secret = isset ($ options ['secret ' ]) ? $ options ['secret ' ] : 'secret ' ;
105117 $ this ->request = new Request ($ this ->options );
@@ -109,6 +121,7 @@ public function __construct(
109121 /**
110122 * Magic getter for the options.
111123 *
124+ * @param string $name
112125 * @return mixed
113126 */
114127 public function __get ($ name )
@@ -132,22 +145,14 @@ public function launch($mustExit = true)
132145 $ this ->extension ->forceExit (22 );
133146 }
134147
135- // Check for whitelist.
148+ // Check for whitelist based on the legacy whitelist rules .
136149 $ request = $ this ->request ->capture ();
137- if ($ this ->extension ->isWhitelisted ($ this ->whitelistRules , $ request )) {
150+ if ($ this ->extension ->isWhitelisted ($ this ->whitelistRulesLegacy , $ request )) {
138151 return true ;
139152 }
140153
141- // Grab the IP address of the request.
142- $ ip = $ this ->extension ->getIpAddress ();
143-
144- // Run the legacy firewall rules processor for backwards compatibility.
145- if (count ($ this ->firewallRulesLegacy ) > 0 ) {
146- $ this ->launchLegacy (true , $ request , $ ip );
147- }
148-
149- // Determine if we have any firewall rules loaded.
150- if (count ($ this ->firewallRules ) == 0 ) {
154+ // Determine if we have any firewall and/or whitelist rules loaded.
155+ if (count ($ this ->firewallRules ) == 0 && count ($ this ->whitelistRules ) == 0 ) {
151156 return true ;
152157 }
153158
@@ -159,9 +164,15 @@ public function launch($mustExit = true)
159164 \Laravel \SerializableClosure \SerializableClosure::setSecretKey ($ this ->secret );
160165 }
161166
167+ // Grab the IP address of the request.
168+ $ ip = $ this ->extension ->getIpAddress ();
169+
162170 // Store the datasets in a shorter variable for easy access.
163171 $ dataset = $ this ->dataset ;
164- foreach ($ this ->firewallRules as $ rule ) {
172+
173+ // Merge the rules together. First iterate through the whitelist rules.
174+ $ rules = array_merge ($ this ->whitelistRules , $ this ->firewallRules );
175+ foreach ($ rules as $ rule ) {
165176 // Get the firewall rule and extract it.
166177 $ vpatch = base64_decode ($ rule ->rule );
167178 if (!$ vpatch ) {
@@ -202,9 +213,16 @@ public function launch($mustExit = true)
202213 } elseif ($ rule ->type == 'REDIRECT ' ) {
203214 $ this ->extension ->logRequest ($ rule ->id , $ request , 'REDIRECT ' );
204215 $ this ->response ->redirect ($ rule ->type_params , $ mustExit );
216+ } elseif ($ rule ->type == 'WHITELIST ' ) {
217+ return $ mustExit ;
205218 }
206219 }
207220
221+ // Run the legacy firewall rules processor for backwards compatibility.
222+ if (count ($ this ->firewallRulesLegacy ) > 0 ) {
223+ $ this ->launchLegacy (true , $ request , $ ip );
224+ }
225+
208226 return true ;
209227 }
210228
@@ -223,6 +241,26 @@ public function launchLegacy($mustExit = true, $request = array(), $ip = '')
223241 $ client_ip = $ ip == '' ? $ this ->extension ->getIpAddress () : $ ip ;
224242 $ requests = count ($ request ) == 0 ? $ this ->request ->capture () : $ request ;
225243
244+ // The request parameter values exploded into pairs.
245+ $ requestParams = array (
246+ 'method ' => 'method ' ,
247+ 'rulesFile ' => 'rules->file ' ,
248+ 'rulesRawPost ' => 'rules->raw->post ' ,
249+ 'rulesUri ' => 'rules->uri ' ,
250+ 'rulesHeadersAll ' => 'rules->headers->all ' ,
251+ 'rulesHeadersKeys ' => 'rules->headers->keys ' ,
252+ 'rulesHeadersValues ' => 'rules->headers->values ' ,
253+ 'rulesHeadersCombinations ' => 'rules->headers->combinations ' ,
254+ 'rulesBodyAll ' => 'rules->body->all ' ,
255+ 'rulesBodyKeys ' => 'rules->body->keys ' ,
256+ 'rulesBodyValues ' => 'rules->body->values ' ,
257+ 'rulesBodyCombinations ' => 'rules->body->combinations ' ,
258+ 'rulesParamsAll ' => 'rules->params->all ' ,
259+ 'rulesParamsKeys ' => 'rules->params->keys ' ,
260+ 'rulesParamsValues ' => 'rules->params->values ' ,
261+ 'rulesParamsCombinations ' => 'rules->params->combinations '
262+ );
263+
226264 // Iterate through all root objects.
227265 foreach ($ this ->firewallRulesLegacy as $ firewall_rule ) {
228266 $ rule_terms = json_decode ($ firewall_rule ['rule ' ]);
@@ -255,8 +293,10 @@ public function launchLegacy($mustExit = true, $request = array(), $ip = '')
255293
256294 // Determine if the requesting method matches.
257295 if ($ rule_terms ->method == $ requests ['method ' ] || $ rule_terms ->method == 'ALL ' || $ rule_terms ->method == 'GET ' || ($ rule_terms ->method == 'FILES ' && $ this ->extension ->isFileUploadRequest ())) {
258- $ test = strtolower (preg_replace ('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]/ ' , '->$0 ' , $ key ));
259- $ exp = explode ('-> ' , $ test );
296+ if (!isset ($ requestParams [$ key ])) {
297+ continue ;
298+ }
299+ $ exp = explode ('-> ' , $ requestParams [$ key ]);
260300
261301 // Determine if a rule exists for this request.
262302 $ rule = $ rule_terms ;
@@ -296,7 +336,7 @@ public function launchLegacy($mustExit = true, $request = array(), $ip = '')
296336 /**
297337 * Determine if the request matches the given firewall or whitelist rule.
298338 *
299- * @param string $rule
339+ * @param string $rule
300340 * @param string|array $request
301341 * @return bool
302342 */
0 commit comments