diff options
author | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2022-07-28 23:30:17 +0200 |
---|---|---|
committer | Arthur Schiwon <blizzz@arthur-schiwon.de> | 2022-07-28 23:30:17 +0200 |
commit | 2a6f46e6891ee82b613be6151d2f51583c45c2bf (patch) | |
tree | 9c2419d41c3fac9674b5b6323e79d4e66b018c64 /lib/private/Log.php | |
parent | bbe15b4b43f95e1600f5122a7bf72a64ee404b36 (diff) | |
download | nextcloud-server-2a6f46e6891ee82b613be6151d2f51583c45c2bf.tar.gz nextcloud-server-2a6f46e6891ee82b613be6151d2f51583c45c2bf.zip |
allow apps to specify methods carrying sensitive parameters
… in order to remove them from logging.
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
Diffstat (limited to 'lib/private/Log.php')
-rw-r--r-- | lib/private/Log.php | 39 |
1 files changed, 32 insertions, 7 deletions
diff --git a/lib/private/Log.php b/lib/private/Log.php index 95e0a833b66..4ab647bc6c1 100644 --- a/lib/private/Log.php +++ b/lib/private/Log.php @@ -36,8 +36,11 @@ declare(strict_types=1); */ namespace OC; +use Exception; use Nextcloud\LogNormalizer\Normalizer; +use OC\AppFramework\Bootstrap\Coordinator; use OCP\Log\IDataLogger; +use Throwable; use function array_merge; use OC\Log\ExceptionSerializer; use OCP\ILogger; @@ -228,7 +231,7 @@ class Log implements ILogger, IDataLogger { $this->crashReporters->delegateBreadcrumb($entry['message'], 'log', $context); } } - } catch (\Throwable $e) { + } catch (Throwable $e) { // make sure we dont hard crash if logging fails } } @@ -300,19 +303,19 @@ class Log implements ILogger, IDataLogger { /** * Logs an exception very detailed * - * @param \Exception|\Throwable $exception + * @param Exception|Throwable $exception * @param array $context * @return void * @since 8.2.0 */ - public function logException(\Throwable $exception, array $context = []) { + public function logException(Throwable $exception, array $context = []) { $app = $context['app'] ?? 'no app in context'; $level = $context['level'] ?? ILogger::ERROR; // if an error is raised before the autoloader is properly setup, we can't serialize exceptions try { - $serializer = new ExceptionSerializer($this->config); - } catch (\Throwable $e) { + $serializer = $this->getSerializer(); + } catch (Throwable $e) { $this->error("Failed to load ExceptionSerializer serializer while trying to log " . $exception->getMessage()); return; } @@ -338,7 +341,7 @@ class Log implements ILogger, IDataLogger { if (!is_null($this->crashReporters)) { $this->crashReporters->delegateReport($exception, $context); } - } catch (\Throwable $e) { + } catch (Throwable $e) { // make sure we dont hard crash if logging fails } } @@ -361,7 +364,7 @@ class Log implements ILogger, IDataLogger { } $context['level'] = $level; - } catch (\Throwable $e) { + } catch (Throwable $e) { // make sure we dont hard crash if logging fails } } @@ -401,4 +404,26 @@ class Log implements ILogger, IDataLogger { } return array_merge(array_diff_key($context, $usedContextKeys), [$messageKey => strtr($message, $replace)]); } + + /** + * @throws Throwable + */ + protected function getSerializer(): ExceptionSerializer { + $serializer = new ExceptionSerializer($this->config); + try { + /** @var Coordinator $coordinator */ + $coordinator = \OCP\Server::get(Coordinator::class); + foreach ($coordinator->getRegistrationContext()->getSensitiveMethods() as $registration) { + $serializer->enlistSensitiveMethods($registration->getName(), $registration->getValue()); + } + // For not every app might be initialized at this time, we cannot assume that the return value + // of getSensitiveMethods() is complete. Running delegates in Coordinator::registerApps() is + // not possible due to dependencies on the one hand. On the other it would work only with + // adding public methods to the PsrLoggerAdapter and this class. + // Thus, serializer cannot be a property. + } catch (Throwable $t) { + // ignore app-defined sensitive methods in this case - they weren't loaded anyway + } + return $serializer; + } } |