From c52c5d6736022f25e60a67989f9aad9875d1836b Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sat, 17 Oct 2020 14:28:02 -0600 Subject: [PATCH 1/5] Deny certain paths for mounts --- .../Controllers/Admin/MountController.php | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Admin/MountController.php b/app/Http/Controllers/Admin/MountController.php index 3f40e555c..d718c7371 100644 --- a/app/Http/Controllers/Admin/MountController.php +++ b/app/Http/Controllers/Admin/MountController.php @@ -105,6 +105,21 @@ class MountController extends Controller $model = (new Mount())->fill($request->validated()); $model->forceFill(['uuid' => Uuid::uuid4()->toString()]); + 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'); + } + $model->saveOrFail(); $mount = $model->fresh(); @@ -128,7 +143,24 @@ class MountController extends Controller return $this->delete($mount); } - $mount->forceFill($request->validated())->save(); + $mount->forceFill($request->validated()); + + 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); + } + + $mount->save(); $this->alert->success('Mount was updated successfully.')->flash(); From f7520b721be068b574f2d6dd3c8733e655ac0504 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sat, 17 Oct 2020 14:29:29 -0600 Subject: [PATCH 2/5] Deny /etc/pterodactyl as a source path for mounts --- app/Http/Controllers/Admin/MountController.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/Http/Controllers/Admin/MountController.php b/app/Http/Controllers/Admin/MountController.php index d718c7371..1985f9396 100644 --- a/app/Http/Controllers/Admin/MountController.php +++ b/app/Http/Controllers/Admin/MountController.php @@ -105,6 +105,11 @@ class MountController extends Controller $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'); + } + 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'); @@ -145,6 +150,11 @@ 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); + } + 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); From 050075b8359536996a32badb2f86c54d5ef131c4 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sat, 17 Oct 2020 14:37:18 -0600 Subject: [PATCH 3/5] Cleanup code in MountController.php --- .../Controllers/Admin/MountController.php | 54 ++++++++----------- app/Models/Mount.php | 20 +++++++ 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/app/Http/Controllers/Admin/MountController.php b/app/Http/Controllers/Admin/MountController.php index 1985f9396..ce5dbc859 100644 --- a/app/Http/Controllers/Admin/MountController.php +++ b/app/Http/Controllers/Admin/MountController.php @@ -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(); diff --git a/app/Models/Mount.php b/app/Models/Mount.php index b69c0c78d..a77181d8e 100644 --- a/app/Models/Mount.php +++ b/app/Models/Mount.php @@ -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. * From 66b9169458edbe1c7fc70c978d35e98e38f527f2 Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sat, 17 Oct 2020 14:42:08 -0600 Subject: [PATCH 4/5] Cleanup code in MountController.php, again. --- .../Controllers/Admin/MountController.php | 32 +------------------ app/Models/Mount.php | 16 ++++++++++ 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/app/Http/Controllers/Admin/MountController.php b/app/Http/Controllers/Admin/MountController.php index ce5dbc859..79c729976 100644 --- a/app/Http/Controllers/Admin/MountController.php +++ b/app/Http/Controllers/Admin/MountController.php @@ -105,20 +105,6 @@ class MountController extends Controller $model = (new Mount())->fill($request->validated()); $model->forceFill(['uuid' => Uuid::uuid4()->toString()]); - 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'); - } - } - - 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(); $mount = $model->fresh(); @@ -142,23 +128,7 @@ class MountController extends Controller return $this->delete($mount); } - $mount->forceFill($request->validated()); - - 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); - } - } - - 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(); + $mount->forceFill($request->validated())->save(); $this->alert->success('Mount was updated successfully.')->flash(); diff --git a/app/Models/Mount.php b/app/Models/Mount.php index a77181d8e..69c47f01c 100644 --- a/app/Models/Mount.php +++ b/app/Models/Mount.php @@ -2,6 +2,8 @@ namespace Pterodactyl\Models; +use Illuminate\Validation\Rules\NotIn; + /** * @property int $id * @property string $uuid @@ -63,6 +65,20 @@ class Mount extends Model 'user_mountable' => 'sometimes|boolean', ]; + /** + * Implement language verification by overriding Eloquence's gather + * rules function. + */ + public static function getRules() + { + $rules = parent::getRules(); + + $rules['source'][] = new NotIn(Mount::$invalidSourcePaths); + $rules['target'][] = new NotIn(Mount::$invalidSourcePaths); + + return $rules; + } + /** * Disable timestamps on this model. * From 8ba291afb2c3f517cbfd7bc67f79202fd23b92ba Mon Sep 17 00:00:00 2001 From: Matthew Penner Date: Sat, 17 Oct 2020 14:43:07 -0600 Subject: [PATCH 5/5] Fix Mount.php validation rules --- app/Models/Mount.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/Mount.php b/app/Models/Mount.php index 69c47f01c..ee9879b19 100644 --- a/app/Models/Mount.php +++ b/app/Models/Mount.php @@ -74,7 +74,7 @@ class Mount extends Model $rules = parent::getRules(); $rules['source'][] = new NotIn(Mount::$invalidSourcePaths); - $rules['target'][] = new NotIn(Mount::$invalidSourcePaths); + $rules['target'][] = new NotIn(Mount::$invalidTargetPaths); return $rules; }