SanitizeDataProcessor.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /*
  3. * This file is part of Raven.
  4. *
  5. * (c) Sentry Team
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Asterisk out passwords from password fields in frames, http,
  12. * and basic extra data.
  13. *
  14. * @package raven
  15. */
  16. class Raven_Processor_SanitizeDataProcessor extends Raven_Processor
  17. {
  18. const MASK = self::STRING_MASK;
  19. const FIELDS_RE = '/(authorization|password|passwd|secret|password_confirmation|card_number|auth_pw)/i';
  20. const VALUES_RE = '/^(?:\d[ -]*?){13,16}$/';
  21. protected $fields_re;
  22. protected $values_re;
  23. protected $session_cookie_name;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function __construct(Raven_Client $client)
  28. {
  29. parent::__construct($client);
  30. $this->fields_re = self::FIELDS_RE;
  31. $this->values_re = self::VALUES_RE;
  32. $this->session_cookie_name = ini_get('session.name');
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public function setProcessorOptions(array $options)
  38. {
  39. if (isset($options['fields_re'])) {
  40. $this->fields_re = $options['fields_re'];
  41. }
  42. if (isset($options['values_re'])) {
  43. $this->values_re = $options['values_re'];
  44. }
  45. }
  46. /**
  47. * Replace any array values with our mask if the field name or the value matches a respective regex
  48. *
  49. * @param mixed $item Associative array value
  50. * @param string $key Associative array key
  51. */
  52. public function sanitize(&$item, $key)
  53. {
  54. if (empty($item)) {
  55. return;
  56. }
  57. if (preg_match($this->values_re, $item)) {
  58. $item = self::STRING_MASK;
  59. }
  60. if (empty($key)) {
  61. return;
  62. }
  63. if (preg_match($this->fields_re, $key)) {
  64. $item = self::STRING_MASK;
  65. }
  66. }
  67. public function sanitizeException(&$data)
  68. {
  69. foreach ($data['exception']['values'] as &$value) {
  70. $this->sanitizeStacktrace($value['stacktrace']);
  71. }
  72. }
  73. public function sanitizeHttp(&$data)
  74. {
  75. $http = &$data['request'];
  76. if (!empty($http['cookies']) && is_array($http['cookies'])) {
  77. $cookies = &$http['cookies'];
  78. if (!empty($cookies[$this->session_cookie_name])) {
  79. $cookies[$this->session_cookie_name] = self::STRING_MASK;
  80. }
  81. }
  82. if (!empty($http['data']) && is_array($http['data'])) {
  83. array_walk_recursive($http['data'], array($this, 'sanitize'));
  84. }
  85. }
  86. public function sanitizeStacktrace(&$data)
  87. {
  88. foreach ($data['frames'] as &$frame) {
  89. if (empty($frame['vars'])) {
  90. continue;
  91. }
  92. array_walk_recursive($frame['vars'], array($this, 'sanitize'));
  93. }
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function process(&$data)
  99. {
  100. if (!empty($data['exception'])) {
  101. $this->sanitizeException($data);
  102. }
  103. if (!empty($data['stacktrace'])) {
  104. $this->sanitizeStacktrace($data['stacktrace']);
  105. }
  106. if (!empty($data['request'])) {
  107. $this->sanitizeHttp($data);
  108. }
  109. if (!empty($data['extra'])) {
  110. array_walk_recursive($data['extra'], array($this, 'sanitize'));
  111. }
  112. }
  113. /**
  114. * @return string
  115. */
  116. public function getFieldsRe()
  117. {
  118. return $this->fields_re;
  119. }
  120. /**
  121. * @param string $fields_re
  122. */
  123. public function setFieldsRe($fields_re)
  124. {
  125. $this->fields_re = $fields_re;
  126. }
  127. /**
  128. * @return string
  129. */
  130. public function getValuesRe()
  131. {
  132. return $this->values_re;
  133. }
  134. /**
  135. * @param string $values_re
  136. */
  137. public function setValuesRe($values_re)
  138. {
  139. $this->values_re = $values_re;
  140. }
  141. }