-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPathNormalizer.php
More file actions
64 lines (54 loc) · 2.62 KB
/
Copy pathPathNormalizer.php
File metadata and controls
64 lines (54 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
declare(strict_types = 1);
namespace ScriptDevelopment\KendoErrorTracker;
use const DIRECTORY_SEPARATOR;
use function preg_replace;
use function str_replace;
/**
* Strips the app's own base path from every absolute path in a stack trace.
*
* Fingerprint stability is the client's responsibility (KD-0771 D6): the same
* exception thrown from `/var/www/html/app/Foo.php` and `/home/forge/app/Foo.php`
* must normalize to the identical `app/Foo.php` so the kendo server hashes them
* to one fingerprint. The strip is an EXACT prefix removal of `base_path()`,
* mirroring `laravel/nightwatch`'s `Location::normalizeFile()` — not a guessed
* list of common deploy-root prefixes (that is the server's best-effort fallback
* for raw-HTTP callers, never the client's).
*
* A frame whose path does NOT start with `base_path()` (vendor installed
* outside the app root, a globally-installed tool) is left otherwise
* untouched by that strip, but still leaks the OS username via `/home/<user>/`
* or `/Users/<user>/` (M-3). A secondary redaction pass replaces just the
* username segment of those two shapes, everywhere in the trace, after the
* exact-prefix strip runs.
*/
final readonly class PathNormalizer
{
private const string HOME_USERNAME = '#/home/[^/\s]+/#';
private const string MAC_USERNAME = '#/Users/[^/\s]+/#';
private string $prefix;
public function __construct(string $basePath)
{
// Mirror nightwatch: the prefix carries a trailing separator so the
// strip leaves a clean relative path with no leading slash.
$this->prefix = $basePath . DIRECTORY_SEPARATOR;
}
/**
* Replace every occurrence of the base-path prefix in the trace string,
* then redact the username segment of any remaining `/home/<user>/` or
* `/Users/<user>/` path that did not start with the prefix.
*
* `getTraceAsString()` embeds absolute file paths inline (`#3 /abs/app/Foo.php(10): ...`),
* so a global replace of the prefix normalizes every frame at once. Paths
* that do not start with the prefix (vendor under a symlinked store, the
* trailing `{main}` marker) are left otherwise untouched — exactly
* nightwatch's "return unchanged when the prefix does not match" behavior
* — but still pass through the username-redaction fallback below.
*/
public function normalize(string $trace): string
{
$trace = str_replace($this->prefix, '', $trace);
$trace = (string) preg_replace(self::HOME_USERNAME, '/home/[REDACTED:user]/', $trace);
return (string) preg_replace(self::MAC_USERNAME, '/Users/[REDACTED:user]/', $trace);
}
}