Skip to content

Commit 0af8a8e

Browse files
author
Andrew Welch
committed
Use PathHelper::ensurePathIsContained
1 parent 6529a67 commit 0af8a8e

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/controllers/DefaultController.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
use Craft;
1616
use craft\errors\AssetDisallowedExtensionException;
17-
use craft\helpers\Json;
17+
use craft\helpers\Json as JsonHelper;
18+
use craft\helpers\Path as PathHelper;
1819
use craft\web\Controller;
20+
use yii\web\BadRequestHttpException;
1921

2022
/**
2123
* @author nystudio107
@@ -65,7 +67,10 @@ public function actionDownloadFile($url)
6567
{
6668
$filePath = parse_url($url, PHP_URL_PATH);
6769
// Remove any relative paths
68-
$filePath = preg_replace('/\.\.\/+/', '', $filePath);
70+
if (!PathHelper::ensurePathIsContained($filePath)) {
71+
throw new BadRequestHttpException('Invalid resource path: ' . $filePath);
72+
}
73+
// Only work for `allowedFileExtensions` file extensions
6974
$extension = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
7075
$allowedExtensions = Craft::$app->getConfig()->getGeneral()->allowedFileExtensions;
7176
if (!in_array($extension, $allowedExtensions, true)) {
@@ -164,6 +169,6 @@ public function actionProgress($filename)
164169
}
165170
}
166171

167-
return Json::encode($result);
172+
return JsonHelper::encode($result);
168173
}
169174
}

0 commit comments

Comments
 (0)