Merge pull request 'Redact admin tokens and embed keys in error logs' (#29) from fix/redact-tokens-in-logs into main
Reviewed-on: #29
This commit was merged in pull request #29.
This commit is contained in:
commit
5dce584992
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