Skip to content

Commit 5f6c153

Browse files
committed
Validate resource existence before validating data sent
1 parent 070239a commit 5f6c153

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ This project follows [Semantic Versioning](http://semver.org) guidelines.
1010
* Fix validation error returned when no environment variables are passed, even if there are no variables required.
1111
* Fix improper permissions on `PATCH /api/servers/<id>/startup` endpoint which was preventing enditing any start variables.
1212

13+
### Changed
14+
* Changes order that validation of resource existence occurs in API requests to not try and use a non-existent model when validating data.
15+
1316
### Added
1417
* Adds back client API for sending commands or power toggles to a server though the Panel API: `/api/client/servers/<identifier>`
1518
* Added proper transformer for Packs and re-enabled missing includes on server.

app/Http/Requests/Api/Application/ApplicationApiRequest.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Pterodactyl\Http\Requests\Api\Application;
44

55
use Pterodactyl\Models\ApiKey;
6-
use Illuminate\Database\Eloquent\Model;
76
use Pterodactyl\Services\Acl\Api\AdminAcl;
87
use Illuminate\Foundation\Http\FormRequest;
98
use Pterodactyl\Exceptions\PterodactylException;
@@ -13,6 +12,14 @@
1312

1413
abstract class ApplicationApiRequest extends FormRequest
1514
{
15+
/**
16+
* Tracks if the request has been validated internally or not to avoid
17+
* making duplicate validation calls.
18+
*
19+
* @var bool
20+
*/
21+
private $hasValidated = false;
22+
1623
/**
1724
* The resource that should be checked when performing the authorization
1825
* function for this request.
@@ -96,6 +103,21 @@ public function getModel(string $model)
96103
return $this->route()->parameter($parameterKey);
97104
}
98105

106+
/**
107+
* Validate that the resource exists and can be accessed prior to booting
108+
* the validator and attempting to use the data.
109+
*
110+
* @throws \Illuminate\Auth\Access\AuthorizationException
111+
*/
112+
protected function prepareForValidation()
113+
{
114+
if (! $this->passesAuthorization()) {
115+
$this->failedAuthorization();
116+
}
117+
118+
$this->hasValidated = true;
119+
}
120+
99121
/*
100122
* Determine if the request passes the authorization check as well
101123
* as the exists check.
@@ -110,6 +132,14 @@ public function getModel(string $model)
110132
*/
111133
protected function passesAuthorization()
112134
{
135+
// If we have already validated we do not need to call this function
136+
// again. This is needed to work around Laravel's normal auth validation
137+
// that occurs after validating the request params since we are doing auth
138+
// validation in the prepareForValidation() function.
139+
if ($this->hasValidated) {
140+
return true;
141+
}
142+
113143
if (! parent::passesAuthorization()) {
114144
return false;
115145
}

app/Http/Requests/Api/Application/Servers/ServerWriteRequest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Pterodactyl\Http\Requests\Api\Application\Servers;
44

5-
use Pterodactyl\Models\Server;
65
use Pterodactyl\Services\Acl\Api\AdminAcl;
76
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
87

@@ -17,16 +16,4 @@ class ServerWriteRequest extends ApplicationApiRequest
1716
* @var int
1817
*/
1918
protected $permission = AdminAcl::WRITE;
20-
21-
/**
22-
* Determine if the requested server exists on the Panel.
23-
*
24-
* @return bool
25-
*/
26-
public function resourceExists(): bool
27-
{
28-
$server = $this->route()->parameter('server');
29-
30-
return $server instanceof Server && $server->exists;
31-
}
3219
}

0 commit comments

Comments
 (0)