Skip to content

Commit

Permalink
fix: prevent traversal outside configured directory (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
zackad authored Jun 10, 2024
1 parent c72fc90 commit 5df601c
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Controller/ManagerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
Expand Down Expand Up @@ -275,8 +277,16 @@ public function uploadFileAction(Request $request): JsonResponse|Response {
#[Route("/file/{fileName}", name: 'file_manager_file')]
public function binaryFileResponseAction(Request $request, string $fileName): BinaryFileResponse {
$fileManager = $this->newFileManager($request->query->all());
$configuredDirectory = $fileManager->getConfiguration()['dir'];

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

return new BinaryFileResponse($file);
Expand Down

0 comments on commit 5df601c

Please sign in to comment.