Skip to content

Commit 2b9f5e0

Browse files
committed
Added: support for holding an array of parameters.
1 parent 7a96171 commit 2b9f5e0

4 files changed

Lines changed: 59 additions & 32 deletions

File tree

src/Processor.php

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -204,52 +204,62 @@ public function executeFirewall($rules)
204204
continue;
205205
}
206206

207-
// Extract the value of the paramater that we want.
208-
$values = $this->request->getParameterValues($rule['parameter']);
209-
if (is_null($values) && $rule['parameter'] !== false && $rule['parameter'] != 'rules') {
210-
continue;
211-
}
212-
213-
// For special parameter values we just set the array to a single null value.
214-
if ($rule['parameter'] === false || $rule['parameter'] == 'rules') {
215-
$values = [null];
207+
// Cast to an array so we can iterate through all parameters.
208+
if (!is_array($rule['parameter'])) {
209+
$parameters = [$rule['parameter']];
210+
} else {
211+
$parameters = $rule['parameter'];
216212
}
217213

218-
// For all field matches, we want to execute the rule against it.
219-
foreach ($values as $value) {
220-
// Apply mutations, if any.
221-
if (isset($rule['mutations']) && is_array($rule['mutations'])) {
222-
$value = $this->request->applyMutation($rule['mutations'], $value);
223-
if (is_null($value)) {
224-
continue;
225-
}
214+
// Iterate through all parameters.
215+
foreach ($parameters as $parameter) {
216+
// Extract the value of the paramater that we want.
217+
$values = $this->request->getParameterValues($parameter);
218+
if (is_null($values) && $parameter !== false && $parameter != 'rules') {
219+
continue;
226220
}
227221

228-
// Perform the matching.
229-
if (isset($rule['match']) && is_array($rule['match']) || isset($rule['rules'])) {
222+
// For special parameter values we just set the array to a single null value.
223+
if ($parameter === false || $parameter == 'rules') {
224+
$values = [null];
225+
}
230226

231-
// Do we have to process child-rules?
232-
if (isset($rule['rules'])) {
233-
$match = $this->executeFirewall($rule['rules']);
234-
} else {
235-
$match = $this->matchParameterValue($rule['match'], $value);
227+
// For all field matches, we want to execute the rule against it.
228+
foreach ($values as $value) {
229+
// Apply mutations, if any.
230+
if (isset($rule['mutations']) && is_array($rule['mutations'])) {
231+
$value = $this->request->applyMutation($rule['mutations'], $value);
232+
if (is_null($value)) {
233+
continue;
234+
}
236235
}
237236

238-
// Is the rule a match?
239-
if ($match) {
240-
// In case there are multiple rules, they may require chained AND conditions.
241-
if ($inclusiveCount <= 1 || !isset($rule['inclusive']) || $rule['inclusive'] !== true) {
242-
return true;
237+
// Perform the matching.
238+
if (isset($rule['match']) && is_array($rule['match']) || isset($rule['rules'])) {
239+
240+
// Do we have to process child-rules?
241+
if (isset($rule['rules'])) {
242+
$match = $this->executeFirewall($rule['rules']);
243243
} else {
244-
$inclusiveHits++;
244+
$match = $this->matchParameterValue($rule['match'], $value);
245+
}
246+
247+
// Is the rule a match?
248+
if ($match) {
249+
// In case there are multiple rules, they may require chained AND conditions.
250+
if ($inclusiveCount <= 1 || !isset($rule['inclusive']) || $rule['inclusive'] !== true) {
251+
return true;
252+
} else {
253+
$inclusiveHits++;
254+
}
245255
}
246256
}
247257
}
248258
}
249259
}
250260

251261
// In case we hit all of the AND conditions.
252-
if ($inclusiveCount > 1 && $inclusiveHits === $inclusiveCount) {
262+
if ($inclusiveCount > 1 && $inclusiveHits >= $inclusiveCount) {
253263
return true;
254264
}
255265

src/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function applyMutation($mutations, $value)
239239
return $value;
240240
}
241241

242-
/**
242+
/**
243243
* Given an array, get all parameters which match a certain wildcard.
244244
*
245245
* @param array $data

tests/FirewallTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,5 +414,14 @@ function shortcode_parse_atts( $text ) {
414414
);
415415
$this->assertFalse($this->processor->launch(false));
416416
$this->alterPayload();
417+
418+
// Block request with test parameter present in the URL.
419+
$this->setUpFirewallProcessor([$this->rules[23]]);
420+
$this->alterPayload(
421+
['GET' => [
422+
'test2' => 'yes'
423+
]]
424+
);
425+
$this->assertFalse($this->processor->launch(false));
417426
}
418427
}

tests/data/Rules.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,5 +182,13 @@
182182
"cat":"TEST",
183183
"type":"BLOCK",
184184
"type_params":null
185+
},
186+
{
187+
"id":24,
188+
"title":"Block test parameter being present in the URL with array of parameters.",
189+
"rules":[{"parameter":["get.test","get.test2"],"match":{"type":"isset"}}],
190+
"cat":"TEST",
191+
"type":"BLOCK",
192+
"type_params":null
185193
}
186194
]

0 commit comments

Comments
 (0)