Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Commit 428e5d1

Browse files
author
Jeroen van Meeuwen
committed
Attempt to fix directory traversal
1 parent 87aebd3 commit 428e5d1

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

config/swoole_http.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
'server' => [
1313
'host' => env('SWOOLE_HTTP_HOST', '127.0.0.1'),
1414
'port' => env('SWOOLE_HTTP_PORT', '1215'),
15-
'public_path' => base_path('public'),
15+
'document_root' => base_path('public'),
1616
// Determine if to use swoole to respond request for static files
1717
'handle_static_files' => env('SWOOLE_HANDLE_STATIC', true),
1818
'access_log' => env('SWOOLE_HTTP_ACCESS_LOG', false),

src/Server/Manager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,11 @@ public function onRequest($swooleRequest, $swooleResponse)
205205
$this->resetOnRequest();
206206
$sandbox = $this->app->make(Sandbox::class);
207207
$handleStatic = $this->container->make('config')->get('swoole_http.server.handle_static_files', true);
208-
$publicPath = $this->container->make('config')->get('swoole_http.server.public_path', base_path('public'));
208+
$documentRoot = $this->container->make('config')->get('swoole_http.server.document_root', base_path('public'));
209209

210210
try {
211211
// handle static file request first
212-
if ($handleStatic && Request::handleStatic($swooleRequest, $swooleResponse, $publicPath)) {
212+
if ($handleStatic && Request::handleStatic($swooleRequest, $swooleResponse, $documentRoot)) {
213213
return;
214214
}
215215
// transform swoole request to illuminate request

src/Transformers/Request.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,20 +175,28 @@ protected static function transformServerParameters(array $server, array $header
175175
*
176176
* @param \Swoole\Http\Request $swooleRequest
177177
* @param \Swoole\Http\Response $swooleResponse
178-
* @param string $publicPath
178+
* @param string $documentRoot
179179
*
180180
* @return boolean
181181
*/
182-
public static function handleStatic($swooleRequest, $swooleResponse, string $publicPath)
182+
public static function handleStatic($swooleRequest, $swooleResponse, string $documentRoot)
183183
{
184184
$uri = $swooleRequest->server['request_uri'] ?? '';
185185
$extension = strtok(pathinfo($uri, PATHINFO_EXTENSION), '?');
186-
$fileName = $publicPath . $uri;
186+
$fileName = @realpath($documentRoot . $uri);
187+
188+
if (!$fileName) {
189+
return false;
190+
}
187191

188192
if ($extension && in_array($extension, static::EXTENSION_BLACKLIST)) {
189193
return false;
190194
}
191195

196+
if (substr($fileName, 0, strlen($documentRoot)) != $documentRoot) {
197+
return false;
198+
}
199+
192200
if (! is_file($fileName) || ! filesize($fileName)) {
193201
return false;
194202
}

tests/Server/ManagerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ManagerTest extends TestCase
6161
'swoole_http.tables' => [],
6262
'swoole_http.providers' => [],
6363
'swoole_http.resetters' => [],
64-
'swoole_http.server.public_path' => '/',
64+
'swoole_http.server.document_root' => '/',
6565
];
6666

6767
public function testGetFramework()

0 commit comments

Comments
 (0)