Skip to content

Commit 5529e9e

Browse files
swissspidyCopilot
andauthored
Harden router.php against path traversal and non-PHP execution (#102)
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent c80dbd7 commit 5529e9e

2 files changed

Lines changed: 54 additions & 8 deletions

File tree

‎features/server.feature‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,39 @@ Feature: Serve WordPress locally
7373
"""
7474
https://localhost:8184
7575
"""
76+
77+
Scenario: Prevent path traversal outside document root
78+
Given a WP install
79+
And I launch in the background `wp server --host=localhost --port=8186`
80+
81+
When I run `curl -sSL --path-as-is http://localhost:8186/%2e%2e/`
82+
Then STDOUT should contain:
83+
"""
84+
WP CLI Site
85+
"""
86+
87+
When I run `curl -sSL --path-as-is http://localhost:8186/..%2f`
88+
Then STDOUT should contain:
89+
"""
90+
WP CLI Site
91+
"""
92+
93+
Scenario: Prevent execution of non-PHP extensions
94+
Given a WP install
95+
And a wp-content/uploads/evil.php_.gif file:
96+
"""
97+
GIF89a;
98+
<?php echo 1337 + 1; ?>
99+
"""
100+
And I launch in the background `wp server --host=localhost --port=8187`
101+
102+
When I run `curl -sS http://localhost:8187/wp-content/uploads/evil.php_.gif`
103+
Then STDOUT should contain:
104+
"""
105+
GIF89a;
106+
<?php echo 1337 + 1; ?>
107+
"""
108+
And STDOUT should not contain:
109+
"""
110+
1338
111+
"""

‎router.php‎

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)