mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-03 21:05:45 -05:00
fix bug introduced by security hardening that blocks mounts from being allocated onto server containers
This commit is contained in:
@@ -56,6 +56,15 @@ class Mount extends Model implements Identifiable
|
||||
'/srv/daemon-data',
|
||||
];
|
||||
|
||||
/**
|
||||
* Source paths that cannot be mounted, nor anything beneath them.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
public static array $protectedSourcePaths = [
|
||||
'/etc/pterodactyl',
|
||||
];
|
||||
|
||||
/**
|
||||
* Blacklisted target paths.
|
||||
*
|
||||
|
||||
@@ -13,7 +13,7 @@ class NotInPathHierarchy implements ValidationRule
|
||||
/**
|
||||
* @param list<string> $invalidPaths
|
||||
*/
|
||||
public function __construct(protected array $invalidPaths) {}
|
||||
public function __construct(protected array $invalidPaths, protected bool $includeDescendants = true) {}
|
||||
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
@@ -24,9 +24,17 @@ class NotInPathHierarchy implements ValidationRule
|
||||
$path = Mount::normalizePath($value);
|
||||
|
||||
foreach ($this->invalidPaths as $blocked) {
|
||||
$blocked = rtrim($blocked, '/');
|
||||
$blocked = Mount::normalizePath($blocked);
|
||||
|
||||
if ($path === $blocked || str_starts_with($path, $blocked.'/')) {
|
||||
if ($path === $blocked) {
|
||||
$fail($this->includeDescendants
|
||||
? "The {$attribute} path cannot be or reside within [{$blocked}]."
|
||||
: "The {$attribute} path cannot be [{$blocked}].");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->includeDescendants && str_starts_with($path, $blocked.'/')) {
|
||||
$fail("The {$attribute} path cannot be or reside within [{$blocked}].");
|
||||
|
||||
return;
|
||||
|
||||
@@ -32,14 +32,15 @@ final class MountRules
|
||||
'required',
|
||||
'string',
|
||||
'starts_with:/',
|
||||
new NotInPathHierarchy(Mount::$invalidSourcePaths),
|
||||
new NotInPathHierarchy(Mount::$protectedSourcePaths),
|
||||
new NotInPathHierarchy(Mount::$invalidSourcePaths, includeDescendants: false),
|
||||
],
|
||||
|
||||
'target' => [
|
||||
'required',
|
||||
'string',
|
||||
'starts_with:/',
|
||||
new NotInPathHierarchy(Mount::$invalidTargetPaths),
|
||||
new NotInPathHierarchy(Mount::$invalidTargetPaths, includeDescendants: false),
|
||||
],
|
||||
|
||||
'read_only' => ['sometimes', 'boolean'],
|
||||
|
||||
@@ -72,6 +72,33 @@ test('non admin forbidden', function (string $method, string $routeName): void {
|
||||
$response = $this->{$method}(route($routeName, ['mount' => $mount->id]));
|
||||
$this->assertAccessDeniedJson($response);
|
||||
})->with('mountEndpointsDataProvider');
|
||||
test('mount paths within server volumes and the container directory are allowed', function (string $source, string $target): void {
|
||||
$response = $this->postJson(route('api.admin.mounts.store'), ['name' => 'Volume Mount', 'source' => $source, 'target' => $target]);
|
||||
$response->assertStatus(Response::HTTP_CREATED);
|
||||
$this->assertDatabaseHas('mounts', ['name' => 'Volume Mount', 'source' => $source, 'target' => $target]);
|
||||
})->with([
|
||||
['/var/lib/pterodactyl/volumes/34eddb08-887e-4a6e-b0f1-e4e249b3181c', '/mnt/shared'],
|
||||
['/var/lib/pterodactyl/volumes/34eddb08-887e-4a6e-b0f1-e4e249b3181c/plugins', '/home/container/plugins'],
|
||||
['/srv/daemon-data/34eddb08-887e-4a6e-b0f1-e4e249b3181c', '/home/container/shared'],
|
||||
]);
|
||||
test('protected mount paths are rejected', function (string $field, string $path): void {
|
||||
$payload = ['name' => 'Protected Mount', 'source' => '/mnt/source', 'target' => '/mnt/target'];
|
||||
$payload[$field] = $path;
|
||||
$response = $this->postJson(route('api.admin.mounts.store'), $payload);
|
||||
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
expect(collect($response->json('errors'))->firstWhere('meta.source_field', $field))->not->toBeNull();
|
||||
})->with([
|
||||
['source', '/var/lib/pterodactyl/volumes'],
|
||||
['source', '/var/lib/pterodactyl/volumes/'],
|
||||
['source', '/var/lib/pterodactyl/volumes/abc/..'],
|
||||
['source', '/srv/daemon-data'],
|
||||
['source', '/etc/pterodactyl'],
|
||||
['source', '/etc/pterodactyl/config.yml'],
|
||||
['source', '/var/lib/pterodactyl/volumes/../../../../etc/pterodactyl'],
|
||||
['target', '/home/container'],
|
||||
['target', '/home/container/'],
|
||||
['target', '/home/container/plugins/..'],
|
||||
]);
|
||||
test('invalid payloads return validation errors', function (): void {
|
||||
$response = $this->postJson(route('api.admin.mounts.store'), []);
|
||||
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
|
||||
|
||||
Reference in New Issue
Block a user