Skip to content
Snippets Groups Projects
Commit ddafdc42 authored by Erick Hitter's avatar Erick Hitter
Browse files

Standardize URL validation

parent 1b4a8ea2
Branches
No related tags found
3 merge requests!6add linting,!5WIP: Add JUnit reporting,!4WIP: Initial release
...@@ -25,7 +25,7 @@ class URL { ...@@ -25,7 +25,7 @@ class URL {
$can_rewrite = true; $can_rewrite = true;
// Validate host. // 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; $can_rewrite = false;
} }
...@@ -45,12 +45,10 @@ class URL { ...@@ -45,12 +45,10 @@ class URL {
* @return string|bool * @return string|bool
*/ */
public function encode( string $url ) : string { public function encode( string $url ) : string {
if ( ! $this->can_rewrite() ) { if ( ! $this->can_rewrite() || ! $this->is_valid_url( $url ) ) {
return false; return false;
} }
// TODO: validate $url.
$key = hash_hmac( 'sha1', $url, Options::instance()->get( 'key' ) ); $key = hash_hmac( 'sha1', $url, Options::instance()->get( 'key' ) );
$url_encoded = bin2hex( $url ); $url_encoded = bin2hex( $url );
...@@ -69,4 +67,14 @@ class URL { ...@@ -69,4 +67,14 @@ class URL {
public function decode( string $url ) : string { public function decode( string $url ) : string {
return false; 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 ) );
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment