<!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>[56186] trunk: Filesystem API: Ensure `wp_tempnam()` does not produce file names longer than 255 characters as this is the limit on most filesystems.</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/56186">56186</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/56186","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>azaozz</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-07-10 20:31:35 +0000 (Mon, 10 Jul 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'>Filesystem API: Ensure `wp_tempnam()` does not produce file names longer than 255 characters as this is the limit on most filesystems.

Props: costdev, doems, mikeschroder, oglekler, mrinal013.
Fixes: <a href="https://core.trac.wordpress.org/ticket/35755">#35755</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesfilephp">trunk/src/wp-admin/includes/file.php</a></li>
<li><a href="#trunktestsphpunittestsfilephp">trunk/tests/phpunit/tests/file.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      2023-07-10 19:15:36 UTC (rev 56185)
+++ trunk/src/wp-admin/includes/file.php        2023-07-10 20:31:35 UTC (rev 56186)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -689,8 +689,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">        // Suffix some random data to avoid filename conflicts.
</span><span class="cx" style="display: block; padding: 0 10px">        $temp_filename .= '-' . wp_generate_password( 6, false );
</span><span class="cx" style="display: block; padding: 0 10px">        $temp_filename .= '.tmp';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $temp_filename  = $dir . wp_unique_filename( $dir, $temp_filename );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $temp_filename  = wp_unique_filename( $dir, $temp_filename );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /*
+        * Filesystems typically have a limit of 255 characters for a filename.
+        *
+        * If the generated unique filename exceeds this, truncate the initial
+        * filename and try again.
+        *
+        * As it's possible that the truncated filename may exist, producing a
+        * suffix of "-1" or "-10" which could exceed the limit again, truncate
+        * it to 252 instead.
+        */
+       $characters_over_limit = strlen( $temp_filename ) - 252;
+       if ( $characters_over_limit > 0 ) {
+               $filename = substr( $filename, 0, -$characters_over_limit );
+               return wp_tempnam( $filename, $dir );
+       }
+
+       $temp_filename = $dir . $temp_filename;
+
</ins><span class="cx" style="display: block; padding: 0 10px">         $fp = @fopen( $temp_filename, 'x' );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! $fp && is_writable( $dir ) && file_exists( $temp_filename ) ) {
</span></span></pre></div>
<a id="trunktestsphpunittestsfilephp"></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/file.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/file.php        2023-07-10 19:15:36 UTC (rev 56185)
+++ trunk/tests/phpunit/tests/file.php  2023-07-10 20:31:35 UTC (rev 56186)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -206,6 +206,199 @@
</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_tempnam()` limits the filename's length to 252 characters.
+        *
+        * @ticket 35755
+        *
+        * @covers ::wp_tempnam
+        *
+        * @dataProvider data_wp_tempnam_should_limit_filename_length_to_252_characters
+        */
+       public function test_wp_tempnam_should_limit_filename_length_to_252_characters( $filename ) {
+               $file = wp_tempnam( $filename );
+
+               if ( file_exists( $file ) ) {
+                       self::unlink( $file );
+               }
+
+               $this->assertLessThanOrEqual( 252, strlen( basename( $file ) ) );
+       }
+
+       /**
+        * Data provider.
+        *
+        * @return array[]
+        */
+       public function data_wp_tempnam_should_limit_filename_length_to_252_characters() {
+               return array(
+                       'the limit before adding characters for uniqueness' => array( 'filename' => str_pad( '', 241, 'filename' ) ),
+                       'one more than the limit before adding characters for uniqueness' => array( 'filename' => str_pad( '', 242, 'filename' ) ),
+                       '251 characters' => array( 'filename' => str_pad( '', 251, 'filename' ) ),
+                       '252 characters' => array( 'filename' => str_pad( '', 252, 'filename' ) ),
+                       '253 characters' => array( 'filename' => str_pad( '', 253, 'filename' ) ),
+               );
+       }
+
+       /**
+        * Tests that `wp_tempnam()` limits the filename's length to 252 characters
+        * when there is a name conflict.
+        *
+        * @ticket 35755
+        *
+        * @covers ::wp_tempnam
+        */
+       public function test_wp_tempnam_should_limit_filename_length_to_252_characters_with_name_conflict() {
+               // Create a conflict by removing the randomness of the generated password.
+               add_filter(
+                       'random_password',
+                       static function() {
+                               return '123456';
+                       },
+                       10,
+                       0
+               );
+
+               // A filename at the limit.
+               $filename = str_pad( '', 252, 'filename' );
+
+               // Create the initial file.
+               $existing_file = wp_tempnam( $filename );
+
+               // Try creating a file with the same name.
+               $actual = wp_tempnam( basename( $existing_file ) );
+
+               self::unlink( $existing_file );
+               self::unlink( $actual );
+
+               $this->assertLessThanOrEqual( 252, strlen( basename( $actual ) ) );
+       }
+
+       /**
+        * Tests that `wp_tempnam()` limits the filename's length to 252 characters
+        * when a 'random_password' filter returns passwords longer than 6 characters.
+        *
+        * @ticket 35755
+        *
+        * @covers ::wp_tempnam
+        */
+       public function test_wp_tempnam_should_limit_filename_length_to_252_characters_when_random_password_is_filtered() {
+               // Force random passwords to 12 characters.
+               add_filter(
+                       'random_password',
+                       static function() {
+                               return '1a2b3c4d5e6f';
+                       },
+                       10,
+                       0
+               );
+
+               // A filename at the limit.
+               $filename = str_pad( '', 252, 'filename' );
+               $actual   = wp_tempnam( $filename );
+
+               self::unlink( $actual );
+
+               $this->assertLessThanOrEqual( 252, strlen( basename( $actual ) ) );
+       }
+
+       /**
+        * Tests that `wp_tempnam()` limits the filename's length to 252 characters
+        * when a 'wp_unique_filename' filter returns a filename longer than 252 characters.
+        *
+        * @ticket 35755
+        *
+        * @covers ::wp_tempnam
+        */
+       public function test_wp_tempnam_should_limit_filename_length_to_252_characters_when_wp_unique_filename_is_filtered() {
+               // Determine the number of additional characters added by `wp_tempnam()`.
+               $temp_dir                    = get_temp_dir();
+               $additional_chars_filename   = wp_unique_filename( $temp_dir, 'filename' );
+               $additional_chars_generated  = wp_tempnam( $additional_chars_filename, $temp_dir );
+               $additional_chars_difference = strlen( basename( $additional_chars_generated ) ) - strlen( $additional_chars_filename );
+
+               $filenames_over_limit = 0;
+
+               // Make the filter send the filename over the limit.
+               add_filter(
+                       'wp_unique_filename',
+                       static function( $filename ) use ( &$filenames_over_limit ) {
+                               if ( strlen( $filename ) === 252 ) {
+                                       $filename .= '1';
+                                       ++$filenames_over_limit;
+                               }
+
+                               return $filename;
+                       },
+                       10,
+                       1
+               );
+
+               // A filename that will hit the limit when `wp_tempnam()` adds characters.
+               $filename = str_pad( '', 252 - $additional_chars_difference, 'filename' );
+               $actual   = wp_tempnam( $filename );
+
+               self::unlink( $additional_chars_generated );
+               self::unlink( $actual );
+
+               $this->assertLessThanOrEqual( 252, strlen( basename( $actual ) ), 'The final filename was over the limit.' );
+               $this->assertSame( 1, $filenames_over_limit, 'One filename should have been over the limit.' );
+       }
+
+       /**
+        * Tests that `wp_tempnam()` limits the filename's length to 252 characters
+        * when both a 'random_password' filter and a 'wp_unique_filename' filter
+        * cause the filename to be greater than 252 characters.
+        *
+        * @ticket 35755
+        *
+        * @covers ::wp_tempnam
+        */
+       public function test_wp_tempnam_should_limit_filename_length_to_252_characters_when_random_password_and_wp_unique_filename_are_filtered() {
+               // Force random passwords to 12 characters.
+               add_filter(
+                       'random_password',
+                       static function() {
+                               return '1a2b3c4d5e6f';
+                       },
+                       10,
+                       0
+               );
+
+               // Determine the number of additional characters added by `wp_tempnam()`.
+               $temp_dir                    = get_temp_dir();
+               $additional_chars_filename   = wp_unique_filename( $temp_dir, 'filename' );
+               $additional_chars_generated  = wp_tempnam( $additional_chars_filename, $temp_dir );
+               $additional_chars_difference = strlen( basename( $additional_chars_generated ) ) - strlen( $additional_chars_filename );
+
+               $filenames_over_limit = 0;
+
+               // Make the filter send the filename over the limit.
+               add_filter(
+                       'wp_unique_filename',
+                       static function( $filename ) use ( &$filenames_over_limit ) {
+                               if ( strlen( $filename ) === 252 ) {
+                                       $filename .= '1';
+                                       ++$filenames_over_limit;
+                               }
+
+                               return $filename;
+                       },
+                       10,
+                       1
+               );
+
+               // A filename that will hit the limit when `wp_tempnam()` adds characters.
+               $filename = str_pad( '', 252 - $additional_chars_difference, 'filename' );
+               $actual   = wp_tempnam( $filename );
+
+               self::unlink( $additional_chars_generated );
+               self::unlink( $actual );
+
+               $this->assertLessThanOrEqual( 252, strlen( basename( $actual ) ), 'The final filename was over the limit.' );
+               $this->assertSame( 1, $filenames_over_limit, 'One filename should have been over the limit.' );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * @ticket 47186
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_file_signature_functions_as_expected() {
</span></span></pre>
</div>
</div>

</body>
</html>