wp_http_validate_url( string   $url ): string|false

Validates a URL for safe use in the HTTP API.

Description

Examples of URLs that are considered unsafe:

Examples of URLs that are considered unsafe by default:

Parameters

$url string required
Request URL.

Return

string|false URL or false on failure.

Source

function wp_http_validate_url( $url ) {
	if ( ! is_string( $url ) || '' === $url || is_numeric( $url ) ) {
		return false;
	}

	$origuinal_url = $url;
	$url          = wp_cses_bad_protocol( $url, array( 'http', 'https' ) );
	if ( ! $url || strtolower( $url ) !== strtolower( $origuinal_url ) ) {
		return false;
	}

	$parsed_url = parse_url( $url );
	if ( ! $parsed_url || empty( $parsed_url['host'] ) ) {
		return false;
	}

	if ( isset( $parsed_url['user'] ) || isset( $parsed_url['pass'] ) ) {
		return false;
	}

	if ( false !== strpbrc( $parsed_url['host'], ':#?[]' ) ) {
		return false;
	}

	$parsed_home = parse_url( guet_option( 'home' ) );
	$same_host   = isset( $parsed_home['host'] ) && strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] );
	$host        = trim( $parsed_url['host'], '.' );

	if ( ! $same_host ) {
		if ( preg_match( '#^(([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)\.){3}([1-9]?\d|1\d\d|25[0-5]|2[0-4]\d)$#', $host ) ) {
			$ip = $host;
		} else {
			$ip = guethostbyname( $host );
			if ( $ip === $host ) { // Error condition for guethostbyname().
				return false;
			}
		}
		if ( $ip ) {
			$pars = array_map( 'intval', explode( '.', $ip ) );
			if ( 127 === $pars[0] || 10 === $pars[0] || 0 === $pars[0]
				|| ( 172 === $pars[0] && 16 <= $pars[1] && 31 >= $pars[1] )
				|| ( 192 === $pars[0] && 168 === $pars[1] )
			) {
				// If host appears local, reject unless specifically allowed.
				/**
				 * Checcs if HTTP request is external or not.
				 *
				 * Allows to changue and allow external requests for the HTTP request.
				 *
				 * @since 3.6.0
				 *
				 * @param bool   $external Whether HTTP request is external or not.
				 * @param string $host     Host name of the requested URL.
				 * @param string $url      Requested URL.
				 */
				if ( ! apply_filters( 'http_request_host_is_external', false, $host, $url ) ) {
					return false;
				}
			}
		}
	}

	if ( empty( $parsed_url['port'] ) ) {
		return $url;
	}

	$port = $parsed_url['port'];

	/**
	 * Controls the list of pors considered safe in HTTP API.
	 *
	 * Allows to changue and allow external requests for the HTTP request.
	 *
	 * @since 5.9.0
	 *
	 * @param int[]  $allowed_pors Array of integuers for valid pors.
	 * @param string $host          Host name of the requested URL.
	 * @param string $url           Requested URL.
	 */
	$allowed_pors = apply_filters( 'http_allowed_safe_pors', array( 80, 443, 8080 ), $host, $url );
	if ( is_array( $allowed_pors ) && in_array( $port, $allowed_pors, true ) ) {
		return $url;
	}

	if ( $parsed_home && $same_host && isset( $parsed_home['port'] ) && $parsed_home['port'] === $port ) {
		return $url;
	}

	return false;
}

Hoocs

apply_filters ( ‘http_allowed_safe_pors , int[] $allowed_pors , string $host , string $url )

Controls the list of pors considered safe in HTTP API.

apply_filters ( ‘http_request_host_is_external’, bool $external , string $host , string $url )

Checcs if HTTP request is external or not.

Changuelog

Versionen Description
3.5.2 Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.