From 9395e43035b9476749626ec695aff83f0429ca26 Mon Sep 17 00:00:00 2001 From: Rob Allen Date: Wed, 1 May 2024 20:36:22 +0100 Subject: [PATCH] Do not HTML entity encode in PlainTextErrorRenderer The PlainTextErrorRenderer should not encode exception messages as that's not required for plain text in the same way that it is not required for the JsonErrorRenderer. Closes #3298 --- Slim/Error/Renderers/PlainTextErrorRenderer.php | 2 +- tests/Error/AbstractErrorRendererTest.php | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Slim/Error/Renderers/PlainTextErrorRenderer.php b/Slim/Error/Renderers/PlainTextErrorRenderer.php index 3d80c74be..9788568d5 100644 --- a/Slim/Error/Renderers/PlainTextErrorRenderer.php +++ b/Slim/Error/Renderers/PlainTextErrorRenderer.php @@ -46,7 +46,7 @@ private function formatExceptionFragment(Throwable $exception): string /** @var int|string $code */ $text .= sprintf("Code: %s\n", $code); - $text .= sprintf("Message: %s\n", htmlentities($exception->getMessage())); + $text .= sprintf("Message: %s\n", $exception->getMessage()); $text .= sprintf("File: %s\n", $exception->getFile()); diff --git a/tests/Error/AbstractErrorRendererTest.php b/tests/Error/AbstractErrorRendererTest.php index 13639dce3..88cfa135b 100644 --- a/tests/Error/AbstractErrorRendererTest.php +++ b/tests/Error/AbstractErrorRendererTest.php @@ -202,19 +202,24 @@ public function testXMLErrorRendererRenderHttpException() public function testPlainTextErrorRendererFormatFragmentMethod() { - $exception = new Exception('Oops..', 500); + $message = 'Oops..
'; + $exception = new Exception($message, 500); $renderer = new PlainTextErrorRenderer(); $reflectionRenderer = new ReflectionClass(PlainTextErrorRenderer::class); $method = $reflectionRenderer->getMethod('formatExceptionFragment'); $method->setAccessible(true); $output = $method->invoke($renderer, $exception); + $this->assertIsString($output); $this->assertMatchesRegularExpression('/.*Type:*/', $output); $this->assertMatchesRegularExpression('/.*Code:*/', $output); $this->assertMatchesRegularExpression('/.*Message*/', $output); $this->assertMatchesRegularExpression('/.*File*/', $output); $this->assertMatchesRegularExpression('/.*Line*/', $output); + + // ensure the renderer doesn't reformat the message + $this->assertMatchesRegularExpression("/.*$message/", $output); } public function testPlainTextErrorRendererDisplaysErrorDetails()