Skip to content

Commit

Permalink
security #cve-2020-5255 [HttpFoundation] Do not set the default Conte…
Browse files Browse the repository at this point in the history
…nt-Type based on the Accept header (yceruto)

This PR was merged into the 4.4 branch.
  • Loading branch information
nicolas-grekas committed Mar 30, 2020
2 parents c935e4a + 0050a4d commit dca3434
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\ErrorHandler\ErrorRenderer;

use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
use Symfony\Component\Serializer\SerializerInterface;
Expand All @@ -30,6 +31,7 @@ class SerializerErrorRenderer implements ErrorRendererInterface

/**
* @param string|callable(FlattenException) $format The format as a string or a callable that should return it
* formats not supported by Request::getMimeTypes() should be given as mime types
* @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
*/
public function __construct(SerializerInterface $serializer, $format, ErrorRendererInterface $fallbackErrorRenderer = null, $debug = false)
Expand Down Expand Up @@ -57,11 +59,16 @@ public function render(\Throwable $exception): FlattenException

try {
$format = \is_string($this->format) ? $this->format : ($this->format)($flattenException);
$headers = [
'Content-Type' => Request::getMimeTypes($format)[0] ?? $format,
'Vary' => 'Accept',
];

return $flattenException->setAsString($this->serializer->serialize($flattenException, $format, [
'exception' => $exception,
'debug' => \is_bool($this->debug) ? $this->debug : ($this->debug)($exception),
]));
]))
->setHeaders($flattenException->getHeaders() + $headers);
} catch (NotEncodableValueException $e) {
return $this->fallbackErrorRenderer->render($exception);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/HttpFoundation/Request.php
Expand Up @@ -1590,7 +1590,9 @@ public function isNoCache()
* Gets the preferred format for the response by inspecting, in the following order:
* * the request format set using setRequestFormat
* * the values of the Accept HTTP header
* * the content type of the body of the request.
*
* Note that if you use this method, you should send the "Vary: Accept" header
* in the response to prevent any issues with intermediary HTTP caches.
*/
public function getPreferredFormat(?string $default = 'html'): ?string
{
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpFoundation/Response.php
Expand Up @@ -275,7 +275,7 @@ public function prepare(Request $request)
} else {
// Content-type based on the Request
if (!$headers->has('Content-Type')) {
$format = $request->getPreferredFormat(null);
$format = $request->getRequestFormat(null);
if (null !== $format && $mimeType = $request->getMimeType($format)) {
$headers->set('Content-Type', $mimeType);
}
Expand Down
15 changes: 14 additions & 1 deletion src/Symfony/Component/HttpFoundation/Tests/ResponseTest.php
Expand Up @@ -497,12 +497,25 @@ public function testPrepareDoesNothingIfRequestFormatIsNotDefined()
$this->assertEquals('text/html; charset=UTF-8', $response->headers->get('content-type'));
}

/**
* Same URL cannot produce different Content-Type based on the value of the Accept header,
* unless explicitly stated in the response object.
*/
public function testPrepareDoesNotSetContentTypeBasedOnRequestAcceptHeader()
{
$response = new Response('foo');
$request = Request::create('/');
$request->headers->set('Accept', 'application/json');
$response->prepare($request);

$this->assertSame('text/html; charset=UTF-8', $response->headers->get('content-type'));
}

public function testPrepareSetContentType()
{
$response = new Response('foo');
$request = Request::create('/');
$request->setRequestFormat('json');
$request->headers->remove('accept');

$response->prepare($request);

Expand Down

6 comments on commit dca3434

@ayenbonsoa
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upgrade symfony/http-foundation to version 5.0.7 or later

@glensc
Copy link
Contributor

@glensc glensc commented on dca3434 Mar 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saedyousef
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upgrade symfony/http-foundation to version 5.0.7 or later

Could you explain how to do it please

@verstraetetycho
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upgrade symfony/http-foundation to version 5.0.7 or later

Could you explain how to do it please

First, go to your composer.json file and add this JSON-line to "require':
"symfony/http-foundation": "^5.0.7"

Following, you run this command in your terminal (best in the same path as your project):
composer update symfony/http-foundation

Finally, you just have to push your changes, and voila.
Hope this helps you

@saedyousef
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upgrade symfony/http-foundation to version 5.0.7 or later

Could you explain how to do it please

First, go to your composer.json file and add this JSON-line to "require':
"symfony/http-foundation": "^5.0.7"

Following, you run this command in your terminal (best in the same path as your project):
composer update symfony/http-foundation

Finally, you just have to push your changes, and voila.
Hope this helps you

I will try this
Thank you

@jxunaied
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dependence fix

Please sign in to comment.