Skip to content

Commit

Permalink
Normalize the way backtraces are normalized and remove args for speci…
Browse files Browse the repository at this point in the history
…al cases, fixes Seldaek#1346
  • Loading branch information
Seldaek committed Aug 16, 2019
1 parent c35fbc2 commit 6688b45
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Added support for RFC3164 (outdated BSD syslog protocol) to SyslogUdpHandler
* Fixed issue in GroupHandler and WhatFailureGroupHandler where setting multiple processors would duplicate records
* Fixed issue in SignalHandler restarting syscalls functionality
* Fixed normalizers handling of exception backtraces to avoid serializing arguments in some cases

### 1.24.0 (2018-11-05)

Expand Down
6 changes: 1 addition & 5 deletions src/Monolog/Formatter/JsonFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,8 @@ protected function normalizeException($e)
foreach ($trace as $frame) {
if (isset($frame['file'])) {
$data['trace'][] = $frame['file'].':'.$frame['line'];
} elseif (isset($frame['function']) && $frame['function'] === '{closure}') {
// We should again normalize the frames, because it might contain invalid items
$data['trace'][] = $frame['function'];
} else {
// We should again normalize the frames, because it might contain invalid items
$data['trace'][] = $this->normalize($frame);
$data['trace'][] = (!empty($frame['class']) ? $frame['class'].$frame['type'] : '').$frame['function'];
}
}
}
Expand Down
17 changes: 1 addition & 16 deletions src/Monolog/Formatter/NormalizerFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,23 +151,8 @@ protected function normalizeException($e)
foreach ($trace as $frame) {
if (isset($frame['file'])) {
$data['trace'][] = $frame['file'].':'.$frame['line'];
} elseif (isset($frame['function']) && $frame['function'] === '{closure}') {
// Simplify closures handling
$data['trace'][] = $frame['function'];
} else {
if (isset($frame['args'])) {
// Make sure that objects present as arguments are not serialized nicely but rather only
// as a class name to avoid any unexpected leak of sensitive information
$frame['args'] = array_map(function ($arg) {
if (is_object($arg) && !($arg instanceof \DateTime || $arg instanceof \DateTimeInterface)) {
return sprintf("[object] (%s)", Utils::getClass($arg));
}

return $arg;
}, $frame['args']);
}
// We should again normalize the frames, because it might contain invalid items
$data['trace'][] = $this->toJson($this->normalize($frame), true);
$data['trace'][] = (!empty($frame['class']) ? $frame['class'].$frame['type'] : '').$frame['function'];
}
}

Expand Down
23 changes: 7 additions & 16 deletions tests/Monolog/Formatter/NormalizerFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,21 +390,12 @@ public function testExceptionTraceWithArgs()
$record = array('context' => array('exception' => $e));
$result = $formatter->format($record);

$this->assertRegExp(
'%"resource":"\[resource\] \(stream\)"%',
$result['context']['exception']['trace'][0]
);

if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
$pattern = '%"wrappedResource":"\[object\] \(Monolog\\\\\\\\Formatter\\\\\\\\TestFooNorm: \)"%';
} else {
$pattern = '%\\\\"foo\\\\":null%';
}

// Tests that the wrapped resource is ignored while encoding, only works for PHP <= 5.4
$this->assertRegExp(
$pattern,
$result['context']['exception']['trace'][0]
$this->assertSame(
array(
PHP_VERSION_ID < 50400 ? 'Monolog\Formatter\{closure}' : 'Monolog\Formatter\NormalizerFormatterTest->Monolog\Formatter\{closure}',
__FILE__.':'.(__LINE__-12),
),
array_slice($result['context']['exception']['trace'], 0, 2)
);
}

Expand All @@ -421,7 +412,7 @@ public function testExceptionTraceDoesNotLeakCallUserFuncArgs()
$result = $formatter->format($record);

$this->assertSame(
'{"function":"throwHelper","class":"Monolog\\\\Formatter\\\\NormalizerFormatterTest","type":"->","args":["[object] (Monolog\\\\Formatter\\\\TestInfoLeak)","'.$dt->format('Y-m-d H:i:s').'"]}',
'Monolog\\Formatter\\NormalizerFormatterTest->throwHelper',
$result['context']['exception']['trace'][0]
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Monolog/Formatter/ScalarFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function buildTrace(\Exception $e)
if (isset($frame['file'])) {
$data[] = $frame['file'].':'.$frame['line'];
} else {
$data[] = json_encode($frame);
$data[] = (!empty($frame['class']) ? $frame['class'].$frame['type'] : '').$frame['function'];
}
}

Expand Down

0 comments on commit 6688b45

Please sign in to comment.