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