Skip to content

Commit 2b457fc

Browse files
committed
Added: "all" parameter.
Changed: match type name.
1 parent 8945778 commit 2b457fc

2 files changed

Lines changed: 58 additions & 5 deletions

File tree

src/Processor.php

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ public function matchParameterValue($match, $value)
289289
return $matchValue == $value;
290290
}
291291

292-
if ($matchType == 'bigger_than' && is_scalar($value) && is_scalar($matchValue)) {
292+
if ($matchType == 'more_than' && is_scalar($value) && is_scalar($matchValue)) {
293293
return $value > $matchValue;
294294
}
295295

@@ -317,7 +317,7 @@ public function matchParameterValue($match, $value)
317317
return @stripos($value, $matchValue) !== false;
318318
}
319319

320-
if ($matchType == 'regex' && is_scalar($value)) {
320+
if ($matchType == 'regex' && is_string($matchValue) && is_scalar($value)) {
321321
return @preg_match($matchValue, @urldecode($value)) === 1;
322322
}
323323

@@ -368,6 +368,14 @@ public function getParameterValue($parameter, $data = [])
368368
}
369369

370370
switch ($type) {
371+
case 'all':
372+
$data = [
373+
'post' => $_POST,
374+
'get' => $_GET,
375+
'url' => isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '',
376+
'raw' => ['raw' => $this->getParameterValue('raw')]
377+
];
378+
break;
371379
case 'post':
372380
$data = $_POST;
373381
break;
@@ -479,6 +487,14 @@ public function applyMutation($mutations, $value)
479487
'intval' => [
480488
'args' => [],
481489
'type' => 'is_scalar'
490+
],
491+
'urldecode' => [
492+
'args' => [],
493+
'type' => 'is_string'
494+
],
495+
'getArrayValues' => [
496+
'args' => [],
497+
'type' => 'is_array'
482498
]
483499
];
484500

@@ -498,8 +514,12 @@ public function applyMutation($mutations, $value)
498514
}
499515

500516
// Call the function with given arguments.
501-
$value = call_user_func_array($mutation, array_merge([$value], $allowed[$mutation]['args']));
502-
517+
if ($mutation == 'getArrayValues') {
518+
$value = $this->getArrayValues($value);
519+
} else {
520+
$value = call_user_func_array($mutation, array_merge([$value], $allowed[$mutation]['args']));
521+
}
522+
503523
// No need to continue in these scenarios.
504524
if (is_null($value) || $value === false || $value === 0) {
505525
return $value;
@@ -512,6 +532,31 @@ public function applyMutation($mutations, $value)
512532
return $value;
513533
}
514534

535+
/**
536+
* Given an array, multi-dimensional or not, extract all of its values.
537+
*
538+
* @param array $data
539+
* @return string
540+
*/
541+
public function getArrayValues($data, $glue = '&')
542+
{
543+
$ret = '';
544+
545+
foreach ($data as $key => $item) {
546+
if (empty($item)) {
547+
continue;
548+
}
549+
550+
if (is_array($item)) {
551+
$ret .= $this->getArrayValues($item, $glue) . $glue;
552+
} else {
553+
$ret .= $key . '=' . $item . $glue;
554+
}
555+
}
556+
557+
return substr($ret, 0, 0 - strlen($glue));
558+
}
559+
515560
/**
516561
* The legacy firewall processor will only iterate over the general firewall rules.
517562
* Will return true if $mustExit is false and all of the rules were processed without a positive detection.

tests/data/Rules.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
{
5151
"id":7,
5252
"title":"Block access to endpoint that should only accept an integer of less than 101.",
53-
"rules":[{"parameter":"get.pid","match":{"type":"ctype_digit","value":false}},{"parameter":"get.pid","match":{"type":"bigger_than","value":100}}],
53+
"rules":[{"parameter":"get.pid","match":{"type":"ctype_digit","value":false}},{"parameter":"get.pid","match":{"type":"more_than","value":100}}],
5454
"cat":"TEST",
5555
"type":"BLOCK",
5656
"type_params":null
@@ -102,5 +102,13 @@
102102
"cat":"TEST",
103103
"type":"BLOCK",
104104
"type_params":null
105+
},
106+
{
107+
"id":14,
108+
"title":"Determine if a certain value is present anywhere in the request.",
109+
"rules":[{"parameter":"all","mutations":["getArrayValues"],"match":{"type":"regex","value":"\/(\\\/something\\\/)\/msi"}}],
110+
"cat":"TEST",
111+
"type":"BLOCK",
112+
"type_params":null
105113
}
106114
]

0 commit comments

Comments
 (0)