Skip to content

Commit 416e814

Browse files
committed
[HttpFoundation] Fix: Encode path in X-Accel-Redirect header
we need to encode the path in X-Accel-Redirect header, otherwise nginx fail when certain characters are present in it (like % or ?) rack/rack#1306
1 parent 6b4f603 commit 416e814

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Symfony/Component/HttpFoundation/BinaryFileResponse.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ public function prepare(Request $request): static
229229
$path = $location.substr($path, \strlen($pathPrefix));
230230
// Only set X-Accel-Redirect header if a valid URI can be produced
231231
// as nginx does not serve arbitrary file paths.
232-
$this->headers->set($type, $path);
232+
// We need to encode the path because nginx fail with 404 if certain
233+
// characters are present in the path.
234+
$this->headers->set($type, rawurlencode($path));
233235
$this->maxlen = 0;
234236
break;
235237
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,15 @@ public function testXAccelMapping($realpath, $mapping, $virtual)
314314
$property->setValue($response, $file);
315315

316316
$response->prepare($request);
317-
$this->assertEquals($virtual, $response->headers->get('X-Accel-Redirect'));
317+
$header = $response->headers->get('X-Accel-Redirect');
318+
319+
if ($header) {
320+
// Making sure the path doesn't contain characters unsupported by nginx
321+
$this->assertMatchesRegularExpression('/^([^?%]|%[0-9A-F]{2})*$/', $header);
322+
$header = rawurldecode($header);
323+
}
324+
325+
$this->assertEquals($virtual, $header);
318326
}
319327

320328
public function testDeleteFileAfterSend()
@@ -361,6 +369,7 @@ public static function getSampleXAccelMappings()
361369
['/home/Foo/bar.txt', '/var/www/=/files/,/home/Foo/=/baz/', '/baz/bar.txt'],
362370
['/home/Foo/bar.txt', '"/var/www/"="/files/", "/home/Foo/"="/baz/"', '/baz/bar.txt'],
363371
['/tmp/bar.txt', '"/var/www/"="/files/", "/home/Foo/"="/baz/"', null],
372+
['/var/www/var/www/files/foo%.txt', '/var/www/=/files/', '/files/var/www/files/foo%.txt'],
364373
];
365374
}
366375

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy