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

View File

@ -70,6 +70,26 @@ class Mount extends Model
*/
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.
*