Skip to content

Commit a99a6f1

Browse files
feat: More readable file paths in JSON error responses (#7772)
1 parent f1718af commit a99a6f1

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

src/Cms/AppErrors.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Closure;
66
use Kirby\Exception\Exception;
7-
use Kirby\Filesystem\F;
87
use Kirby\Http\Response;
98
use Kirby\Toolkit\I18n;
109
use Throwable;
@@ -38,6 +37,25 @@ trait AppErrors
3837
*/
3938
protected Whoops $whoops;
4039

40+
/**
41+
* Replaces absolute file paths with placeholders such as
42+
* {kirby_folder}, {site_folder} or {index_folder} to avoid
43+
* exposing too many details about the filesystem and keeping
44+
* error responses short and readable in debug mode.
45+
*
46+
* @since 5.3.0
47+
*/
48+
protected function disguiseFilePath(string $file): string
49+
{
50+
$disguise = [
51+
$this->root('kirby') => '{kirby}',
52+
$this->root('site') => '{site}',
53+
$this->root('index') => '{index}'
54+
];
55+
56+
return str_replace(array_keys($disguise), array_values($disguise), $file);
57+
}
58+
4159
/**
4260
* Registers the PHP error handler for CLI usage
4361
*/
@@ -150,10 +168,7 @@ protected function handleJsonErrors(): void
150168
'code' => $code,
151169
'message' => $exception->getMessage(),
152170
'details' => $details,
153-
'file' => F::relativepath(
154-
$exception->getFile(),
155-
$this->environment()->get('DOCUMENT_ROOT', '')
156-
),
171+
'file' => $this->disguiseFilePath($exception->getFile()),
157172
'line' => $exception->getLine(),
158173
], $httpCode);
159174
} else {

tests/Cms/App/AppErrorsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public function testHandleJsonErrors(): void
344344
'details' => [
345345
'Some error message'
346346
],
347-
'file' => basename(__FILE__),
347+
'file' => '{kirby}/tests/Cms/App/AppErrorsTest.php',
348348
'line' => $exception->getLine()
349349
]), $this->_getBufferedContent($handlers[0]));
350350
$this->assertInstanceOf('Whoops\Handler\CallbackHandler', $handlers[1]);

0 commit comments

Comments
 (0)