Skip to content

Commit

Permalink
Fix LogRecord "extra" data leaking between handlers (#1819)
Browse files Browse the repository at this point in the history
Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>
  • Loading branch information
cosminardeleanu and Seldaek committed Oct 27, 2023
1 parent 8ff4ab5 commit b0f4bf7
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/Monolog/Handler/FallbackGroupHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function handle(LogRecord $record): bool
}
foreach ($this->handlers as $handler) {
try {
$handler->handle($record);
$handler->handle(clone $record);
break;
} catch (Throwable $e) {
// What throwable?
Expand All @@ -58,7 +58,7 @@ public function handleBatch(array $records): void

foreach ($this->handlers as $handler) {
try {
$handler->handleBatch($records);
$handler->handleBatch(array_map(fn ($record) => clone $record, $records));
break;
} catch (Throwable $e) {
// What throwable?
Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/Handler/GroupHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function handle(LogRecord $record): bool
}

foreach ($this->handlers as $handler) {
$handler->handle($record);
$handler->handle(clone $record);
}

return false === $this->bubble;
Expand All @@ -90,7 +90,7 @@ public function handleBatch(array $records): void
}

foreach ($this->handlers as $handler) {
$handler->handleBatch($records);
$handler->handleBatch(array_map(fn ($record) => clone $record, $records));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/Handler/WhatFailureGroupHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function handle(LogRecord $record): bool

foreach ($this->handlers as $handler) {
try {
$handler->handle($record);
$handler->handle(clone $record);
} catch (Throwable) {
// What failure?
}
Expand All @@ -57,7 +57,7 @@ public function handleBatch(array $records): void

foreach ($this->handlers as $handler) {
try {
$handler->handleBatch($records);
$handler->handleBatch(array_map(fn ($record) => clone $record, $records));
} catch (Throwable) {
// What failure?
}
Expand Down
8 changes: 4 additions & 4 deletions src/Monolog/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,11 @@ public function addRecord(int|Level $level, string $message, array $context = []
$recordInitialized = count($this->processors) === 0;

$record = new LogRecord(
datetime: $datetime ?? new DateTimeImmutable($this->microsecondTimestamps, $this->timezone),
channel: $this->name,
level: self::toMonologLevel($level),
message: $message,
context: $context,
level: self::toMonologLevel($level),
channel: $this->name,
datetime: $datetime ?? new DateTimeImmutable($this->microsecondTimestamps, $this->timezone),
extra: [],
);
$handled = false;
Expand All @@ -386,7 +386,7 @@ public function addRecord(int|Level $level, string $message, array $context = []
// once the record is initialized, send it to all handlers as long as the bubbling chain is not interrupted
try {
$handled = true;
if (true === $handler->handle($record)) {
if (true === $handler->handle(clone $record)) {
break;
}
} catch (Throwable $e) {
Expand Down
4 changes: 1 addition & 3 deletions tests/Monolog/Handler/ExceptionTestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ class ExceptionTestHandler extends TestHandler
/**
* @inheritDoc
*/
public function handle(LogRecord $record): bool
protected function write(LogRecord $record): void
{
throw new Exception("ExceptionTestHandler::handle");

parent::handle($record);
}
}
34 changes: 34 additions & 0 deletions tests/Monolog/Handler/FallbackGroupHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Monolog\Handler;

use Monolog\Level;
use Monolog\LogRecord;
use Monolog\Test\TestCase;

class FallbackGroupHandlerTest extends TestCase
Expand Down Expand Up @@ -138,4 +139,37 @@ public function testHandleBatchUsesProcessors()
$this->assertTrue($records[0]['extra']['foo2']);
$this->assertTrue($records[1]['extra']['foo2']);
}

public function testProcessorsDoNotInterfereBetweenHandlers()
{
$t1 = new ExceptionTestHandler();
$t2 = new TestHandler();
$handler = new FallbackGroupHandler([$t1, $t2]);

$t1->pushProcessor(function (LogRecord $record) {
$record->extra['foo'] = 'bar';

return $record;
});
$handler->handle($this->getRecord());

self::assertSame([], $t2->getRecords()[0]->extra);
}

public function testProcessorsDoNotInterfereBetweenHandlersWithBatch()
{
$t1 = new ExceptionTestHandler();
$t2 = new TestHandler();
$handler = new FallbackGroupHandler([$t1, $t2]);

$t1->pushProcessor(function (LogRecord $record) {
$record->extra['foo'] = 'bar';

return $record;
});

$handler->handleBatch([$this->getRecord()]);

self::assertSame([], $t2->getRecords()[0]->extra);
}
}
34 changes: 34 additions & 0 deletions tests/Monolog/Handler/GroupHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Monolog\Handler;

use Monolog\LogRecord;
use Monolog\Test\TestCase;
use Monolog\Level;

Expand Down Expand Up @@ -117,4 +118,37 @@ public function testHandleBatchUsesProcessors()
$this->assertTrue($records[1]['extra']['foo2']);
}
}

public function testProcessorsDoNotInterfereBetweenHandlers()
{
$t1 = new TestHandler();
$t2 = new TestHandler();
$handler = new GroupHandler([$t1, $t2]);

$t1->pushProcessor(function (LogRecord $record) {
$record->extra['foo'] = 'bar';

return $record;
});
$handler->handle($this->getRecord());

self::assertSame([], $t2->getRecords()[0]->extra);
}

public function testProcessorsDoNotInterfereBetweenHandlersWithBatch()
{
$t1 = new TestHandler();
$t2 = new TestHandler();
$handler = new GroupHandler([$t1, $t2]);

$t1->pushProcessor(function (LogRecord $record) {
$record->extra['foo'] = 'bar';

return $record;
});

$handler->handleBatch([$this->getRecord()]);

self::assertSame([], $t2->getRecords()[0]->extra);
}
}
34 changes: 34 additions & 0 deletions tests/Monolog/Handler/WhatFailureGroupHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Monolog\Handler;

use Monolog\LogRecord;
use Monolog\Test\TestCase;
use Monolog\Level;

Expand Down Expand Up @@ -136,4 +137,37 @@ public function testHandleException()
$records = $test->getRecords();
$this->assertTrue($records[0]['extra']['foo']);
}

public function testProcessorsDoNotInterfereBetweenHandlers()
{
$t1 = new TestHandler();
$t2 = new TestHandler();
$handler = new WhatFailureGroupHandler([$t1, $t2]);

$t1->pushProcessor(function (LogRecord $record) {
$record->extra['foo'] = 'bar';

return $record;
});
$handler->handle($this->getRecord());

self::assertSame([], $t2->getRecords()[0]->extra);
}

public function testProcessorsDoNotInterfereBetweenHandlersWithBatch()
{
$t1 = new TestHandler();
$t2 = new TestHandler();
$handler = new WhatFailureGroupHandler([$t1, $t2]);

$t1->pushProcessor(function (LogRecord $record) {
$record->extra['foo'] = 'bar';

return $record;
});

$handler->handleBatch([$this->getRecord()]);

self::assertSame([], $t2->getRecords()[0]->extra);
}
}
21 changes: 15 additions & 6 deletions tests/Monolog/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,21 @@ public static function useMicrosecondTimestampsProvider()
];
}

public function testProcessorsDoNotInterfereBetweenHandlers()
{
$logger = new Logger('foo');
$logger->pushHandler($t1 = new TestHandler());
$logger->pushHandler($t2 = new TestHandler());
$t1->pushProcessor(function (LogRecord $record) {
$record->extra['foo'] = 'bar';

return $record;
});
$logger->error('Foo');

self::assertSame([], $t2->getRecords()[0]->extra);
}

/**
* @covers Logger::setExceptionHandler
*/
Expand Down Expand Up @@ -804,9 +819,6 @@ public function testLogWithDateTime()
}
}

/**
* @requires PHP 8.1
*/
public function testLogCycleDetectionWithFibersWithoutCycle()
{
$logger = new Logger(__METHOD__);
Expand All @@ -832,9 +844,6 @@ public function testLogCycleDetectionWithFibersWithoutCycle()
self::assertCount(10, $testHandler->getRecords());
}

/**
* @requires PHP 8.1
*/
public function testLogCycleDetectionWithFibersWithCycle()
{
$logger = new Logger(__METHOD__);
Expand Down

0 comments on commit b0f4bf7

Please sign in to comment.