Merge branch 'hf12-regexSkip' into develop

This commit is contained in:
Stefan Kalscheuer 2018-05-19 14:31:47 +02:00
commit 74be5a2334
3 changed files with 91 additions and 4 deletions

View File

@ -85,6 +85,9 @@ Because of this, an IP blacklist can only be applied while processing the reques
### 1.5.0 / unreleased ### ### 1.5.0 / unreleased ###
* Minimum required WordPress version is 4.7 * Minimum required WordPress version is 4.7
### 1.4.4 / 19.05.2018 ###
* Fix live filter chain when regular expressions are active (#12)
### 1.4.3 / 09.01.2018 ### ### 1.4.3 / 09.01.2018 ###
* Fix issues with multisite installation (#11) * Fix issues with multisite installation (#11)

View File

@ -187,8 +187,10 @@ class StatifyBlacklist {
$regexp .= 'i'; $regexp .= 'i';
} }
// Check blacklist (return NULL to continue filtering). // Check blacklist (no return to continue filtering #12).
return ( 1 === preg_match( $regexp, $referer ) ) ? true : null; if ( 1 === preg_match( $regexp, $referer ) ) {
return true;
}
} else { } else {
// Extract relevant domain parts. // Extract relevant domain parts.
$referer = wp_parse_url( wp_get_raw_referer() ); $referer = wp_parse_url( wp_get_raw_referer() );
@ -218,8 +220,10 @@ class StatifyBlacklist {
$regexp .= 'i'; $regexp .= 'i';
} }
// Check blacklist (return NULL to continue filtering). // Check blacklist (no return to continue filtering #12).
return ( 1 === preg_match( $regexp, $target ) ) ? true : null; if ( 1 === preg_match( $regexp, $target ) ) {
return true;
}
} else { } else {
// Extract target page. // Extract target page.
// @codingStandardsIgnoreStart The globals are checked. // @codingStandardsIgnoreStart The globals are checked.

View File

@ -486,6 +486,86 @@ class StatifyBlacklist_Test extends PHPUnit\Framework\TestCase {
} }
// TODO: Test target regex filter. // TODO: Test target regex filter.
/**
* Test combined filters.
*
* @since 1.4.4
*
* @return void
*/
public function test_combined_filters() {
// Prepare Options: simple referer + simple target + ip.
StatifyBlacklist::$_options = array(
'referer' => array(
'active' => 1,
'cron' => 0,
'regexp' => 0,
'blacklist' => array(
'example.com' => 0,
),
),
'target' => array(
'active' => 1,
'cron' => 0,
'regexp' => 0,
'blacklist' => array(
'/excluded/page/' => 0
),
),
'ip' => array(
'active' => 1,
'blacklist' => array(
'192.0.2.123'
),
),
'version' => StatifyBlacklist::VERSION_MAIN,
);
// No multisite.
StatifyBlacklist::$multisite = false;
// No match.
$_SERVER['HTTP_REFERER'] = 'https://example.net';
$_SERVER['REQUEST_URI'] = '/normal/page/';
$_SERVER['REMOTE_ADDR'] = '192.0.2.234';
unset( $_SERVER['HTTP_X_FORWARDED_FOR'] );
unset( $_SERVER['HTTP_X_REAL_IP'] );
// Matching Referer.
$_SERVER['HTTP_REFERER'] = 'https://example.com';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
// Matching target.
$_SERVER['HTTP_REFERER'] = 'https://example.net';
$_SERVER['REQUEST_URI'] = '/excluded/page/';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
// Matching IP.
$_SERVER['REQUEST_URI'] = '/normal/page/';
$_SERVER['REMOTE_ADDR'] = '192.0.2.123';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['REMOTE_ADDR'] = '192.0.2.234';
// Same for RegExp filters.
StatifyBlacklist::$_options['referer']['regexp'] = 1;
StatifyBlacklist::$_options['referer']['blacklist'] = array( 'example\.com' => 0 );
StatifyBlacklist::$_options['target']['regexp'] = 1;
StatifyBlacklist::$_options['target']['blacklist'] = array( '\/excluded\/.*' => 0 );
$this->assertNull( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['HTTP_REFERER'] = 'https://example.com';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['HTTP_REFERER'] = 'https://example.net';
$_SERVER['REQUEST_URI'] = '/excluded/page/';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['REQUEST_URI'] = '/normal/page/';
$_SERVER['REMOTE_ADDR'] = '192.0.2.123';
$this->assertTrue( StatifyBlacklist::apply_blacklist_filter() );
$_SERVER['REMOTE_ADDR'] = '192.0.2.234';
}
} }