Skip to content

Commit 6929438

Browse files
authored
Merge pull request #32898 from nextcloud/fix/noid/logger-overwrites-vars
Fix logger overwriting vars in some circumstances
2 parents 0c14ee6 + 8b2b594 commit 6929438

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

lib/private/Log/ExceptionSerializer.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
use OCP\HintException;
4343

4444
class ExceptionSerializer {
45+
public const SENSITIVE_VALUE_PLACEHOLDER = '*** sensitive parameters replaced ***';
46+
4547
public const methodsWithSensitiveParameters = [
4648
// Session/User
4749
'completeLogin',
@@ -180,7 +182,7 @@ private function editTrace(array &$sensitiveValues, array $traceLine): array {
180182
if (isset($traceLine['args'])) {
181183
$sensitiveValues = array_merge($sensitiveValues, $traceLine['args']);
182184
}
183-
$traceLine['args'] = ['*** sensitive parameters replaced ***'];
185+
$traceLine['args'] = [self::SENSITIVE_VALUE_PLACEHOLDER];
184186
return $traceLine;
185187
}
186188

@@ -208,14 +210,16 @@ private function filterTrace(array $trace) {
208210
}
209211

210212
private function removeValuesFromArgs($args, $values) {
211-
foreach ($args as &$arg) {
213+
$workArgs = [];
214+
foreach ($args as $arg) {
212215
if (in_array($arg, $values, true)) {
213-
$arg = '*** sensitive parameter replaced ***';
216+
$arg = self::SENSITIVE_VALUE_PLACEHOLDER;
214217
} elseif (is_array($arg)) {
215218
$arg = $this->removeValuesFromArgs($arg, $values);
216219
}
220+
$workArgs[] = $arg;
217221
}
218-
return $args;
222+
return $workArgs;
219223
}
220224

221225
private function encodeTrace($trace) {
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
7+
*
8+
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
24+
*
25+
*/
26+
27+
namespace lib\Log;
28+
29+
use OC\Log\ExceptionSerializer;
30+
use OC\SystemConfig;
31+
use Test\TestCase;
32+
33+
class ExceptionSerializerTest extends TestCase {
34+
private ExceptionSerializer $serializer;
35+
36+
public function setUp(): void {
37+
parent::setUp();
38+
39+
$config = $this->createMock(SystemConfig::class);
40+
$this->serializer = new ExceptionSerializer($config);
41+
}
42+
43+
private function emit($arguments) {
44+
\call_user_func_array([$this, 'bind'], $arguments);
45+
}
46+
47+
private function bind(array &$myValues): void {
48+
throw new \Exception('my exception');
49+
}
50+
51+
/**
52+
* this test ensures that the serializer does not overwrite referenced
53+
* variables. It is crafted after a scenario we experienced: the DAV server
54+
* emitting the "validateTokens" event, of which later on a handled
55+
* exception was passed to the logger. The token was replaced, the original
56+
* variable overwritten.
57+
*/
58+
public function testSerializer() {
59+
try {
60+
$secret = ['Secret'];
61+
$this->emit([&$secret]);
62+
} catch (\Exception $e) {
63+
$serializedData = $this->serializer->serializeException($e);
64+
$this->assertSame(['Secret'], $secret);
65+
$this->assertSame(ExceptionSerializer::SENSITIVE_VALUE_PLACEHOLDER, $serializedData['Trace'][0]['args'][0]);
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)