mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-04 13:25:47 -05:00
docker image validation
This commit is contained in:
@@ -10,6 +10,7 @@ use Pterodactyl\Enum\Permissions;
|
||||
use Pterodactyl\Http\Requests\Api\Admin\AdminApiRequest;
|
||||
use Pterodactyl\Http\Requests\Concerns\ValidatesExtensionFields;
|
||||
use Pterodactyl\Models\Egg;
|
||||
use Pterodactyl\Rules\DockerImageList;
|
||||
|
||||
class StoreEggRequest extends AdminApiRequest
|
||||
{
|
||||
@@ -30,7 +31,7 @@ class StoreEggRequest extends AdminApiRequest
|
||||
return [
|
||||
'name' => ['required', 'string', 'max:191'],
|
||||
'description' => ['nullable', 'string'],
|
||||
'docker_images' => ['required', 'string', 'regex:/^[\w#\.\/\- ]*\|?~?[\w\.\/\-:@ ]*$/im'],
|
||||
'docker_images' => ['required', 'string', new DockerImageList],
|
||||
'force_outgoing_ip' => ['sometimes', 'boolean'],
|
||||
'file_denylist' => ['array'],
|
||||
'features' => ['sometimes', 'array'],
|
||||
@@ -101,10 +102,8 @@ class StoreEggRequest extends AdminApiRequest
|
||||
*/
|
||||
protected function normalizeDockerImages(?string $input = null): array
|
||||
{
|
||||
$data = array_map(trim(...), explode("\n", $input ?? ''));
|
||||
|
||||
$images = [];
|
||||
foreach ($data as $value) {
|
||||
foreach (DockerImageList::lines($input ?? '') as $value) {
|
||||
$parts = explode('|', $value, 2);
|
||||
$images[$parts[0]] = empty($parts[1]) ? $parts[0] : $parts[1];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pterodactyl\Rules;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
use Pterodactyl\Validation\EggRules;
|
||||
|
||||
/**
|
||||
* A newline-separated list of Docker images, as the admin egg form submits them. Each
|
||||
* line is held to the rule for a single image; a multi-line pattern would accept the
|
||||
* whole list as soon as one line matched.
|
||||
*/
|
||||
class DockerImageList implements ValidationRule
|
||||
{
|
||||
/** @return list<string> */
|
||||
public static function lines(string $value): array
|
||||
{
|
||||
return array_values(array_filter(array_map(trim(...), explode("\n", $value)), fn (string $line): bool => $line !== ''));
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes the rule in the generated API documentation.
|
||||
*
|
||||
* @return array{description: string}
|
||||
*/
|
||||
public function docs(): array
|
||||
{
|
||||
return ['description' => 'One image per line, optionally prefixed with a display name and a pipe (<code>Java 21|ghcr.io/pterodactyl/yolks:java_21</code>). Each must be at most 191 characters and match the regex /^[\w#\.\/\- ]*\|?~?[\w\.\/\-:@ ]*$/.'];
|
||||
}
|
||||
|
||||
public function validate(string $attribute, mixed $value, Closure $fail): void
|
||||
{
|
||||
if (! is_string($value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$images = self::lines($value);
|
||||
if ($images === []) {
|
||||
$fail('The :attribute field must list at least one image.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($images as $image) {
|
||||
if (Validator::make(['image' => $image], ['image' => EggRules::DOCKER_IMAGE])->fails()) {
|
||||
$fail(sprintf('The :attribute entry "%s" is not a valid Docker image.', Str::limit($image, 64)));
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -2106,7 +2106,7 @@ export type AdminCreateEggRequest = {
|
||||
*/
|
||||
description?: string | null;
|
||||
/**
|
||||
* Docker images available to this resource. Must match the regex /^[\w#\.\/\- ]*\|?~?[\w\.\/\-:@ ]*$/im.
|
||||
* Docker images available to this resource. One image per line, optionally prefixed with a display name and a pipe (<code>Java 21|ghcr.io/pterodactyl/yolks:java_21</code>). Each must be at most 191 characters and match the regex /^[\w#\.\/\- ]*\|?~?[\w\.\/\-:@ ]*$/.
|
||||
*/
|
||||
docker_images: string;
|
||||
/**
|
||||
@@ -2157,7 +2157,7 @@ export type AdminUpdateEggRequest = {
|
||||
*/
|
||||
description?: string | null;
|
||||
/**
|
||||
* Docker images available to this resource. Must match the regex /^[\w#\.\/\- ]*\|?~?[\w\.\/\-:@ ]*$/im.
|
||||
* Docker images available to this resource. One image per line, optionally prefixed with a display name and a pipe (<code>Java 21|ghcr.io/pterodactyl/yolks:java_21</code>). Each must be at most 191 characters and match the regex /^[\w#\.\/\- ]*\|?~?[\w\.\/\-:@ ]*$/.
|
||||
*/
|
||||
docker_images: string;
|
||||
/**
|
||||
|
||||
@@ -2298,7 +2298,7 @@ export type AdminCreateEggRequest = {
|
||||
*/
|
||||
description?: string | null;
|
||||
/**
|
||||
* Docker images available to this resource. Must match the regex /^[\w#\.\/\- ]*\|?~?[\w\.\/\-:@ ]*$/im.
|
||||
* Docker images available to this resource. One image per line, optionally prefixed with a display name and a pipe (<code>Java 21|ghcr.io/pterodactyl/yolks:java_21</code>). Each must be at most 191 characters and match the regex /^[\w#\.\/\- ]*\|?~?[\w\.\/\-:@ ]*$/.
|
||||
*/
|
||||
docker_images: string;
|
||||
/**
|
||||
@@ -2350,7 +2350,7 @@ export type AdminUpdateEggRequest = {
|
||||
*/
|
||||
description?: string | null;
|
||||
/**
|
||||
* Docker images available to this resource. Must match the regex /^[\w#\.\/\- ]*\|?~?[\w\.\/\-:@ ]*$/im.
|
||||
* Docker images available to this resource. One image per line, optionally prefixed with a display name and a pipe (<code>Java 21|ghcr.io/pterodactyl/yolks:java_21</code>). Each must be at most 191 characters and match the regex /^[\w#\.\/\- ]*\|?~?[\w\.\/\-:@ ]*$/.
|
||||
*/
|
||||
docker_images: string;
|
||||
/**
|
||||
|
||||
@@ -56,6 +56,23 @@ test('create egg', function (): void {
|
||||
expect($egg->docker_images)->toBe(['Java 17' => 'ghcr.io/pterodactyl/yolks:java_17', 'Java 11' => 'ghcr.io/pterodactyl/yolks:java_11']);
|
||||
$response->assertJson(['object' => 'egg', 'attributes' => ['id' => $egg->id, 'uuid' => $egg->uuid, 'name' => $egg->name, 'author' => $egg->author, 'description' => $egg->description, 'docker_images' => $egg->docker_images, 'startup' => $egg->startup, 'force_outgoing_ip' => $egg->force_outgoing_ip], 'meta' => ['resource' => route('api.admin.eggs.view', ['egg' => $egg->id])]]);
|
||||
});
|
||||
test('every docker image line must be valid on its own', function (string $images): void {
|
||||
$this->postJson(route('api.admin.eggs.store'), array_merge(validCreatePayload(), ['docker_images' => $images]))
|
||||
->assertUnprocessable()
|
||||
->assertJsonPath('errors.0.meta.source_field', 'docker_images');
|
||||
|
||||
$this->assertDatabaseMissing('eggs', ['name' => 'Test Egg']);
|
||||
})->with([
|
||||
'shell characters after a valid line' => ["Java 17|ghcr.io/pterodactyl/yolks:java_17\nghcr.io/x;rm -rf /\$(id)"],
|
||||
'an image over 191 characters' => ["Java 17|ghcr.io/pterodactyl/yolks:java_17\n".str_repeat('a', 192)],
|
||||
'only blank lines' => ["\n \n"],
|
||||
]);
|
||||
test('blank lines between docker images are ignored', function (): void {
|
||||
$this->postJson(route('api.admin.eggs.store'), array_merge(validCreatePayload(), ['docker_images' => "Java 17|ghcr.io/pterodactyl/yolks:java_17\n\nJava 11|ghcr.io/pterodactyl/yolks:java_11\n"]))
|
||||
->assertCreated();
|
||||
|
||||
expect(Egg::query()->where('name', 'Test Egg')->firstOrFail()->docker_images)->toBe(['Java 17' => 'ghcr.io/pterodactyl/yolks:java_17', 'Java 11' => 'ghcr.io/pterodactyl/yolks:java_11']);
|
||||
});
|
||||
test('update egg', function (): void {
|
||||
$egg = Egg::factory()->create();
|
||||
$response = $this->putJson(route('api.admin.eggs.update', ['egg' => $egg->id]), array_merge(validCreatePayload(), ['name' => 'Updated Egg', 'startup' => 'java -jar updated.jar']));
|
||||
|
||||
Reference in New Issue
Block a user