diff --git a/plugins/Monolog/Formatter/LineMessageFormatter.php b/plugins/Monolog/Formatter/LineMessageFormatter.php index 6639b5618e4..04688204c70 100644 --- a/plugins/Monolog/Formatter/LineMessageFormatter.php +++ b/plugins/Monolog/Formatter/LineMessageFormatter.php @@ -53,6 +53,8 @@ public function format(array $record) $total = ''; foreach ($messages as $message) { + // escape control characters + $message = addcslashes($message, "\x00..\x09\x0B..\x1F\x7F"); $message = $this->prefixMessageWithRequestId($record, $message); $total .= $this->formatMessage($class, $message, $date, $record); } diff --git a/plugins/Monolog/tests/Unit/Formatter/LineMessageFormatterTest.php b/plugins/Monolog/tests/Unit/Formatter/LineMessageFormatterTest.php index 93f3b608a5a..351bb47f6b9 100644 --- a/plugins/Monolog/tests/Unit/Formatter/LineMessageFormatterTest.php +++ b/plugins/Monolog/tests/Unit/Formatter/LineMessageFormatterTest.php @@ -59,7 +59,7 @@ public function testItShouldIndentMultilineMessage() $formatter = new LineMessageFormatter('%level% %message%'); $record = array( - 'message' => "Hello world\ntest\ntest", + 'message' => "Hello world\ntest\x0Atest", 'datetime' => DateTime::createFromFormat('U', 0), 'level_name' => 'ERROR', ); @@ -90,6 +90,25 @@ public function testItShouldSplitInlineLineBreaksIntoManyMessagesIfDisabled() ERROR [1234] test ERROR [1234] test +LOG; + + $this->assertEquals($formatted, $formatter->format($record)); + } + + public function testItShouldEscapeControlCharacters() + { + $formatter = new LineMessageFormatter('%level% %message%', $allowInlineLineBreaks = false); + + $record = array( + 'message' => "Hello world\x1Btest\ntesttest", + 'datetime' => DateTime::createFromFormat('U', 0), + 'level_name' => 'ERROR', + ); + + $formatted = <<assertEquals($formatted, $formatter->format($record));