From ab3f169141508aaf48c8aee4b4d63323757cbba6 Mon Sep 17 00:00:00 2001 From: robert dennis <31261583+robertdrakedennis@users.noreply.github.com> Date: Sun, 11 Oct 2026 02:50:49 -0400 Subject: [PATCH] docker image validation --- .../Api/Admin/Eggs/StoreEggRequest.php | 7 +-- app/Rules/DockerImageList.php | 57 +++++++++++++++++++ .../sdk/types/api/generated/types.gen.d.ts | 4 +- resources/scripts/api/generated/types.gen.ts | 4 +- .../Api/Admin/Eggs/EggCrudControllerTest.php | 17 ++++++ 5 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 app/Rules/DockerImageList.php diff --git a/app/Http/Requests/Api/Admin/Eggs/StoreEggRequest.php b/app/Http/Requests/Api/Admin/Eggs/StoreEggRequest.php index 7f89fe8cc..8cb7cc3f8 100644 --- a/app/Http/Requests/Api/Admin/Eggs/StoreEggRequest.php +++ b/app/Http/Requests/Api/Admin/Eggs/StoreEggRequest.php @@ -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]; } diff --git a/app/Rules/DockerImageList.php b/app/Rules/DockerImageList.php new file mode 100644 index 000000000..4ee3619b0 --- /dev/null +++ b/app/Rules/DockerImageList.php @@ -0,0 +1,57 @@ + */ + 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 (Java 21|ghcr.io/pterodactyl/yolks:java_21). 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; + } + } + } +} diff --git a/packages/sdk/types/api/generated/types.gen.d.ts b/packages/sdk/types/api/generated/types.gen.d.ts index 5194d59d3..7ac1f7898 100644 --- a/packages/sdk/types/api/generated/types.gen.d.ts +++ b/packages/sdk/types/api/generated/types.gen.d.ts @@ -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 (Java 21|ghcr.io/pterodactyl/yolks:java_21). 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 (Java 21|ghcr.io/pterodactyl/yolks:java_21). Each must be at most 191 characters and match the regex /^[\w#\.\/\- ]*\|?~?[\w\.\/\-:@ ]*$/. */ docker_images: string; /** diff --git a/resources/scripts/api/generated/types.gen.ts b/resources/scripts/api/generated/types.gen.ts index fea7e9f27..5e6b17e07 100644 --- a/resources/scripts/api/generated/types.gen.ts +++ b/resources/scripts/api/generated/types.gen.ts @@ -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 (Java 21|ghcr.io/pterodactyl/yolks:java_21). 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 (Java 21|ghcr.io/pterodactyl/yolks:java_21). Each must be at most 191 characters and match the regex /^[\w#\.\/\- ]*\|?~?[\w\.\/\-:@ ]*$/. */ docker_images: string; /** diff --git a/tests/Integration/Api/Admin/Eggs/EggCrudControllerTest.php b/tests/Integration/Api/Admin/Eggs/EggCrudControllerTest.php index e240c47ae..5942b48a2 100644 --- a/tests/Integration/Api/Admin/Eggs/EggCrudControllerTest.php +++ b/tests/Integration/Api/Admin/Eggs/EggCrudControllerTest.php @@ -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']));