1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Facade\Ignition\Middleware; |
4
|
|
|
|
5
|
|
|
use Facade\FlareClient\Report; |
6
|
|
|
|
7
|
|
|
class UpdateMaskedRequestParameters |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
public function handle(Report $report, $next) |
11
|
|
|
{ |
12
|
|
|
if ($fields_to_mask = $this->getFieldsToMask()) { |
13
|
|
|
$context = $report->allContext(); |
14
|
|
|
|
15
|
|
|
$this->maskProperties($fields_to_mask, $context); |
16
|
|
|
|
17
|
|
|
$this->maskQueryString($fields_to_mask, $context['request']['url']); |
18
|
|
|
|
19
|
|
|
$report->userProvidedContext($context); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
return $next($report); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getFieldsToMask(): array |
26
|
|
|
{ |
27
|
|
|
return config('ignition.masked_request_parameters'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function maskProperties(array $fields_to_mask, array &$context, &$changed = null) |
31
|
|
|
{ |
32
|
|
|
$fields_to_mask = array_combine($fields_to_mask, $fields_to_mask); |
33
|
|
|
|
34
|
|
|
foreach ($context as $key => &$value) { |
35
|
|
|
if (is_iterable($value)) { |
36
|
|
|
$this->maskProperties($fields_to_mask, $value, $changed); |
37
|
|
|
|
38
|
|
|
continue; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
if (isset($fields_to_mask[$key])) { |
43
|
|
|
$this->maskProperty($value, $changed); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function maskProperty(&$value, &$changed = null) |
49
|
|
|
{ |
50
|
|
|
if (is_iterable($value)) { |
51
|
|
|
foreach ($value as &$_value) { |
52
|
|
|
$this->maskProperty($_value, $changed); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$initial_value = $value; |
59
|
|
|
|
60
|
|
|
switch (gettype($this->cast($value))) { |
61
|
|
|
case "boolean": |
62
|
|
|
$value = $this->randomiseValueFromArray([true, false]); |
63
|
|
|
|
64
|
|
|
break; |
65
|
|
|
|
66
|
|
|
case "integer": |
67
|
|
|
$value = $this->randomInteger(strlen($value)); |
68
|
|
|
|
69
|
|
|
break; |
70
|
|
|
|
71
|
|
|
case "double": |
72
|
|
|
$value = $this->randomFloat(strlen($value)); |
73
|
|
|
|
74
|
|
|
break; |
75
|
|
|
|
76
|
|
|
case "string": |
77
|
|
|
if (filter_var($value, FILTER_VALIDATE_EMAIL)) { |
78
|
|
|
/*if (class_exists(\Faker\Factory::class)) { |
79
|
|
|
$value = (\Faker\Factory::create())->safeEmail; |
80
|
|
|
|
81
|
|
|
break; |
82
|
|
|
}*/ |
83
|
|
|
|
84
|
|
|
$value = strtolower($this->randomString(30)) . '@example.com'; |
85
|
|
|
|
86
|
|
|
break; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$value = $this->randomString(strlen($value)); |
90
|
|
|
|
91
|
|
|
break; |
92
|
|
|
|
93
|
|
|
case "NULL": |
94
|
|
|
case "unknown type": |
95
|
|
|
$value = null; |
96
|
|
|
|
97
|
|
|
break; |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($changed !== null && $initial_value !== $value) { |
102
|
|
|
$changed = true; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function maskQueryString(array $fields_to_mask, string &$url) |
107
|
|
|
{ |
108
|
|
|
$url_parts = parse_url($url); |
109
|
|
|
if (isset($url_parts['query'])) { |
110
|
|
|
parse_str($url_parts['query'], $query_strings); |
111
|
|
|
|
112
|
|
|
if ($url_parts['query']) { |
113
|
|
|
$changes = false; |
114
|
|
|
foreach ($fields_to_mask as $field) { |
115
|
|
|
if (isset($query_strings[$field])) { |
116
|
|
|
$this->maskProperty($query_strings[$field], $changes); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($changes) { |
121
|
|
|
$url = $url_parts['scheme'] . '://' . $url_parts['host'] . $url_parts['path'] . '?' . http_build_query($query_strings); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function randomiseValueFromArray(array $possible_values) |
128
|
|
|
{ |
129
|
|
|
return $possible_values[array_rand($possible_values)]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function randomString($length): string |
133
|
|
|
{ |
134
|
|
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; |
135
|
|
|
|
136
|
|
|
$randomString = ''; |
137
|
|
|
$charactersLength = strlen($characters); |
138
|
|
|
for ($i = 0; $i < $length; $i++) { |
139
|
|
|
$randomString .= $characters[rand(0, $charactersLength - 1)]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $randomString; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function randomFloat($length): float |
146
|
|
|
{ |
147
|
|
|
$decimal = rand (0, 100) / 100; |
148
|
|
|
$decimal_places = strlen($decimal)-1; |
149
|
|
|
|
150
|
|
|
return (float) substr($this->randomInteger($length), 0, -$decimal_places) . $decimal; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function randomInteger($length): int |
154
|
|
|
{ |
155
|
|
|
return rand(1, (int) str_pad('', $length, 9)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function cast($value) |
159
|
|
|
{ |
160
|
|
|
if (is_numeric($value)) { |
161
|
|
|
if ((int) $value == $value) { |
162
|
|
|
return (int) $value; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return (float) $value; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $value; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|