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 <lukas@statuscode.ch>
This commit is contained in:
Lukas Reschke 2017-08-17 15:01:50 +02:00
parent d87729c42a
commit d5c2f7f7af
No known key found for this signature in database
GPG Key ID: B9F6980CF6E759B1
1 changed files with 12 additions and 1 deletions

View File

@ -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) {