ExceptionSerializer.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Robin Appelman <robin@icewind.nl>
  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. namespace OC\Log;
  22. use OC\HintException;
  23. class ExceptionSerializer {
  24. const methodsWithSensitiveParameters = [
  25. // Session/User
  26. 'completeLogin',
  27. 'login',
  28. 'checkPassword',
  29. 'checkPasswordNoLogging',
  30. 'loginWithPassword',
  31. 'updatePrivateKeyPassword',
  32. 'validateUserPass',
  33. 'loginWithToken',
  34. '{closure}',
  35. 'createSessionToken',
  36. // Provisioning
  37. 'addUser',
  38. // TokenProvider
  39. 'getToken',
  40. 'isTokenPassword',
  41. 'getPassword',
  42. 'decryptPassword',
  43. 'logClientIn',
  44. 'generateToken',
  45. 'validateToken',
  46. // TwoFactorAuth
  47. 'solveChallenge',
  48. 'verifyChallenge',
  49. // ICrypto
  50. 'calculateHMAC',
  51. 'encrypt',
  52. 'decrypt',
  53. // LoginController
  54. 'tryLogin',
  55. 'confirmPassword',
  56. // LDAP
  57. 'bind',
  58. 'areCredentialsValid',
  59. 'invokeLDAPMethod',
  60. // Encryption
  61. 'storeKeyPair',
  62. 'setupUser',
  63. ];
  64. private function filterTrace(array $trace) {
  65. $sensitiveValues = [];
  66. $trace = array_map(function (array $traceLine) use (&$sensitiveValues) {
  67. foreach (self::methodsWithSensitiveParameters as $sensitiveMethod) {
  68. if (strpos($traceLine['function'], $sensitiveMethod) !== false) {
  69. $sensitiveValues = array_merge($sensitiveValues, $traceLine['args']);
  70. $traceLine['args'] = ['*** sensitive parameters replaced ***'];
  71. return $traceLine;
  72. }
  73. }
  74. return $traceLine;
  75. }, $trace);
  76. return array_map(function (array $traceLine) use ($sensitiveValues) {
  77. if (isset($traceLine['args'])) {
  78. $traceLine['args'] = $this->removeValuesFromArgs($traceLine['args'], $sensitiveValues);
  79. }
  80. return $traceLine;
  81. }, $trace);
  82. }
  83. private function removeValuesFromArgs($args, $values) {
  84. foreach ($args as &$arg) {
  85. if (in_array($arg, $values, true)) {
  86. $arg = '*** sensitive parameter replaced ***';
  87. } else if (is_array($arg)) {
  88. $arg = $this->removeValuesFromArgs($arg, $values);
  89. }
  90. }
  91. return $args;
  92. }
  93. private function encodeTrace($trace) {
  94. $filteredTrace = $this->filterTrace($trace);
  95. return array_map(function (array $line) {
  96. if (isset($line['args'])) {
  97. $line['args'] = array_map([$this, 'encodeArg'], $line['args']);
  98. }
  99. return $line;
  100. }, $filteredTrace);
  101. }
  102. private function encodeArg($arg) {
  103. if (is_object($arg)) {
  104. $data = get_object_vars($arg);
  105. $data['__class__'] = get_class($arg);
  106. return array_map([$this, 'encodeArg'], $data);
  107. } else if (is_array($arg)) {
  108. return array_map([$this, 'encodeArg'], $arg);
  109. } else {
  110. return $arg;
  111. }
  112. }
  113. public function serializeException(\Throwable $exception) {
  114. $data = [
  115. 'Exception' => get_class($exception),
  116. 'Message' => $exception->getMessage(),
  117. 'Code' => $exception->getCode(),
  118. 'Trace' => $this->encodeTrace($exception->getTrace()),
  119. 'File' => $exception->getFile(),
  120. 'Line' => $exception->getLine(),
  121. ];
  122. if ($exception instanceof HintException) {
  123. $data['Hint'] = $exception->getHint();
  124. }
  125. if ($exception->getPrevious()) {
  126. $data['Previous'] = $this->serializeException($exception->getPrevious());
  127. }
  128. return $data;
  129. }
  130. }