wppaste
WordPress

wp_http_validate_url( string $url ): string|false

Since
3.5.2
Source
wp-includes/http.php:559
Validates a URL as safe for use in the HTTP API.

Description

The only supported protocols are http and https.

Examples of URLs that are considered unsafe:

  • ftp://example.com/caniload.php - Invalid protocol - only http and https are allowed.
  • http:///example.com/caniload.php - Malformed URL.
  • http://user:[email protected]/caniload.php - Login information.
  • http://example.invalid/caniload.php - Invalid hostname, as the IP cannot be looked up in DNS.

Examples of URLs that are considered unsafe by default but can be allowed with filters:

  • http://192.168.0.1/caniload.php - IP address from LAN network.
    This can be changed with the 'http_request_host_is_external' filter.
  • http://198.143.164.252:81/caniload.php - By default, only ports 80, 443, and 8080 are allowed.
    This can be changed with the 'http_allowed_safe_ports' filter.

Compatibility

WordPress
since 3.5.2
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$urlstring
Request URL.

Return value

string|false
Returns false if the URL is not safe, or the original URL if it is safe.

Performance profile

How much work a call to wp_http_validate_url() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
4–170

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 224.

Plugin surface
2 hooks

Third-party callbacks on 'http_request_host_is_external', 'http_allowed_safe_ports' run inside this call, and their cost is not bounded by anything here.

Called by
7

7 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below wp_http_validate_url()
  • serializeserialisationmaybe_unserialize()one call below wp_http_validate_url()

Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 10 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_http_validate_url() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always4–6none
is_string($url) && $url !== "" && !$url15wp_kses_bad_protocol()
is_string($url) && $url !== "" && !$url28–32wp_kses_bad_protocol(), strtolower(), strtolower(), parse_url()
is_string($url) && $url !== "" && !$url && !isset($parsed_url) && !isset($parsed_home)56wp_kses_bad_protocol(), strtolower(), strtolower(), parse_url(), strpbrk(), get_option(), parse_url()
is_string($url) && $url !== "" && !$url && !isset($parsed_url) && !isset($parsed_home) && !$host && $ip !== false65wp_kses_bad_protocol(), strtolower(), strtolower(), parse_url(), strpbrk(), get_option(), parse_url(), gethostbyname()
is_string($url) && $url !== "" && !$url && !empty($parsed_url) && !isset($parsed_url) && !isset($parsed_home)67–76wp_kses_bad_protocol(), strtolower(), strtolower(), parse_url(), strpbrk(), get_option(), parse_url(), apply_filters()
is_string($url) && $url !== "" && !$url && !empty($parsed_url) && !isset($parsed_url) && !isset($parsed_home) && !$host && $ip !== false76–85wp_kses_bad_protocol(), strtolower(), strtolower(), parse_url(), strpbrk(), get_option(), parse_url(), gethostbyname(), apply_filters()
is_string($url) && $url !== "" && !$url && !empty($parsed_url) && !isset($parsed_url) && !isset($parsed_home) && !$host && $ip !== false82–163wp_kses_bad_protocol(), strtolower(), strtolower(), parse_url(), strpbrk(), get_option(), parse_url(), gethostbyname(), explode(), array_map(), apply_filters()
is_string($url) && $url !== "" && !$url && !empty($parsed_url) && !isset($parsed_url) && !isset($parsed_home) && !$host && $ip !== false && apply_filters()95–170wp_kses_bad_protocol(), strtolower(), strtolower(), parse_url(), strpbrk(), get_option(), parse_url(), gethostbyname(), explode(), array_map(), apply_filters(), apply_filters()
is_string($url) && $url !== "" && !$url && !isset($parsed_url) && !isset($parsed_home) && !$host && $ip !== false119–143wp_kses_bad_protocol(), strtolower(), strtolower(), parse_url(), strpbrk(), get_option(), parse_url(), gethostbyname(), explode(), array_map()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev2244–17057
8.52244–17057
8.42244–1705712 fewer instructions than PHP 8.3
8.32364–18257
8.22364–18257
8.12364–18257
7.42364–18257

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 2

