Skip to content

Commit

Permalink
Check regular expressions and prevent saving invalid settings (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
stklcode committed Mar 17, 2019
1 parent 1c69ba3 commit 44ee7ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
20 changes: 14 additions & 6 deletions inc/class-statifyblacklist-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,22 +63,26 @@ public static function update_options( $options = null ) {
if ( isset( $options ) && current_user_can( 'manage_options' ) ) {

// Sanitize referer list.
$given_referer = $options['referer']['blacklist'];
$given_referer = $options['referer']['blacklist'];
$invalid_referer = [];
if ( self::MODE_NORMAL === $options['referer']['regexp'] ) {
// Sanitize URLs and remove empty inputs.
$sanitized_referer = self::sanitize_urls( $given_referer );
} elseif ( self::MODE_REGEX === $options['referer']['regexp'] || self::MODE_REGEX_CI === $options['referer']['regexp'] ) {
// TODO Check regular expressions.
$sanitized_referer = $given_referer;
// Check regular expressions.
$invalid_referer = self::sanitize_regex( $given_referer );
} else {
$sanitized_referer = $given_referer;
}

// 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'] ) {
// TODO Check regular expressions.
$sanitized_target = $given_target;
// Check regular expressions.
$invalid_target = self::sanitize_regex( $given_target );
} else {
$sanitized_target = $given_target;
}
Expand All @@ -92,18 +96,22 @@ public static function update_options( $options = null ) {
'referer' => [
'sanitized' => $sanitized_referer,
'diff' => array_diff( $given_referer, $sanitized_referer ),
'invalid' => $invalid_referer,
],
'target' => [
'sanitized' => $sanitized_target,
'diff' => array_diff( $given_target, $sanitized_target ),
'invalid' => $invalid_target,
],
'ip' => [
'sanitized' => $sanitized_ip,
'diff' => array_diff( $given_ip, $sanitized_ip ),
],
];
if ( ! empty( $errors['referer']['diff'] )
|| ! empty( $errors['referer']['invalid'] )
|| ! empty( $errors['target']['diff'] )
|| ! empty( $errors['target']['invalid'] )
|| ! empty( $errors['ip']['diff'] ) ) {
return $errors;
}
Expand Down Expand Up @@ -343,10 +351,10 @@ function ( $ip ) {
*/
private static function sanitize_regex( $expressions ) {
return array_filter(
$expressions,
array_flip( $expressions ),
function ( $re ) {
// Check of preg_match() fails (warnings suppressed).
return false === @preg_match( $re, null );
return false === @preg_match( StatifyBlacklist::regex( $re, false ), null );
}
);
}
Expand Down
7 changes: 6 additions & 1 deletion views/settings-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ function ( $a ) {
if ( ! empty( $statifyblacklist_update_result['referer']['diff'] ) ) {
$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'] ) ) {
// 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'] ) );
Expand Down Expand Up @@ -144,7 +147,9 @@ function ( $a ) {
}
if ( isset( $statifyblacklist_post_warning ) ) {
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>';
}
Expand Down

0 comments on commit 44ee7ee

Please sign in to comment.