<!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>[51626] trunk: Code Modernization: Check the return type of `parse_url()` in `download_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/51626">51626</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/51626","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>SergeyBiryukov</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-08-17 00:14:20 +0000 (Tue, 17 Aug 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'>Code Modernization: Check the return type of `parse_url()` in `download_url()`.

As per the PHP manual:
> If the `component` parameter is omitted, an associative array is returned.
> If the `component` parameter is specified, `parse_url()` returns a string (or an int, in the case of `PHP_URL_PORT`) instead of an array. If the requested component doesn't exist within the given URL, `null` will be returned.

Reference: [https://www.php.net/manual/en/function.parse-url.php#refsect1-function.parse-url-returnvalues PHP Manual: parse_url(): Return Values]

This commit adds three unit tests for `download_url()`:

* The first test is "girl-scouting" to make sure that the code up to the point where the error is expected is tested.
* The second test exposed a PHP 8.1 `basename(): Passing null to parameter <a href="https://core.trac.wordpress.org/ticket/1">#1</a> ($path) of type string is deprecated` error due to the call to `parse_url()` returning `null` when the component requested does not exist in the passed URL.
* The output of the call to `parse_url()` stored in the `$url_path` variable is used in more places in the function logic. The third test exposes a second PHP 8.1 deprecation notice, this time for `substr(): Passing null to parameter <a href="https://core.trac.wordpress.org/ticket/1">#1</a> ($string) of type string is deprecated`.

This commit also removes duplicate `parse_url()` calls. Neither `$url` nor `$url_filename` are changed between when they are first received/defined and when they are re-used, so there is no need to repeat the function calls.

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

Props jrf, hellofromTonya, SergeyBiryukov.
See <a href="https://core.trac.wordpress.org/ticket/53635">#53635</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesfilephp">trunk/src/wp-admin/includes/file.php</a></li>
<li><a href="#trunktestsphpunittestsadminincludesFilephp">trunk/tests/phpunit/tests/admin/includesFile.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesfilephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/file.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/file.php      2021-08-16 22:51:47 UTC (rev 51625)
+++ trunk/src/wp-admin/includes/file.php        2021-08-17 00:14:20 UTC (rev 51626)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1126,7 +1126,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return new WP_Error( 'http_no_url', __( 'Invalid URL Provided.' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $url_filename = basename( parse_url( $url, PHP_URL_PATH ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $url_path     = parse_url( $url, PHP_URL_PATH );
+       $url_filename = '';
+       if ( is_string( $url_path ) && '' !== $url_path ) {
+               $url_filename = basename( $url_path );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $tmpfname = wp_tempnam( $url_filename );
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! $tmpfname ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1212,9 +1216,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        // WordPress.org stores signatures at $package_url.sig.
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        $signature_url = false;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $url_path      = parse_url( $url, PHP_URL_PATH );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( '.zip' === substr( $url_path, -4 ) || '.tar.gz' === substr( $url_path, -7 ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( is_string( $url_path ) && ( '.zip' === substr( $url_path, -4 ) || '.tar.gz' === substr( $url_path, -7 ) ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                                 $signature_url = str_replace( $url_path, $url_path . '.sig', $url );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1243,7 +1246,7 @@
</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">                // Perform the checks.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $signature_verification = verify_file_signature( $tmpfname, $signature, basename( parse_url( $url, PHP_URL_PATH ) ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $signature_verification = verify_file_signature( $tmpfname, $signature, $url_filename );
</ins><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">        if ( is_wp_error( $signature_verification ) ) {
</span></span></pre></div>
<a id="trunktestsphpunittestsadminincludesFilephp"></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/admin/includesFile.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/admin/includesFile.php  2021-08-16 22:51:47 UTC (rev 51625)
+++ trunk/tests/phpunit/tests/admin/includesFile.php    2021-08-17 00:14:20 UTC (rev 51626)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -77,4 +77,78 @@
</span><span class="cx" style="display: block; padding: 0 10px">        public function __return_5() {
</span><span class="cx" style="display: block; padding: 0 10px">                return 5;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Verify that a WP_Error object is returned when invalid input is passed as the `$url` parameter.
+        *
+        * @covers ::download_url
+        * @dataProvider data_download_url_empty_url
+        *
+        * @param mixed $url Input URL.
+        */
+       public function test_download_url_empty_url( $url ) {
+               $error = download_url( $url );
+               $this->assertWPError( $error );
+               $this->assertSame( 'http_no_url', $error->get_error_code() );
+               $this->assertSame( 'Invalid URL Provided.', $error->get_error_message() );
+       }
+
+       /**
+        * Data provider.
+        *
+        * @return array
+        */
+       public function data_download_url_empty_url() {
+               return array(
+                       'null'         => array( null ),
+                       'false'        => array( false ),
+                       'integer 0'    => array( 0 ),
+                       'empty string' => array( '' ),
+                       'string 0'     => array( '0' ),
+               );
+       }
+
+       /**
+        * Test that PHP 8.1 "passing null to non-nullable" deprecation notice
+        * is not thrown when the `$url` does not have a path component.
+        *
+        * @ticket 53635
+        * @covers ::download_url
+        */
+       public function test_download_url_no_warning_for_url_without_path() {
+               $result = download_url( 'https://example.com' );
+
+               $this->assertIsString( $result );
+               $this->assertNotEmpty( $result ); // File path will be generated, but will never be empty.
+       }
+
+       /**
+        * Test that PHP 8.1 "passing null to non-nullable" deprecation notice
+        * is not thrown when the `$url` does not have a path component,
+        * and signature verification via a local file is requested.
+        *
+        * @ticket 53635
+        * @covers ::download_url
+        */
+       public function test_download_url_no_warning_for_url_without_path_with_signature_verification() {
+               add_filter(
+                       'wp_signature_hosts',
+                       static function( $urls ) {
+                               $urls[] = 'example.com';
+                               return $urls;
+                       }
+               );
+               $error = download_url( 'https://example.com', 300, true );
+
+               /*
+                * Note: This test is not testing the signature verification itself.
+                * There is no signature available for the domain used in the test,
+                * which is why an error is expected and that's fine.
+                * The point of the test is to verify that the call to `verify_file_signature()`
+                * is actually reached and that no PHP deprecation notice is thrown
+                * before this point.
+                */
+               $this->assertWPError( $error );
+               $this->assertSame( 'signature_verification_no_signature', $error->get_error_code() );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>