From d5c2f7f7af75ab086c84c830204c7e16afa3646d Mon Sep 17 00:00:00 2001 From: Lukas Reschke Date: Thu, 17 Aug 2017 15:01:50 +0200 Subject: [PATCH] Ensure log message is UTF-8 encoded 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. Without this somebody passing an invalid User Agent may make json_encode return false which will get logged as empty newline. Signed-off-by: Lukas Reschke --- lib/private/Log/File.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/private/Log/File.php b/lib/private/Log/File.php index 904aa1d93f..954a19d098 100644 --- a/lib/private/Log/File.php +++ b/lib/private/Log/File.php @@ -114,7 +114,18 @@ class File { 'user', 'version' ); - $entry = json_encode($entry); + // 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); + if($testEncode === false) { + $entry[$key] = utf8_encode($value); + } + } + } + $entry = json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR); $handle = @fopen(self::$logFile, 'a'); @chmod(self::$logFile, 0640); if ($handle) {