From 426e42795c0d4e648f71ff433cce8bc01a35bcd4 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 21 Jun 2023 17:17:59 +0200 Subject: [PATCH 1/5] Fix phpstan error --- src/Monolog/ErrorHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/ErrorHandler.php b/src/Monolog/ErrorHandler.php index 039a6594c..1406d34e8 100644 --- a/src/Monolog/ErrorHandler.php +++ b/src/Monolog/ErrorHandler.php @@ -198,7 +198,7 @@ private function handleException(\Throwable $e): void ($this->previousExceptionHandler)($e); } - if (!headers_sent() && in_array(strtolower(ini_get('display_errors')), ['0', '', 'false', 'off', 'none', 'no'], true)) { + if (!headers_sent() && in_array(strtolower((string) ini_get('display_errors')), ['0', '', 'false', 'off', 'none', 'no'], true)) { http_response_code(500); } From 1b93764d154ba06b18fe11f252bf991b9ef9aa62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=93=AD=E6=98=95?= <715557344@qq.com> Date: Thu, 22 Jun 2023 14:44:58 +0800 Subject: [PATCH 2/5] Fixed bug that `restore_error_handler` will not be executed if there is an exception before restore should happen (#1815) --- src/Monolog/Handler/StreamHandler.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Monolog/Handler/StreamHandler.php b/src/Monolog/Handler/StreamHandler.php index 651835122..82c048e1c 100644 --- a/src/Monolog/Handler/StreamHandler.php +++ b/src/Monolog/Handler/StreamHandler.php @@ -135,11 +135,14 @@ protected function write(array $record): void $this->createDir($url); $this->errorMessage = null; set_error_handler([$this, 'customErrorHandler']); - $stream = fopen($url, 'a'); - if ($this->filePermission !== null) { - @chmod($url, $this->filePermission); + try { + $stream = fopen($url, 'a'); + if ($this->filePermission !== null) { + @chmod($url, $this->filePermission); + } + } finally { + restore_error_handler(); } - restore_error_handler(); if (!is_resource($stream)) { $this->stream = null; From 70e1c2f405de7b9c6a47fb71d2e620a0218fa266 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 27 Oct 2023 13:49:01 +0200 Subject: [PATCH 3/5] Fix string interpolation of newline --- src/Monolog/Formatter/LineFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index b31b2971a..e6e789833 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -155,7 +155,7 @@ protected function normalizeException(\Throwable $e, int $depth = 0): string do { $depth++; if ($depth > $this->maxNormalizeDepth) { - $str .= '\n[previous exception] Over ' . $this->maxNormalizeDepth . ' levels deep, aborting normalization'; + $str .= "\n[previous exception] Over " . $this->maxNormalizeDepth . ' levels deep, aborting normalization'; break; } From ed80d53ab293f2a62beb9a6d2e9e6c53f2a37d7c Mon Sep 17 00:00:00 2001 From: Egor Korobov Date: Fri, 8 Sep 2023 17:40:20 +0300 Subject: [PATCH 4/5] Fix fatal error in NormalizeFormatter: method_exists(): The script tried to execute a method or access a property of an incomplete object. Closes #1833 Fixes #1834 --- src/Monolog/Formatter/NormalizerFormatter.php | 3 +++ .../Monolog/Formatter/NormalizerFormatterTest.php | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Monolog/Formatter/NormalizerFormatter.php b/src/Monolog/Formatter/NormalizerFormatter.php index 5441bc0aa..e4409f523 100644 --- a/src/Monolog/Formatter/NormalizerFormatter.php +++ b/src/Monolog/Formatter/NormalizerFormatter.php @@ -174,6 +174,9 @@ protected function normalize($data, int $depth = 0) if ($data instanceof \JsonSerializable) { /** @var null|scalar|array $value */ $value = $data->jsonSerialize(); + } elseif (\get_class($data) === '__PHP_Incomplete_Class') { + $accessor = new \ArrayObject($data); + $value = $accessor['__PHP_Incomplete_Class_Name']; } elseif (method_exists($data, '__toString')) { /** @var string $value */ $value = $data->__toString(); diff --git a/tests/Monolog/Formatter/NormalizerFormatterTest.php b/tests/Monolog/Formatter/NormalizerFormatterTest.php index 162f34b31..0c7ba76a0 100644 --- a/tests/Monolog/Formatter/NormalizerFormatterTest.php +++ b/tests/Monolog/Formatter/NormalizerFormatterTest.php @@ -415,6 +415,20 @@ public function testExceptionTraceDoesNotLeakCallUserFuncArgs() ); } + public function testCanNormalizeIncompleteObject(): void + { + $serialized = "O:17:\"Monolog\TestClass\":1:{s:23:\"\x00Monolog\TestClass\x00name\";s:4:\"test\";}"; + $object = unserialize($serialized); + + $formatter = new NormalizerFormatter(); + $record = ['context' => ['object' => $object]]; + $result = $formatter->format($record); + + $this->assertEquals([ + '__PHP_Incomplete_Class' => 'Monolog\\TestClass', + ], $result['context']['object']); + } + private function throwHelper($arg) { throw new \RuntimeException('Thrown'); From 531ed6d873472b6633b9f65a83441e07508e3da0 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 27 Oct 2023 14:14:00 +0200 Subject: [PATCH 5/5] enforce string value for safety --- src/Monolog/Formatter/NormalizerFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Formatter/NormalizerFormatter.php b/src/Monolog/Formatter/NormalizerFormatter.php index e4409f523..f926a842f 100644 --- a/src/Monolog/Formatter/NormalizerFormatter.php +++ b/src/Monolog/Formatter/NormalizerFormatter.php @@ -176,7 +176,7 @@ protected function normalize($data, int $depth = 0) $value = $data->jsonSerialize(); } elseif (\get_class($data) === '__PHP_Incomplete_Class') { $accessor = new \ArrayObject($data); - $value = $accessor['__PHP_Incomplete_Class_Name']; + $value = (string) $accessor['__PHP_Incomplete_Class_Name']; } elseif (method_exists($data, '__toString')) { /** @var string $value */ $value = $data->__toString();