2 hooks fire while wp_http_validate_url() runs, in this order:

  1. apply_filters( http_request_host_is_external )filterline 635 (+76 into the body)

    Checks if HTTP request is external or not.

  2. apply_filters( http_allowed_safe_ports )filterline 660 (+101 into the body)

    Controls the list of ports considered safe in HTTP API.

Uses · 3

Used by · 7

Source code

function wp_http_validate_url( $url ) {	if ( ! is_string( $url ) || '' === $url || is_numeric( $url ) ) {		return false;	} 	$original_url = $url;	$url          = wp_kses_bad_protocol( $url, array( 'http', 'https' ) );	if ( ! $url || strtolower( $url ) !== strtolower( $original_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 !== strpbrk( $parsed_url['host'], ':#?[]' ) ) {		return false;	} 	$parsed_home = parse_url( get_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 = gethostbyname( $host );			if ( $ip === $host ) { // Error condition for gethostbyname().				return false;			}		}		if ( $ip ) {			$parts = array_map( 'intval', explode( '.', $ip ) ); 			/*			 * These IP address ranges are not considered valid external hosts for HTTP requests.			 *			 * If the host resolves to an IP address in these ranges, the request will be rejected unless the 'http_request_host_is_external' filter allows it.			 *			 * References:			 *			 * - IPv4 Special-Purpose Address Space: https://www.iana.org/assignments/iana-ipv4-special-registry/iana-ipv4-special-registry.xhtml			 * - IPv4 Multicast Address Assignments: https://www.rfc-editor.org/rfc/rfc5771.html			 */			if ( 127 === $parts[0] || 10 === $parts[0] || 0 === $parts[0]          // 127.0.0.0/8 (loopback), 10.0.0.0/8 (private), 0.0.0.0/8 (this network).				|| ( 172 === $parts[0] && 16 <= $parts[1] && 31 >= $parts[1] )     // 172.16.0.0/12 (private).				|| ( 192 === $parts[0] && 168 === $parts[1] )                      // 192.168.0.0/16 (private).				|| ( 192 === $parts[0] && 0 === $parts[1] && 0 === $parts[2] )     // 192.0.0.0/24 (IETF protocol assignments).				|| ( 192 === $parts[0] && 0 === $parts[1] && 2 === $parts[2] )     // 192.0.2.0/24 (TEST-NET-1).				|| ( 192 === $parts[0] && 88 === $parts[1] && 99 === $parts[2] )   // 192.88.99.0/24 (6to4 relay anycast).				|| ( 198 === $parts[0] && 51 === $parts[1] && 100 === $parts[2] )  // 198.51.100.0/24 (TEST-NET-2).				|| ( 203 === $parts[0] && 0 === $parts[1] && 113 === $parts[2] )   // 203.0.113.0/24 (TEST-NET-3).				|| ( 169 === $parts[0] && 254 === $parts[1] )                      // 169.254.0.0/16 (link-local and cloud metadata).				|| ( 100 === $parts[0] && 64 <= $parts[1] && 127 >= $parts[1] )    // 100.64.0.0/10 (CGNAT).				|| ( 198 === $parts[0] && 18 <= $parts[1] && 19 >= $parts[1] )     // 198.18.0.0/15 (benchmarking).				|| ( 224 <= $parts[0] && 239 >= $parts[0] )                        // 224.0.0.0/4 (multicast).				|| 240 <= $parts[0]                                                // 240.0.0.0/4 (reserved, includes 255.255.255.255 broadcast).			) {				// If host appears local, reject unless specifically allowed.				/**				 * Checks if HTTP request is external or not.				 *				 * Allows to change 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;				}			}

Changelog

Introduced in 3.5.2. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/http.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.