8000 [HttpFoundation] Do not set X-Accel-Redirect for paths outside of X-A… · symfony/symfony@a662f61 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit a662f61

Browse files
committed
[HttpFoundation] Do not set X-Accel-Redirect for paths outside of X-Accel-Mapping
Currently BinaryFileResponse, when configured with X-Accel-Redirect sendfile type, will only substitute file paths specified in X-Accel-Mapping. But if the provided file path does not have a defined prefix, then the resulting header will include the absolute path. Nginx expects a valid URI, therefore this will result in an issue that is very hard to detect and debug as it will not show up in error logs and instead the request would just hang for some time and then be re-served without query parameters(?).
1 parent a26c6d3 commit a662f61

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,18 @@ public function prepare(Request $request)
227227

228228
if (substr($path, 0, \strlen($pathPrefix)) === $pathPrefix) {
229229
$path = $location.substr($path, \strlen($pathPrefix));
230+
// Only set X-Accel-Redirect header if a valid URI can be produced
231+
// as nginx does not serve arbitrary file paths.
232+
$this->headers->set($type, $path);
233+
$this->maxlen = 0;
230234
break;
231235
}
232236
}
233237
}
238+
} else {
239+
$this->headers->set($type, $path);
240+
$this->maxlen = 0;
234241
}
235-
$this->headers->set($type, $path);
236-
$this->maxlen = 0;
237242
} elseif ($request->headers->has('Range')) {
238243
// Process the range headers.
239244
if (!$request->headers->has('If-Range') || $this->hasValidIfRangeHeader($request->headers->get('If-Range'))) {

src/Symfony/Component/HttpFoundation/Tests/BinaryFileResponseTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,7 @@ public function getSampleXAccelMappings()
338338
return [
339339
['/var/www/var/www/files/foo.txt', '/var/www/=/files/', '/files/var/www/files/foo.txt'],
340340
['/home/foo/bar.txt', '/var/www/=/files/,/home/foo/=/baz/', '/baz/bar.txt'],
341+
['/tmp/bar.txt', '"/var/www/"="/files/", "/home/Foo/"="/baz/"', null],
341342
];
342343
}
343344

0 commit comments

Comments
 (0)
0