fix(api): return 422 for a non-string custom_container_name_prefix

This commit is contained in:
Andras Bacsai
2026-09-30 18:10:10 +02:00
parent 71b1e825ef
commit df943b4828
2 changed files with 36 additions and 1 deletions
@@ -156,7 +156,7 @@ class ApplicationsController extends Controller
: $request->input($field);
}
if (array_key_exists('custom_container_name_prefix', $settings)) {
if (array_key_exists('custom_container_name_prefix', $settings) && is_scalar($settings['custom_container_name_prefix'] ?? '')) {
$settings['custom_container_name_prefix'] = str($settings['custom_container_name_prefix'])->slug()->value() ?: null;
}
@@ -166,6 +166,12 @@ class ApplicationsController extends Controller
private function containerNamePrefixValidationResponse(array $settings, Server $server, ?Application $application = null): ?JsonResponse
{
$prefix = $settings['custom_container_name_prefix'] ?? null;
if (! is_null($prefix) && ! is_string($prefix)) {
return response()->json([
'message' => 'Validation failed.',
'errors' => ['custom_container_name_prefix' => ['The custom container name prefix field must be a string.']],
], 422);
}
if (! filled($prefix) || ! ApplicationSetting::isContainerNamePrefixInUse($prefix, $server, $application?->id)) {
return null;
}
@@ -549,3 +549,32 @@ test('PATCH /api/v1/applications/{uuid} rejects a container name prefix that is
expect($this->application->fresh()->settings->custom_container_name_prefix)->toBeNull();
});
test('application creation rejects a non-string container name prefix with 422', function () {
Queue::fake();
$this->withHeaders(applicationSettingsApiHeaders($this->bearerToken))
->postJson('/api/v1/applications/public', [
'project_uuid' => $this->project->uuid,
'environment_uuid' => $this->environment->uuid,
'server_uuid' => $this->server->uuid,
'git_repository' => 'https://gitlab.com/coolify/prefix-test',
'git_branch' => 'main',
'build_pack' => 'nixpacks',
'ports_exposes' => '3000',
'custom_container_name_prefix' => ['nested' => 'value'],
])
->assertUnprocessable()
->assertJsonValidationErrors('custom_container_name_prefix');
expect(Application::query()->where('git_repository', 'like', '%prefix-test%')->exists())->toBeFalse();
});
test('PATCH /api/v1/applications/{uuid} rejects a non-string container name prefix with 422', function () {
$this->withHeaders(applicationSettingsApiHeaders($this->bearerToken))
->patchJson("/api/v1/applications/{$this->application->uuid}", ['custom_container_name_prefix' => ['a', 'b']])
->assertUnprocessable()
->assertJsonValidationErrors('custom_container_name_prefix');
expect($this->application->fresh()->settings->custom_container_name_prefix)->toBeNull();
});