From 4c7be83c5f9a6d2e5434794831711cfa9d421dcb Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 5 Sep 2024 11:31:06 +0200 Subject: [PATCH] [HttpFoundation] Update links for X-Accel-Redirect and fail properly when X-Accel-Mapping is missing --- BinaryFileResponse.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/BinaryFileResponse.php b/BinaryFileResponse.php index 1878caae1..ccfd6389a 100644 --- a/BinaryFileResponse.php +++ b/BinaryFileResponse.php @@ -244,8 +244,12 @@ public function prepare(Request $request) } if ('x-accel-redirect' === strtolower($type)) { // Do X-Accel-Mapping substitutions. - // @link https://www.nginx.com/resources/wiki/start/topics/examples/x-accel/#x-accel-redirect - $parts = HeaderUtils::split($request->headers->get('X-Accel-Mapping', ''), ',='); + // @link https://github.com/rack/rack/blob/main/lib/rack/sendfile.rb + // @link https://mattbrictson.com/blog/accelerated-rails-downloads + if (!$request->headers->has('X-Accel-Mapping')) { + throw new \LogicException('The "X-Accel-Mapping" header must be set when "X-Sendfile-Type" is set to "X-Accel-Redirect".'); + } + $parts = HeaderUtils::split($request->headers->get('X-Accel-Mapping'), ',='); foreach ($parts as $part) { [$pathPrefix, $location] = $part; if (substr($path, 0, \strlen($pathPrefix)) === $pathPrefix) {