Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/wp-includes/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,9 +587,24 @@ function wp_http_validate_url( $url ) {
$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'], '.' );
$is_ipv4 = (bool) 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
);

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 ) ) {
if (
! $is_ipv4
Copy link
Member

@SirLouen SirLouen Dec 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manhphuc I think you are shortcircuiting a little late
We have the $parse_url['host'] available much earlier.

Also, ipv4 will return a truthy value, so doing this won't be disruptive for such addresses
https://3v4l.org/Jau1h

I recommend you to debug a bit to set it in the best place. See what kind of values you are receiving earlier.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion!

I’ve moved the IPv4 detection to right after normalizing $host, so it’s available earlier and reused in the ! $same_host branch. This keeps the behavior unchanged for IPv4 addresses, while avoiding the later short-circuit.

Let me know if you’d prefer it even earlier in the function.

&& extension_loaded( 'filter' )
&& ! filter_var(
$host,
FILTER_VALIDATE_DOMAIN,
array( 'flags' => FILTER_FLAG_HOSTNAME )
)
) {
return false;
}
if ( $is_ipv4 ) {
$ip = $host;
} else {
$ip = gethostbyname( $host );
Expand Down
3 changes: 3 additions & 0 deletions tests/phpunit/tests/http/http.php
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,9 @@ public function data_wp_http_validate_url_should_not_validate() {
'url' => 'https://example.com:81/caniload.php',
'cb_safe_ports' => 'callback_remove_safe_ports',
),
'underscore_in_hostname' => array(
'url' => 'https://foo_bar.example.com/',
),
);
}

Expand Down
Loading