Below is a differential between the unpatched vulnerable code and the patched update, for reference.
--- a/limit-login-attempts-reloaded/core/CloudApp.php
+++ b/limit-login-attempts-reloaded/core/CloudApp.php
@@ -5,10 +5,12 @@
use Exception;
use LLARCoreHttpHttp;
-if ( ! defined( 'ABSPATH' ) ) exit;
+if ( ! defined( 'ABSPATH' ) ) {
+ exit;
+}
+
+class CloudApp {
-class CloudApp
-{
/**
* @var null|string
*/
@@ -53,16 +55,15 @@
* App constructor.
* @param array $config
*/
- public function __construct( array $config )
- {
+ public function __construct( array $config ) {
if ( empty( $config ) ) {
return false;
}
// Read required fields defensively: a missing field must surface as a
// user-facing error later, not as a PHP warning here.
- $this->id = isset( $config['id'] ) ? 'app_' . $config['id'] : null;
- $this->api = isset( $config['api'] ) ? $config['api'] : null;
+ $this->id = isset( $config['id'] ) ? 'app_' . $config['id'] : null;
+ $this->api = isset( $config['api'] ) ? $config['api'] : null;
$this->config = $config;
foreach ( array( 'id', 'api', 'header', 'key' ) as $required_field ) {
@@ -77,8 +78,7 @@
*
* @return array
*/
- public function get_missing_config_fields()
- {
+ public function get_missing_config_fields() {
return $this->missing_config_fields;
}
@@ -87,8 +87,7 @@
*
* @return bool
*/
- public function is_config_valid()
- {
+ public function is_config_valid() {
return empty( $this->missing_config_fields );
}
@@ -96,8 +95,7 @@
* @param $error
* @return bool
*/
- public function add_error( $error )
- {
+ public function add_error( $error ) {
if ( ! $error ) {
return false;
}
@@ -108,24 +106,21 @@
/**
* @return array
*/
- public function get_errors()
- {
+ public function get_errors() {
return $this->login_errors;
}
/**
* @return null|string
*/
- public function get_id()
- {
+ public function get_id() {
return $this->id;
}
/**
* @return array
*/
- public function get_config()
- {
+ public function get_config() {
return $this->config;
}
@@ -133,8 +128,7 @@
* @param $link
* @return false[]
*/
- public static function setup( $link )
- {
+ public static function setup( $link ) {
$return = array(
'success' => false,
);
@@ -146,18 +140,27 @@
$link = 'https://' . $link;
$domain = parse_url( home_url( '/' ) );
- $link = add_query_arg( 'domain', $domain['host'], $link );
+ $link = add_query_arg( 'domain', $domain['host'], $link );
$plugin_data = get_plugin_data( LLA_PLUGIN_DIR . 'limit-login-attempts-reloaded.php' );
- $link = add_query_arg( 'version', $plugin_data['Version'], $link );
+ $link = add_query_arg( 'version', $plugin_data['Version'], $link );
// Provisioning is slower than regular API calls — allow more time.
$setup_response = Http::get( $link, array( 'timeout' => 20 ) );
- $response_data = isset( $setup_response['data'] ) ? $setup_response['data'] : '{}';
+ // The outbound URL policy blocked the destination (the setup code is
+ // user input): report a clear reason without fetching — and without
+ // logging the attacker-influenced URL.
+ if ( ! empty( $setup_response['blocked'] ) ) {
+ $return['error'] = __( 'The Setup Code points to a destination this site cannot contact. Setup codes are issued by your app provider and always point to a public HTTPS address.', 'limit-login-attempts-reloaded' );
+
+ return $return;
+ }
+
+ $response_data = isset( $setup_response['data'] ) ? $setup_response['data'] : '{}';
$setup_response_body = json_decode( $response_data, true );
- if ( $setup_response['status'] === 200 && empty( $setup_response['error'] ) ) {
+ if ( 200 === $setup_response['status'] && empty( $setup_response['error'] ) ) {
$return['success'] = true;
$return['app_config'] = $setup_response_body;
@@ -188,8 +191,7 @@
return $return;
}
- public static function activate_license_key( $setup_code )
- {
+ public static function activate_license_key( $setup_code ) {
$link = strrev( $setup_code );
/**
@@ -269,8 +271,7 @@
* @return bool|mixed
* @throws Exception
*/
- public function stats()
- {
+ public function stats() {
if ( empty( $this->stats_cache ) ) {
$this->stats_cache = $this->request( 'stats' );
}
@@ -281,11 +282,10 @@
/**
* @return bool|mixed
*/
- public static function stats_global()
- {
+ public static function stats_global() {
$response = Http::get( 'https://api.limitloginattempts.com/v1/global-stats' );
- if ( $response['status'] !== 200 ) {
+ if ( 200 !== $response['status'] ) {
return false;
}
@@ -298,8 +298,7 @@
* @return bool|mixed
* @throws Exception
*/
- public function acl_check( $data )
- {
+ public function acl_check( $data ) {
$this->prepare_settings( 'acl', $data );
return $this->request( 'acl', 'post', $data );
@@ -311,142 +310,136 @@
* @return bool|mixed
* @throws Exception
*/
- public function acl( $data )
- {
+ public function acl( $data ) {
return $this->request( 'acl', 'get', $data );
}
- /**
- * @return bool|mixed
- * @throws Exception
- */
- public function info()
- {
- if ( ! $this->is_config_valid() ) {
- if ( ! defined( 'LLAR_FAILED_INFO_NOTICE_SHOWN' ) ) {
- define( 'LLAR_FAILED_INFO_NOTICE_SHOWN', true );
- $this->render_config_incomplete_notice();
- }
-
- return false;
- }
-
- $info = $this->request_info();
-
- if ( ! $info && ! defined( 'LLAR_FAILED_INFO_NOTICE_SHOWN' ) && $this->is_info_network_failure() ) {
- define( 'LLAR_FAILED_INFO_NOTICE_SHOWN', true );
- $this->render_info_network_failure_notice();
- }
-
- return $info;
- }
-
- /**
- * Whether the last /info call failed before reaching the Cloud API (transport / DNS / timeout).
- *
- * @return bool
- */
- public function is_info_network_failure()
- {
- if ( 0 < (int) $this->last_response_code ) {
- return false;
- }
-
- return true;
- }
-
- /**
- * @return bool|mixed
- * @throws Exception
- */
- private function request_info()
- {
- if ( ! $this->api ) {
- error_log( 'LLAR: CloudApp info request skipped - Cloud API endpoint is not configured. Missing fields: ' . implode( ', ', $this->missing_config_fields ) );
- $this->last_error_message = 'Cloud API endpoint is not configured.';
-
- return false;
- }
-
- $headers = array();
- $headers[] = "{$this->config['header']}: {$this->config['key']}";
-
- $response = Http::get(
- $this->api . '/info',
- array(
- 'headers' => $headers,
- )
- );
-
- $this->last_response_code = ! empty( $response['status'] ) ? $response['status'] : 0;
- $this->last_error_message = ! empty( $response['error'] ) ? $response['error'] : null;
-
- $this->maybe_fallback_to_local_on_trial_expired( $response );
-
- $info = false;
- if ( ! empty( $response['data'] ) ) {
- $decoded = json_decode( $response['data'], true );
- if ( is_array( $decoded ) && ! empty( $decoded ) ) {
- $info = Helpers::sanitize_stripslashes_deep( $decoded );
- }
- }
-
- if ( 200 !== (int) $this->last_response_code ) {
- error_log( 'LLAR: CloudApp info request failed: ' . $this->api . '/info ' . $this->last_response_code );
- }
-
- return $info;
- }
-
- /**
- * Admin notice when the stored app config is incomplete: a required
- * field (e.g. "api") is missing, so no Cloud API call is possible.
- */
- private function render_config_incomplete_notice()
- {
- $message = sprintf(
- /* translators: %s: comma-separated list of missing config fields. */
- __( 'The Cloud App configuration is incomplete (missing field(s): %s). Please re-enter your Setup Code on the Settings tab to re-import the app configuration.', 'limit-login-attempts-reloaded' ),
- esc_html( implode( ', ', $this->missing_config_fields ) )
- );
-
- echo '<div class="notice notice-error" style="display: block;"><p>' . $message . '</p></div>';
- }
-
- /**
- * Admin notice when /info could not reach the Cloud API at all.
- */
- private function render_info_network_failure_notice()
- {
- $details = array();
-
- if ( null !== $this->last_response_code ) {
- $details[] = sprintf(
- /* translators: %d: HTTP response code. */
- __( 'Code: %d', 'limit-login-attempts-reloaded' ),
- intval( $this->last_response_code )
- );
- }
-
- if ( ! empty( $this->last_error_message ) ) {
- $details[] = sprintf(
- /* translators: %s: technical error reason. */
- __( 'Reason: %s', 'limit-login-attempts-reloaded' ),
- esc_html( $this->get_readable_error_reason( $this->last_error_message ) )
- );
- }
-
- $message = __( 'Oops, it looks like the site is having a temporary network issue.', 'limit-login-attempts-reloaded' );
-
- if ( ! empty( $details ) ) {
- $message .= ' (' . implode( '; ', $details ) . ')';
- }
-
- $refresh_label = __( 'Click here to refresh the page', 'limit-login-attempts-reloaded' );
-
- echo '<div class="notice notice-error" style="display: block;"><p>' . $message . '</p>';
- echo '<p><a href="javascript:void(0);" onclick="window.location.reload();" class="button button-primary">' . esc_html( $refresh_label ) . '</a></p></div>';
- }
+ /**
+ * @return bool|mixed
+ * @throws Exception
+ */
+ public function info() {
+ if ( ! $this->is_config_valid() ) {
+ if ( ! defined( 'LLAR_FAILED_INFO_NOTICE_SHOWN' ) ) {
+ define( 'LLAR_FAILED_INFO_NOTICE_SHOWN', true );
+ $this->render_config_incomplete_notice();
+ }
+
+ return false;
+ }
+
+ $info = $this->request_info();
+
+ if ( ! $info && ! defined( 'LLAR_FAILED_INFO_NOTICE_SHOWN' ) && $this->is_info_network_failure() ) {
+ define( 'LLAR_FAILED_INFO_NOTICE_SHOWN', true );
+ $this->render_info_network_failure_notice();
+ }
+
+ return $info;
+ }
+
+ /**
+ * Whether the last /info call failed before reaching the Cloud API (transport / DNS / timeout).
+ *
+ * @return bool
+ */
+ public function is_info_network_failure() {
+ if ( 0 < (int) $this->last_response_code ) {
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * @return bool|mixed
+ * @throws Exception
+ */
+ private function request_info() {
+ if ( ! $this->api ) {
+ error_log( 'LLAR: CloudApp info request skipped - Cloud API endpoint is not configured. Missing fields: ' . implode( ', ', $this->missing_config_fields ) );
+ $this->last_error_message = 'Cloud API endpoint is not configured.';
+
+ return false;
+ }
+
+ $headers = array();
+ $headers[] = "{$this->config['header']}: {$this->config['key']}";
+
+ $response = Http::get(
+ $this->api . '/info',
+ array(
+ 'headers' => $headers,
+ )
+ );
+
+ $this->last_response_code = ! empty( $response['status'] ) ? $response['status'] : 0;
+ $this->last_error_message = ! empty( $response['error'] ) ? $response['error'] : null;
+
+ $this->maybe_fallback_to_local_on_trial_expired( $response );
+
+ $info = false;
+ if ( ! empty( $response['data'] ) ) {
+ $decoded = json_decode( $response['data'], true );
+ if ( is_array( $decoded ) && ! empty( $decoded ) ) {
+ $info = Helpers::sanitize_stripslashes_deep( $decoded );
+ }
+ }
+
+ if ( 200 !== (int) $this->last_response_code ) {
+ error_log( 'LLAR: CloudApp info request failed: ' . $this->api . '/info ' . $this->last_response_code );
+ }
+
+ return $info;
+ }
+
+ /**
+ * Admin notice when the stored app config is incomplete: a required
+ * field (e.g. "api") is missing, so no Cloud API call is possible.
+ */
+ private function render_config_incomplete_notice() {
+ $message = sprintf(
+ /* translators: %s: comma-separated list of missing config fields. */
+ __( 'The Cloud App configuration is incomplete (missing field(s): %s). Please re-enter your Setup Code on the Settings tab to re-import the app configuration.', 'limit-login-attempts-reloaded' ),
+ implode( ', ', $this->missing_config_fields )
+ );
+
+ echo '<div class="notice notice-error" style="display: block;"><p>' . esc_html( $message ) . '</p></div>';
+ }
+
+ /**
+ * Admin notice when /info could not reach the Cloud API at all.
+ */
+ private function render_info_network_failure_notice() {
+ $details = array();
+
+ if ( null !== $this->last_response_code ) {
+ $details[] = sprintf(
+ /* translators: %d: HTTP response code. */
+ __( 'Code: %d', 'limit-login-attempts-reloaded' ),
+ intval( $this->last_response_code )
+ );
+ }
+
+ if ( ! empty( $this->last_error_message ) ) {
+ $details[] = sprintf(
+ /* translators: %s: technical error reason. */
+ __( 'Reason: %s', 'limit-login-attempts-reloaded' ),
+ $this->get_readable_error_reason( $this->last_error_message )
+ );
+ }
+
+ $message = __( 'Oops, it looks like the site is having a temporary network issue.', 'limit-login-attempts-reloaded' );
+
+ if ( ! empty( $details ) ) {
+ $message .= ' (' . implode( '; ', $details ) . ')';
+ }
+
+ $refresh_label = __( 'Click here to refresh the page', 'limit-login-attempts-reloaded' );
+
+ echo '<div class="notice notice-error" style="display: block;"><p>' . esc_html( $message ) . '</p>';
+ echo '<p><a href="javascript:void(0);" onclick="window.location.reload();" class="button button-primary">' . esc_html( $refresh_label ) . '</a></p></div>';
+ }
/**
* @param $data
@@ -454,8 +447,7 @@
* @return bool|mixed
* @throws Exception
*/
- public function acl_create( $data )
- {
+ public function acl_create( $data ) {
return $this->request( 'acl/create', 'post', $data );
}
@@ -465,8 +457,7 @@
* @return bool|mixed
* @throws Exception
*/
- public function acl_delete( $data )
- {
+ public function acl_delete( $data ) {
return $this->request( 'acl/delete', 'post', $data );
}
@@ -474,8 +465,7 @@
* @return bool|mixed
* @throws Exception
*/
- public function country()
- {
+ public function country() {
return $this->request( 'country', 'get' );
}
@@ -484,8 +474,7 @@
* @return bool|mixed
* @throws Exception
*/
- public function country_add( $data )
- {
+ public function country_add( $data ) {
return $this->request( 'country/add', 'post', $data );
}
@@ -494,8 +483,7 @@
* @return bool|mixed
* @throws Exception
*/
- public function country_remove( $data )
- {
+ public function country_remove( $data ) {
return $this->request( 'country/remove', 'post', $data );
}
@@ -505,8 +493,7 @@
* @return bool|mixed
* @throws Exception
*/
- public function country_rule( $data )
- {
+ public function country_rule( $data ) {
return $this->request( 'country/rule', 'post', $data );
}
@@ -516,8 +503,7 @@
* @return bool|mixed
* @throws Exception
*/
- public function lockout_check( $data )
- {
+ public function lockout_check( $data ) {
$this->prepare_settings( 'lockout', $data );
return $this->request( 'lockout', 'post', $data );
@@ -530,12 +516,11 @@
* @return bool|mixed
* @throws Exception
*/
- public function log($limit = 25, $offset = '')
- {
+ public function log( $limit = 25, $offset = '' ) {
$data = array();
- $data['limit'] = $limit;
- $data['offset'] = $offset;
+ $data['limit'] = $limit;
+ $data['offset'] = $offset;
$data['is_short'] = 1;
return $this->request( 'log', 'get', $data );
@@ -549,12 +534,11 @@
* @return bool|mixed
* @throws Exception
*/
- public function get_login($limit = 25, $offset = '')
- {
+ public function get_login( $limit = 25, $offset = '' ) {
$data = array();
- $data['limit'] = $limit;
- $data['offset'] = $offset;
+ $data['limit'] = $limit;
+ $data['offset'] = $offset;
$data['is_short'] = 1;
return $this->request( 'login', 'get', $data );
@@ -568,11 +552,10 @@
* @return bool|mixed
* @throws Exception
*/
- public function get_lockouts($limit = 25, $offset = '')
- {
+ public function get_lockouts( $limit = 25, $offset = '' ) {
$data = array();
- $data['limit'] = $limit;
+ $data['limit'] = $limit;
$data['offset'] = $offset;
return $this->request( 'lockout', 'get', $data );
@@ -583,8 +566,7 @@
*
* @param $method
*/
- public function prepare_settings( $method, &$data )
- {
+ public function prepare_settings( $method, &$data ) {
$settings = array();
if ( ! empty( $this->config['settings'] ) ) {
@@ -592,7 +574,7 @@
foreach ( $this->config['settings'] as $setting_name => $setting_data ) {
if ( in_array( $method, $setting_data['methods'] ) ) {
- $settings[$setting_name] = $setting_data['value'];
+ $settings[ $setting_name ] = $setting_data['value'];
}
}
}
@@ -609,8 +591,7 @@
* @return bool|mixed
* @throws Exception
*/
- public function request( $method, $type = 'get', $data = null )
- {
+ public function request( $method, $type = 'get', $data = null ) {
if ( ! $method ) {
throw new Exception( 'You must specify API method.' );
}
@@ -624,15 +605,18 @@
return false;
}
- $headers = array();
+ $headers = array();
$headers[] = "{$this->config['header']}: {$this->config['key']}";
- $response = Http::$type( $this->api . '/' . $method, array(
- 'data' => $data,
- 'headers' => $headers
- ) );
+ $response = Http::$type(
+ $this->api . '/' . $method,
+ array(
+ 'data' => $data,
+ 'headers' => $headers,
+ )
+ );
- $this->last_response_code = !empty( $response['status'] ) ? $response['status'] : 0;
+ $this->last_response_code = ! empty( $response['status'] ) ? $response['status'] : 0;
$this->last_error_message = ! empty( $response['error'] ) ? $response['error'] : null;
$this->maybe_fallback_to_local_on_trial_expired( $response );
--- a/limit-login-attempts-reloaded/core/http/Http.php
+++ b/limit-login-attempts-reloaded/core/http/Http.php
@@ -19,20 +19,51 @@
private static $transport;
/**
+ * IPv4 ranges that must never be fetched: this address, private,
+ * link-local (incl. cloud metadata 169.254.169.254), CGNAT, documentation,
+ * benchmarking, multicast and reserved space.
+ *
+ * @var array
+ */
+ private static $blocked_ipv4_ranges = array(
+ array( '0.0.0.0', '0.255.255.255' ), // "this" network
+ array( '10.0.0.0', '10.255.255.255' ), // private
+ array( '100.64.0.0', '100.127.255.255' ), // CGNAT
+ array( '127.0.0.0', '127.255.255.255' ), // loopback
+ array( '169.254.0.0', '169.254.255.255' ), // link-local, cloud metadata
+ array( '172.16.0.0', '172.31.255.255' ), // private
+ array( '192.0.0.0', '192.0.0.255' ), // IETF protocol assignments
+ array( '192.0.2.0', '192.0.2.255' ), // TEST-NET-1
+ array( '192.88.99.0', '192.88.99.255' ), // 6to4 relay anycast
+ array( '192.168.0.0', '192.168.255.255' ), // private
+ array( '198.18.0.0', '198.19.255.255' ), // benchmarking
+ array( '198.51.100.0', '198.51.100.255' ), // TEST-NET-2
+ array( '203.0.113.0', '203.0.113.255' ), // TEST-NET-3
+ array( '224.0.0.0', '239.255.255.255' ), // multicast
+ array( '240.0.0.0', '255.255.255.255' ), // reserved, broadcast
+ );
+
+ /**
* @throws Exception
*/
public static function init() {
- if( function_exists( 'fopen' ) && ini_get( 'allow_url_fopen' ) === '1' ) {
- self::$transport = new HttpTransportFopen();
- } else if( function_exists( 'wp_remote_get' ) ) {
- self::$transport = new HttpTransportWp();
- } else if( function_exists( 'curl_version' ) ) {
- self::$transport = new HttpTransportCurl();
- } else {
- throw new Exception( 'Unable to determine HTTP transport.' );
- }
+ switch ( true ) {
+ case function_exists( 'fopen' ) && ini_get( 'allow_url_fopen' ) === '1':
+ self::$transport = new HttpTransportFopen();
+ break;
+
+ case function_exists( 'wp_remote_get' ):
+ self::$transport = new HttpTransportWp();
+ break;
+
+ case function_exists( 'curl_version' ):
+ self::$transport = new HttpTransportCurl();
+ break;
+ default:
+ throw new Exception( 'Unable to determine HTTP transport.' );
+ }
}
/**
@@ -43,6 +74,18 @@
*/
public static function get( $url, $options = array() ) {
+ $destination = self::resolve_destination( $url );
+
+ if ( null === $destination ) {
+ return self::unresolvable_response();
+ }
+
+ if ( false === $destination ) {
+ return self::policy_rejection();
+ }
+
+ $options['destination'] = $destination;
+
return self::$transport->get( $url, $options );
}
@@ -88,11 +131,381 @@
*/
public static function post( $url, $options = array() ) {
- $options['headers'] = array_merge(
+ $destination = self::resolve_destination( $url );
+
+ if ( null === $destination ) {
+ return self::unresolvable_response();
+ }
+
+ if ( false === $destination ) {
+ return self::policy_rejection();
+ }
+
+ $options['destination'] = $destination;
+ $options['headers'] = array_merge(
array( 'Content-Type: application/json; charset=utf-8' ),
- !empty( $options['headers'] ) ? $options['headers'] : array()
+ ! empty( $options['headers'] ) ? $options['headers'] : array()
);
return self::$transport->post( $url, $options );
}
-}
No newline at end of file
+
+ /**
+ * Outbound URL policy (SSRF guard) — the single source of truth.
+ *
+ * @param string $url
+ *
+ * @return bool
+ */
+ public static function is_url_allowed( $url ) {
+
+ return is_array( self::resolve_destination( $url ) );
+ }
+
+ /**
+ * Normalize a hostname: strip brackets, trailing dots, lowercase.
+ *
+ * @param string $host
+ *
+ * @return string
+ */
+ public static function normalize_host( $host ) {
+
+ if ( ! is_string( $host ) || '' === $host ) {
+ return '';
+ }
+
+ return strtolower( rtrim( trim( $host, '[]' ), '.' ) );
+ }
+
+ /**
+ * Whether $host is the site's own host (core same-host policy).
+ *
+ * @param string $host already-normalized or raw
+ *
+ * @return bool
+ */
+ public static function is_same_host( $host ) {
+
+ $host = self::normalize_host( $host );
+ $home = self::normalize_host( (string) wp_parse_url( home_url( '/' ), PHP_URL_HOST ) );
+
+ return '' !== $host && '' !== $home && $host === $home;
+ }
+
+ /**
+ * Transport-shaped policy rejection. Shared by the facade and transports
+ * so callers always see `blocked => true` with a stable error string.
+ *
+ * @param string|null $error
+ *
+ * @return array
+ */
+ public static function policy_rejection( $error = null ) {
+
+ return array(
+ 'data' => null,
+ 'status' => 0,
+ 'error' => $error
+ ? $error
+ : 'Destination rejected by the outbound URL policy (loopback, private networks and non-standard ports are not fetchable).',
+ 'blocked' => true,
+ );
+ }
+
+ /**
+ * Validate an outbound destination and resolve it to the exact IPs the
+ * transport should connect to.
+ *
+ * Two layers:
+ *
+ * 1. wp_http_validate_url() — scheme, embedded credentials, port
+ * whitelist (80/443/8080), same-host allowance and the core filters
+ * (`http_request_host_is_external`, `http_allowed_safe_ports`) that
+ * self-hosted setups use as the escape hatch.
+ *
+ * 2. Our own range check over EVERY A and AAAA record. Core only rejects
+ * 0/8, 10/8, 127/8, 172.16/12 and 192.168/16 (still true in 6.5), so
+ * link-local 169.254.0.0/16 — the cloud metadata endpoint — and the
+ * rest of the IANA special-purpose space would otherwise pass. Doing
+ * this ourselves keeps the policy independent of the WordPress version
+ * the site happens to run.
+ *
+ * The returned IP list is what closes DNS rebinding: the transport pins
+ * the connection to these addresses (CURLOPT_RESOLVE / rewritten URL +
+ * Host + TLS peer name) instead of resolving the name a second time.
+ *
+ * The site's own host is allowed (core parity — self-hosted setups talk
+ * to themselves); no pinning is applied there.
+ *
+ * Fail-closed: anything that cannot be validated is not fetchable.
+ *
+ * @param string $url
+ *
+ * @return array|false|null array on allow, false on policy/invalid, null on NXDOMAIN
+ */
+ public static function resolve_destination( $url ) {
+
+ if ( ! is_string( $url ) || '' === $url ) {
+ return false;
+ }
+
+ if ( ! function_exists( 'wp_http_validate_url' ) ) {
+ return false;
+ }
+
+ $parsed = wp_parse_url( $url );
+
+ if ( empty( $parsed['host'] ) ) {
+ return false;
+ }
+
+ $scheme = isset( $parsed['scheme'] ) ? strtolower( $parsed['scheme'] ) : 'http';
+ $host = self::normalize_host( $parsed['host'] );
+ $port = isset( $parsed['port'] ) ? (int) $parsed['port'] : ( 'https' === $scheme ? 443 : 80 );
+
+ if ( '' === $host ) {
+ return false;
+ }
+
+ $core_ok = (bool) wp_http_validate_url( $url );
+
+ if ( ! $core_ok ) {
+ // WordPress rejects IPv6 literals (strpbrk on ':'). Validate those
+ // ourselves so public AAAA destinations stay reachable; everything
+ // else that fails core is rejected before our DNS lookup.
+ // Caveat: some WP HTTP transports may still refuse a bracketed
+ // IPv6 authority URL — that fails closed as a transport error,
+ // not a security bypass. Prefer Curl (CURLOPT_RESOLVE) for
+ // hostname→AAAA pins.
+ if ( ! filter_var( $host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 ) ) {
+ return false;
+ }
+
+ $safe_ports = apply_filters( 'http_allowed_safe_ports', array( 80, 443, 8080 ), $host, $url );
+ if ( ! is_array( $safe_ports ) || ! in_array( $port, $safe_ports, true ) ) {
+ return false;
+ }
+
+ $host_is_external = (bool) apply_filters( 'http_request_host_is_external', false, $host, $url );
+ if ( self::is_non_public_ip( $host ) && ! $host_is_external ) {
+ return false;
+ }
+
+ return array(
+ 'scheme' => $scheme,
+ 'host' => $host,
+ 'port' => $port,
+ 'ips' => array( $host ),
+ 'same_host' => false,
+ );
+ }
+
+ if ( self::is_same_host( $host ) ) {
+ return array(
+ 'scheme' => $scheme,
+ 'host' => $host,
+ 'port' => $port,
+ 'ips' => array(),
+ 'same_host' => true,
+ );
+ }
+
+ // One plugin-side resolution for pinning + our range check.
+ // null = resolved empty despite core OK (rare resolver mismatch).
+ $ips = self::resolve_host( $host );
+
+ if ( empty( $ips ) ) {
+ return null;
+ }
+
+ // Core's escape hatch for self-hosted/internal endpoints. Core only
+ // consults it for its own short private list; our range check is
+ // wider, so it has to honour the same filter — otherwise a site that
+ // deliberately points an internal app/MFA endpoint at a container
+ // name or a link-local address would be locked out with no way back.
+ $host_is_external = (bool) apply_filters( 'http_request_host_is_external', false, $host, $url );
+
+ foreach ( $ips as $ip ) {
+ if ( self::is_non_public_ip( $ip ) && ! $host_is_external ) {
+ return false;
+ }
+ }
+
+ return array(
+ 'scheme' => $scheme,
+ 'host' => $host,
+ 'port' => $port,
+ 'ips' => $ips,
+ 'same_host' => false,
+ );
+ }
+
+ /**
+ * Resolve a hostname to every A and AAAA record it publishes.
+ * A single record is not enough: a name with a public A and a loopback
+ * AAAA (or several A records, one of them private) must be rejected as a
+ * whole.
+ *
+ * @param string $host
+ *
+ * @return array
+ */
+ public static function resolve_host( $host ) {
+
+ if ( filter_var( $host, FILTER_VALIDATE_IP ) ) {
+ return array( $host );
+ }
+
+ $ips = array();
+
+ if ( function_exists( 'dns_get_record' ) ) {
+ foreach ( (array) @dns_get_record( $host, DNS_A | DNS_AAAA ) as $record ) {
+ if ( ! empty( $record['ip'] ) ) {
+ $ips[] = $record['ip'];
+ }
+ if ( ! empty( $record['ipv6'] ) ) {
+ $ips[] = $record['ipv6'];
+ }
+ }
+ }
+
+ if ( empty( $ips ) ) {
+ $ip = gethostbyname( $host );
+
+ if ( $ip !== $host ) {
+ $ips[] = $ip;
+ }
+ }
+
+ return array_values( array_unique( $ips ) );
+ }
+
+ /**
+ * @param string $ip
+ *
+ * @return bool
+ */
+ public static function is_non_public_ip( $ip ) {
+
+ $long = ip2long( $ip );
+
+ if ( false !== $long ) {
+ foreach ( self::$blocked_ipv4_ranges as $range ) {
+ if ( $long >= ip2long( $range[0] ) && $long <= ip2long( $range[1] ) ) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ $bin = @inet_pton( $ip );
+
+ if ( false === $bin || 16 !== strlen( $bin ) ) {
+ return true;
+ }
+
+ if ( inet_pton( '::1' ) === $bin || inet_pton( '::' ) === $bin ) {
+ return true;
+ }
+
+ $first = ord( $bin[0] );
+
+ // fc00::/7 unique local, fe80::/10 link-local.
+ if ( 0xfc === $first || 0xfd === $first ) {
+ return true;
+ }
+ if ( 0xfe === $first && 0x80 === ( ord( $bin[1] ) & 0xc0 ) ) {
+ return true;
+ }
+
+ // Prefix blocks (IANA special-purpose / documentation / discard / Teredo /
+ // local-use NAT64). Checked before IPv4-embedded unwraps so a special
+ // prefix cannot slip through as "public wrapper + public v4".
+ $prefixes = array(
+ array( '100::', 64 ), // discard-only
+ array( '2001:db8::', 32 ), // documentation
+ array( '2001::', 32 ), // Teredo
+ array( '64:ff9b:1::', 48 ), // local-use NAT64
+ );
+
+ foreach ( $prefixes as $prefix ) {
+ if ( self::ipv6_has_prefix( $bin, $prefix[0], $prefix[1] ) ) {
+ return true;
+ }
+ }
+
+ // ::/96 — "this host", loopback and the deprecated IPv4-compatible space.
+ if ( substr( $bin, 0, 12 ) === str_repeat( "