From ddafdc42fe02cb91f895a10d744e328e56056bb7 Mon Sep 17 00:00:00 2001 From: Erick Hitter <git-contrib@ethitter.com> Date: Sun, 18 Feb 2018 15:50:02 -0800 Subject: [PATCH] Standardize URL validation --- inc/class-urls.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/inc/class-urls.php b/inc/class-urls.php index 5c787da..00f51cd 100644 --- a/inc/class-urls.php +++ b/inc/class-urls.php @@ -25,7 +25,7 @@ class URL { $can_rewrite = true; // Validate host. - if ( empty( $host ) || ( ! filter_var( $host, FILTER_VALIDATE_URL ) && ! filter_var( $host, FILTER_VALIDATE_IP ) ) ) { + if ( $this->is_valid_url( $host ) ) { $can_rewrite = false; } @@ -45,12 +45,10 @@ class URL { * @return string|bool */ public function encode( string $url ) : string { - if ( ! $this->can_rewrite() ) { + if ( ! $this->can_rewrite() || ! $this->is_valid_url( $url ) ) { return false; } - // TODO: validate $url. - $key = hash_hmac( 'sha1', $url, Options::instance()->get( 'key' ) ); $url_encoded = bin2hex( $url ); @@ -69,4 +67,14 @@ class URL { public function decode( string $url ) : string { return false; } + + /** + * Can we encode this URL? + * + * @param string $url URL to validate. + * @return bool + */ + private function is_valid_url( string $url ) : bool { + return empty( $url ) || ( ! filter_var( $url, FILTER_VALIDATE_URL ) && ! filter_var( $url, FILTER_VALIDATE_IP ) ); + } } -- GitLab