From 21a51410b55a4c3e790e86c8ce7604de3547cf04 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Wed, 11 Nov 2026 10:23:42 +0000 Subject: [PATCH 1/2] Escape null bytes in Sentry log messages --- src/Debug/SentryLogHandler.php | 2 ++ test/phpunit/Debug/SentryLogHandlerTest.php | 30 +++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/Debug/SentryLogHandler.php b/src/Debug/SentryLogHandler.php index 094808b8..b0744ab0 100644 --- a/src/Debug/SentryLogHandler.php +++ b/src/Debug/SentryLogHandler.php @@ -23,6 +23,8 @@ public function setClient(ClientInterface $client):void { /** @param array $context */ public function handle(string $level, string $message, array $context = []):void { + // Anonymous PHP class names contain null bytes that log storage can reject. + $message = str_replace("\0", "\\0", $message); // Context is deliberately excluded: arbitrary values can contain secrets. $this->pending []= [ "level" => strtoupper($level), diff --git a/test/phpunit/Debug/SentryLogHandlerTest.php b/test/phpunit/Debug/SentryLogHandlerTest.php index 0904f9c8..92e15929 100644 --- a/test/phpunit/Debug/SentryLogHandlerTest.php +++ b/test/phpunit/Debug/SentryLogHandlerTest.php @@ -16,6 +16,36 @@ use RuntimeException; class SentryLogHandlerTest extends TestCase { + #[\PHPUnit\Framework\Attributes\DataProvider("logMessages")] + public function testNullBytesAreEscapedBeforeMessagesAreBounded(string $message, string $expected):void { + $client = self::createMock(ClientInterface::class); + $client->method("getOptions")->willReturn(new Options()); + $client->expects(self::once())->method("captureEvent")->willReturnCallback( + function(Event $event) use ($expected):EventId { + self::assertSame($expected, $event->getLogs()[0]->getBody()); + self::assertStringNotContainsString("\0", $event->getLogs()[0]->getBody()); + return $event->getId(); + }, + ); + $handler = new SentryLogHandler(); + $handler->setClient($client); + $handler->handle("ERROR", $message); + $handler->flush(); + } + + /** @return array */ + public static function logMessages():array { + $exception = new class("CSRF Token not found") extends RuntimeException {}; + $message = get_class($exception) . ": " . $exception->getMessage(); + return [ + "anonymous exception" => [$message, str_replace("\0", "\\0", $message)], + "multiple null bytes" => ["\0before\0after\0", "\\0before\\0after\\0"], + "ordinary message" => ["Ordinary message", "Ordinary message"], + "literal escape" => ["Already escaped \\0", "Already escaped \\0"], + "expanded message stays bounded" => [str_repeat("\0", 8192), str_repeat("\\0", 4096)], + ]; + } + #[\PHPUnit\Framework\Attributes\DataProvider("unavailableClients")] public function testUnavailableDeliveryFallsBackOnceWithoutContext(string $failure):void { $handler = new SentryLogHandler(); From 08ab15c4c736786b38e86fef873cfcb5a1c38e32 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Wed, 11 Nov 2026 10:31:00 +0000 Subject: [PATCH 2/2] Represent null bytes with the Unicode null symbol --- src/Debug/SentryLogHandler.php | 4 ++-- test/phpunit/Debug/SentryLogHandlerTest.php | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Debug/SentryLogHandler.php b/src/Debug/SentryLogHandler.php index b0744ab0..e4d9a467 100644 --- a/src/Debug/SentryLogHandler.php +++ b/src/Debug/SentryLogHandler.php @@ -24,11 +24,11 @@ public function setClient(ClientInterface $client):void { /** @param array $context */ public function handle(string $level, string $message, array $context = []):void { // Anonymous PHP class names contain null bytes that log storage can reject. - $message = str_replace("\0", "\\0", $message); + $message = str_replace("\0", "␀", $message); // Context is deliberately excluded: arbitrary values can contain secrets. $this->pending []= [ "level" => strtoupper($level), - "message" => substr($message, 0, 8192), + "message" => mb_strcut($message, 0, 8192, "UTF-8"), "timestamp" => microtime(true), ]; if(count($this->pending) >= self::BATCH_SIZE) { diff --git a/test/phpunit/Debug/SentryLogHandlerTest.php b/test/phpunit/Debug/SentryLogHandlerTest.php index 92e15929..0417240c 100644 --- a/test/phpunit/Debug/SentryLogHandlerTest.php +++ b/test/phpunit/Debug/SentryLogHandlerTest.php @@ -17,7 +17,7 @@ class SentryLogHandlerTest extends TestCase { #[\PHPUnit\Framework\Attributes\DataProvider("logMessages")] - public function testNullBytesAreEscapedBeforeMessagesAreBounded(string $message, string $expected):void { + public function testNullBytesAreReplacedBeforeMessagesAreBounded(string $message, string $expected):void { $client = self::createMock(ClientInterface::class); $client->method("getOptions")->willReturn(new Options()); $client->expects(self::once())->method("captureEvent")->willReturnCallback( @@ -38,11 +38,13 @@ public static function logMessages():array { $exception = new class("CSRF Token not found") extends RuntimeException {}; $message = get_class($exception) . ": " . $exception->getMessage(); return [ - "anonymous exception" => [$message, str_replace("\0", "\\0", $message)], - "multiple null bytes" => ["\0before\0after\0", "\\0before\\0after\\0"], + "anonymous exception" => [$message, str_replace("\0", "␀", $message)], + "multiple null bytes" => ["\0before\0after\0", "␀before␀after␀"], "ordinary message" => ["Ordinary message", "Ordinary message"], "literal escape" => ["Already escaped \\0", "Already escaped \\0"], - "expanded message stays bounded" => [str_repeat("\0", 8192), str_repeat("\\0", 4096)], + "symbol stays unchanged" => ["Visible ␀ marker", "Visible ␀ marker"], + "symbol crossing byte limit" => [str_repeat("a", 8191) . "\0", str_repeat("a", 8191)], + "expanded message stays bounded" => [str_repeat("\0", 8192), str_repeat("␀", 2730)], ]; }