From 8731b5afe7185868df6c0e483149f8a7da4deab5 Mon Sep 17 00:00:00 2001 From: ernolf Date: Sun, 19 Jul 2026 03:06:46 +0200 Subject: [PATCH] fix: harden log encoding and the rejection rethrow - json entries with broken UTF-8 header bytes are written with substituted characters instead of being silently dropped (json_encode returned false and only an empty line was appended) - rethrowing a non-throwable rejection no longer string-casts objects blindly; objects are reported by class name like in the log entry Signed-off-by: ernolf --- lib/Http/Client/Middleware/HttpClientLoggerMiddleware.php | 4 +++- lib/Http/Client/Middleware/LogWriter.php | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/Http/Client/Middleware/HttpClientLoggerMiddleware.php b/lib/Http/Client/Middleware/HttpClientLoggerMiddleware.php index ce3db81..c46e785 100644 --- a/lib/Http/Client/Middleware/HttpClientLoggerMiddleware.php +++ b/lib/Http/Client/Middleware/HttpClientLoggerMiddleware.php @@ -270,7 +270,9 @@ function ($reason) use ($request, $reqHeaders, $reqId) { if ($reason instanceof \Throwable) { throw $reason; } - throw new \RuntimeException('HTTP request rejected: ' . (string)$reason); + throw new \RuntimeException( + 'HTTP request rejected: ' . (is_object($reason) ? get_class($reason) : (string)$reason) + ); } ); }; diff --git a/lib/Http/Client/Middleware/LogWriter.php b/lib/Http/Client/Middleware/LogWriter.php index 082b8ba..5ad6a20 100644 --- a/lib/Http/Client/Middleware/LogWriter.php +++ b/lib/Http/Client/Middleware/LogWriter.php @@ -28,7 +28,12 @@ public static function write( LoggerInterface $logger, ): void { if (in_array($format, ['json', 'both'], true)) { - self::append($jsonFile, json_encode($entry, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . PHP_EOL, $logger); + // Remote servers can send headers with broken UTF-8; substitute + // those bytes instead of losing the whole entry. + $json = json_encode($entry, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_INVALID_UTF8_SUBSTITUTE); + if ($json !== false) { + self::append($jsonFile, $json . PHP_EOL, $logger); + } } if (in_array($format, ['plain', 'both'], true)) { self::append($plainFile, $plainLine, $logger);