Skip to content

Commit 7544bb4

Browse files
author
Andrew Welch
committed
The download files action now strips any relative paths from the incoming request & restricts files to allowedFileExtensions
1 parent 389d861 commit 7544bb4

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/controllers/DefaultController.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
use nystudio107\transcoder\Transcoder;
1414

1515
use Craft;
16-
use craft\web\Controller;
16+
use craft\errors\AssetDisallowedExtensionException;
1717
use craft\helpers\Json;
18+
use craft\web\Controller;
1819

1920
/**
2021
* @author nystudio107
@@ -63,6 +64,14 @@ public function beforeAction($action)
6364
public function actionDownloadFile($url)
6465
{
6566
$filePath = parse_url($url, PHP_URL_PATH);
67+
// Remove any relative paths
68+
$filePath = preg_replace('/\.\.\/+/', '', $filePath);
69+
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
70+
$allowedExtensions = Craft::$app->getConfig()->getGeneral()->allowedFileExtensions;
71+
if (!in_array($extension, $allowedExtensions, true)) {
72+
throw new AssetDisallowedExtensionException("File “{$filePath}” cannot be downloaded because “{$extension}” is not allowed.");
73+
}
74+
6675
$filePath = $_SERVER['DOCUMENT_ROOT'].$filePath;
6776
Craft::$app->getResponse()->sendFile(
6877
$filePath,

0 commit comments

Comments
 (0)