Skip to content

Commit 56dba66

Browse files
authored
Merge pull request #7736 from kenjis/update-BaseExceptionHandler-maskSensitiveData
fix: [4.4] merge Exception::maskSensitiveData() fix into BaseExceptionHandler
2 parents 69cfe7b + ec6835d commit 56dba66

3 files changed

Lines changed: 118 additions & 17 deletions

File tree

system/Debug/BaseExceptionHandler.php

Lines changed: 31 additions & 14 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 [
@@ -86,33 +86,50 @@ protected function collectVars(Throwable $exception, int $statusCode): array
8686

8787
/**
8888
* Mask sensitive data in the trace.
89+
*/
90+
protected function maskSensitiveData(array $trace, array $keysToMask, string $path = ''): array
91+
{
92+
foreach ($trace as $i => $line) {
93+
$trace[$i]['args'] = $this->maskData($line['args'], $keysToMask);
94+
}
95+
96+
return $trace;
97+
}
98+
99+
/**
100+
* @param array|object $args
89101
*
90-
* @param array|object $trace
102+
* @return array|object
91103
*/
92-
protected function maskSensitiveData(&$trace, array $keysToMask, string $path = ''): void
104+
private function maskData($args, array $keysToMask, string $path = '')
93105
{
94106
foreach ($keysToMask as $keyToMask) {
95107
$explode = explode('/', $keyToMask);
96108
$index = end($explode);
97109

98110
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} = '******************';
111+
if (is_array($args) && array_key_exists($index, $args)) {
112+
$args[$index] = '******************';
113+
} elseif (
114+
is_object($args) && property_exists($args, $index)
115+
&& isset($args->{$index}) && is_scalar($args->{$index})
116+
) {
117+
$args->{$index} = '******************';
103118
}
104119
}
105120
}
106121

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);
122+
if (is_array($args)) {
123+
foreach ($args as $pathKey => $subarray) {
124+
$args[$pathKey] = $this->maskData($subarray, $keysToMask, $path . '/' . $pathKey);
125+
}
126+
} elseif (is_object($args)) {
127+
foreach ($args as $pathKey => $subarray) {
128+
$args->{$pathKey} = $this->maskData($subarray, $keysToMask, $path . '/' . $pathKey);
114129
}
115130
}
131+
132+
return $args;
116133
}
117134

118135
/**

system/Debug/Exceptions.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,11 @@ protected function collectVars(Throwable $exception, int $statusCode): array
334334
/**
335335
* Mask sensitive data in the trace.
336336
*
337-
* @param array|object $trace
337+
* @param array $trace
338338
*
339-
* @return array|object
339+
* @return array
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)