Skip to content
Open
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.

Copy link
Member

Choose a reason for hiding this comment

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

The ipv4 check is totally irrelevant for this patch. Leave it where it is
What you should do is an earlier return in the filter. Ignore the ipv4 check this is why I gave you the 3v4l.org example.

Copy link
Member

Choose a reason for hiding this comment

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

What about if it is an IPv6? Should the regex be updated to account for that?

Copy link
Author

Choose a reason for hiding this comment

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

The ipv4 check is totally irrelevant for this patch. Leave it where it is What you should do is an earlier return in the filter. Ignore the ipv4 check this is why I gave you the 3v4l.org example.

Thanks for the clarification!

I’ve removed the earlier $is_ipv4 refactor and kept the IPv4 handling where it was.
The early return now only applies when the host is not an IPv4 address and filter_var() reports it as invalid, before doing any DNS resolution.

Let me know if you’d like the IPv4 regex extraction into a helper/variable for readability, but I kept it inline to minimize churn.

Copy link
Author

Choose a reason for hiding this comment

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

What about if it is an IPv6? Should the regex be updated to account for that?

Good question.

This change keeps the existing IP-handling behavior intact. IPv6 hosts are already rejected earlier in wp_http_validate_url() due to the current host character checks, so I didn’t expand the IPv4 regex here (to keep scope focused and avoid behavior changes).

If you think IPv6 support should be revisited in this area, I’m happy to follow up in a separate PR/ticket.

&& extension_loaded( 'filter' )
&& ! filter_var(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
&& ! filter_var(
&& false === filter_var(

Copy link
Author

Choose a reason for hiding this comment

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

Good catch — updated to a strict false === filter_var() check to avoid loose negation and align with common core patterns. Thanks!

$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