mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-01 11:55:51 -05:00
drop json guard: extensions
This commit is contained in:
@@ -10,7 +10,7 @@ use Pterodactyl\Services\Extensions\ExtensionSettingsDefinition;
|
||||
final readonly class UpdateExtensionSettings implements UpdatesExtensionSettings
|
||||
{
|
||||
/**
|
||||
* @param ExtensionSettingValues $settings
|
||||
* @param array<string, mixed> $settings
|
||||
* @return list<ExtensionSettingField>
|
||||
*/
|
||||
public function update(ExtensionSettingsDefinition $definition, array $settings): array
|
||||
|
||||
@@ -9,7 +9,7 @@ use Pterodactyl\Services\Extensions\ExtensionSettingsDefinition;
|
||||
interface UpdatesExtensionSettings
|
||||
{
|
||||
/**
|
||||
* @param ExtensionSettingValues $settings
|
||||
* @param array<string, mixed> $settings
|
||||
* @return list<ExtensionSettingField>
|
||||
*/
|
||||
public function update(ExtensionSettingsDefinition $definition, array $settings): array;
|
||||
|
||||
@@ -8,28 +8,24 @@ use Illuminate\Http\Request;
|
||||
use Pterodactyl\Http\Controllers\Api\Admin\AdminApiController;
|
||||
use Pterodactyl\Services\Extensions\Contracts\ManagesExtensionSettings;
|
||||
use Pterodactyl\Services\Extensions\ExtensionSettingValueGuard;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
|
||||
abstract class ExtensionAdminSettingsController extends AdminApiController
|
||||
{
|
||||
abstract protected function extensionSettings(): ManagesExtensionSettings;
|
||||
|
||||
/**
|
||||
* @param ApiPayload9 $extra
|
||||
* @return ApiPayload
|
||||
* @param array<string, mixed> $extra
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function settingsPayload(array $extra = []): array
|
||||
{
|
||||
$payload = [
|
||||
return [
|
||||
...$extra,
|
||||
'settings' => $this->extensionSettings()->publicSettings(),
|
||||
];
|
||||
JsonValueGuard::assertPayload($payload);
|
||||
|
||||
return $payload;
|
||||
}
|
||||
|
||||
/** @return ApiPayload */
|
||||
/** @return array<string, mixed> */
|
||||
protected function updateExtensionSettings(Request $request): array
|
||||
{
|
||||
$data = $request->validate($this->extensionSettings()->validationRules());
|
||||
|
||||
@@ -17,7 +17,7 @@ class UpdateExtensionSettingsRequest extends UpdateExtensionRequest
|
||||
return ['settings' => ['bail', 'present', 'array', new ValidExtensionSettings]];
|
||||
}
|
||||
|
||||
/** @return ExtensionSettingValues */
|
||||
/** @return array<string, mixed> */
|
||||
public function settings(ExtensionSettingsDefinition $definition): array
|
||||
{
|
||||
$input = $this->validated('settings');
|
||||
|
||||
@@ -8,8 +8,8 @@ use Illuminate\Contracts\Validation\Validator;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Pterodactyl\Http\Requests\Api\Application\ApplicationApiRequest;
|
||||
use Pterodactyl\Services\Extensions\ExtensionFields;
|
||||
use Pterodactyl\Services\Extensions\ExtensionSettingValueGuard;
|
||||
use Pterodactyl\Services\Extensions\ValidatedExtensionValues;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
use UnexpectedValueException;
|
||||
|
||||
/**
|
||||
@@ -86,7 +86,7 @@ trait ValidatesExtensionFields
|
||||
* ignored without being read.
|
||||
*
|
||||
* @param list<string> $running
|
||||
* @return array<string, array<array-key, ApiValue9>>
|
||||
* @return array<string, array<mixed>>
|
||||
*/
|
||||
private function submittedExtensionValues(Validator $validator, array $running): array
|
||||
{
|
||||
@@ -110,7 +110,8 @@ trait ValidatesExtensionFields
|
||||
}
|
||||
|
||||
try {
|
||||
$submitted[$extension] = JsonValueGuard::jsonArray($values);
|
||||
ExtensionSettingValueGuard::assertValue($values);
|
||||
$submitted[$extension] = $values;
|
||||
} catch (UnexpectedValueException) {
|
||||
$validator->errors()->add('extensions.'.$extension, sprintf('The %s values are nested too deeply.', $extension));
|
||||
}
|
||||
|
||||
@@ -6,12 +6,12 @@ namespace Pterodactyl\Services\Extensions\Contracts;
|
||||
|
||||
interface ManagesExtensionSettings
|
||||
{
|
||||
/** @return ExtensionSettingValues */
|
||||
/** @return array<string, mixed> */
|
||||
public function publicSettings(): array;
|
||||
|
||||
/** @return NormalizedValidationRules */
|
||||
public function validationRules(): array;
|
||||
|
||||
/** @param ExtensionSettingValues $settings */
|
||||
/** @param array<string, mixed> $settings */
|
||||
public function update(array $settings): void;
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@ use Illuminate\Console\Application as Artisan;
|
||||
use Illuminate\Console\Scheduling\Schedule;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use InvalidArgumentException;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
use ReflectionClass;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
@@ -76,7 +75,7 @@ final class ExtensionConsoleRegistry
|
||||
if ($names === null) {
|
||||
$command = $this->app->make($class);
|
||||
throw_unless($command instanceof Command, InvalidArgumentException::class, sprintf('Extension "%s" registered "%s", which is not a console command.', $identifier, $class));
|
||||
$names = [$command->getName(), ...array_map(JsonValueGuard::string(...), $command->getAliases())];
|
||||
$names = [$command->getName(), ...array_filter($command->getAliases(), is_string(...))];
|
||||
}
|
||||
|
||||
foreach ($names as $name) {
|
||||
|
||||
@@ -11,8 +11,8 @@ use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Validation\Factory as ValidationFactory;
|
||||
use Pterodactyl\Extensions\Fields;
|
||||
use Pterodactyl\Rules\ExtensionFieldValue;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
use Throwable;
|
||||
use UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* Runs the Fields extensions register for a model: authorizes and validates their
|
||||
@@ -93,7 +93,7 @@ final readonly class ExtensionFields
|
||||
* and its values are refused.
|
||||
*
|
||||
* @param Model|class-string<Model> $model the model being updated, or the class being created
|
||||
* @param array<string, array<array-key, ApiValue9>> $submitted each extension's submitted values
|
||||
* @param array<string, array<mixed>> $submitted each extension's submitted values
|
||||
* @param list<string> $defaults extensions to validate even when $submitted leaves them out
|
||||
*/
|
||||
public function validate(Validator $validator, Model|string $model, array $submitted, bool $applicationApi = false, array $defaults = []): ValidatedExtensionValues
|
||||
@@ -208,7 +208,7 @@ final readonly class ExtensionFields
|
||||
*
|
||||
* @param class-string<Fields> $fields
|
||||
* @param Model|class-string<Model> $model
|
||||
* @param array<array-key, ApiValue9> $submitted
|
||||
* @param array<mixed> $submitted
|
||||
* @return array{array<string, array<string>>, ExtensionFieldValues}
|
||||
*/
|
||||
private function check(string $extension, string $fields, Model|string $model, array $submitted): array
|
||||
@@ -268,7 +268,6 @@ final readonly class ExtensionFields
|
||||
$settings = $this->extensions->settings($extension)->fields($model);
|
||||
$values = [];
|
||||
foreach ($this->fieldNames($this->rules($fields, $model)) as $field) {
|
||||
// SAFETY: the panel only stores values that passed the ExtensionFieldValue rule.
|
||||
$values[$field] = ExtensionSettingValueGuard::fieldValue($settings->get($field));
|
||||
}
|
||||
|
||||
@@ -283,8 +282,8 @@ final readonly class ExtensionFields
|
||||
*
|
||||
* @param class-string<Fields> $fields
|
||||
* @param Model|class-string<Model> $model
|
||||
* @param array<array-key, ApiValue9> $submitted
|
||||
* @return array<array-key, ApiValue9>
|
||||
* @param array<mixed> $submitted
|
||||
* @return array<mixed>
|
||||
*/
|
||||
private function withStoredSecrets(string $extension, string $fields, Model|string $model, array $submitted): array
|
||||
{
|
||||
@@ -356,9 +355,20 @@ final readonly class ExtensionFields
|
||||
*/
|
||||
private function secrets(string $fields, Model|string $model): array
|
||||
{
|
||||
return method_exists($fields, 'secrets')
|
||||
? JsonValueGuard::stringList($this->container->call($fields.'@secrets', $this->modelParameters($model)))
|
||||
: [];
|
||||
if (! method_exists($fields, 'secrets')) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$secrets = $this->container->call($fields.'@secrets', $this->modelParameters($model));
|
||||
throw_unless(is_array($secrets), UnexpectedValueException::class, 'Extension field secrets must be a list of field names.');
|
||||
|
||||
$names = [];
|
||||
foreach ($secrets as $name) {
|
||||
throw_unless(is_string($name), UnexpectedValueException::class, 'Extension field secrets must be a list of field names.');
|
||||
$names[] = $name;
|
||||
}
|
||||
|
||||
return $names;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -65,7 +65,7 @@ final readonly class ExtensionHeadTag
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array<array-key, ApiValue9> $entry `['tag' => 'meta'|'link', <attribute> => <value>, ...]`
|
||||
* @param array<mixed> $entry `['tag' => 'meta'|'link', <attribute> => <value>, ...]`
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
|
||||
@@ -8,7 +8,6 @@ use Closure;
|
||||
use Illuminate\Contracts\Support\Htmlable;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use InvalidArgumentException;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
use Throwable;
|
||||
use UnexpectedValueException;
|
||||
|
||||
@@ -33,7 +32,7 @@ final class ExtensionHeadTags implements Htmlable
|
||||
public function __construct(private readonly ExtensionRepository $extensions) {}
|
||||
|
||||
/**
|
||||
* @param array<array-key, ApiValue9>|Closure $tags
|
||||
* @param array<mixed>|Closure $tags
|
||||
*
|
||||
* @throws InvalidArgumentException|UnexpectedValueException
|
||||
*/
|
||||
@@ -89,7 +88,7 @@ final class ExtensionHeadTags implements Htmlable
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<array-key, ApiValue9> $tags
|
||||
* @param array<mixed> $tags
|
||||
* @return list<ExtensionHeadTag>
|
||||
*
|
||||
* @throws InvalidArgumentException|UnexpectedValueException
|
||||
@@ -100,7 +99,8 @@ final class ExtensionHeadTags implements Htmlable
|
||||
|
||||
$parsed = [];
|
||||
foreach ($tags as $tag) {
|
||||
$parsed[] = ExtensionHeadTag::fromArray(JsonValueGuard::jsonArray($tag));
|
||||
throw_unless(is_array($tag), UnexpectedValueException::class, 'Each head tag must be an array of attributes.');
|
||||
$parsed[] = ExtensionHeadTag::fromArray($tag);
|
||||
}
|
||||
|
||||
return $parsed;
|
||||
@@ -148,12 +148,14 @@ final class ExtensionHeadTags implements Htmlable
|
||||
$key = $this->cacheKey($identifier, $version);
|
||||
$seconds = config()->integer('extensions.head_tags_cache_seconds');
|
||||
$cached = $seconds > 0 ? Cache::get($key) : null;
|
||||
if ($cached !== null) {
|
||||
return $this->parse(JsonValueGuard::jsonArray($cached));
|
||||
if (is_array($cached)) {
|
||||
return $this->parse($cached);
|
||||
}
|
||||
|
||||
try {
|
||||
$parsed = $this->parse(JsonValueGuard::jsonArray($tags()));
|
||||
$resolved = $tags();
|
||||
throw_unless(is_array($resolved), UnexpectedValueException::class, 'Head tag callbacks must return a list of tags.');
|
||||
$parsed = $this->parse($resolved);
|
||||
} catch (Throwable $throwable) {
|
||||
// Remember the failure too, or a broken callback would run on every page load.
|
||||
$this->remember($key, $seconds, []);
|
||||
|
||||
@@ -5,7 +5,6 @@ declare(strict_types=1);
|
||||
namespace Pterodactyl\Services\Extensions;
|
||||
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
|
||||
/**
|
||||
* The lucide icon names this panel build can render for `nav.icon`. The list is
|
||||
@@ -36,6 +35,6 @@ final class ExtensionIconCatalog
|
||||
/** @return list<string> */
|
||||
public function names(): array
|
||||
{
|
||||
return JsonValueGuard::stringList(File::json(base_path(self::PATH)));
|
||||
return array_values(array_filter(File::json(base_path(self::PATH)), is_string(...)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ final class ExtensionLock
|
||||
private int $depth = 0;
|
||||
|
||||
/**
|
||||
* @template T of object|ApiValue10|void
|
||||
* @template T
|
||||
*
|
||||
* @param Closure(): T $callback
|
||||
* @return T
|
||||
|
||||
@@ -12,7 +12,6 @@ use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use JsonException;
|
||||
use Pterodactyl\Exceptions\Extensions\InvalidExtensionException;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
use ReflectionClass;
|
||||
use UnexpectedValueException;
|
||||
|
||||
@@ -234,7 +233,7 @@ class ExtensionManifestValidator
|
||||
* The PSR-4 map of the package's composer.json, which Composer and editors read. It is
|
||||
* used when extension.json declares no `autoload` of its own.
|
||||
*
|
||||
* @return array<array-key, JsonInputValue>
|
||||
* @return array<mixed>
|
||||
*/
|
||||
private function composerAutoload(string $directory): array
|
||||
{
|
||||
@@ -244,19 +243,21 @@ class ExtensionManifestValidator
|
||||
}
|
||||
|
||||
try {
|
||||
$composer = JsonValueGuard::decodeArray8(File::get($path));
|
||||
} catch (JsonException|UnexpectedValueException) {
|
||||
$composer = json_decode(File::get($path), true, flags: JSON_THROW_ON_ERROR);
|
||||
} catch (JsonException) {
|
||||
throw new InvalidExtensionException("{$path} is not valid JSON.");
|
||||
}
|
||||
|
||||
$psr4 = is_array($composer['autoload'] ?? null) ? $composer['autoload']['psr-4'] ?? [] : [];
|
||||
throw_unless(is_array($composer), InvalidExtensionException::class, "{$path} is not valid JSON.");
|
||||
|
||||
$psr4 = data_get($composer, 'autoload.psr-4', []);
|
||||
throw_unless(is_array($psr4), InvalidExtensionException::class, 'composer.json "autoload.psr-4" must map "Vendor\\\\Prefix\\\\" to a source directory.');
|
||||
|
||||
return $psr4;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<array-key, JsonInputValue> $autoload
|
||||
* @param array<mixed> $autoload
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function normalizeAutoload(array $autoload, string $field): array
|
||||
|
||||
@@ -18,7 +18,7 @@ class ExtensionPackageLocator
|
||||
public const int MAX_ARCHIVE_UNCOMPRESSED_BYTES = 200 * 1024 * 1024;
|
||||
|
||||
/**
|
||||
* @template T of object|ApiValue10
|
||||
* @template T
|
||||
*
|
||||
* @param Closure(string): T $callback
|
||||
* @return T
|
||||
|
||||
@@ -288,7 +288,7 @@ class ExtensionRepository
|
||||
* as ctx.config; with $publicOnly just the ->public() ones, for guests.
|
||||
* Never allowed to break page rendering.
|
||||
*
|
||||
* @return ExtensionSettingValues
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function frontendConfig(string $identifier, bool $publicOnly): array
|
||||
{
|
||||
|
||||
@@ -21,10 +21,10 @@ class ExtensionSettingDefinition
|
||||
/** Fields whose values are structured or stored elsewhere; these cannot be ->secret(). */
|
||||
private const array RICH_FIELDS = ['color', 'textarea', 'multiselect', 'list', 'file'];
|
||||
|
||||
/** @var (Closure(ExtensionSettingValue): ExtensionSettingValue)|null */
|
||||
/** @var (Closure(mixed): mixed)|null */
|
||||
private ?Closure $normalizer = null;
|
||||
|
||||
/** @var (Closure(ExtensionSettingValue): ExtensionSettingValue)|null */
|
||||
/** @var (Closure(mixed): mixed)|null */
|
||||
private ?Closure $publicSerializer = null;
|
||||
|
||||
private ?string $label = null;
|
||||
@@ -60,7 +60,6 @@ class ExtensionSettingDefinition
|
||||
private array $mimes = [];
|
||||
|
||||
/**
|
||||
* @param ExtensionSettingValue $default
|
||||
* @param ValidationRuleSet $rules
|
||||
*/
|
||||
public function __construct(
|
||||
@@ -72,7 +71,6 @@ class ExtensionSettingDefinition
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param ExtensionSettingValue $default
|
||||
* @param ValidationRuleSet $rules
|
||||
*/
|
||||
public static function make(string $key, string $input, mixed $default, array $rules = [], ?string $public = null): self
|
||||
@@ -80,7 +78,7 @@ class ExtensionSettingDefinition
|
||||
return new self($key, $input, $default, $rules, $public);
|
||||
}
|
||||
|
||||
/** @param callable(ExtensionSettingValue): ExtensionSettingValue $normalizer */
|
||||
/** @param callable(mixed): mixed $normalizer */
|
||||
public function normalizeUsing(callable $normalizer): self
|
||||
{
|
||||
$this->normalizer = $normalizer instanceof Closure ? $normalizer : Closure::fromCallable($normalizer);
|
||||
@@ -88,7 +86,7 @@ class ExtensionSettingDefinition
|
||||
return $this;
|
||||
}
|
||||
|
||||
/** @param callable(ExtensionSettingValue): ExtensionSettingValue $serializer */
|
||||
/** @param callable(mixed): mixed $serializer */
|
||||
public function publicUsing(callable $serializer): self
|
||||
{
|
||||
$this->publicSerializer = $serializer instanceof Closure ? $serializer : Closure::fromCallable($serializer);
|
||||
@@ -351,9 +349,6 @@ class ExtensionSettingDefinition
|
||||
/**
|
||||
* Coerce a submitted or stored value to the field type's shape, then apply
|
||||
* normalizeUsing(). A value that does not fit falls back to the default.
|
||||
*
|
||||
* @param ExtensionSettingValue $value
|
||||
* @return ExtensionSettingValue
|
||||
*/
|
||||
public function normalize(mixed $value): mixed
|
||||
{
|
||||
@@ -368,10 +363,6 @@ class ExtensionSettingDefinition
|
||||
return $this->normalizer instanceof Closure ? ($this->normalizer)($value) : $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ExtensionSettingValue $value
|
||||
* @return ExtensionSettingValue
|
||||
*/
|
||||
public function serializePublic(mixed $value): mixed
|
||||
{
|
||||
if ($this->secret) {
|
||||
@@ -392,7 +383,6 @@ class ExtensionSettingDefinition
|
||||
* `constraints` carries the limits the field's control enforces up front,
|
||||
* and `visibility` who receives the value.
|
||||
*
|
||||
* @param ExtensionSettingValue $currentValue
|
||||
* @return ExtensionSettingField
|
||||
*/
|
||||
public function describe(mixed $currentValue): array
|
||||
|
||||
@@ -147,7 +147,7 @@ class ExtensionSettingFiles
|
||||
continue;
|
||||
}
|
||||
|
||||
$value = rescue(fn (): bool|float|int|string|array|null => ExtensionSettingValueGuard::decode($encoded), report: false);
|
||||
$value = rescue(fn (): mixed => ExtensionSettingValueGuard::decode($encoded), report: false);
|
||||
if (ExtensionSettingValueGuard::fileReference($value) !== null) {
|
||||
$references[] = $row->id;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ use Closure;
|
||||
use Illuminate\Contracts\Validation\Rule as RuleContract;
|
||||
use Illuminate\Contracts\Validation\ValidationRule as ValidationRuleContract;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use Pterodactyl\Support\JsonValueGuard;
|
||||
use Stringable;
|
||||
use UnexpectedValueException;
|
||||
|
||||
@@ -20,21 +19,20 @@ final class ExtensionSettingValueGuard
|
||||
private const string COLOR_PATTERN = '/\A(?:#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})|oklch\((?&n)%? (?&n)%? (?&n)(?:deg)?(?: \/ (?&n)%?)?\))\z(?(DEFINE)(?<n>\d{1,3}(?:\.\d{1,6})?|\.\d{1,6}))/';
|
||||
|
||||
/**
|
||||
* @return ExtensionSettingValue
|
||||
* A stored setting, decrypted first when it is a secret.
|
||||
*/
|
||||
public static function decode(string $encoded, bool $secret = false): bool|float|int|string|array|null
|
||||
public static function decode(string $encoded, bool $secret = false): mixed
|
||||
{
|
||||
$value = JsonValueGuard::decode($encoded);
|
||||
$value = json_decode($encoded, true, flags: JSON_THROW_ON_ERROR);
|
||||
if (! $secret) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
throw_unless(is_string($value), UnexpectedValueException::class, 'Encrypted extension settings must be strings.');
|
||||
|
||||
return JsonValueGuard::decode(Crypt::decryptString($value));
|
||||
return json_decode(Crypt::decryptString($value), true, flags: JSON_THROW_ON_ERROR);
|
||||
}
|
||||
|
||||
/** @param ExtensionSettingValue $value */
|
||||
public static function encode(mixed $value, bool $secret = false): string
|
||||
{
|
||||
$encoded = json_encode($value, JSON_THROW_ON_ERROR);
|
||||
@@ -43,14 +41,25 @@ final class ExtensionSettingValueGuard
|
||||
}
|
||||
|
||||
/**
|
||||
* @phpstan-assert ExtensionSettingValue $value
|
||||
* Rejects what JSON cannot hold, such as objects and uploaded files, and nesting
|
||||
* deeper than ten levels.
|
||||
*/
|
||||
public static function assertValue(mixed $value, int $remainingDepth = 10): void
|
||||
{
|
||||
JsonValueGuard::assertValue($value, $remainingDepth);
|
||||
if (! is_array($value)) {
|
||||
throw_unless($value === null || is_scalar($value), UnexpectedValueException::class, 'Extension setting values must be JSON scalars or arrays.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
throw_if($remainingDepth === 0, UnexpectedValueException::class, 'Extension setting values may be nested at most 10 levels.');
|
||||
|
||||
foreach ($value as $item) {
|
||||
self::assertValue($item, $remainingDepth - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/** @phpstan-assert ExtensionSettingValues $value */
|
||||
/** @phpstan-assert array<string, mixed> $value */
|
||||
public static function assertValues(mixed $value): void
|
||||
{
|
||||
throw_unless(is_array($value), UnexpectedValueException::class, 'Extension settings must be an object.');
|
||||
@@ -159,10 +168,8 @@ final class ExtensionSettingValueGuard
|
||||
/**
|
||||
* The canonical form of a colour setting (lower case, single spaces), or
|
||||
* null when the value is not a colour this panel accepts.
|
||||
*
|
||||
* @param ExtensionSettingValue $value
|
||||
*/
|
||||
public static function color(bool|float|int|string|array|null $value): ?string
|
||||
public static function color(mixed $value): ?string
|
||||
{
|
||||
if (! is_string($value) || mb_strlen($value) > 64) {
|
||||
return null;
|
||||
@@ -177,10 +184,8 @@ final class ExtensionSettingValueGuard
|
||||
/**
|
||||
* A multi-line text setting with its line endings normalized, or null when
|
||||
* the value is not a string.
|
||||
*
|
||||
* @param ExtensionSettingValue $value
|
||||
*/
|
||||
public static function text(bool|float|int|string|array|null $value): ?string
|
||||
public static function text(mixed $value): ?string
|
||||
{
|
||||
return is_string($value) ? str_replace(["\r\n", "\r"], "\n", $value) : null;
|
||||
}
|
||||
@@ -189,10 +194,9 @@ final class ExtensionSettingValueGuard
|
||||
* The string items of a list setting, in order, or null when the value is
|
||||
* not a list.
|
||||
*
|
||||
* @param ExtensionSettingValue $value
|
||||
* @return list<string>|null
|
||||
*/
|
||||
public static function strings(bool|float|int|string|array|null $value): ?array
|
||||
public static function strings(mixed $value): ?array
|
||||
{
|
||||
if (! is_array($value) || ! array_is_list($value)) {
|
||||
return null;
|
||||
@@ -206,11 +210,10 @@ final class ExtensionSettingValueGuard
|
||||
* declaration order, or null when the value is not a list. Anything that is
|
||||
* not a declared choice is dropped.
|
||||
*
|
||||
* @param ExtensionSettingValue $value
|
||||
* @param list<string|int> $choices
|
||||
* @return list<string|int>|null
|
||||
*/
|
||||
public static function choices(bool|float|int|string|array|null $value, array $choices): ?array
|
||||
public static function choices(mixed $value, array $choices): ?array
|
||||
{
|
||||
if (! is_array($value) || ! array_is_list($value)) {
|
||||
return null;
|
||||
@@ -219,12 +222,10 @@ final class ExtensionSettingValueGuard
|
||||
$selected = [];
|
||||
foreach ($value as $item) {
|
||||
if (is_string($item) || is_int($item)) {
|
||||
// SAFETY: only strings and integers reach this cast, and both stringify losslessly.
|
||||
$selected[(string) $item] = true;
|
||||
}
|
||||
}
|
||||
|
||||
// SAFETY: declared choices are strings or integers, which stringify losslessly.
|
||||
return array_values(array_filter($choices, fn (string|int $choice): bool => isset($selected[(string) $choice])));
|
||||
}
|
||||
|
||||
@@ -233,10 +234,9 @@ final class ExtensionSettingValueGuard
|
||||
* stores: text is a string or a number, a select value is identical to one of
|
||||
* the declared choices. Null always fits; other fields have their own checks.
|
||||
*
|
||||
* @param ExtensionSettingValue $value
|
||||
* @param list<string|int|bool> $choices
|
||||
*/
|
||||
public static function fitsField(string $field, bool|float|int|string|array|null $value, array $choices = []): bool
|
||||
public static function fitsField(string $field, mixed $value, array $choices = []): bool
|
||||
{
|
||||
return $value === null || match ($field) {
|
||||
'text', 'password' => is_string($value) || is_int($value) || is_float($value),
|
||||
@@ -249,16 +249,13 @@ final class ExtensionSettingValueGuard
|
||||
|
||||
/**
|
||||
* The stored name of an uploaded settings file, or null when the value is not one.
|
||||
*
|
||||
* @param ExtensionSettingValue $value
|
||||
*/
|
||||
public static function fileReference(bool|float|int|string|array|null $value): ?string
|
||||
public static function fileReference(mixed $value): ?string
|
||||
{
|
||||
return is_string($value) && preg_match(ExtensionSettingFiles::NAME_PATTERN, $value) === 1 ? $value : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ExtensionSettingValue $value
|
||||
* @param 'string'|'number'|'boolean'|'array'|'object'|'null'|'json' $type
|
||||
*/
|
||||
public static function assertFrontendType(mixed $value, string $type): void
|
||||
|
||||
@@ -38,7 +38,7 @@ class ExtensionSettings
|
||||
'database_host' => DatabaseHost::class,
|
||||
];
|
||||
|
||||
/** @var ExtensionSettingValues|null all rows, loaded once per instance on first read */
|
||||
/** @var array<string, mixed>|null all rows, loaded once per instance on first read */
|
||||
private ?array $loaded = null;
|
||||
|
||||
public function __construct(
|
||||
@@ -118,10 +118,6 @@ class ExtensionSettings
|
||||
return $this->for($server);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ExtensionSettingValue $default
|
||||
* @return ExtensionSettingValue
|
||||
*/
|
||||
public function get(string $key, mixed $default = null): mixed
|
||||
{
|
||||
$loaded = $this->loaded ??= $this->all();
|
||||
@@ -129,7 +125,7 @@ class ExtensionSettings
|
||||
return array_key_exists($key, $loaded) ? $loaded[$key] : $default;
|
||||
}
|
||||
|
||||
/** @return ExtensionSettingValues */
|
||||
/** @return array<string, mixed> */
|
||||
public function all(): array
|
||||
{
|
||||
$values = [];
|
||||
@@ -140,26 +136,24 @@ class ExtensionSettings
|
||||
return $this->loaded = $values;
|
||||
}
|
||||
|
||||
/** @param ExtensionSettingValue $value */
|
||||
public function set(string $key, mixed $value): void
|
||||
{
|
||||
$this->setMany([$key => $value]);
|
||||
}
|
||||
|
||||
/** @param ExtensionSettingValue $value */
|
||||
public function setSecret(string $key, mixed $value): void
|
||||
{
|
||||
$this->setManySecrets([$key => $value], [$key]);
|
||||
}
|
||||
|
||||
/** @param ExtensionSettingValues $values */
|
||||
/** @param array<string, mixed> $values */
|
||||
public function setMany(array $values): void
|
||||
{
|
||||
$this->persist($values, []);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ExtensionSettingValues $values
|
||||
* @param array<string, mixed> $values
|
||||
* @param list<string> $secretKeys
|
||||
*/
|
||||
public function setManySecrets(array $values, array $secretKeys): void
|
||||
@@ -176,7 +170,7 @@ class ExtensionSettings
|
||||
unset($this->loaded[$key]);
|
||||
}
|
||||
|
||||
/** @return ExtensionSettingValues keys matching the prefix, prefix stripped */
|
||||
/** @return array<string, mixed> keys matching the prefix, prefix stripped */
|
||||
public function getByPrefix(string $prefix): array
|
||||
{
|
||||
return $this->query()
|
||||
@@ -198,7 +192,7 @@ class ExtensionSettings
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ExtensionSettingValues $values
|
||||
* @param array<string, mixed> $values
|
||||
* @param list<string> $secretKeys
|
||||
*/
|
||||
private function persist(array $values, array $secretKeys): void
|
||||
|
||||
@@ -44,8 +44,6 @@ class ExtensionSettingsDefinition implements ManagesExtensionSettings
|
||||
/**
|
||||
* The setting's current value. A file setting reads as the public URL of
|
||||
* its uploaded file, or its default while nothing is uploaded.
|
||||
*
|
||||
* @return ExtensionSettingValue
|
||||
*/
|
||||
public function get(string $key): mixed
|
||||
{
|
||||
@@ -63,7 +61,7 @@ class ExtensionSettingsDefinition implements ManagesExtensionSettings
|
||||
return $definition->normalize($this->settings->get($definition->key, $definition->default));
|
||||
}
|
||||
|
||||
/** @return ExtensionSettingValues */
|
||||
/** @return array<string, mixed> */
|
||||
public function publicSettings(): array
|
||||
{
|
||||
$values = [];
|
||||
@@ -91,7 +89,7 @@ class ExtensionSettingsDefinition implements ManagesExtensionSettings
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/** @param ExtensionSettingValues $input */
|
||||
/** @param array<string, mixed> $input */
|
||||
public function update(array $input): void
|
||||
{
|
||||
$input = $this->withoutBlankSecrets($input);
|
||||
@@ -121,8 +119,8 @@ class ExtensionSettingsDefinition implements ManagesExtensionSettings
|
||||
* Drops secret inputs that would replace the stored value with nothing or with
|
||||
* its own mask, whatever the field type, so they keep the stored value.
|
||||
*
|
||||
* @param ExtensionSettingValues $input
|
||||
* @return ExtensionSettingValues
|
||||
* @param array<string, mixed> $input
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function withoutBlankSecrets(array $input): array
|
||||
{
|
||||
@@ -182,7 +180,7 @@ class ExtensionSettingsDefinition implements ManagesExtensionSettings
|
||||
* are visible to every logged-in user. With $publicOnly, only those also
|
||||
* marked ->public(), which is what visitors who are not signed in receive.
|
||||
*
|
||||
* @return ExtensionSettingValues
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function frontendConfig(bool $publicOnly = false): array
|
||||
{
|
||||
|
||||
+3
-5
@@ -73,17 +73,15 @@ parameters:
|
||||
GeneralSettingsData: "array{'app:name': string, 'pterodactyl:auth:2fa_required': int, 'app:locale': string}"
|
||||
AdvancedSettingsData: "array{'recaptcha:enabled': string, 'recaptcha:secret_key'?: string, 'recaptcha:website_key': string, 'pterodactyl:guzzle:timeout': int, 'pterodactyl:guzzle:connect_timeout': int, 'pterodactyl:client_features:allocations:enabled': string, 'pterodactyl:client_features:allocations:range_start': int|null, 'pterodactyl:client_features:allocations:range_end': int|null}"
|
||||
MailSettingsData: "array{'mail:mailers:smtp:host': string, 'mail:mailers:smtp:port': int, 'mail:mailers:smtp:encryption': string|null, 'mail:mailers:smtp:username': string|null, 'mail:from:address': string, 'mail:from:name': string|null, 'mail:mailers:smtp:password'?: string}"
|
||||
ExtensionSettingValue: 'JsonValue'
|
||||
ExtensionSettingValues: 'array<string, ExtensionSettingValue>'
|
||||
ExtensionSettingField: 'array{input: string, label: string, help: string|null, tab: string|null, field: string, options: list<array{value: string|int|bool, label: string}>, value: ExtensionSettingValue, constraints: array{max_length: int|null, max_items: int|null, max_kilobytes: int|null, accept: list<string>}, visibility: ''admin''|''frontend''|''public''}'
|
||||
ExtensionFieldValue: 'ApiScalar|list<ApiScalar>'
|
||||
ExtensionSettingField: 'array{input: string, label: string, help: string|null, tab: string|null, field: string, options: list<array{value: string|int|bool, label: string}>, value: mixed, constraints: array{max_length: int|null, max_items: int|null, max_kilobytes: int|null, accept: list<string>}, visibility: ''admin''|''frontend''|''public''}'
|
||||
ExtensionFieldValue: 'bool|float|int|string|list<bool|float|int|string|null>|null'
|
||||
ExtensionFieldValues: 'array<string, ExtensionFieldValue>'
|
||||
ExtensionFieldInput: 'array<string, ExtensionFieldValues>'
|
||||
ExtensionFieldEntry: 'array{fields: class-string<\Pterodactyl\Extensions\Fields>, registration: \Pterodactyl\Services\Extensions\ExtensionRegistration, applicationApi: bool}'
|
||||
ExtensionScreenMatcher: 'array{any?: list<string>, all?: list<string>}'
|
||||
ExtensionScreenCondition: 'array{eggFeatures?: ExtensionScreenMatcher, eggTags?: ExtensionScreenMatcher, match?: string, runtime?: bool}'
|
||||
ExtensionScreenDefinition: 'array{id: string, area: string, path: string, parent?: string, nav?: array{label: string, exact?: bool, params?: array<string, string>, order?: int, group?: string, badge?: string, icon?: string}, permission?: list<string>, when?: ExtensionScreenCondition}'
|
||||
ExtensionManifestInput: 'array{id: string, name: string, version: string, requires?: array{panel?: string, sdk?: string, php?: string, extensions?: array<string, string>}, description?: string|null, author?: string|null, provider?: string|null, icon?: string|null, autoload?: array<array-key, ApiValue9>, ui?: array{entry: string, mode?: string, prefix?: string, screens?: list<ExtensionScreenDefinition>, components?: list<string>}, routes?: array{root?: list<string>}, ...<string, ApiValue10>}'
|
||||
ExtensionManifestInput: 'array{id: string, name: string, version: string, requires?: array{panel?: string, sdk?: string, php?: string, extensions?: array<string, string>}, description?: string|null, author?: string|null, provider?: string|null, icon?: string|null, autoload?: array<mixed>, ui?: array{entry: string, mode?: string, prefix?: string, screens?: list<ExtensionScreenDefinition>, components?: list<string>}, routes?: array{root?: list<string>}, ...<string, mixed>}'
|
||||
paths:
|
||||
- app
|
||||
ignoreErrors:
|
||||
|
||||
@@ -10,10 +10,22 @@ use stdClass;
|
||||
use UnexpectedValueException;
|
||||
|
||||
uses(TestCase::class);
|
||||
test('delegates decoding and assertion to the json value guard', function () {
|
||||
test('decodes stored values and rejects what json cannot hold', function () {
|
||||
expect(ExtensionSettingValueGuard::decode('{"enabled":true,"limits":[1,2.5,null]}'))->toBe(['enabled' => true, 'limits' => [1, 2.5, null]]);
|
||||
expect(fn () => ExtensionSettingValueGuard::assertValue(new stdClass()))->toThrow(UnexpectedValueException::class);
|
||||
});
|
||||
test('decodes encrypted values', function () {
|
||||
$encoded = ExtensionSettingValueGuard::encode(['key' => 'secret'], secret: true);
|
||||
|
||||
expect($encoded)->not->toContain('secret')
|
||||
->and(ExtensionSettingValueGuard::decode($encoded, secret: true))->toBe(['key' => 'secret']);
|
||||
});
|
||||
test('values may be nested at most ten levels', function () {
|
||||
$nested = fn (int $depth): array => array_reduce(range(1, $depth), fn (mixed $value): array => [$value], 'leaf');
|
||||
|
||||
expect(fn () => ExtensionSettingValueGuard::assertValue($nested(10)))->not->toThrow(UnexpectedValueException::class)
|
||||
->and(fn () => ExtensionSettingValueGuard::assertValue($nested(11)))->toThrow(UnexpectedValueException::class);
|
||||
});
|
||||
test('color accepts hex and oklch colours only', function (mixed $value, ?string $expected) {
|
||||
expect(ExtensionSettingValueGuard::color($value))->toBe($expected);
|
||||
})->with([
|
||||
|
||||
Reference in New Issue
Block a user