Files
Plenario/tests/TestCase/Error/RedactingErrorLoggerTest.php
T
gnilebeinandClaude Opus 5.5 0678d72eda
CI / Deploy / test (push) Successful in 54s
CI / Deploy / deploy (push) Skipped
Redact admin tokens and embed keys in error logs
CakePHP's error logger writes the request URL, the referer and exception
messages (e.g. missing routes) to the log, which leaked the secret admin
token and embed key into logs/error.log.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-08 21:53:30 +01:00

74 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Test\TestCase\Error;
use App\Error\RedactingErrorLogger;
use App\Test\TestCase\PlenarioTestTrait;
use Cake\Log\Engine\ArrayLog;
use Cake\Log\Log;
use Cake\TestSuite\IntegrationTestTrait;
use Cake\TestSuite\TestCase;
/**
* Admin tokens and embed keys must never end up in the error log.
*/
class RedactingErrorLoggerTest extends TestCase
{
use IntegrationTestTrait;
use PlenarioTestTrait;
/**
* @var list<string>
*/
protected array $fixtures = ['app.Polls', 'app.Slides', 'app.SlideOptions', 'app.Responses'];
protected function setUp(): void
{
parent::setUp();
Log::drop('error');
Log::setConfig('error', ['className' => ArrayLog::class, 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency']]);
}
protected function tearDown(): void
{
Log::drop('error');
parent::tearDown();
}
public function testRedactsTokensAndKeys(): void
{
$token = str_repeat('a', 20) . '-_B9';
$message = "Request URL: /admin/$token/slides/5/edit\n"
. "Referer URL: https://example.org/e/$token/12\n"
. "A route matching \"/admin/$token?x=1\" could not be found.\n"
. "Request URL: /admin/$token";
$redacted = RedactingErrorLogger::redact($message);
$this->assertStringNotContainsString($token, $redacted);
$this->assertStringContainsString('/admin/[redacted]/slides/5/edit', $redacted);
$this->assertStringContainsString('/e/[redacted]/12', $redacted);
$this->assertStringContainsString('"/admin/[redacted]?x=1"', $redacted);
}
public function testLeavesOtherUrlsAlone(): void
{
$message = 'Request URL: /p/K7P2QX/state and /admin/short';
$this->assertSame($message, RedactingErrorLogger::redact($message));
}
public function testErrorLogNeverContainsTheAdminToken(): void
{
[, $token] = $this->createPoll();
$this->get("/admin/$token/slides/999999/edit");
$this->assertResponseCode(404);
$logs = implode("\n", Log::engine('error')->read());
$this->assertStringContainsString('/admin/[redacted]/slides/999999/edit', $logs);
$this->assertStringNotContainsString($token, $logs);
}
}