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>
This commit is contained in:
1 parent
8b0e82d806
commit
0678d72eda
4 files changed
+124
-1
No files matched your search
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Error;
|
||||
|
||||
use Cake\Error\ErrorLogger;
|
||||
use Stringable;
|
||||
|
||||
/**
|
||||
* Error logger that never writes secrets from URLs to the log.
|
||||
*
|
||||
* The admin token (/admin/{token}) and the embed key (/e/{key}) are the only
|
||||
* authorisation of PlenarIO. CakePHP logs the request URL, the referer and
|
||||
* exception messages (e.g. "A route matching /admin/… could not be found"),
|
||||
* so every message is redacted before it is written.
|
||||
* Configured via `Error.logger` in config/app.php.
|
||||
*/
|
||||
class RedactingErrorLogger extends ErrorLogger
|
||||
{
|
||||
/**
|
||||
* Same character set and length as the token/key route patterns in config/routes.php.
|
||||
*/
|
||||
protected const PATTERN = '#/(admin|e)/[A-Za-z0-9_-]{20,64}(?=[/?\#"\'\s]|$)#m';
|
||||
|
||||
public const PLACEHOLDER = '[redacted]';
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function log($level, Stringable|string $message, array $context = []): void
|
||||
{
|
||||
parent::log($level, static::redact((string)$message), $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces admin tokens and embed keys in URLs with a placeholder.
|
||||
*
|
||||
* @param string $message Log message
|
||||
* @return string
|
||||
*/
|
||||
public static function redact(string $message): string
|
||||
{
|
||||
return (string)preg_replace(self::PATTERN, '/$1/' . self::PLACEHOLDER, $message);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user