Check regular expressions and prevent saving invalid settings (#13)
This commit is contained in:
@ -64,21 +64,25 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
|||||||
|
|
||||||
// Sanitize referer list.
|
// Sanitize referer list.
|
||||||
$given_referer = $options['referer']['blacklist'];
|
$given_referer = $options['referer']['blacklist'];
|
||||||
|
$invalid_referer = [];
|
||||||
if ( self::MODE_NORMAL === $options['referer']['regexp'] ) {
|
if ( self::MODE_NORMAL === $options['referer']['regexp'] ) {
|
||||||
// Sanitize URLs and remove empty inputs.
|
// Sanitize URLs and remove empty inputs.
|
||||||
$sanitized_referer = self::sanitize_urls( $given_referer );
|
$sanitized_referer = self::sanitize_urls( $given_referer );
|
||||||
} elseif ( self::MODE_REGEX === $options['referer']['regexp'] || self::MODE_REGEX_CI === $options['referer']['regexp'] ) {
|
} elseif ( self::MODE_REGEX === $options['referer']['regexp'] || self::MODE_REGEX_CI === $options['referer']['regexp'] ) {
|
||||||
// TODO Check regular expressions.
|
|
||||||
$sanitized_referer = $given_referer;
|
$sanitized_referer = $given_referer;
|
||||||
|
// Check regular expressions.
|
||||||
|
$invalid_referer = self::sanitize_regex( $given_referer );
|
||||||
} else {
|
} else {
|
||||||
$sanitized_referer = $given_referer;
|
$sanitized_referer = $given_referer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sanitize target list.
|
// Sanitize target list.
|
||||||
$given_target = $options['target']['blacklist'];
|
$given_target = $options['target']['blacklist'];
|
||||||
|
$invalid_target = [];
|
||||||
if ( self::MODE_REGEX === $options['target']['regexp'] || self::MODE_REGEX_CI === $options['target']['regexp'] ) {
|
if ( self::MODE_REGEX === $options['target']['regexp'] || self::MODE_REGEX_CI === $options['target']['regexp'] ) {
|
||||||
// TODO Check regular expressions.
|
|
||||||
$sanitized_target = $given_target;
|
$sanitized_target = $given_target;
|
||||||
|
// Check regular expressions.
|
||||||
|
$invalid_target = self::sanitize_regex( $given_target );
|
||||||
} else {
|
} else {
|
||||||
$sanitized_target = $given_target;
|
$sanitized_target = $given_target;
|
||||||
}
|
}
|
||||||
@ -92,10 +96,12 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
|||||||
'referer' => [
|
'referer' => [
|
||||||
'sanitized' => $sanitized_referer,
|
'sanitized' => $sanitized_referer,
|
||||||
'diff' => array_diff( $given_referer, $sanitized_referer ),
|
'diff' => array_diff( $given_referer, $sanitized_referer ),
|
||||||
|
'invalid' => $invalid_referer,
|
||||||
],
|
],
|
||||||
'target' => [
|
'target' => [
|
||||||
'sanitized' => $sanitized_target,
|
'sanitized' => $sanitized_target,
|
||||||
'diff' => array_diff( $given_target, $sanitized_target ),
|
'diff' => array_diff( $given_target, $sanitized_target ),
|
||||||
|
'invalid' => $invalid_target,
|
||||||
],
|
],
|
||||||
'ip' => [
|
'ip' => [
|
||||||
'sanitized' => $sanitized_ip,
|
'sanitized' => $sanitized_ip,
|
||||||
@ -103,7 +109,9 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
if ( ! empty( $errors['referer']['diff'] )
|
if ( ! empty( $errors['referer']['diff'] )
|
||||||
|
|| ! empty( $errors['referer']['invalid'] )
|
||||||
|| ! empty( $errors['target']['diff'] )
|
|| ! empty( $errors['target']['diff'] )
|
||||||
|
|| ! empty( $errors['target']['invalid'] )
|
||||||
|| ! empty( $errors['ip']['diff'] ) ) {
|
|| ! empty( $errors['ip']['diff'] ) ) {
|
||||||
return $errors;
|
return $errors;
|
||||||
}
|
}
|
||||||
@ -343,10 +351,10 @@ class StatifyBlacklist_Admin extends StatifyBlacklist {
|
|||||||
*/
|
*/
|
||||||
private static function sanitize_regex( $expressions ) {
|
private static function sanitize_regex( $expressions ) {
|
||||||
return array_filter(
|
return array_filter(
|
||||||
$expressions,
|
array_flip( $expressions ),
|
||||||
function ( $re ) {
|
function ( $re ) {
|
||||||
// Check of preg_match() fails (warnings suppressed).
|
// Check of preg_match() fails (warnings suppressed).
|
||||||
return false === @preg_match( $re, null );
|
return false === @preg_match( StatifyBlacklist::regex( $re, false ), null );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -116,6 +116,9 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
|
|||||||
if ( ! empty( $statifyblacklist_update_result['referer']['diff'] ) ) {
|
if ( ! empty( $statifyblacklist_update_result['referer']['diff'] ) ) {
|
||||||
$statifyblacklist_post_warning[] = __( 'Some URLs are invalid and have been sanitized.', 'statify-blacklist' );
|
$statifyblacklist_post_warning[] = __( 'Some URLs are invalid and have been sanitized.', 'statify-blacklist' );
|
||||||
}
|
}
|
||||||
|
if ( ! empty( $statifyblacklist_update_result['referer']['invalid'] ) ) {
|
||||||
|
$statifyblacklist_post_warning[] = __( 'Some regular expressions are invalid:', 'statify-blacklist' ) . '<br>' . implode( '<br>', $statifyblacklist_update_result['referer']['invalid'] );
|
||||||
|
}
|
||||||
if ( ! empty( $statifyblacklist_update_result['ip']['diff'] ) ) {
|
if ( ! empty( $statifyblacklist_update_result['ip']['diff'] ) ) {
|
||||||
// translators: List of invalid IP addresses (comma separated).
|
// translators: List of invalid IP addresses (comma separated).
|
||||||
$statifyblacklist_post_warning[] = sprintf( __( 'Some IPs are invalid: %s', 'statify-blacklist' ), implode( ', ', $statifyblacklist_update_result['ip']['diff'] ) );
|
$statifyblacklist_post_warning[] = sprintf( __( 'Some IPs are invalid: %s', 'statify-blacklist' ), implode( ', ', $statifyblacklist_update_result['ip']['diff'] ) );
|
||||||
@ -144,7 +147,9 @@ if ( ! empty( $_POST['statifyblacklist'] ) ) {
|
|||||||
}
|
}
|
||||||
if ( isset( $statifyblacklist_post_warning ) ) {
|
if ( isset( $statifyblacklist_post_warning ) ) {
|
||||||
foreach ( $statifyblacklist_post_warning as $w ) {
|
foreach ( $statifyblacklist_post_warning as $w ) {
|
||||||
print '<div class="notice notice-warning"><p>' . esc_html( $w ) . '</p></div>';
|
print '<div class="notice notice-warning"><p>' .
|
||||||
|
wp_kses( $w, [ 'br' => [] ] ) .
|
||||||
|
'</p></div>';
|
||||||
}
|
}
|
||||||
print '<div class="notice notice-warning"><p>' . esc_html( 'Settings have not been saved yet.', 'statify-blacklist' ) . '</p></div>';
|
print '<div class="notice notice-warning"><p>' . esc_html( 'Settings have not been saved yet.', 'statify-blacklist' ) . '</p></div>';
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user