From da192ee70e5cb92fff8873643a0140d13bca66e1 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Wed, 19 Apr 2023 16:32:29 -0700 Subject: [PATCH] download: fix invalid destructuring in range (fix #590) Signed-off-by: Varun Patil --- lib/Controller/DownloadController.php | 6 +++--- lib/Util.php | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/Controller/DownloadController.php b/lib/Controller/DownloadController.php index b4e939e8..9d69886a 100644 --- a/lib/Controller/DownloadController.php +++ b/lib/Controller/DownloadController.php @@ -133,10 +133,10 @@ class DownloadController extends GenericApiController // check if http_range is sent by browser $range = $this->request->getHeader('Range'); if (!empty($range)) { - [$sizeUnit, $rangeOrig] = explode('=', $range, 2); + [$sizeUnit, $rangeOrig] = Util::explode_exact('=', $range, 2); if ('bytes' === $sizeUnit) { // http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt - [$range, $extra] = explode(',', $rangeOrig, 2); + [$range, $extra] = Util::explode_exact(',', $rangeOrig, 2); } } @@ -147,7 +147,7 @@ class DownloadController extends GenericApiController // Get file reading parameters $size = $file->getSize(); - [$seekStart, $seekEnd] = explode('-', $range, 2); + [$seekStart, $seekEnd] = Util::explode_exact('-', $range, 2); $seekEnd = (empty($seekEnd)) ? ($size - 1) : min(abs((int) $seekEnd), $size - 1); $seekStart = (empty($seekStart) || $seekEnd < abs((int) $seekStart)) ? 0 : max(abs((int) $seekStart), 0); diff --git a/lib/Util.php b/lib/Util.php index 18276121..4886f5d9 100644 --- a/lib/Util.php +++ b/lib/Util.php @@ -304,6 +304,20 @@ class Util return mb_ereg_replace('\/\/+', '/', $path); // remove extra slashes } + /** + * Explode a string into fixed number of components. + * + * @param string $delimiter Delimiter + * @param string $string String to explode + * @param int $count Number of components + * + * @return string[] Array of components + */ + public static function explode_exact(string $delimiter, string $string, int $count): array + { + return array_pad(explode($delimiter, $string, $count), $count, ''); + } + /** * Get a system config key with the correct default. *