Skip to content

Commit 1964001

Browse files
authored
Merge pull request #978 from jordscream/not_expose_sensitive_information
Do not expose sensitive information
2 parents 824c6e0 + 0635c0f commit 1964001

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

src/Action/ExceptionAction.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ public function __construct(SerializerInterface $serializer, array $errorFormats
6666
public function __invoke(FlattenException $exception, Request $request): Response
6767
{
6868
$exceptionClass = $exception->getClass();
69+
$statusCode = $exception->getStatusCode();
70+
6971
foreach ($this->exceptionToStatus as $class => $status) {
7072
if (is_a($exceptionClass, $class, true)) {
71-
$exception->setStatusCode($status);
73+
$statusCode = $status;
7274

7375
break;
7476
}
@@ -80,6 +82,6 @@ public function __invoke(FlattenException $exception, Request $request): Respons
8082
$headers['X-Content-Type-Options'] = 'nosniff';
8183
$headers['X-Frame-Options'] = 'deny';
8284

83-
return new Response($this->serializer->serialize($exception, $format['key']), $exception->getStatusCode(), $headers);
85+
return new Response($this->serializer->serialize($exception, $format['key'], ['statusCode' => $statusCode]), $statusCode, $headers);
8486
}
8587
}

src/Hydra/Serializer/ErrorNormalizer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use ApiPlatform\Core\Api\UrlGeneratorInterface;
1515
use Symfony\Component\Debug\Exception\FlattenException;
16+
use Symfony\Component\HttpFoundation\Response;
1617
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1718

1819
/**
@@ -42,6 +43,11 @@ public function normalize($object, $format = null, array $context = [])
4243
$message = $object->getMessage();
4344
if ($this->debug) {
4445
$trace = $object->getTrace();
46+
} elseif ($object instanceof FlattenException) {
47+
$statusCode = $context['statusCode'] ?? $object->getStatusCode();
48+
if ($statusCode >= 500 && $statusCode < 600) {
49+
$message = Response::$statusTexts[$statusCode];
50+
}
4551
}
4652

4753
$data = [

tests/Action/ExceptionActionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testActionWithCatchableException()
3333
$flattenException = FlattenException::create($serializerException->reveal());
3434

3535
$serializer = $this->prophesize(SerializerInterface::class);
36-
$serializer->serialize($flattenException, 'jsonproblem')->willReturn();
36+
$serializer->serialize($flattenException, 'jsonproblem', ['statusCode' => Response::HTTP_BAD_REQUEST])->willReturn();
3737

3838
$exceptionAction = new ExceptionAction($serializer->reveal(), ['jsonproblem' => ['application/problem+json'], 'jsonld' => ['application/ld+json']], [ExceptionInterface::class => Response::HTTP_BAD_REQUEST, InvalidArgumentException::class => Response::HTTP_BAD_REQUEST]);
3939

@@ -57,7 +57,7 @@ public function testActionWithUncatchableException()
5757
$flattenException = FlattenException::create($serializerException->reveal());
5858

5959
$serializer = $this->prophesize(SerializerInterface::class);
60-
$serializer->serialize($flattenException, 'jsonproblem')->willReturn();
60+
$serializer->serialize($flattenException, 'jsonproblem', ['statusCode' => $flattenException->getStatusCode()])->willReturn();
6161

6262
$exceptionAction = new ExceptionAction($serializer->reveal(), ['jsonproblem' => ['application/problem+json'], 'jsonld' => ['application/ld+json']]);
6363

tests/Hydra/Serializer/ErrorNormalizerTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use ApiPlatform\Core\Api\UrlGeneratorInterface;
1515
use ApiPlatform\Core\Hydra\Serializer\ErrorNormalizer;
1616
use Symfony\Component\Debug\Exception\FlattenException;
17+
use Symfony\Component\HttpFoundation\Response;
1718

1819
/**
1920
* @author Kévin Dunglas <[email protected]>
@@ -35,6 +36,47 @@ public function testSupportNormalization()
3536
$this->assertFalse($normalizer->supportsNormalization(new \stdClass(), ErrorNormalizer::FORMAT));
3637
}
3738

39+
/**
40+
* @dataProvider providerStatusCode
41+
*
42+
* @param $status http status code of the Exception
43+
* @param $originalMessage original message of the Exception
44+
* @param $debug simulates kernel debug variable
45+
*/
46+
public function testErrorServerNormalize($status, $originalMessage, $debug)
47+
{
48+
$urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class);
49+
$urlGeneratorProphecy->generate('api_jsonld_context', ['shortName' => 'Error'])->willReturn('/context/foo')->shouldBeCalled();
50+
51+
$normalizer = new ErrorNormalizer($urlGeneratorProphecy->reveal(), $debug);
52+
$exception = FlattenException::create(new \Exception($originalMessage), $status);
53+
54+
$expected = [
55+
'@context' => '/context/foo',
56+
'@type' => 'hydra:Error',
57+
'hydra:title' => 'An error occurred',
58+
'hydra:description' => ($debug || $status < 500) ? $originalMessage : Response::$statusTexts[$status],
59+
];
60+
61+
if ($debug) {
62+
$expected['trace'] = $exception->getTrace();
63+
}
64+
65+
$this->assertEquals($expected, $normalizer->normalize($exception, null, ['statusCode' => $status]));
66+
}
67+
68+
public function providerStatusCode()
69+
{
70+
return [
71+
[Response::HTTP_INTERNAL_SERVER_ERROR, 'Sensitive SQL error displayed', false],
72+
[Response::HTTP_GATEWAY_TIMEOUT, 'Sensitive server error displayed', false],
73+
[Response::HTTP_BAD_REQUEST, 'Bad Request Message', false],
74+
[Response::HTTP_INTERNAL_SERVER_ERROR, 'Sensitive SQL error displayed', true],
75+
[Response::HTTP_GATEWAY_TIMEOUT, 'Sensitive server error displayed', true],
76+
[Response::HTTP_BAD_REQUEST, 'Bad Request Message', true],
77+
];
78+
}
79+
3880
public function testNormalize()
3981
{
4082
$urlGeneratorProphecy = $this->prophesize(UrlGeneratorInterface::class);

0 commit comments

Comments
 (0)