wp_http_validate_url( string $url ): string|false
- Since
- 3.5.2
- Source
wp-includes/http.php:552
Description
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:
- http://192.168.0.1/caniload.php - IPs from LAN networks.
This can be changed with the 'http_request_host_is_external' filter. - http://198.143.164.252:81/caniload.php - By default, only 80, 443, and 8080 ports 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- URL or false on failure.
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
- Scaling
- Constant
- Instructions
- 4–170
- Plugin surface
- 2 hooks
- Called by
- 6
Reads stored settings via get_option(), cached per request but not free on a cold cache.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 224.
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.
6 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below wp_http_validate_url() - serializeserialisation
maybe_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 4–6 | none |
is_string($url) && $url !== "" && !$url | 15 | wp_kses_bad_protocol() |
is_string($url) && $url !== "" && !$url | 28–32 | wp_kses_bad_protocol(), strtolower(), strtolower(), parse_url() |
is_string($url) && $url !== "" && !$url && !isset($parsed_url) && !isset($parsed_home) | 56 | wp_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 !== false | 65 | wp_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–76 | wp_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 !== false | 76–85 | wp_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 !== false | 82–163 | wp_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–170 | wp_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 !== false | 119–143 | wp_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 224 | 4–170 | 57 | |
| 8.5 | 224 | 4–170 | 57 | |
| 8.4 | 224 | 4–170 | 57 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 236 | 4–182 | 57 | |
| 8.2 | 236 | 4–182 | 57 | |
| 8.1 | 236 | 4–182 | 57 | |
| 7.4 | 236 | 4–182 | 57 |
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:
- apply_filters( http_request_host_is_external )filterline 628 (+76 into the body)
Checks if HTTP request is external or not.
- apply_filters( http_allowed_safe_ports )filterline 652 (+100 into the body)
Controls the list of ports considered safe in HTTP API.
Uses · 3
- wp_kses_bad_protocol()Sanitizes a string and removed disallowed URL protocols.
- get_option()Retrieves an option value based on an option name.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 6
- WP_Font_Collection::load_from_json()Loads font collection data from a JSON file or URL.
- WP_Http::request()Send an HTTP request to a URI.
- WP_Http::validate_redirects()Validate redirected URLs.
- WP_REST_Font_Faces_Controller::sanitize_src()Sanitizes a single src value for a font face.
- WP_REST_Font_Faces_Controller::validate_create_font_face_settings()Validates settings when creating a font face.
- pingback_ping_source_uri()Default filter attached to pingback_ping_source_uri to validate the pingback's Source URI.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.