Skip to content

Commit 5df601c

Browse files
authored
fix: prevent traversal outside configured directory (#144)
1 parent c72fc90 commit 5df601c

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

Controller/ManagerController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@
2525
use Symfony\Component\Form\FormFactoryInterface;
2626
use Symfony\Component\Form\FormInterface;
2727
use Symfony\Component\HttpFoundation\BinaryFileResponse;
28+
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
2829
use Symfony\Component\HttpFoundation\JsonResponse;
2930
use Symfony\Component\HttpFoundation\RedirectResponse;
3031
use Symfony\Component\HttpFoundation\Request;
3132
use Symfony\Component\HttpFoundation\Response;
33+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
3234
use Symfony\Component\Routing\Annotation\Route;
3335
use Symfony\Component\Routing\RouterInterface;
3436
use Symfony\Component\Validator\Constraints\NotBlank;
@@ -275,8 +277,16 @@ public function uploadFileAction(Request $request): JsonResponse|Response {
275277
#[Route("/file/{fileName}", name: 'file_manager_file')]
276278
public function binaryFileResponseAction(Request $request, string $fileName): BinaryFileResponse {
277279
$fileManager = $this->newFileManager($request->query->all());
280+
$configuredDirectory = $fileManager->getConfiguration()['dir'];
278281

279282
$file = $fileManager->getCurrentPath().\DIRECTORY_SEPARATOR.urldecode($fileName);
283+
$realFilePath = realpath($file);
284+
if (false === $realFilePath) {
285+
throw new FileNotFoundException($file);
286+
}
287+
if (!str_starts_with($realFilePath, realpath($configuredDirectory))) {
288+
throw new BadRequestHttpException('Accessing outside configured directory is not allowed.');
289+
}
280290
$this->dispatch(FileManagerEvents::FILE_ACCESS, ['path' => $file]);
281291

282292
return new BinaryFileResponse($file);

0 commit comments

Comments
 (0)