<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[52084] trunk: HTTP API: Introduce `'http_allowed_safe_ports'` filter in `wp_http_validate_url()`.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/52084">52084</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/52084","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>hellofromTonya</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-11-09 22:37:19 +0000 (Tue, 09 Nov 2021)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>HTTP API: Introduce `'http_allowed_safe_ports'` filter in `wp_http_validate_url()`.

Adds a new filter `'http_allowed_safe_ports'` to control which ports are allowed for remote requests. By default, ports 80, 443, and 8080 are allowed for safe remote requests.

Adds tests. 

Follow-up to <a href="https://core.trac.wordpress.org/changeset/24480">[24480]</a>.

Props xknown, johnbillion, jorbin, costdev, dd32.
Fixes <a href="https://core.trac.wordpress.org/ticket/54331">#54331</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludeshttpphp">trunk/src/wp-includes/http.php</a></li>
<li><a href="#trunktestsphpunittestshttphttpphp">trunk/tests/phpunit/tests/http/http.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludeshttpphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/http.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/http.php    2021-11-09 22:25:40 UTC (rev 52083)
+++ trunk/src/wp-includes/http.php      2021-11-09 22:37:19 UTC (rev 52084)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -514,6 +514,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @return string|false URL or false on failure.
</span><span class="cx" style="display: block; padding: 0 10px">  */
</span><span class="cx" style="display: block; padding: 0 10px"> function wp_http_validate_url( $url ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        if ( ! is_string( $url ) || '' === $url || is_numeric( $url ) ) {
+               return false;
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $original_url = $url;
</span><span class="cx" style="display: block; padding: 0 10px">        $url          = wp_kses_bad_protocol( $url, array( 'http', 'https' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! $url || strtolower( $url ) !== strtolower( $original_url ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -534,15 +538,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $parsed_home = parse_url( get_option( 'home' ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        $same_host   = isset( $parsed_home['host'] ) && strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] );
+       $host        = trim( $parsed_url['host'], '.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( isset( $parsed_home['host'] ) ) {
-               $same_host = strtolower( $parsed_home['host'] ) === strtolower( $parsed_url['host'] );
-       } else {
-               $same_host = false;
-       }
-
</del><span class="cx" style="display: block; padding: 0 10px">         if ( ! $same_host ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $host = trim( $parsed_url['host'], '.' );
</del><span class="cx" style="display: block; padding: 0 10px">                 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 ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $ip = $host;
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -581,7 +580,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $port = $parsed_url['port'];
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( 80 === $port || 443 === $port || 8080 === $port ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Controls the list of ports considered safe in HTTP API.
+        *
+        * Allows to change and allow external requests for the HTTP request.
+        *
+        * @since 5.9.0
+        *
+        * @param array  $allowed_ports Array of integers for valid ports.
+        * @param string $host          Host name of the requested URL.
+        * @param string $url           Requested URL.
+        */
+       $allowed_ports = apply_filters( 'http_allowed_safe_ports', array( 80, 443, 8080 ), $host, $url );
+       if ( in_array( $port, $allowed_ports, true ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $url;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunktestsphpunittestshttphttpphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/http/http.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/http/http.php   2021-11-09 22:25:40 UTC (rev 52083)
+++ trunk/tests/phpunit/tests/http/http.php     2021-11-09 22:37:19 UTC (rev 52084)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -392,4 +392,179 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * Test that wp_http_validate_url validates URLs.
+        *
+        * @ticket 54331
+        *
+        * @dataProvider data_wp_http_validate_url_should_validate
+        *
+        * @covers ::wp_http_validate_url
+        *
+        * @param string       $url            The URL to validate.
+        * @param false|string $cb_safe_ports  The name of the callback to http_allowed_safe_ports or false if none.
+        *                                     Default false.
+        * @param bool         $external_host  Whether or not the host is external.
+        *                                     Default false.
+        */
+       public function test_wp_http_validate_url_should_validate( $url, $cb_safe_ports = false, $external_host = false ) {
+               if ( $external_host ) {
+                       add_filter( 'http_request_host_is_external', '__return_true' );
+               }
+
+               if ( $cb_safe_ports ) {
+                       add_filter( 'http_allowed_safe_ports', array( $this, $cb_safe_ports ) );
+               }
+
+               $this->assertSame( $url, wp_http_validate_url( $url ) );
+       }
+
+       /**
+        * Data provider.
+        *
+        * @return array
+        */
+       public function data_wp_http_validate_url_should_validate() {
+               return array(
+                       'no port specified'                 => array(
+                               'url' => 'http://example.com/caniload.php',
+                       ),
+                       'an external request when allowed'  => array(
+                               'url'           => 'http://172.20.0.123/caniload.php',
+                               'cb_safe_ports' => false,
+                               'external_host' => true,
+                       ),
+                       'a port considered safe by default' => array(
+                               'url' => 'https://example.com:8080/caniload.php',
+                       ),
+                       'a port considered safe by filter'  => array(
+                               'url'           => 'https://example.com:81/caniload.php',
+                               'cb_safe_ports' => 'callback_custom_safe_ports',
+                       ),
+               );
+       }
+
+       /**
+        * Tests that wp_http_validate_url validates a url that uses an unsafe port
+        * but which matches the host and port used by the site's home url.
+        *
+        * @ticket 54331
+        *
+        * @covers ::wp_http_validate_url
+        */
+       public function test_wp_http_validate_url_should_validate_with_an_unsafe_port_when_the_host_and_port_match_the_home_url() {
+               $original_home    = get_option( 'home' );
+               $home_parsed      = parse_url( $original_home );
+               $home_scheme_host = implode( '://', array_slice( $home_parsed, 0, 2 ) );
+               $home_modified    = $home_scheme_host . ':83';
+
+               update_option( 'home', $home_modified );
+
+               $url = $home_modified . '/caniload.php';
+               $this->assertSame( $url, wp_http_validate_url( $url ) );
+
+               update_option( 'home', $original_home );
+       }
+
+       /**
+        * Test that wp_http_validate_url does not validate invalid URLs.
+        *
+        * @ticket 54331
+        *
+        * @dataProvider data_wp_http_validate_url_should_not_validate
+        *
+        * @covers ::wp_http_validate_url
+        *
+        * @param string       $url            The URL to validate.
+        * @param false|string $cb_safe_ports  The name of the callback to http_allowed_safe_ports or false if none.
+        *                                     Default false.
+        * @param bool         $external_host  Whether or not the host is external.
+        *                                     Default false.
+        */
+       public function test_wp_http_validate_url_should_not_validate( $url, $cb_safe_ports = false, $external_host = false ) {
+               if ( $external_host ) {
+                       add_filter( 'http_request_host_is_external', '__return_true' );
+               }
+
+               if ( $cb_safe_ports ) {
+                       add_filter( 'http_allowed_safe_ports', array( $this, $cb_safe_ports ) );
+               }
+
+               $this->assertFalse( wp_http_validate_url( $url ) );
+       }
+
+       /**
+        * Data provider.
+        *
+        * @return array
+        */
+       public function data_wp_http_validate_url_should_not_validate() {
+               return array(
+                       'url as false'                                 => array(
+                               'url' => false,
+                       ),
+                       'url as null'                                  => array(
+                               'url' => null,
+                       ),
+                       'url as int 0'                                 => array(
+                               'url' => 0,
+                       ),
+                       'url as string 0'                              => array(
+                               'url' => '0',
+                       ),
+                       'url as int 1'                                 => array(
+                               'url' => 1,
+                       ),
+                       'url as string 1'                              => array(
+                               'url' => '1',
+                       ),
+                       'url as array()'                               => array(
+                               'url' => array(),
+                       ),
+                       'an empty url'                                 => array(
+                               'url' => '',
+                       ),
+                       'a url with a non-http/https protocol'         => array(
+                               'url' => 'ftp://example.com:81/caniload.php',
+                       ),
+                       'a malformed url'                              => array(
+                               'url' => 'http:///example.com:81/caniload.php',
+                       ),
+                       'a host that cannot be parsed'                 => array(
+                               'url' => 'http:example.com/caniload.php',
+                       ),
+                       'login information'                            => array(
+                               'url' => 'http://user:pass@example.com/caniload.php',
+                       ),
+                       'a host with invalid characters'               => array(
+                               'url' => 'http://[exam]ple.com/caniload.php',
+                       ),
+                       'a host whose IPv4 address cannot be resolved' => array(
+                               'url' => 'http://exampleeeee.com/caniload.php',
+                       ),
+                       'an external request when not allowed'         => array(
+                               'url'           => 'http://192.168.0.1/caniload.php',
+                               'external_host' => false,
+                       ),
+                       'a port not considered safe by default'        => array(
+                               'url' => 'https://example.com:81/caniload.php',
+                       ),
+                       'a port not considered safe by filter'         => array(
+                               'url'           => 'https://example.com:82/caniload.php',
+                               'cb_safe_ports' => 'callback_custom_safe_ports',
+                       ),
+                       'all safe ports removed by filter'             => array(
+                               'url'           => 'https://example.com:81/caniload.php',
+                               'cb_safe_ports' => 'callback_remove_safe_ports',
+                       ),
+               );
+       }
+
+       public function callback_custom_safe_ports( $ports ) {
+               return array( 81, 444, 8081 );
+       }
+
+       public function callback_remove_safe_ports( $ports ) {
+               return array();
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>