Skip to content

Commit 10f2270

Browse files
Merge pull request #128 from TheDragonCode/1.x
Fixed token path resolution and validation logic
2 parents e6a27c7 + f9a343a commit 10f2270

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

app/Services/Token.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,19 @@ protected static function fromServer(): ?string
1919
protected static function fromComposer(): ?string
2020
{
2121
foreach (static::composerPath() as $path) {
22-
if (! $data = Process::run('cat ' . $path)) {
22+
$resolved = static::resolvePath($path);
23+
24+
if (! $resolved || ! is_file($resolved) || ! is_readable($resolved)) {
25+
continue;
26+
}
27+
28+
$contents = @file_get_contents($resolved);
29+
if ($contents === false) {
2330
continue;
2431
}
2532

33+
$data = json_decode($contents, true, 512, JSON_THROW_ON_ERROR);
34+
2635
if ($token = $data['github-oauth']['github.com'] ?? null) {
2736
return $token;
2837
}
@@ -42,4 +51,25 @@ protected static function composerPath(): array
4251
'%USERPROFILE%/AppData/Roaming/Composer/auth.json',
4352
];
4453
}
54+
55+
protected static function resolvePath(string $path): ?string
56+
{
57+
if (str_contains($path, '%USERPROFILE%')) {
58+
$userProfile = getenv('USERPROFILE') ?: ($_SERVER['USERPROFILE'] ?? null);
59+
60+
if ($userProfile) {
61+
$path = str_replace('%USERPROFILE%', rtrim($userProfile, '\\/'), $path);
62+
}
63+
}
64+
65+
if (str_starts_with($path, '~')) {
66+
$home = getenv('HOME') ?: ($_SERVER['HOME'] ?? null);
67+
68+
if ($home) {
69+
$path = rtrim($home, '\\/') . substr($path, 1);
70+
}
71+
}
72+
73+
return str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $path);
74+
}
4575
}

0 commit comments

Comments
 (0)