|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2018 Robin Appelman <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @license GNU AGPL version 3 or any later version |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
10
|
|
|
* License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OC\Log; |
|
23
|
|
|
|
|
24
|
|
|
use OC\Core\Controller\SetupController; |
|
25
|
|
|
use OC\HintException; |
|
26
|
|
|
use OC\Setup; |
|
27
|
|
|
|
|
28
|
|
|
class ExceptionSerializer { |
|
29
|
|
|
const methodsWithSensitiveParameters = [ |
|
30
|
|
|
// Session/User |
|
31
|
|
|
'completeLogin', |
|
32
|
|
|
'login', |
|
33
|
|
|
'checkPassword', |
|
34
|
|
|
'checkPasswordNoLogging', |
|
35
|
|
|
'loginWithPassword', |
|
36
|
|
|
'updatePrivateKeyPassword', |
|
37
|
|
|
'validateUserPass', |
|
38
|
|
|
'loginWithToken', |
|
39
|
|
|
'{closure}', |
|
40
|
|
|
'createSessionToken', |
|
41
|
|
|
|
|
42
|
|
|
// Provisioning |
|
43
|
|
|
'addUser', |
|
44
|
|
|
|
|
45
|
|
|
// TokenProvider |
|
46
|
|
|
'getToken', |
|
47
|
|
|
'isTokenPassword', |
|
48
|
|
|
'getPassword', |
|
49
|
|
|
'decryptPassword', |
|
50
|
|
|
'logClientIn', |
|
51
|
|
|
'generateToken', |
|
52
|
|
|
'validateToken', |
|
53
|
|
|
|
|
54
|
|
|
// TwoFactorAuth |
|
55
|
|
|
'solveChallenge', |
|
56
|
|
|
'verifyChallenge', |
|
57
|
|
|
|
|
58
|
|
|
// ICrypto |
|
59
|
|
|
'calculateHMAC', |
|
60
|
|
|
'encrypt', |
|
61
|
|
|
'decrypt', |
|
62
|
|
|
|
|
63
|
|
|
// LoginController |
|
64
|
|
|
'tryLogin', |
|
65
|
|
|
'confirmPassword', |
|
66
|
|
|
|
|
67
|
|
|
// LDAP |
|
68
|
|
|
'bind', |
|
69
|
|
|
'areCredentialsValid', |
|
70
|
|
|
'invokeLDAPMethod', |
|
71
|
|
|
|
|
72
|
|
|
// Encryption |
|
73
|
|
|
'storeKeyPair', |
|
74
|
|
|
'setupUser', |
|
75
|
|
|
|
|
76
|
|
|
// files_external: OC_Mount_Config |
|
77
|
|
|
'getBackendStatus', |
|
78
|
|
|
|
|
79
|
|
|
// files_external: UserStoragesController |
|
80
|
|
|
'update', |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
const methodsWithSensitiveParametersByClass = [ |
|
84
|
|
|
SetupController::class => [ |
|
85
|
|
|
'run', |
|
86
|
|
|
'display', |
|
87
|
|
|
'loadAutoConfig', |
|
88
|
|
|
], |
|
89
|
|
|
Setup::class => [ |
|
90
|
|
|
'install' |
|
91
|
|
|
] |
|
92
|
|
|
]; |
|
93
|
|
|
|
|
94
|
|
|
private function editTrace(array &$sensitiveValues, array $traceLine): array { |
|
95
|
|
|
$sensitiveValues = array_merge($sensitiveValues, $traceLine['args']); |
|
96
|
|
|
$traceLine['args'] = ['*** sensitive parameters replaced ***']; |
|
97
|
|
|
return $traceLine; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
private function filterTrace(array $trace) { |
|
101
|
|
|
$sensitiveValues = []; |
|
102
|
|
|
$trace = array_map(function (array $traceLine) use (&$sensitiveValues) { |
|
103
|
|
|
$className = $traceLine['class'] ?? ''; |
|
104
|
|
|
if ($className && isset(self::methodsWithSensitiveParametersByClass[$className]) |
|
105
|
|
|
&& in_array($traceLine['function'], self::methodsWithSensitiveParametersByClass[$className], true)) { |
|
106
|
|
|
return $this->editTrace($sensitiveValues, $traceLine); |
|
107
|
|
|
} |
|
108
|
|
|
foreach (self::methodsWithSensitiveParameters as $sensitiveMethod) { |
|
109
|
|
|
if (strpos($traceLine['function'], $sensitiveMethod) !== false) { |
|
110
|
|
|
return $this->editTrace($sensitiveValues, $traceLine); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
return $traceLine; |
|
114
|
|
|
}, $trace); |
|
115
|
|
|
return array_map(function (array $traceLine) use ($sensitiveValues) { |
|
116
|
|
|
if (isset($traceLine['args'])) { |
|
117
|
|
|
$traceLine['args'] = $this->removeValuesFromArgs($traceLine['args'], $sensitiveValues); |
|
118
|
|
|
} |
|
119
|
|
|
return $traceLine; |
|
120
|
|
|
}, $trace); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
private function removeValuesFromArgs($args, $values) { |
|
124
|
|
|
foreach ($args as &$arg) { |
|
125
|
|
|
if (in_array($arg, $values, true)) { |
|
126
|
|
|
$arg = '*** sensitive parameter replaced ***'; |
|
127
|
|
|
} else if (is_array($arg)) { |
|
128
|
|
|
$arg = $this->removeValuesFromArgs($arg, $values); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
return $args; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
private function encodeTrace($trace) { |
|
135
|
|
|
$filteredTrace = $this->filterTrace($trace); |
|
136
|
|
|
return array_map(function (array $line) { |
|
137
|
|
|
if (isset($line['args'])) { |
|
138
|
|
|
$line['args'] = array_map([$this, 'encodeArg'], $line['args']); |
|
139
|
|
|
} |
|
140
|
|
|
return $line; |
|
141
|
|
|
}, $filteredTrace); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
private function encodeArg($arg) { |
|
145
|
|
|
if (is_object($arg)) { |
|
146
|
|
|
$data = get_object_vars($arg); |
|
147
|
|
|
$data['__class__'] = get_class($arg); |
|
148
|
|
|
return array_map([$this, 'encodeArg'], $data); |
|
149
|
|
|
} else if (is_array($arg)) { |
|
150
|
|
|
return array_map([$this, 'encodeArg'], $arg); |
|
151
|
|
|
} else { |
|
152
|
|
|
return $arg; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
public function serializeException(\Throwable $exception) { |
|
157
|
|
|
$data = [ |
|
158
|
|
|
'Exception' => get_class($exception), |
|
159
|
|
|
'Message' => $exception->getMessage(), |
|
160
|
|
|
'Code' => $exception->getCode(), |
|
161
|
|
|
'Trace' => $this->encodeTrace($exception->getTrace()), |
|
162
|
|
|
'File' => $exception->getFile(), |
|
163
|
|
|
'Line' => $exception->getLine(), |
|
164
|
|
|
]; |
|
165
|
|
|
|
|
166
|
|
|
if ($exception instanceof HintException) { |
|
167
|
|
|
$data['Hint'] = $exception->getHint(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
if ($exception->getPrevious()) { |
|
171
|
|
|
$data['Previous'] = $this->serializeException($exception->getPrevious()); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $data; |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|