Skip to content

Commit 23539c9

Browse files
committed
fix: merge Exception::maskSensitiveData() fix into BaseExceptionHandler
1 parent def79d1 commit 23539c9

3 files changed

Lines changed: 117 additions & 14 deletions

File tree

system/Debug/BaseExceptionHandler.php

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ protected function collectVars(Throwable $exception, int $statusCode): array
7070
$trace = $exception->getTrace();
7171

7272
if ($this->config->sensitiveDataInTrace !== []) {
73-
$this->maskSensitiveData($trace, $this->config->sensitiveDataInTrace);
73+
$trace = $this->maskSensitiveData($trace, $this->config->sensitiveDataInTrace);
7474
}
7575

7676
return [
@@ -89,30 +89,49 @@ protected function collectVars(Throwable $exception, int $statusCode): array
8989
*
9090
* @param array|object $trace
9191
*/
92-
protected function maskSensitiveData(&$trace, array $keysToMask, string $path = ''): void
92+
protected function maskSensitiveData($trace, array $keysToMask, string $path = '')
93+
{
94+
foreach ($trace as $i => $line) {
95+
$trace[$i]['args'] = $this->maskData($line['args'], $keysToMask);
96+
}
97+
98+
return $trace;
99+
}
100+
101+
/**
102+
* @param array|object $args
103+
*
104+
* @return array|object
105+
*/
106+
private function maskData($args, array $keysToMask, string $path = '')
93107
{
94108
foreach ($keysToMask as $keyToMask) {
95109
$explode = explode('/', $keyToMask);
96110
$index = end($explode);
97111

98112
if (strpos(strrev($path . '/' . $index), strrev($keyToMask)) === 0) {
99-
if (is_array($trace) && array_key_exists($index, $trace)) {
100-
$trace[$index] = '******************';
101-
} elseif (is_object($trace) && property_exists($trace, $index) && isset($trace->{$index})) {
102-
$trace->{$index} = '******************';
113+
if (is_array($args) && array_key_exists($index, $args)) {
114+
$args[$index] = '******************';
115+
} elseif (
116+
is_object($args) && property_exists($args, $index)
117+
&& isset($args->{$index}) && is_scalar($args->{$index})
118+
) {
119+
$args->{$index} = '******************';
103120
}
104121
}
105122
}
106123

107-
if (is_object($trace)) {
108-
$trace = get_object_vars($trace);
109-
}
110-
111-
if (is_array($trace)) {
112-
foreach ($trace as $pathKey => $subarray) {
113-
$this->maskSensitiveData($subarray, $keysToMask, $path . '/' . $pathKey);
124+
if (is_array($args)) {
125+
foreach ($args as $pathKey => $subarray) {
126+
$args[$pathKey] = $this->maskData($subarray, $keysToMask, $path . '/' . $pathKey);
127+
}
128+
} elseif (is_object($args)) {
129+
foreach ($args as $pathKey => $subarray) {
130+
$args->{$pathKey} = $this->maskData($subarray, $keysToMask, $path . '/' . $pathKey);
114131
}
115132
}
133+
134+
return $args;
116135
}
117136

118137
/**

system/Debug/Exceptions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ protected function collectVars(Throwable $exception, int $statusCode): array
338338
*
339339
* @return array|object
340340
*
341-
* @deprecated No longer used. Moved to BaseExceptionHandler.
341+
* @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
342342
*/
343343
protected function maskSensitiveData($trace, array $keysToMask, string $path = '')
344344
{
@@ -353,6 +353,8 @@ protected function maskSensitiveData($trace, array $keysToMask, string $path = '
353353
* @param array|object $args
354354
*
355355
* @return array|object
356+
*
357+
* @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
356358
*/
357359
private function maskData($args, array $keysToMask, string $path = '')
358360
{

tests/system/Debug/ExceptionHandlerTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace CodeIgniter\Debug;
1313

14+
use App\Controllers\Home;
1415
use CodeIgniter\Exceptions\PageNotFoundException;
1516
use CodeIgniter\Test\CIUnitTestCase;
1617
use CodeIgniter\Test\StreamFilterTrait;
@@ -140,4 +141,85 @@ public function testHandleCLIPageNotFoundException(): void
140141

141142
$this->resetStreamFilterBuffer();
142143
}
144+
145+
public function testMaskSensitiveData(): void
146+
{
147+
$maskSensitiveData = $this->getPrivateMethodInvoker($this->handler, 'maskSensitiveData');
148+
149+
$trace = [
150+
0 => [
151+
'file' => '/var/www/CodeIgniter4/app/Controllers/Home.php',
152+
'line' => 15,
153+
'function' => 'f',
154+
'class' => Home::class,
155+
'type' => '->',
156+
'args' => [
157+
0 => (object) [
158+
'password' => 'secret1',
159+
],
160+
1 => (object) [
161+
'default' => [
162+
'password' => 'secret2',
163+
],
164+
],
165+
2 => [
166+
'password' => 'secret3',
167+
],
168+
3 => [
169+
'default' => ['password' => 'secret4'],
170+
],
171+
],
172+
],
173+
1 => [
174+
'file' => '/var/www/CodeIgniter4/system/CodeIgniter.php',
175+
'line' => 932,
176+
'function' => 'index',
177+
'class' => Home::class,
178+
'type' => '->',
179+
'args' => [
180+
],
181+
],
182+
];
183+
$keysToMask = ['password'];
184+
$path = '';
185+
186+
$newTrace = $maskSensitiveData($trace, $keysToMask, $path);
187+
188+
$this->assertSame(['password' => '******************'], (array) $newTrace[0]['args'][0]);
189+
$this->assertSame(['password' => '******************'], $newTrace[0]['args'][1]->default);
190+
$this->assertSame(['password' => '******************'], $newTrace[0]['args'][2]);
191+
$this->assertSame(['password' => '******************'], $newTrace[0]['args'][3]['default']);
192+
}
193+
194+
public function testMaskSensitiveDataTraceDataKey(): void
195+
{
196+
$maskSensitiveData = $this->getPrivateMethodInvoker($this->handler, 'maskSensitiveData');
197+
198+
$trace = [
199+
0 => [
200+
'file' => '/var/www/CodeIgniter4/app/Controllers/Home.php',
201+
'line' => 15,
202+
'function' => 'f',
203+
'class' => Home::class,
204+
'type' => '->',
205+
'args' => [
206+
],
207+
],
208+
1 => [
209+
'file' => '/var/www/CodeIgniter4/system/CodeIgniter.php',
210+
'line' => 932,
211+
'function' => 'index',
212+
'class' => Home::class,
213+
'type' => '->',
214+
'args' => [
215+
],
216+
],
217+
];
218+
$keysToMask = ['file'];
219+
$path = '';
220+
221+
$newTrace = $maskSensitiveData($trace, $keysToMask, $path);
222+
223+
$this->assertSame('/var/www/CodeIgniter4/app/Controllers/Home.php', $newTrace[0]['file']);
224+
}
143225
}

0 commit comments

Comments
 (0)