mirror of
https://github.com/RSS-Bridge/rss-bridge.git
synced 2025-08-04 18:14:44 +02:00
feat: sanitize root folder also in php error messages (#3262)
This commit is contained in:
parent
a01c1f6ab0
commit
007f2b2d8a
5 changed files with 38 additions and 17 deletions
|
@ -34,8 +34,8 @@ final class Logger
|
|||
unset($context['e']);
|
||||
$context['type'] = get_class($e);
|
||||
$context['code'] = $e->getCode();
|
||||
$context['message'] = $e->getMessage();
|
||||
$context['file'] = trim_path_prefix($e->getFile());
|
||||
$context['message'] = sanitize_root($e->getMessage());
|
||||
$context['file'] = sanitize_root($e->getFile());
|
||||
$context['line'] = $e->getLine();
|
||||
$context['url'] = get_current_url();
|
||||
$context['trace'] = trace_to_call_points(trace_from_exception($e));
|
||||
|
@ -58,6 +58,7 @@ final class Logger
|
|||
}
|
||||
}
|
||||
}
|
||||
// Intentionally not sanitizing $message
|
||||
$text = sprintf(
|
||||
"[%s] rssbridge.%s %s %s\n",
|
||||
now()->format('Y-m-d H:i:s'),
|
||||
|
@ -65,6 +66,7 @@ final class Logger
|
|||
$message,
|
||||
$context ? Json::encode($context) : ''
|
||||
);
|
||||
// Log to stderr/stdout whatever that is
|
||||
error_log($text);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,8 +34,12 @@ final class RssBridge
|
|||
if ((error_reporting() & $code) === 0) {
|
||||
return false;
|
||||
}
|
||||
$text = sprintf('%s at %s line %s', $message, trim_path_prefix($file), $line);
|
||||
// Drop the current frame
|
||||
$text = sprintf(
|
||||
'%s at %s line %s',
|
||||
sanitize_root($message),
|
||||
sanitize_root($file),
|
||||
$line
|
||||
);
|
||||
Logger::warning($text);
|
||||
if (Debug::isEnabled()) {
|
||||
print sprintf("<pre>%s</pre>\n", e($text));
|
||||
|
@ -49,8 +53,8 @@ final class RssBridge
|
|||
$message = sprintf(
|
||||
'Fatal Error %s: %s in %s line %s',
|
||||
$error['type'],
|
||||
$error['message'],
|
||||
trim_path_prefix($error['file']),
|
||||
sanitize_root($error['message']),
|
||||
sanitize_root($error['file']),
|
||||
$error['line']
|
||||
);
|
||||
Logger::error($message);
|
||||
|
|
|
@ -50,8 +50,8 @@ function create_sane_exception_message(\Throwable $e): string
|
|||
return sprintf(
|
||||
'%s: %s in %s line %s',
|
||||
get_class($e),
|
||||
$e->getMessage(),
|
||||
trim_path_prefix($e->getFile()),
|
||||
sanitize_root($e->getMessage()),
|
||||
sanitize_root($e->getFile()),
|
||||
$e->getLine()
|
||||
);
|
||||
}
|
||||
|
@ -74,7 +74,7 @@ function trace_from_exception(\Throwable $e): array
|
|||
$trace = [];
|
||||
foreach ($frames as $frame) {
|
||||
$trace[] = [
|
||||
'file' => trim_path_prefix($frame['file'] ?? ''),
|
||||
'file' => sanitize_root($frame['file'] ?? ''),
|
||||
'line' => $frame['line'] ?? null,
|
||||
'class' => $frame['class'] ?? null,
|
||||
'type' => $frame['type'] ?? null,
|
||||
|
@ -121,9 +121,17 @@ function frame_to_call_point(array $frame): string
|
|||
*
|
||||
* Example: "/home/davidsf/rss-bridge/index.php" => "index.php"
|
||||
*/
|
||||
function trim_path_prefix(string $filePath): string
|
||||
function sanitize_root(string $filePath): string
|
||||
{
|
||||
return mb_substr($filePath, mb_strlen(dirname(__DIR__)) + 1);
|
||||
// Root folder of the project e.g. /home/satoshi/repos/rss-bridge
|
||||
$root = dirname(__DIR__);
|
||||
return _sanitize_path_name($filePath, $root);
|
||||
}
|
||||
|
||||
function _sanitize_path_name(string $s, string $pathName): string
|
||||
{
|
||||
// Remove all occurrences of $pathName in the string
|
||||
return str_replace(["$pathName/", $pathName], '', $s);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue