|
25 | 25 | use Symfony\Component\Form\FormFactoryInterface; |
26 | 26 | use Symfony\Component\Form\FormInterface; |
27 | 27 | use Symfony\Component\HttpFoundation\BinaryFileResponse; |
| 28 | +use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException; |
28 | 29 | use Symfony\Component\HttpFoundation\JsonResponse; |
29 | 30 | use Symfony\Component\HttpFoundation\RedirectResponse; |
30 | 31 | use Symfony\Component\HttpFoundation\Request; |
31 | 32 | use Symfony\Component\HttpFoundation\Response; |
| 33 | +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
32 | 34 | use Symfony\Component\Routing\Annotation\Route; |
33 | 35 | use Symfony\Component\Routing\RouterInterface; |
34 | 36 | use Symfony\Component\Validator\Constraints\NotBlank; |
@@ -275,8 +277,16 @@ public function uploadFileAction(Request $request): JsonResponse|Response { |
275 | 277 | #[Route("/file/{fileName}", name: 'file_manager_file')] |
276 | 278 | public function binaryFileResponseAction(Request $request, string $fileName): BinaryFileResponse { |
277 | 279 | $fileManager = $this->newFileManager($request->query->all()); |
| 280 | + $configuredDirectory = $fileManager->getConfiguration()['dir']; |
278 | 281 |
|
279 | 282 | $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 | + } |
280 | 290 | $this->dispatch(FileManagerEvents::FILE_ACCESS, ['path' => $file]); |
281 | 291 |
|
282 | 292 | return new BinaryFileResponse($file); |
|
0 commit comments