Skip to content

Commit

Permalink
[Bug] createCSV in CustomReportsController (#14498)
Browse files Browse the repository at this point in the history
* Check for csv file extension

* Added separate function for csv filename
  • Loading branch information
mcop1 committed Mar 1, 2023
1 parent 42b9d43 commit 7f788fa
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions bundles/AdminBundle/Controller/Reports/CustomReportController.php
Expand Up @@ -24,6 +24,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\InvalidArgumentException;

/**
* @Route("/custom-report")
Expand Down Expand Up @@ -410,6 +411,14 @@ public function chartAction(Request $request)
]);
}

protected function getTemporaryFileFromFileName(string $exportFileName): string {
$exportFileName = basename($exportFileName);
if(!str_ends_with($exportFileName, ".csv")) {
throw new InvalidArgumentException($exportFileName . " is not a valid csv file.");
}
return PIMCORE_SYSTEM_TEMP_DIRECTORY . '/' . $exportFileName;
}

/**
* @Route("/create-csv", name="pimcore_admin_reports_customreport_createcsv", methods={"GET"})
*
Expand Down Expand Up @@ -459,7 +468,7 @@ public function createCsvAction(Request $request)
$exportFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . '/report-export-' . uniqid() . '.csv';
@unlink($exportFile);
} else {
$exportFile = PIMCORE_SYSTEM_TEMP_DIRECTORY.'/'.$exportFile;
$exportFile = $this->getTemporaryFileFromFileName($exportFile);
}

$fp = fopen($exportFile, 'a');
Expand Down Expand Up @@ -497,7 +506,7 @@ public function downloadCsvAction(Request $request)
{
$this->checkPermission('reports');
if ($exportFile = $request->get('exportFile')) {
$exportFile = PIMCORE_SYSTEM_TEMP_DIRECTORY . '/' . basename($exportFile);
$exportFile = $this->getTemporaryFileFromFileName($exportFile);
$response = new BinaryFileResponse($exportFile);
$response->headers->set('Content-Type', 'text/csv; charset=UTF-8');
$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'export.csv');
Expand Down

0 comments on commit 7f788fa

Please sign in to comment.