Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

Merge remote-tracking branch 'origin/next' into 9076-investigate-pre-deployment

Andras Bacsai2abb073b527e
diff --cc app/Jobs/ApplicationDeploymentJob.php
index c4121ba16d,3c52e03a1e..d070cefc65
--- a/app/Jobs/ApplicationDeploymentJob.php
+++ b/app/Jobs/ApplicationDeploymentJob.php
@@@ -4047,30 -4047,35 +4094,33 @@@ COPY ./nginx.conf /etc/nginx/conf.d/def
}
$this->application_deployment_queue->addLogEntry('Executing pre-deployment command (see debug log for output/errors).');
- foreach ($containers as $container) {
- $containerName = data_get($container, 'Names');
- if ($containerName) {
- $this->validateContainerName($containerName);
- }
- if ($containers->count() == 1 || str_starts_with($containerName, $this->application->pre_deployment_command_container.'-'.$this->application->uuid)) {
- // Security: pre_deployment_command is intentionally treated as arbitrary shell input.
- // Users (team members with deployment access) need full shell flexibility to run commands
- // like "php artisan migrate", "npm run build", etc. inside their own application containers.
- // The trust boundary is at the application/team ownership level — only authenticated team
- // members can set these commands, and execution is scoped to the application's own container.
- // The single-quote escaping here prevents breaking out of the sh -c wrapper, but does not
- // restrict the command itself. Container names are validated separately via validateContainerName().
- // Newlines are normalized to spaces to prevent injection via SSH heredoc transport
- // (matches the pattern used for health_check_command at line ~2824).
- $preCommand = str_replace(["\r\n", "\r", "\n"], ' ', $this->application->pre_deployment_command);
- $cmd = "sh -c '".str_replace("'", "'\''", $preCommand)."'";
- $exec = "docker exec {$containerName} {$cmd}";
- $this->execute_remote_command(
- [
- 'command' => $exec,
- 'hidden' => true,
- ],
- );
-
- return;
- }
- }
- throw new DeploymentException('Pre-deployment command: Could not find a valid container. Is the container name correct?');
+ $container = $this->resolveCommandContainer($containers, $this->application->pre_deployment_command_container, 'Pre-deployment');
+ if ($container === null) {
+ throw new DeploymentException('Pre-deployment command: Could not find a valid container. Is the container name correct?');
+ }
+
+ $containerName = data_get($container, 'Names');
+ if ($containerName) {
+ $this->validateContainerName($containerName);
+ }
+ // Security: pre_deployment_command is intentionally treated as arbitrary shell input.
+ // Users (team members with deployment access) need full shell flexibility to run commands
+ // like "php artisan migrate", "npm run build", etc. inside their own application containers.
+ // The trust boundary is at the application/team ownership level — only authenticated team
+ // members can set these commands, and execution is scoped to the application's own container.
+ // The single-quote escaping here prevents breaking out of the sh -c wrapper, but does not
+ // restrict the command itself. Container names are validated separately via validateContainerName().
- $cmd = "sh -c '".str_replace("'", "'\''", $this->application->pre_deployment_command)."'";
++ // Newlines are normalized to spaces to prevent injection via SSH heredoc transport
++ // (matches the pattern used for health_check_command at line ~2824).
++ $preCommand = str_replace(["\r\n", "\r", "\n"], ' ', $this->application->pre_deployment_command);
++ $cmd = "sh -c '".str_replace("'", "'\''", $preCommand)."'";
+ $exec = "docker exec {$containerName} {$cmd}";
+ $this->execute_remote_command(
+ [
+ 'command' => $exec,
+ 'hidden' => true,
+ ],
+ );
}
private function run_post_deployment_command()
@@@ -4082,40 -4087,38 +4132,42 @@@
$this->application_deployment_queue->addLogEntry('Executing post-deployment command (see debug log for output).');
$containers = getCurrentApplicationContainerStatus($this->server, $this->application->id, $this->pull_request_id);
- foreach ($containers as $container) {
- $containerName = data_get($container, 'Names');
- if ($containerName) {
- $this->validateContainerName($containerName);
- }
- if ($containers->count() == 1 || str_starts_with($containerName, $this->application->post_deployment_command_container.'-'.$this->application->uuid)) {
- // Security: post_deployment_command is intentionally treated as arbitrary shell input.
- // See the equivalent comment in run_pre_deployment_command() for the full security rationale.
- // Newlines are normalized to spaces to prevent injection via SSH heredoc transport.
- $postCommand = str_replace(["\r\n", "\r", "\n"], ' ', $this->application->post_deployment_command);
- $cmd = "sh -c '".str_replace("'", "'\''", $postCommand)."'";
- $exec = "docker exec {$containerName} {$cmd}";
- try {
- $this->execute_remote_command(
- [
- 'command' => $exec,
- 'hidden' => true,
- 'save' => 'post-deployment-command-output',
- ],
- );
- } catch (Exception $e) {
- $post_deployment_command_output = $this->saved_outputs->get('post-deployment-command-output');
- if ($post_deployment_command_output) {
- $this->application_deployment_queue->addLogEntry('Post-deployment command failed.');
- $this->application_deployment_queue->addLogEntry($post_deployment_command_output, 'stderr');
- }
- }
+ if ($containers->count() == 0) {
+ $this->application_deployment_queue->addLogEntry('Post-deployment command: No running containers found. Skipping.');
- return;
+ return;
+ }
+
+ $container = $this->resolveCommandContainer($containers, $this->application->post_deployment_command_container, 'Post-deployment');
+ if ($container === null) {
+ throw new DeploymentException('Post-deployment command: Could not find a valid container. Is the container name correct?');
+ }
+
+ $containerName = data_get($container, 'Names');
+ if ($containerName) {
+ $this->validateContainerName($containerName);
+ }
+ // Security: post_deployment_command is intentionally treated as arbitrary shell input.
+ // See the equivalent comment in run_pre_deployment_command() for the full security rationale.
- $cmd = "sh -c '".str_replace("'", "'\''", $this->application->post_deployment_command)."'";
++ // Newlines are normalized to spaces to prevent injection via SSH heredoc transport.
++ $postCommand = str_replace(["\r\n", "\r", "\n"], ' ', $this->application->post_deployment_command);
++ $cmd = "sh -c '".str_replace("'", "'\''", $postCommand)."'";
+ $exec = "docker exec {$containerName} {$cmd}";
+ try {
+ $this->execute_remote_command(
+ [
+ 'command' => $exec,
+ 'hidden' => true,
+ 'save' => 'post-deployment-command-output',
+ ],
+ );
+ } catch (Exception $e) {
+ $post_deployment_command_output = $this->saved_outputs->get('post-deployment-command-output');
+ if ($post_deployment_command_output) {
+ $this->application_deployment_queue->addLogEntry('Post-deployment command failed.');
+ $this->application_deployment_queue->addLogEntry($post_deployment_command_output, 'stderr');
}
}
- throw new DeploymentException('Post-deployment command: Could not find a valid container. Is the container name correct?');
}
/**