<!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>[55425] trunk: Upgrade/Install: Introduce `WP_Automatic_Updater::is_allowed_dir()` method.</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/55425">55425</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/55425","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>2023-02-26 15:17:45 +0000 (Sun, 26 Feb 2023)</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'>Upgrade/Install: Introduce `WP_Automatic_Updater::is_allowed_dir()` method.

As part of determining whether to perform automatic updates, WordPress checks if it is running within a version-controlled environment, recursively looking up the filesystem to the top of the drive, looking for a Subversion, Git, Mercurial, or Bazaar directory, erring on the side of detecting a VCS checkout somewhere.

This commit avoids a PHP warning if the `open_basedir` directive is in use and any of the directories checked in the process are not allowed:
{{{
is_dir(): open_basedir restriction in effect. File(/.git) is not within the allowed path(s)
}}}

Follow-up to <a href="https://core.trac.wordpress.org/changeset/25421">[25421]</a>, <a href="https://core.trac.wordpress.org/changeset/25700">[25700]</a>, <a href="https://core.trac.wordpress.org/changeset/25764">[25764]</a>, <a href="https://core.trac.wordpress.org/changeset/25835">[25835]</a>, <a href="https://core.trac.wordpress.org/changeset/25859">[25859]</a>.

Props costdev, markjaquith, meyegui, dd32, arnolp, robin-labadie, hellofromTonya, afragen, pbiron, SergeyBiryukov.
Fixes <a href="https://core.trac.wordpress.org/ticket/42619">#42619</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesclasswpautomaticupdaterphp">trunk/src/wp-admin/includes/class-wp-automatic-updater.php</a></li>
<li><a href="#trunktestsphpunittestsadminwpAutomaticUpdaterphp">trunk/tests/phpunit/tests/admin/wpAutomaticUpdater.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesclasswpautomaticupdaterphp"></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/class-wp-automatic-updater.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/class-wp-automatic-updater.php        2023-02-25 21:54:03 UTC (rev 55424)
+++ trunk/src/wp-admin/includes/class-wp-automatic-updater.php  2023-02-26 15:17:45 UTC (rev 55425)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -57,6 +57,54 @@
</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">+         * Checks whether access to a given directory is allowed.
+        *
+        * This is used when detecting version control checkouts. Takes into account
+        * the PHP `open_basedir` restrictions, so that WordPress does not try to access
+        * directories it is not allowed to.
+        *
+        * @since 6.2.0
+        *
+        * @param string $dir The directory to check.
+        * @return bool True if access to the directory is allowed, false otherwise.
+        */
+       public function is_allowed_dir( $dir ) {
+               if ( is_string( $dir ) ) {
+                       $dir = trim( $dir );
+               }
+
+               if ( ! is_string( $dir ) || '' === $dir ) {
+                       _doing_it_wrong(
+                               __METHOD__,
+                               sprintf(
+                                       /* translators: %s: The "$dir" argument. */
+                                       __( 'The "%s" argument must be a non-empty string.' ),
+                                       '$dir'
+                               ),
+                               '6.2.0'
+                       );
+
+                       return false;
+               }
+
+               $open_basedir = ini_get( 'open_basedir' );
+
+               if ( empty( $open_basedir ) ) {
+                       return true;
+               }
+
+               $open_basedir_list = explode( PATH_SEPARATOR, $open_basedir );
+
+               foreach ( $open_basedir_list as $basedir ) {
+                       if ( '' !== trim( $basedir ) && str_starts_with( $dir, $basedir ) ) {
+                               return true;
+                       }
+               }
+
+               return false;
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Checks for version control checkouts.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * Checks for Subversion, Git, Mercurial, and Bazaar. It recursively looks up the
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -102,7 +150,11 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Search all directories we've found for evidence of version control.
</span><span class="cx" style="display: block; padding: 0 10px">                foreach ( $vcs_dirs as $vcs_dir ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        foreach ( $check_dirs as $check_dir ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $checkout = @is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( ! $this->is_allowed_dir( $check_dir ) ) {
+                                       continue;
+                               }
+
+                               $checkout = is_dir( rtrim( $check_dir, '\\/' ) . "/$vcs_dir" );
</ins><span class="cx" style="display: block; padding: 0 10px">                                 if ( $checkout ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        break 2;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span></span></pre></div>
<a id="trunktestsphpunittestsadminwpAutomaticUpdaterphp"></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/wpAutomaticUpdater.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/admin/wpAutomaticUpdater.php    2023-02-25 21:54:03 UTC (rev 55424)
+++ trunk/tests/phpunit/tests/admin/wpAutomaticUpdater.php      2023-02-26 15:17:45 UTC (rev 55425)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -572,4 +572,138 @@
</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">+
+       /**
+        * Tests that `WP_Automatic_Updater::is_allowed_dir()` returns true
+        * when the `open_basedir` directive is not set.
+        *
+        * @ticket 42619
+        *
+        * @covers WP_Automatic_Updater::is_allowed_dir
+        */
+       public function test_is_allowed_dir_should_return_true_if_open_basedir_is_not_set() {
+               $this->assertTrue( self::$updater->is_allowed_dir( ABSPATH ) );
+       }
+
+       /**
+        * Tests that `WP_Automatic_Updater::is_allowed_dir()` returns true
+        * when the `open_basedir` directive is set and the path is allowed.
+        *
+        * Runs in a separate process to ensure that `open_basedir` changes
+        * don't impact other tests should an error occur.
+        *
+        * This test does not preserve global state to prevent the exception
+        * "Serialization of 'Closure' is not allowed" when running in
+        * a separate process.
+        *
+        * @ticket 42619
+        *
+        * @covers WP_Automatic_Updater::is_allowed_dir
+        *
+        * @runInSeparateProcess
+        * @preserveGlobalState disabled
+        */
+       public function test_is_allowed_dir_should_return_true_if_open_basedir_is_set_and_path_is_allowed() {
+               // The repository for PHPUnit and test suite resources.
+               $abspath_parent      = trailingslashit( dirname( ABSPATH ) );
+               $abspath_grandparent = trailingslashit( dirname( $abspath_parent ) );
+
+               $open_basedir_backup = ini_get( 'open_basedir' );
+               // Allow access to the directory one level above the repository.
+               ini_set( 'open_basedir', wp_normalize_path( $abspath_grandparent ) );
+
+               // Checking an allowed directory should succeed.
+               $actual = self::$updater->is_allowed_dir( wp_normalize_path( ABSPATH ) );
+
+               ini_set( 'open_basedir', $open_basedir_backup );
+
+               $this->assertTrue( $actual );
+       }
+
+       /**
+        * Tests that `WP_Automatic_Updater::is_allowed_dir()` returns false
+        * when the `open_basedir` directive is set and the path is not allowed.
+        *
+        * Runs in a separate process to ensure that `open_basedir` changes
+        * don't impact other tests should an error occur.
+        *
+        * This test does not preserve global state to prevent the exception
+        * "Serialization of 'Closure' is not allowed" when running in
+        * a separate process.
+        *
+        * @ticket 42619
+        *
+        * @covers WP_Automatic_Updater::is_allowed_dir
+        *
+        * @runInSeparateProcess
+        * @preserveGlobalState disabled
+        */
+       public function test_is_allowed_dir_should_return_false_if_open_basedir_is_set_and_path_is_not_allowed() {
+               // The repository for PHPUnit and test suite resources.
+               $abspath_parent      = trailingslashit( dirname( ABSPATH ) );
+               $abspath_grandparent = trailingslashit( dirname( $abspath_parent ) );
+
+               $open_basedir_backup = ini_get( 'open_basedir' );
+               // Allow access to the directory one level above the repository.
+               ini_set( 'open_basedir', wp_normalize_path( $abspath_grandparent ) );
+
+               // Checking a directory not within the allowed path should trigger an `open_basedir` warning.
+               $actual = self::$updater->is_allowed_dir( '/.git' );
+
+               ini_set( 'open_basedir', $open_basedir_backup );
+
+               $this->assertFalse( $actual );
+       }
+
+       /**
+        * Tests that `WP_Automatic_Updater::is_allowed_dir()` throws `_doing_it_wrong()`
+        * when an invalid `$dir` argument is provided.
+        *
+        * @ticket 42619
+        *
+        * @covers WP_Automatic_Updater::is_allowed_dir
+        *
+        * @expectedIncorrectUsage WP_Automatic_Updater::is_allowed_dir
+        *
+        * @dataProvider data_is_allowed_dir_should_throw_doing_it_wrong_with_invalid_dir
+        *
+        * @param mixed $dir The directory to check.
+        */
+       public function test_is_allowed_dir_should_throw_doing_it_wrong_with_invalid_dir( $dir ) {
+               $this->assertFalse( self::$updater->is_allowed_dir( $dir ) );
+       }
+
+       /**
+        * Data provider.
+        *
+        * @return array[]
+        */
+       public function data_is_allowed_dir_should_throw_doing_it_wrong_with_invalid_dir() {
+               return array(
+                       // Type checks and boolean comparisons.
+                       'null'                              => array( 'dir' => null ),
+                       '(bool) false'                      => array( 'dir' => false ),
+                       '(bool) true'                       => array( 'dir' => true ),
+                       '(int) 0'                           => array( 'dir' => 0 ),
+                       '(int) -0'                          => array( 'dir' => -0 ),
+                       '(int) 1'                           => array( 'dir' => 1 ),
+                       '(int) -1'                          => array( 'dir' => -1 ),
+                       '(float) 0.0'                       => array( 'dir' => 0.0 ),
+                       '(float) -0.0'                      => array( 'dir' => -0.0 ),
+                       '(float) 1.0'                       => array( 'dir' => 1.0 ),
+                       'empty string'                      => array( 'dir' => '' ),
+                       'empty array'                       => array( 'dir' => array() ),
+                       'populated array'                   => array( 'dir' => array( ABSPATH ) ),
+                       'empty object'                      => array( 'dir' => new stdClass() ),
+                       'populated object'                  => array( 'dir' => (object) array( ABSPATH ) ),
+                       'INF'                               => array( 'dir' => INF ),
+                       'NAN'                               => array( 'dir' => NAN ),
+
+                       // Ensures that `trim()` has been called.
+                       'string with only spaces'           => array( 'dir' => '   ' ),
+                       'string with only tabs'             => array( 'dir' => "\t\t" ),
+                       'string with only newlines'         => array( 'dir' => "\n\n" ),
+                       'string with only carriage returns' => array( 'dir' => "\r\r" ),
+               );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>