From 398a137feeb71ad862471e9cd8659662d694ec0f Mon Sep 17 00:00:00 2001 From: Josh Date: Wed, 10 Jun 2026 11:41:47 -0400 Subject: [PATCH] fix(log): simplify JSON log serialization for invalid UTF-8 Signed-off-by: Josh --- lib/private/Log/LogDetails.php | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/lib/private/Log/LogDetails.php b/lib/private/Log/LogDetails.php index 21abb6d85cb46..7c97d1026d62c 100644 --- a/lib/private/Log/LogDetails.php +++ b/lib/private/Log/LogDetails.php @@ -95,17 +95,10 @@ public function logDetails(string $app, string|array $message, int $level): arra public function logDetailsAsJSON(string $app, string|array $message, int $level): string { $entry = $this->logDetails($app, $message, $level); - // PHP's json_encode only accept proper UTF-8 strings, loop over all - // elements to ensure that they are properly UTF-8 compliant or convert - // them manually. - foreach ($entry as $key => $value) { - if (is_string($value)) { - $testEncode = json_encode($value, JSON_UNESCAPED_SLASHES); - if ($testEncode === false) { - $entry[$key] = mb_convert_encoding($value, 'UTF-8', mb_detect_encoding($value)); - } - } - } - return json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES); + + // Log output should remain valid JSON even if some values contain invalid UTF-8. + // Substitute malformed byte sequences instead of trying to guess the original encoding, + // which can't be relied upon anyway. + return json_encode($entry, JSON_UNESCAPED_SLASHES | JSON_INVALID_UTF8_SUBSTITUTE | JSON_PARTIAL_OUTPUT_ON_ERROR); } }