@@ -141,26 +141,36 @@ static function ( $buffer ) {
141141// Normalize slashes for file operations
142142$ wpcli_server_file = str_replace ( array ( '/ ' , '\\' ), DIRECTORY_SEPARATOR , $ wpcli_server_file );
143143
144- if ( file_exists ( $ wpcli_server_file ) ) {
145- if ( is_dir ( $ wpcli_server_file ) && substr ( $ wpcli_server_path , -1 ) !== '/ ' ) {
146- header ( "Location: $ wpcli_server_path/ " );
144+ $ wpcli_server_real_root = realpath ( $ wpcli_server_root );
145+ $ wpcli_server_real_file = file_exists ( $ wpcli_server_file ) ? realpath ( $ wpcli_server_file ) : false ;
146+
147+ $ wpcli_server_is_inside_root = false ;
148+ if ( false !== $ wpcli_server_real_root && false !== $ wpcli_server_real_file ) {
149+ if ( $ wpcli_server_real_file === $ wpcli_server_real_root || 0 === strpos ( $ wpcli_server_real_file , rtrim ( $ wpcli_server_real_root , DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR ) ) {
150+ $ wpcli_server_is_inside_root = true ;
151+ }
152+ }
153+
154+ if ( $ wpcli_server_is_inside_root ) {
155+ if ( is_dir ( $ wpcli_server_real_file ) && substr ( $ wpcli_server_path , -1 ) !== '/ ' ) {
156+ header ( 'Location: ' . str_replace ( array ( "\r" , "\n" ), '' , $ wpcli_server_path ) . '/ ' );
147157 exit ;
148158 }
149159
150160 // Check if this is a PHP file by examining the extension
151- if ( pathinfo ( $ wpcli_server_file , PATHINFO_EXTENSION ) === ' php ' ) {
161+ if ( ' php ' === strtolower ( pathinfo ( $ wpcli_server_real_file , PATHINFO_EXTENSION ) ) ) {
152162 // Set $_SERVER variables to mimic direct access to the PHP file
153163 $ _SERVER ['SCRIPT_NAME ' ] = $ wpcli_server_path ;
154164 $ _SERVER ['PHP_SELF ' ] = $ wpcli_server_path ;
155- $ _SERVER ['SCRIPT_FILENAME ' ] = $ wpcli_server_file ;
165+ $ _SERVER ['SCRIPT_FILENAME ' ] = $ wpcli_server_real_file ;
156166
157- chdir ( dirname ( $ wpcli_server_file ) );
158- require_once $ wpcli_server_file ;
167+ chdir ( dirname ( $ wpcli_server_real_file ) );
168+ require_once $ wpcli_server_real_file ;
159169 } else {
160170 return false ;
161171 }
162172} else {
163- // File doesn't exist - route to index.php for pretty permalinks
173+ // File doesn't exist or is outside document root - route to index.php for pretty permalinks
164174 $ _SERVER ['SCRIPT_NAME ' ] = '/index.php ' ;
165175 $ _SERVER ['PHP_SELF ' ] = '/index.php ' ;
166176 $ _SERVER ['SCRIPT_FILENAME ' ] = $ wpcli_server_root . DIRECTORY_SEPARATOR . 'index.php ' ;
0 commit comments