Skip to content

Commit f16746e

Browse files
authored
Merge pull request #24876 from nextcloud/backport/24874/stable20
[stable20] Avoid huge exception argument logging
2 parents f00e1e7 + 6762e61 commit f16746e

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

lib/private/Log.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public function logException(\Throwable $exception, array $context = []) {
315315
$app = $context['app'] ?? 'no app in context';
316316
$level = $context['level'] ?? ILogger::ERROR;
317317

318-
$serializer = new ExceptionSerializer();
318+
$serializer = new ExceptionSerializer($this->config);
319319
$data = $serializer->serializeException($exception);
320320
$data['CustomMessage'] = $context['message'] ?? '--';
321321

lib/private/Log/ExceptionSerializer.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OC\HintException;
3333
use OC\Security\IdentityProof\Key;
3434
use OC\Setup;
35+
use OC\SystemConfig;
3536

3637
class ExceptionSerializer {
3738
public const methodsWithSensitiveParameters = [
@@ -91,6 +92,13 @@ class ExceptionSerializer {
9192
'imagecreatefromstring',
9293
];
9394

95+
/** @var SystemConfig */
96+
private $systemConfig;
97+
98+
public function __construct(SystemConfig $systemConfig) {
99+
$this->systemConfig = $systemConfig;
100+
}
101+
94102
public const methodsWithSensitiveParametersByClass = [
95103
SetupController::class => [
96104
'run',
@@ -162,11 +170,21 @@ private function encodeArg($arg) {
162170
$data = get_object_vars($arg);
163171
$data['__class__'] = get_class($arg);
164172
return array_map([$this, 'encodeArg'], $data);
165-
} elseif (is_array($arg)) {
173+
}
174+
175+
if (is_array($arg)) {
176+
// Only log the first 5 elements of an array unless we are on debug
177+
if ((int)$this->systemConfig->getValue('loglevel', 2) !== 0) {
178+
$elemCount = count($arg);
179+
if ($elemCount > 5) {
180+
$arg = array_slice($arg, 0, 5);
181+
$arg[] = 'And ' . ($elemCount - 5) . ' more entries, set log level to debug to see all entries';
182+
}
183+
}
166184
return array_map([$this, 'encodeArg'], $arg);
167-
} else {
168-
return $arg;
169185
}
186+
187+
return $arg;
170188
}
171189

172190
public function serializeException(\Throwable $exception) {

0 commit comments

Comments
 (0)