Cleanup code in MountController.php

This commit is contained in:
Matthew Penner 2020-10-17 14:37:18 -06:00
parent f7520b721b
commit 050075b835
2 changed files with 41 additions and 33 deletions

View file

@ -3,6 +3,7 @@
namespace Pterodactyl\Http\Controllers\Admin; namespace Pterodactyl\Http\Controllers\Admin;
use Ramsey\Uuid\Uuid; use Ramsey\Uuid\Uuid;
use Illuminate\Support\Str;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Pterodactyl\Models\Nest; use Pterodactyl\Models\Nest;
use Pterodactyl\Models\Mount; use Pterodactyl\Models\Mount;
@ -101,28 +102,21 @@ class MountController extends Controller
*/ */
public function create(MountFormRequest $request) public function create(MountFormRequest $request)
{ {
/** @var \Pterodactyl\Models\Mount $mount */
$model = (new Mount())->fill($request->validated()); $model = (new Mount())->fill($request->validated());
$model->forceFill(['uuid' => Uuid::uuid4()->toString()]); $model->forceFill(['uuid' => Uuid::uuid4()->toString()]);
if (str_starts_with($model->source, '/etc/pterodactyl')) { foreach (Mount::$invalidSourcePaths as $path) {
$this->alert->danger('Invalid source path: "/etc/pterodactyl" cannot be used as a source path.')->flash(); if (Str::startsWith($model->source, $path)) {
return redirect()->route('admin.mounts'); $this->alert->danger('"' . $path . '" cannot be used as a source path.')->flash();
return redirect()->route('admin.mounts');
}
} }
if (str_starts_with($model->source, '/var/lib/pterodactyl/volumes')) { foreach (Mount::$invalidTargetPaths as $path) {
$this->alert->danger('Invalid source path: "/var/lib/pterodactyl/volumes" cannot be used as a source path.')->flash(); if (Str::startsWith($model->target, $path)) {
return redirect()->route('admin.mounts'); $this->alert->danger('"' . $path . '" cannot be used as a target path.')->flash();
} return redirect()->route('admin.mounts');
}
if (str_starts_with($model->source, '/srv/daemon-data')) {
$this->alert->danger('Invalid source path: "/srv/daemon-data" cannot be used as a source path.')->flash();
return redirect()->route('admin.mounts');
}
if (str_starts_with($model->target, '/home/container')) {
$this->alert->danger('Invalid target path: "/home/container" cannot be used as a target path.')->flash();
return redirect()->route('admin.mounts');
} }
$model->saveOrFail(); $model->saveOrFail();
@ -150,24 +144,18 @@ class MountController extends Controller
$mount->forceFill($request->validated()); $mount->forceFill($request->validated());
if (str_starts_with($mount->source, '/etc/pterodactyl')) { foreach (Mount::$invalidSourcePaths as $path) {
$this->alert->danger('Invalid source path: "/etc/pterodactyl" cannot be used as a source path.')->flash(); if (Str::startsWith($mount->source, $path)) {
return redirect()->route('admin.mounts.view', $mount->id); $this->alert->danger('"' . $path . '" cannot be used as a source path.')->flash();
return redirect()->route('admin.mounts.view', $mount->id);
}
} }
if (str_starts_with($mount->source, '/var/lib/pterodactyl/volumes')) { foreach (Mount::$invalidTargetPaths as $path) {
$this->alert->danger('Invalid source path: "/var/lib/pterodactyl/volumes" cannot be used as a source path.')->flash(); if (Str::startsWith($mount->target, $path)) {
return redirect()->route('admin.mounts.view', $mount->id); $this->alert->danger('"' . $path . '" cannot be used as a target path.')->flash();
} return redirect()->route('admin.mounts.view', $mount->id);
}
if (str_starts_with($mount->source, '/srv/daemon-data')) {
$this->alert->danger('Invalid source path: "/srv/daemon-data" cannot be used as a source path.')->flash();
return redirect()->route('admin.mounts.view', $mount->id);
}
if (str_starts_with($mount->target, '/home/container')) {
$this->alert->danger('Invalid target path: "/home/container" cannot be used as a target path.')->flash();
return redirect()->route('admin.mounts.view', $mount->id);
} }
$mount->save(); $mount->save();

View file

@ -70,6 +70,26 @@ class Mount extends Model
*/ */
public $timestamps = false; public $timestamps = false;
/**
* Blacklisted source paths
*
* @var string[]
*/
public static $invalidSourcePaths = [
'/etc/pterodactyl',
'/var/lib/pterodactyl/volumes',
'/srv/daemon-data',
];
/**
* Blacklisted target paths
*
* @var string[]
*/
public static $invalidTargetPaths = [
'/home/container',
];
/** /**
* Returns all eggs that have this mount assigned. * Returns all eggs that have this mount assigned.
* *