typed request input

This commit is contained in:
robert dennis
2026-10-12 05:18:57 -04:00
parent bb0e915fde
commit d823ff24cd
16 changed files with 49 additions and 52 deletions
@@ -26,7 +26,6 @@ use Pterodactyl\Http\Requests\Api\Admin\Nodes\Allocations\StoreAllocationRequest
use Pterodactyl\Http\Requests\Api\Admin\Nodes\Allocations\UpdateAllocationRequest;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Node;
use Pterodactyl\Support\JsonValueGuard;
use Pterodactyl\Transformers\Api\Admin\AllocationTransformer;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
@@ -132,7 +131,7 @@ class AllocationController extends AdminApiController
{
$this->assertAllocationBelongsToNode($node, $allocation);
$allocation = $allocations->update($allocation, JsonValueGuard::nullableString($request->validated('alias')));
$allocation = $allocations->update($allocation, $request->filled('alias') ? $request->string('alias')->toString() : null);
Activity::event('admin:node-allocation.alias')
->subject($allocation)
@@ -16,7 +16,6 @@ use Pterodactyl\Http\Controllers\Api\Admin\AdminApiController;
use Pterodactyl\Http\Requests\Api\Admin\Nodes\Allocations\BlockDeleteAllocationRequest;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Node;
use Pterodactyl\Support\JsonValueGuard;
use Throwable;
#[Group('Admin API', 'Root administrator endpoints for managing panel configuration and resources.')]
@@ -32,7 +31,7 @@ class DeleteAllocationBlockController extends AdminApiController
#[ScribeResponse(status: 204, description: 'Unassigned allocations for the IP were deleted.')]
public function __invoke(BlockDeleteAllocationRequest $request, DeletesAllocations $deleter, Node $node): Response
{
$ip = JsonValueGuard::string($request->validated('ip'));
$ip = $request->string('ip')->toString();
// Delete through the action so extension wrappers see each allocation, while
// the transaction keeps the block delete all-or-nothing like the bulk query was.
@@ -19,7 +19,6 @@ use Pterodactyl\Facades\Fractal;
use Pterodactyl\Http\Requests\Api\Client\Account\UpdateEmailRequest;
use Pterodactyl\Http\Requests\Api\Client\Account\UpdatePasswordRequest;
use Pterodactyl\Models\User;
use Pterodactyl\Support\JsonValueGuard;
use Pterodactyl\Transformers\Api\Client\AccountTransformer;
use Throwable;
@@ -69,7 +68,7 @@ class AccountController extends ClientApiController
#[ScribeResponse(self::THROTTLED_ERROR, status: 429, description: 'The account has changed email too many times in the current throttle window.')]
public function updateEmail(UpdateEmailRequest $request, UpdatesUserEmails $emails): JsonResponse
{
$emails->update($request->user(), JsonValueGuard::string($request->validated('email')));
$emails->update($request->user(), $request->string('email')->toString());
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
@@ -87,7 +86,7 @@ class AccountController extends ClientApiController
#[ScribeResponse(self::INVALID_PASSWORD_ERROR, status: 400, description: 'The current password is invalid.')]
public function updatePassword(UpdatePasswordRequest $request, UpdatesUserPasswords $passwords): JsonResponse
{
$passwords->update($request->user(), JsonValueGuard::string($request->validated('password')));
$passwords->update($request->user(), $request->string('password')->toString());
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
@@ -14,7 +14,6 @@ use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\SendCommandRequest;
use Pterodactyl\Models\Server;
use Pterodactyl\Support\JsonValueGuard;
#[Group('Client API', 'Endpoints authenticated as a panel user using a client API token.')]
#[Subgroup('Server Overview', 'Inspect and control an accessible server.')]
@@ -40,7 +39,7 @@ class CommandController extends ClientApiController
#[ScribeResponse(self::OFFLINE_ERROR, status: 502, description: 'The server is not online or Wings rejected the command.')]
public function index(SendCommandRequest $request, SendsServerCommands $commands, Server $server): Response
{
$command = JsonValueGuard::string($request->validated('command'));
$command = $request->string('command')->toString();
$commands->send($server, $command);
return $this->returnNoContent();
@@ -7,6 +7,7 @@ namespace Pterodactyl\Http\Controllers\Api\Client\Servers;
use Carbon\CarbonImmutable;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Knuckles\Scribe\Attributes\Endpoint;
use Knuckles\Scribe\Attributes\Group;
@@ -123,7 +124,7 @@ class FileController extends ClientApiController
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not list the directory.')]
public function directory(ListFilesRequest $request, ListsDirectories $operation, Server $server): array
{
$directory = JsonValueGuard::nullableString($request->validated('directory')) ?? '/';
$directory = $this->path($request, 'directory') ?? '/';
$contents = $operation->list($server, $directory);
return Fractal::collection($contents)
@@ -142,7 +143,7 @@ class FileController extends ClientApiController
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not read the file.')]
public function contents(GetFileContentsRequest $request, ReadsFileContents $operation, Server $server): Response
{
$file = JsonValueGuard::string($request->validated('file'));
$file = $request->string('file')->toString();
$response = $operation->read($server, $file);
Activity::event('server:file.read')->property('file', $file)->log();
@@ -165,7 +166,7 @@ class FileController extends ClientApiController
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not create the download URL.')]
public function download(GetFileContentsRequest $request, NodeJWTService $jwtService, Server $server): array
{
$file = JsonValueGuard::string($request->validated('file'));
$file = $request->string('file')->toString();
$token = $jwtService
->setExpiresAt(CarbonImmutable::now()->addMinutes(15))
->setUser($request->user())
@@ -201,7 +202,7 @@ class FileController extends ClientApiController
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not write the file.')]
public function write(WriteFileContentRequest $request, WritesFileContents $operation, ListsDirectories $lister, Server $server): JsonResponse
{
$file = JsonValueGuard::string($request->validated('file'));
$file = $request->string('file')->toString();
$permission = $this->fileExists($lister, $server, $file) ? Permissions::FileUpdate : Permissions::FileCreate;
throw_unless($request->user()->can($permission->value, $server), AuthorizationException::class);
@@ -220,7 +221,7 @@ class FileController extends ClientApiController
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not create the folder.')]
public function create(CreateFolderRequest $request, CreatesDirectories $operation, Server $server): JsonResponse
{
$operation->create($server, JsonValueGuard::string($request->validated('name')), JsonValueGuard::string($request->validated('root', '/')));
$operation->create($server, $request->string('name')->toString(), $this->path($request, 'root') ?? '/');
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
@@ -235,7 +236,7 @@ class FileController extends ClientApiController
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not rename the files.')]
public function rename(RenameFileRequest $request, RenamesFiles $operation, Server $server): JsonResponse
{
$operation->rename($server, JsonValueGuard::nullableString($request->validated('root')), JsonValueGuard::jsonArray($request->validated('files')));
$operation->rename($server, $this->path($request, 'root'), JsonValueGuard::jsonArray($request->validated('files')));
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
@@ -250,7 +251,7 @@ class FileController extends ClientApiController
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not copy the file.')]
public function copy(CopyFileRequest $request, CopiesFiles $operation, Server $server): JsonResponse
{
$operation->copy($server, JsonValueGuard::string($request->validated('location')));
$operation->copy($server, $request->string('location')->toString());
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
@@ -265,7 +266,7 @@ class FileController extends ClientApiController
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not create the archive.')]
public function compress(CompressFilesRequest $request, CompressesFiles $operation, Server $server): array
{
$file = $operation->compress($server, JsonValueGuard::nullableString($request->validated('root')), JsonValueGuard::jsonArray($request->validated('files')));
$file = $operation->compress($server, $this->path($request, 'root'), JsonValueGuard::jsonArray($request->validated('files')));
return Fractal::item($file)
->transformWith($this->getTransformer(FileObjectTransformer::class))
@@ -280,7 +281,7 @@ class FileController extends ClientApiController
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not extract the archive.')]
public function decompress(DecompressFilesRequest $request, DecompressesFiles $operation, Server $server): JsonResponse
{
$operation->decompress($server, JsonValueGuard::nullableString($request->validated('root')), JsonValueGuard::string($request->validated('file')));
$operation->decompress($server, $this->path($request, 'root'), $request->string('file')->toString());
return new JsonResponse([], JsonResponse::HTTP_NO_CONTENT);
}
@@ -295,7 +296,7 @@ class FileController extends ClientApiController
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not delete the files.')]
public function delete(DeleteFileRequest $request, DeletesFiles $operation, Server $server): JsonResponse
{
$operation->delete($server, JsonValueGuard::nullableString($request->validated('root')), JsonValueGuard::jsonArray($request->validated('files')));
$operation->delete($server, $this->path($request, 'root'), JsonValueGuard::jsonArray($request->validated('files')));
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
@@ -310,7 +311,7 @@ class FileController extends ClientApiController
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not update file permissions.')]
public function chmod(ChmodFilesRequest $request, ChangesFilePermissions $operation, Server $server): JsonResponse
{
$operation->change($server, JsonValueGuard::nullableString($request->validated('root')), JsonValueGuard::jsonArray($request->validated('files')));
$operation->change($server, $this->path($request, 'root'), JsonValueGuard::jsonArray($request->validated('files')));
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
@@ -325,20 +326,29 @@ class FileController extends ClientApiController
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not pull the remote file.')]
public function pull(PullFileRequest $request, PullsFiles $operation, Server $server): JsonResponse
{
$url = JsonValueGuard::string($request->validated('url'));
$directory = JsonValueGuard::nullableString($request->validated('directory'));
$options = $request->safe()->only(['filename']);
$url = $request->string('url')->toString();
$directory = $this->path($request, 'directory');
$options = $request->safe()->has('filename') ? ['filename' => $this->path($request, 'filename')] : [];
foreach (['use_header', 'foreground'] as $flag) {
if ($request->safe()->has($flag)) {
$options[$flag] = $request->boolean($flag);
}
}
$operation->pull($server, $url, $directory, JsonValueGuard::jsonArray($options));
$operation->pull($server, $url, $directory, $options);
return new JsonResponse([], Response::HTTP_NO_CONTENT);
}
/**
* File routes skip TrimStrings, so a path made only of spaces is kept as given;
* only a missing or null value is read as absent.
*/
private function path(Request $request, string $key): ?string
{
return $request->input($key) === null ? null : $request->string($key)->toString();
}
/**
* @throws DaemonConnectionException
*/
@@ -14,7 +14,6 @@ use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\GetServerLogsRequest;
use Pterodactyl\Models\Server;
use Pterodactyl\Support\JsonValueGuard;
#[Group('Client API', 'Endpoints authenticated as a panel user using a client API token.')]
#[Subgroup('Server Overview', 'Inspect and control an accessible server.')]
@@ -53,7 +52,7 @@ class LogController extends ClientApiController
#[ScribeResponse(self::DAEMON_ERROR, status: 502, description: 'Wings could not read the console output.')]
public function __invoke(GetServerLogsRequest $request, ReadsServerLogs $logs, Server $server): array
{
$lines = JsonValueGuard::integer($request->validated('lines') ?? ReadsServerLogs::MAX_LINES);
$lines = $request->integer('lines', ReadsServerLogs::MAX_LINES);
return [
'object' => 'server_logs',
@@ -24,7 +24,6 @@ use Pterodactyl\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationReq
use Pterodactyl\Http\Requests\Api\Client\Servers\Network\UpdateAllocationRequest;
use Pterodactyl\Models\Allocation;
use Pterodactyl\Models\Server;
use Pterodactyl\Support\JsonValueGuard;
use Pterodactyl\Transformers\Api\Client\AllocationTransformer;
#[Group('Client API', 'Endpoints authenticated as a panel user using a client API token.')]
@@ -73,7 +72,7 @@ class NetworkAllocationController extends ClientApiController
#[ResponseFromTransformer(AllocationTransformer::class, Allocation::class, description: 'Allocation notes updated.', factoryStates: ['withServer'], resourceKey: 'allocation')]
public function update(UpdateAllocationRequest $request, UpdatesAllocationNotes $notes, Server $server, Allocation $allocation): array
{
$allocation = $notes->update($allocation, JsonValueGuard::nullableString($request->validated('notes')));
$allocation = $notes->update($allocation, $request->filled('notes') ? $request->string('notes')->toString() : null);
return Fractal::item($allocation)
->transformWith($this->getTransformer(AllocationTransformer::class))
@@ -13,7 +13,6 @@ use Pterodactyl\Contracts\Servers\SendsServerPower;
use Pterodactyl\Http\Controllers\Api\Client\ClientApiController;
use Pterodactyl\Http\Requests\Api\Client\Servers\SendPowerRequest;
use Pterodactyl\Models\Server;
use Pterodactyl\Support\JsonValueGuard;
#[Group('Client API', 'Endpoints authenticated as a panel user using a client API token.')]
#[Subgroup('Server Overview', 'Inspect and control an accessible server.')]
@@ -26,7 +25,7 @@ class PowerController extends ClientApiController
#[ScribeResponse(status: 204, description: 'Power signal accepted by Wings.')]
public function index(SendPowerRequest $request, SendsServerPower $power, Server $server): Response
{
$signal = JsonValueGuard::string($request->validated('signal'));
$signal = $request->string('signal')->toString();
$power->send($server, $signal);
return $this->returnNoContent();
@@ -17,7 +17,6 @@ use Pterodactyl\Http\Requests\Api\Client\Servers\Startup\UpdateStartupVariableRe
use Pterodactyl\Models\EggVariable;
use Pterodactyl\Models\Server;
use Pterodactyl\Services\Servers\StartupCommandService;
use Pterodactyl\Support\JsonValueGuard;
use Pterodactyl\Transformers\Api\Client\EggVariableTransformer;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use UnexpectedValueException;
@@ -75,7 +74,7 @@ class StartupController extends ClientApiController
throw_unless($variable->user_editable, BadRequestHttpException::class, 'The environment variable you are trying to edit is read-only.');
$value = JsonValueGuard::nullableScalarString($request->validated('value'));
$value = $request->filled('value') ? $request->string('value')->toString() : null;
$variables->update($server, $variable->id, $value ?? '');
@@ -19,7 +19,6 @@ use Pterodactyl\Contracts\Users\TogglesTwoFactor;
use Pterodactyl\Exceptions\Http\Base\InvalidPasswordProvidedException;
use Pterodactyl\Http\Requests\Api\Client\Account\DisableTwoFactorRequest;
use Pterodactyl\Http\Requests\Api\Client\Account\EnableTwoFactorRequest;
use Pterodactyl\Support\JsonValueGuard;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Throwable;
@@ -95,10 +94,10 @@ class TwoFactorController extends ClientApiController
public function store(EnableTwoFactorRequest $request, TogglesTwoFactor $twoFactor): JsonResponse
{
$user = $this->authenticatedUser($request);
$password = JsonValueGuard::string($request->validated('password'));
$password = $request->string('password')->toString();
throw_unless(Hash::check($password, $user->password), InvalidPasswordProvidedException::class, trans('validation.internal.invalid_password'));
$code = JsonValueGuard::string($request->validated('code'));
$code = $request->string('code')->toString();
$tokens = $twoFactor->toggle($user, $code, true);
@@ -123,7 +122,7 @@ class TwoFactorController extends ClientApiController
public function delete(DisableTwoFactorRequest $request, DisablesTwoFactor $twoFactor): JsonResponse
{
$user = $this->authenticatedUser($request);
$password = JsonValueGuard::string($request->validated('password'));
$password = $request->string('password')->toString();
throw_unless(Hash::check($password, $user->password), InvalidPasswordProvidedException::class, trans('validation.internal.invalid_password'));
$twoFactor->disable($user);
@@ -92,7 +92,7 @@ class LoginCheckpointController extends AbstractLoginController
$this->sendFailedLoginResponse($request, null, self::TOKEN_EXPIRED_MESSAGE);
}
if (! $checkpoint->matches(JsonValueGuard::nullableString($request->validated('confirmation_token')) ?? '')) {
if (! $checkpoint->matches($request->string('confirmation_token')->toString())) {
$this->sendFailedLoginResponse($request);
}
@@ -104,7 +104,7 @@ class LoginCheckpointController extends AbstractLoginController
// Recovery tokens go through a slightly different pathway for usage.
if (($recoveryToken = $request->validated('recovery_token')) !== null) {
if ($this->isValidRecoveryToken($user, JsonValueGuard::string($recoveryToken))) {
if ($this->isValidRecoveryToken($user, $request->string('recovery_token')->toString())) {
Event::dispatch(new ProvidedAuthenticationToken($user, true));
return $this->sendLoginResponse($logins->establish($user), $request);
@@ -123,7 +123,7 @@ class LoginCheckpointController extends AbstractLoginController
$verified = $google2FA->verifyKeyNewer(
$decrypted,
JsonValueGuard::nullableString($request->validated('authentication_code')) ?? '',
$request->string('authentication_code')->toString(),
$oldTimestamp,
Fluent::make(config()->array('pterodactyl.auth.2fa'))->integer('window', 1)
);
@@ -20,7 +20,6 @@ use Pterodactyl\Contracts\Users\CompletesLogins;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Http\Requests\Auth\LoginRequest;
use Pterodactyl\Models\User;
use Pterodactyl\Support\JsonValueGuard;
#[Group('Authentication', 'Browser session authentication endpoints used by the panel frontend.', authenticated: false)]
class LoginController extends AbstractLoginController
@@ -118,8 +117,8 @@ class LoginController extends AbstractLoginController
$this->sendLockoutResponse($request);
}
$username = JsonValueGuard::string($request->validated('user'));
$password = JsonValueGuard::string($request->validated('password'));
$username = $request->string('user')->toString();
$password = $request->string('password')->toString();
$user = User::query()->where($this->getField($username), $username)->first();
if ($user === null) {
@@ -26,8 +26,8 @@ class LocaleController extends Controller
*/
public function __invoke(LocaleRequest $request): JsonResponse
{
$locale = JsonValueGuard::string($request->validated('locale'));
$namespace = JsonValueGuard::string($request->validated('namespace'));
$locale = $request->string('locale')->toString();
$namespace = $request->string('namespace')->toString();
$segments = explode('::', $namespace, 2);
$translations = count($segments) === 2
? $this->loader->load($locale, $segments[1], $segments[0])
@@ -15,7 +15,6 @@ use Pterodactyl\Http\Requests\Concerns\FiltersDeployTags;
use Pterodactyl\Http\Requests\Concerns\ValidatesExtensionFields;
use Pterodactyl\Models\Objects\DeploymentObject;
use Pterodactyl\Models\Server;
use Pterodactyl\Support\JsonValueGuard;
class StoreServerRequest extends AdminApiRequest
{
@@ -119,8 +118,8 @@ class StoreServerRequest extends AdminApiRequest
$object = new DeploymentObject;
$object->setDedicated($this->boolean('deploy.dedicated_ip'));
$object->setLocations(JsonValueGuard::normalizedIntegerList(JsonValueGuard::integerStringList($this->input('deploy.locations', []))));
$object->setPorts(JsonValueGuard::stringList($this->input('deploy.port_range', [])));
$object->setLocations(array_map(fn (int|string $index): int => $this->integer("deploy.locations.{$index}"), array_keys($this->array('deploy.locations'))));
$object->setPorts(array_map(fn (int|string $index): string => $this->string("deploy.port_range.{$index}")->toString(), array_keys($this->array('deploy.port_range'))));
$object->setTags($this->resolveDeployTagSlugs());
return $object;
@@ -15,7 +15,6 @@ use Pterodactyl\Http\Requests\Concerns\ValidatesExtensionFields;
use Pterodactyl\Models\Objects\DeploymentObject;
use Pterodactyl\Models\Server;
use Pterodactyl\Services\Acl\Api\AdminAcl;
use Pterodactyl\Support\JsonValueGuard;
class StoreServerRequest extends ApplicationApiRequest
{
@@ -136,8 +135,8 @@ class StoreServerRequest extends ApplicationApiRequest
$object = new DeploymentObject;
$object->setDedicated($this->boolean('deploy.dedicated_ip'));
$object->setLocations(JsonValueGuard::normalizedIntegerList(JsonValueGuard::integerStringList($this->input('deploy.locations', []))));
$object->setPorts(JsonValueGuard::stringList($this->input('deploy.port_range', [])));
$object->setLocations(array_map(fn (int|string $index): int => $this->integer("deploy.locations.{$index}"), array_keys($this->array('deploy.locations'))));
$object->setPorts(array_map(fn (int|string $index): string => $this->string("deploy.port_range.{$index}")->toString(), array_keys($this->array('deploy.port_range'))));
$object->setTags($this->resolveDeployTagSlugs());
return $object;
@@ -8,7 +8,6 @@ use Illuminate\Support\Facades\Hash;
use Pterodactyl\Actions\Users\CreateUser;
use Pterodactyl\Exceptions\Http\Base\InvalidPasswordProvidedException;
use Pterodactyl\Http\Requests\Api\Client\ClientApiRequest;
use Pterodactyl\Support\JsonValueGuard;
class UpdatePasswordRequest extends ClientApiRequest
{
@@ -25,6 +24,6 @@ class UpdatePasswordRequest extends ClientApiRequest
protected function passedValidation(): void
{
throw_unless(Hash::check(JsonValueGuard::string($this->validated('current_password')), $this->user()->password), InvalidPasswordProvidedException::class, trans('validation.internal.invalid_password'));
throw_unless(Hash::check($this->string('current_password')->toString(), $this->user()->password), InvalidPasswordProvidedException::class, trans('validation.internal.invalid_password'));
}
}