@@ -36,177 +36,177 @@ |
||
| 36 | 36 | use OC\SystemConfig; |
| 37 | 37 | |
| 38 | 38 | class ExceptionSerializer { |
| 39 | - public const methodsWithSensitiveParameters = [ |
|
| 40 | - // Session/User |
|
| 41 | - 'completeLogin', |
|
| 42 | - 'login', |
|
| 43 | - 'checkPassword', |
|
| 44 | - 'checkPasswordNoLogging', |
|
| 45 | - 'loginWithPassword', |
|
| 46 | - 'updatePrivateKeyPassword', |
|
| 47 | - 'validateUserPass', |
|
| 48 | - 'loginWithToken', |
|
| 49 | - '{closure}', |
|
| 50 | - 'createSessionToken', |
|
| 51 | - |
|
| 52 | - // Provisioning |
|
| 53 | - 'addUser', |
|
| 54 | - |
|
| 55 | - // TokenProvider |
|
| 56 | - 'getToken', |
|
| 57 | - 'isTokenPassword', |
|
| 58 | - 'getPassword', |
|
| 59 | - 'decryptPassword', |
|
| 60 | - 'logClientIn', |
|
| 61 | - 'generateToken', |
|
| 62 | - 'validateToken', |
|
| 63 | - |
|
| 64 | - // TwoFactorAuth |
|
| 65 | - 'solveChallenge', |
|
| 66 | - 'verifyChallenge', |
|
| 67 | - |
|
| 68 | - // ICrypto |
|
| 69 | - 'calculateHMAC', |
|
| 70 | - 'encrypt', |
|
| 71 | - 'decrypt', |
|
| 72 | - |
|
| 73 | - // LoginController |
|
| 74 | - 'tryLogin', |
|
| 75 | - 'confirmPassword', |
|
| 76 | - |
|
| 77 | - // LDAP |
|
| 78 | - 'bind', |
|
| 79 | - 'areCredentialsValid', |
|
| 80 | - 'invokeLDAPMethod', |
|
| 81 | - |
|
| 82 | - // Encryption |
|
| 83 | - 'storeKeyPair', |
|
| 84 | - 'setupUser', |
|
| 85 | - 'checkSignature', |
|
| 86 | - |
|
| 87 | - // files_external: OCA\Files_External\MountConfig |
|
| 88 | - 'getBackendStatus', |
|
| 89 | - |
|
| 90 | - // files_external: UserStoragesController |
|
| 91 | - 'update', |
|
| 92 | - |
|
| 93 | - // Preview providers, don't log big data strings |
|
| 94 | - 'imagecreatefromstring', |
|
| 95 | - ]; |
|
| 96 | - |
|
| 97 | - /** @var SystemConfig */ |
|
| 98 | - private $systemConfig; |
|
| 99 | - |
|
| 100 | - public function __construct(SystemConfig $systemConfig) { |
|
| 101 | - $this->systemConfig = $systemConfig; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - public const methodsWithSensitiveParametersByClass = [ |
|
| 105 | - SetupController::class => [ |
|
| 106 | - 'run', |
|
| 107 | - 'display', |
|
| 108 | - 'loadAutoConfig', |
|
| 109 | - ], |
|
| 110 | - Setup::class => [ |
|
| 111 | - 'install' |
|
| 112 | - ], |
|
| 113 | - Key::class => [ |
|
| 114 | - '__construct' |
|
| 115 | - ], |
|
| 116 | - ]; |
|
| 117 | - |
|
| 118 | - private function editTrace(array &$sensitiveValues, array $traceLine): array { |
|
| 119 | - if (isset($traceLine['args'])) { |
|
| 120 | - $sensitiveValues = array_merge($sensitiveValues, $traceLine['args']); |
|
| 121 | - } |
|
| 122 | - $traceLine['args'] = ['*** sensitive parameters replaced ***']; |
|
| 123 | - return $traceLine; |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - private function filterTrace(array $trace) { |
|
| 127 | - $sensitiveValues = []; |
|
| 128 | - $trace = array_map(function (array $traceLine) use (&$sensitiveValues) { |
|
| 129 | - $className = $traceLine['class'] ?? ''; |
|
| 130 | - if ($className && isset(self::methodsWithSensitiveParametersByClass[$className]) |
|
| 131 | - && in_array($traceLine['function'], self::methodsWithSensitiveParametersByClass[$className], true)) { |
|
| 132 | - return $this->editTrace($sensitiveValues, $traceLine); |
|
| 133 | - } |
|
| 134 | - foreach (self::methodsWithSensitiveParameters as $sensitiveMethod) { |
|
| 135 | - if (strpos($traceLine['function'], $sensitiveMethod) !== false) { |
|
| 136 | - return $this->editTrace($sensitiveValues, $traceLine); |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - return $traceLine; |
|
| 140 | - }, $trace); |
|
| 141 | - return array_map(function (array $traceLine) use ($sensitiveValues) { |
|
| 142 | - if (isset($traceLine['args'])) { |
|
| 143 | - $traceLine['args'] = $this->removeValuesFromArgs($traceLine['args'], $sensitiveValues); |
|
| 144 | - } |
|
| 145 | - return $traceLine; |
|
| 146 | - }, $trace); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - private function removeValuesFromArgs($args, $values) { |
|
| 150 | - foreach ($args as &$arg) { |
|
| 151 | - if (in_array($arg, $values, true)) { |
|
| 152 | - $arg = '*** sensitive parameter replaced ***'; |
|
| 153 | - } elseif (is_array($arg)) { |
|
| 154 | - $arg = $this->removeValuesFromArgs($arg, $values); |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - return $args; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - private function encodeTrace($trace) { |
|
| 161 | - $filteredTrace = $this->filterTrace($trace); |
|
| 162 | - return array_map(function (array $line) { |
|
| 163 | - if (isset($line['args'])) { |
|
| 164 | - $line['args'] = array_map([$this, 'encodeArg'], $line['args']); |
|
| 165 | - } |
|
| 166 | - return $line; |
|
| 167 | - }, $filteredTrace); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - private function encodeArg($arg) { |
|
| 171 | - if (is_object($arg)) { |
|
| 172 | - $data = get_object_vars($arg); |
|
| 173 | - $data['__class__'] = get_class($arg); |
|
| 174 | - return array_map([$this, 'encodeArg'], $data); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - if (is_array($arg)) { |
|
| 178 | - // Only log the first 5 elements of an array unless we are on debug |
|
| 179 | - if ((int)$this->systemConfig->getValue('loglevel', 2) !== 0) { |
|
| 180 | - $elemCount = count($arg); |
|
| 181 | - if ($elemCount > 5) { |
|
| 182 | - $arg = array_slice($arg, 0, 5); |
|
| 183 | - $arg[] = 'And ' . ($elemCount - 5) . ' more entries, set log level to debug to see all entries'; |
|
| 184 | - } |
|
| 185 | - } |
|
| 186 | - return array_map([$this, 'encodeArg'], $arg); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - return $arg; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - public function serializeException(\Throwable $exception) { |
|
| 193 | - $data = [ |
|
| 194 | - 'Exception' => get_class($exception), |
|
| 195 | - 'Message' => $exception->getMessage(), |
|
| 196 | - 'Code' => $exception->getCode(), |
|
| 197 | - 'Trace' => $this->encodeTrace($exception->getTrace()), |
|
| 198 | - 'File' => $exception->getFile(), |
|
| 199 | - 'Line' => $exception->getLine(), |
|
| 200 | - ]; |
|
| 201 | - |
|
| 202 | - if ($exception instanceof HintException) { |
|
| 203 | - $data['Hint'] = $exception->getHint(); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - if ($exception->getPrevious()) { |
|
| 207 | - $data['Previous'] = $this->serializeException($exception->getPrevious()); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - return $data; |
|
| 211 | - } |
|
| 39 | + public const methodsWithSensitiveParameters = [ |
|
| 40 | + // Session/User |
|
| 41 | + 'completeLogin', |
|
| 42 | + 'login', |
|
| 43 | + 'checkPassword', |
|
| 44 | + 'checkPasswordNoLogging', |
|
| 45 | + 'loginWithPassword', |
|
| 46 | + 'updatePrivateKeyPassword', |
|
| 47 | + 'validateUserPass', |
|
| 48 | + 'loginWithToken', |
|
| 49 | + '{closure}', |
|
| 50 | + 'createSessionToken', |
|
| 51 | + |
|
| 52 | + // Provisioning |
|
| 53 | + 'addUser', |
|
| 54 | + |
|
| 55 | + // TokenProvider |
|
| 56 | + 'getToken', |
|
| 57 | + 'isTokenPassword', |
|
| 58 | + 'getPassword', |
|
| 59 | + 'decryptPassword', |
|
| 60 | + 'logClientIn', |
|
| 61 | + 'generateToken', |
|
| 62 | + 'validateToken', |
|
| 63 | + |
|
| 64 | + // TwoFactorAuth |
|
| 65 | + 'solveChallenge', |
|
| 66 | + 'verifyChallenge', |
|
| 67 | + |
|
| 68 | + // ICrypto |
|
| 69 | + 'calculateHMAC', |
|
| 70 | + 'encrypt', |
|
| 71 | + 'decrypt', |
|
| 72 | + |
|
| 73 | + // LoginController |
|
| 74 | + 'tryLogin', |
|
| 75 | + 'confirmPassword', |
|
| 76 | + |
|
| 77 | + // LDAP |
|
| 78 | + 'bind', |
|
| 79 | + 'areCredentialsValid', |
|
| 80 | + 'invokeLDAPMethod', |
|
| 81 | + |
|
| 82 | + // Encryption |
|
| 83 | + 'storeKeyPair', |
|
| 84 | + 'setupUser', |
|
| 85 | + 'checkSignature', |
|
| 86 | + |
|
| 87 | + // files_external: OCA\Files_External\MountConfig |
|
| 88 | + 'getBackendStatus', |
|
| 89 | + |
|
| 90 | + // files_external: UserStoragesController |
|
| 91 | + 'update', |
|
| 92 | + |
|
| 93 | + // Preview providers, don't log big data strings |
|
| 94 | + 'imagecreatefromstring', |
|
| 95 | + ]; |
|
| 96 | + |
|
| 97 | + /** @var SystemConfig */ |
|
| 98 | + private $systemConfig; |
|
| 99 | + |
|
| 100 | + public function __construct(SystemConfig $systemConfig) { |
|
| 101 | + $this->systemConfig = $systemConfig; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + public const methodsWithSensitiveParametersByClass = [ |
|
| 105 | + SetupController::class => [ |
|
| 106 | + 'run', |
|
| 107 | + 'display', |
|
| 108 | + 'loadAutoConfig', |
|
| 109 | + ], |
|
| 110 | + Setup::class => [ |
|
| 111 | + 'install' |
|
| 112 | + ], |
|
| 113 | + Key::class => [ |
|
| 114 | + '__construct' |
|
| 115 | + ], |
|
| 116 | + ]; |
|
| 117 | + |
|
| 118 | + private function editTrace(array &$sensitiveValues, array $traceLine): array { |
|
| 119 | + if (isset($traceLine['args'])) { |
|
| 120 | + $sensitiveValues = array_merge($sensitiveValues, $traceLine['args']); |
|
| 121 | + } |
|
| 122 | + $traceLine['args'] = ['*** sensitive parameters replaced ***']; |
|
| 123 | + return $traceLine; |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + private function filterTrace(array $trace) { |
|
| 127 | + $sensitiveValues = []; |
|
| 128 | + $trace = array_map(function (array $traceLine) use (&$sensitiveValues) { |
|
| 129 | + $className = $traceLine['class'] ?? ''; |
|
| 130 | + if ($className && isset(self::methodsWithSensitiveParametersByClass[$className]) |
|
| 131 | + && in_array($traceLine['function'], self::methodsWithSensitiveParametersByClass[$className], true)) { |
|
| 132 | + return $this->editTrace($sensitiveValues, $traceLine); |
|
| 133 | + } |
|
| 134 | + foreach (self::methodsWithSensitiveParameters as $sensitiveMethod) { |
|
| 135 | + if (strpos($traceLine['function'], $sensitiveMethod) !== false) { |
|
| 136 | + return $this->editTrace($sensitiveValues, $traceLine); |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + return $traceLine; |
|
| 140 | + }, $trace); |
|
| 141 | + return array_map(function (array $traceLine) use ($sensitiveValues) { |
|
| 142 | + if (isset($traceLine['args'])) { |
|
| 143 | + $traceLine['args'] = $this->removeValuesFromArgs($traceLine['args'], $sensitiveValues); |
|
| 144 | + } |
|
| 145 | + return $traceLine; |
|
| 146 | + }, $trace); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + private function removeValuesFromArgs($args, $values) { |
|
| 150 | + foreach ($args as &$arg) { |
|
| 151 | + if (in_array($arg, $values, true)) { |
|
| 152 | + $arg = '*** sensitive parameter replaced ***'; |
|
| 153 | + } elseif (is_array($arg)) { |
|
| 154 | + $arg = $this->removeValuesFromArgs($arg, $values); |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + return $args; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + private function encodeTrace($trace) { |
|
| 161 | + $filteredTrace = $this->filterTrace($trace); |
|
| 162 | + return array_map(function (array $line) { |
|
| 163 | + if (isset($line['args'])) { |
|
| 164 | + $line['args'] = array_map([$this, 'encodeArg'], $line['args']); |
|
| 165 | + } |
|
| 166 | + return $line; |
|
| 167 | + }, $filteredTrace); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + private function encodeArg($arg) { |
|
| 171 | + if (is_object($arg)) { |
|
| 172 | + $data = get_object_vars($arg); |
|
| 173 | + $data['__class__'] = get_class($arg); |
|
| 174 | + return array_map([$this, 'encodeArg'], $data); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + if (is_array($arg)) { |
|
| 178 | + // Only log the first 5 elements of an array unless we are on debug |
|
| 179 | + if ((int)$this->systemConfig->getValue('loglevel', 2) !== 0) { |
|
| 180 | + $elemCount = count($arg); |
|
| 181 | + if ($elemCount > 5) { |
|
| 182 | + $arg = array_slice($arg, 0, 5); |
|
| 183 | + $arg[] = 'And ' . ($elemCount - 5) . ' more entries, set log level to debug to see all entries'; |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | + return array_map([$this, 'encodeArg'], $arg); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + return $arg; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + public function serializeException(\Throwable $exception) { |
|
| 193 | + $data = [ |
|
| 194 | + 'Exception' => get_class($exception), |
|
| 195 | + 'Message' => $exception->getMessage(), |
|
| 196 | + 'Code' => $exception->getCode(), |
|
| 197 | + 'Trace' => $this->encodeTrace($exception->getTrace()), |
|
| 198 | + 'File' => $exception->getFile(), |
|
| 199 | + 'Line' => $exception->getLine(), |
|
| 200 | + ]; |
|
| 201 | + |
|
| 202 | + if ($exception instanceof HintException) { |
|
| 203 | + $data['Hint'] = $exception->getHint(); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + if ($exception->getPrevious()) { |
|
| 207 | + $data['Previous'] = $this->serializeException($exception->getPrevious()); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + return $data; |
|
| 211 | + } |
|
| 212 | 212 | } |