Skip to content

Commit 48d5ded

Browse files
authored
Merge pull request #7725 from kenjis/fix-sensitiveDataInTrace
fix: $sensitiveDataInTrace does not work
2 parents f6d834e + 13782b3 commit 48d5ded

File tree

3 files changed

+116
-18
lines changed

3 files changed

+116
-18
lines changed

phpstan-baseline.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -931,11 +931,6 @@
931931
'count' => 1,
932932
'path' => __DIR__ . '/system/Debug/Exceptions.php',
933933
];
934-
$ignoreErrors[] = [
935-
'message' => '#^Method CodeIgniter\\\\Debug\\\\Exceptions\\:\\:maskSensitiveData\\(\\) has no return type specified\\.$#',
936-
'count' => 1,
937-
'path' => __DIR__ . '/system/Debug/Exceptions.php',
938-
];
939934
$ignoreErrors[] = [
940935
'message' => '#^Method CodeIgniter\\\\Debug\\\\Exceptions\\:\\:render\\(\\) has no return type specified\\.$#',
941936
'count' => 1,

system/Debug/Exceptions.php

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

295295
if ($this->config->sensitiveDataInTrace !== []) {
296-
$this->maskSensitiveData($trace, $this->config->sensitiveDataInTrace);
296+
$trace = $this->maskSensitiveData($trace, $this->config->sensitiveDataInTrace);
297297
}
298298

299299
return [
@@ -311,31 +311,52 @@ protected function collectVars(Throwable $exception, int $statusCode): array
311311
* Mask sensitive data in the trace.
312312
*
313313
* @param array|object $trace
314+
*
315+
* @return array|object
314316
*/
315-
protected function maskSensitiveData(&$trace, array $keysToMask, string $path = '')
317+
protected function maskSensitiveData($trace, array $keysToMask, string $path = '')
318+
{
319+
foreach ($trace as $i => $line) {
320+
$trace[$i]['args'] = $this->maskData($line['args'], $keysToMask);
321+
}
322+
323+
return $trace;
324+
}
325+
326+
/**
327+
* @param array|object $args
328+
*
329+
* @return array|object
330+
*/
331+
private function maskData($args, array $keysToMask, string $path = '')
316332
{
317333
foreach ($keysToMask as $keyToMask) {
318334
$explode = explode('/', $keyToMask);
319335
$index = end($explode);
320336

321337
if (strpos(strrev($path . '/' . $index), strrev($keyToMask)) === 0) {
322-
if (is_array($trace) && array_key_exists($index, $trace)) {
323-
$trace[$index] = '******************';
324-
} elseif (is_object($trace) && property_exists($trace, $index) && isset($trace->{$index})) {
325-
$trace->{$index} = '******************';
338+
if (is_array($args) && array_key_exists($index, $args)) {
339+
$args[$index] = '******************';
340+
} elseif (
341+
is_object($args) && property_exists($args, $index)
342+
&& isset($args->{$index}) && is_scalar($args->{$index})
343+
) {
344+
$args->{$index} = '******************';
326345
}
327346
}
328347
}
329348

330-
if (is_object($trace)) {
331-
$trace = get_object_vars($trace);
332-
}
333-
334-
if (is_array($trace)) {
335-
foreach ($trace as $pathKey => $subarray) {
336-
$this->maskSensitiveData($subarray, $keysToMask, $path . '/' . $pathKey);
349+
if (is_array($args)) {
350+
foreach ($args as $pathKey => $subarray) {
351+
$args[$pathKey] = $this->maskData($subarray, $keysToMask, $path . '/' . $pathKey);
352+
}
353+
} elseif (is_object($args)) {
354+
foreach ($args as $pathKey => $subarray) {
355+
$args->{$pathKey} = $this->maskData($subarray, $keysToMask, $path . '/' . $pathKey);
337356
}
338357
}
358+
359+
return $args;
339360
}
340361

341362
/**

tests/system/Debug/ExceptionsTest.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\Database\Exceptions\DatabaseException;
1516
use CodeIgniter\Entity\Exceptions\CastException;
1617
use CodeIgniter\Exceptions\ConfigException;
@@ -147,4 +148,85 @@ public function testRenderBacktrace(): void
147148
);
148149
}
149150
}
151+
152+
public function testMaskSensitiveData(): void
153+
{
154+
$maskSensitiveData = $this->getPrivateMethodInvoker($this->exception, 'maskSensitiveData');
155+
156+
$trace = [
157+
0 => [
158+
'file' => '/var/www/CodeIgniter4/app/Controllers/Home.php',
159+
'line' => 15,
160+
'function' => 'f',
161+
'class' => Home::class,
162+
'type' => '->',
163+
'args' => [
164+
0 => (object) [
165+
'password' => 'secret1',
166+
],
167+
1 => (object) [
168+
'default' => [
169+
'password' => 'secret2',
170+
],
171+
],
172+
2 => [
173+
'password' => 'secret3',
174+
],
175+
3 => [
176+
'default' => ['password' => 'secret4'],
177+
],
178+
],
179+
],
180+
1 => [
181+
'file' => '/var/www/CodeIgniter4/system/CodeIgniter.php',
182+
'line' => 932,
183+
'function' => 'index',
184+
'class' => Home::class,
185+
'type' => '->',
186+
'args' => [
187+
],
188+
],
189+
];
190+
$keysToMask = ['password'];
191+
$path = '';
192+
193+
$newTrace = $maskSensitiveData($trace, $keysToMask, $path);
194+
195+
$this->assertSame(['password' => '******************'], (array) $newTrace[0]['args'][0]);
196+
$this->assertSame(['password' => '******************'], $newTrace[0]['args'][1]->default);
197+
$this->assertSame(['password' => '******************'], $newTrace[0]['args'][2]);
198+
$this->assertSame(['password' => '******************'], $newTrace[0]['args'][3]['default']);
199+
}
200+
201+
public function testMaskSensitiveDataTraceDataKey(): void
202+
{
203+
$maskSensitiveData = $this->getPrivateMethodInvoker($this->exception, 'maskSensitiveData');
204+
205+
$trace = [
206+
0 => [
207+
'file' => '/var/www/CodeIgniter4/app/Controllers/Home.php',
208+
'line' => 15,
209+
'function' => 'f',
210+
'class' => Home::class,
211+
'type' => '->',
212+
'args' => [
213+
],
214+
],
215+
1 => [
216+
'file' => '/var/www/CodeIgniter4/system/CodeIgniter.php',
217+
'line' => 932,
218+
'function' => 'index',
219+
'class' => Home::class,
220+
'type' => '->',
221+
'args' => [
222+
],
223+
],
224+
];
225+
$keysToMask = ['file'];
226+
$path = '';
227+
228+
$newTrace = $maskSensitiveData($trace, $keysToMask, $path);
229+
230+
$this->assertSame('/var/www/CodeIgniter4/app/Controllers/Home.php', $newTrace[0]['file']);
231+
}
150232
}

0 commit comments

Comments
 (0)