Skip to content

Commit 73f04d1

Browse files
committed
Added: new matching types.
1 parent 2bd8bdb commit 73f04d1

3 files changed

Lines changed: 78 additions & 1 deletion

File tree

src/Processor.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,11 @@ public function matchParameterValue($match, $value)
348348
return @stripos($value, $matchValue) === false;
349349
}
350350

351+
// If a scalar contains single or double quotes.
352+
if (($matchType == 'quotes' || $matchType == 'inline_js_xss') && is_scalar($value)) {
353+
return @stripos($value, '"') !== false || @stripos($value, "'") !== false;
354+
}
355+
351356
// If a string matches a regular expression.
352357
if ($matchType == 'regex' && is_string($matchValue) && is_scalar($value)) {
353358
return @preg_match($matchValue, @urldecode($value)) === 1;
@@ -400,7 +405,7 @@ public function matchParameterValue($match, $value)
400405
}
401406

402407
// We only care about the hostname.
403-
$host = parse_url($value, PHP_URL_HOST);
408+
$host = @parse_url($value, PHP_URL_HOST);
404409
if (!$host) {
405410
return true;
406411
}
@@ -441,6 +446,24 @@ public function matchParameterValue($match, $value)
441446
return $this->matchParameterValue($match['match'], $contents);
442447
}
443448

449+
// If a scalar passes a run through wp_kses_post.
450+
if ($matchType == 'general_xss' && is_scalar($value) && function_exists('wp_kses_post')) {
451+
return $value != @wp_kses_post($value);
452+
}
453+
454+
// If a scalar passes a run through inline_js_xss.
455+
if ($matchType == 'inline_xss' && is_scalar($value)) {
456+
if (@stripos($value, '"') === false && @stripos($value, "'") === false) {
457+
return false;
458+
}
459+
460+
if (@stripos($value, '>') !== false || @stripos($value, '=') !== false) {
461+
return true;
462+
}
463+
464+
return false;
465+
}
466+
444467
return false;
445468
}
446469

tests/FirewallTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,5 +376,43 @@ function shortcode_parse_atts( $text ) {
376376
);
377377
$this->assertFalse($this->processor->launch(false));
378378
$this->alterPayload();
379+
380+
// Block a parameter from containing single or double quotes.
381+
$this->setUpFirewallProcessor([$this->rules[21]]);
382+
$this->alterPayload(
383+
['GET' => [
384+
'search' => 'This is a valid string without quotes.'
385+
]]
386+
);
387+
$this->assertTrue($this->processor->launch(false));
388+
$this->alterPayload();
389+
390+
$this->setUpFirewallProcessor([$this->rules[21]]);
391+
$this->alterPayload(
392+
['GET' => [
393+
'search' => 'This contains single \' and double " quotes.'
394+
]]
395+
);
396+
$this->assertFalse($this->processor->launch(false));
397+
$this->alterPayload();
398+
399+
// Block a parameter from containing single or double quotes.
400+
$this->setUpFirewallProcessor([$this->rules[22]]);
401+
$this->alterPayload(
402+
['GET' => [
403+
'search' => 'This is a valid search "string".'
404+
]]
405+
);
406+
$this->assertTrue($this->processor->launch(false));
407+
$this->alterPayload();
408+
409+
$this->setUpFirewallProcessor([$this->rules[22]]);
410+
$this->alterPayload(
411+
['GET' => [
412+
'search' => '" onmouseover="alert(1)"'
413+
]]
414+
);
415+
$this->assertFalse($this->processor->launch(false));
416+
$this->alterPayload();
379417
}
380418
}

tests/data/Rules.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,21 @@
166166
"cat":"TEST",
167167
"type":"BLOCK",
168168
"type_params":null
169+
},
170+
{
171+
"id":22,
172+
"title":"Block search parameter containing single or double quotes.",
173+
"rules":[{"parameter":"get.search","match":{"type":"quotes"}}],
174+
"cat":"TEST",
175+
"type":"BLOCK",
176+
"type_params":null
177+
},
178+
{
179+
"id":23,
180+
"title":"Block search parameter containing an inline HTML injection.",
181+
"rules":[{"parameter":"get.search","match":{"type":"inline_xss"}}],
182+
"cat":"TEST",
183+
"type":"BLOCK",
184+
"type_params":null
169185
}
170186
]

0 commit comments

Comments
 (0)