2023-10-12 07:12:46 +00:00
|
|
|
<?php
|
|
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
namespace App\Livewire\Project\Service;
|
2023-10-12 07:12:46 +00:00
|
|
|
|
2024-03-21 11:44:32 +00:00
|
|
|
use App\Models\Service;
|
2025-08-19 12:15:31 +00:00
|
|
|
use App\Support\ValidationPatterns;
|
2024-04-15 14:54:03 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2025-11-07 13:03:19 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2023-10-12 07:12:46 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
|
|
class StackForm extends Component
|
|
|
|
|
{
|
2024-03-21 11:44:32 +00:00
|
|
|
public Service $service;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-04-15 14:54:03 +00:00
|
|
|
public Collection $fields;
|
2024-06-10 20:43:34 +00:00
|
|
|
|
|
|
|
|
protected $listeners = ['saveCompose'];
|
|
|
|
|
|
2025-10-13 13:38:59 +00:00
|
|
|
// Explicit properties
|
|
|
|
|
public string $name;
|
|
|
|
|
|
|
|
|
|
public ?string $description = null;
|
|
|
|
|
|
|
|
|
|
public string $dockerComposeRaw;
|
|
|
|
|
|
2025-11-07 13:03:19 +00:00
|
|
|
public ?string $dockerCompose = null;
|
2025-10-13 13:38:59 +00:00
|
|
|
|
|
|
|
|
public ?bool $connectToDockerNetwork = null;
|
|
|
|
|
|
2025-08-19 12:15:31 +00:00
|
|
|
protected function rules(): array
|
|
|
|
|
{
|
|
|
|
|
$baseRules = [
|
2025-10-13 13:38:59 +00:00
|
|
|
'dockerComposeRaw' => 'required',
|
2025-11-07 13:03:19 +00:00
|
|
|
'dockerCompose' => 'nullable',
|
2025-10-13 13:38:59 +00:00
|
|
|
'name' => ValidationPatterns::nameRules(),
|
|
|
|
|
'description' => ValidationPatterns::descriptionRules(),
|
|
|
|
|
'connectToDockerNetwork' => 'nullable',
|
2025-08-19 12:15:31 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
|
|
// Add dynamic field rules
|
|
|
|
|
foreach ($this->fields ?? collect() as $key => $field) {
|
|
|
|
|
$rules = data_get($field, 'rules', 'nullable');
|
|
|
|
|
$baseRules["fields.$key.value"] = $rules;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $baseRules;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function messages(): array
|
|
|
|
|
{
|
|
|
|
|
return array_merge(
|
|
|
|
|
ValidationPatterns::combinedMessages(),
|
|
|
|
|
[
|
2025-10-13 13:38:59 +00:00
|
|
|
'name.required' => 'The Name field is required.',
|
|
|
|
|
'dockerComposeRaw.required' => 'The Docker Compose Raw field is required.',
|
|
|
|
|
'dockerCompose.required' => 'The Docker Compose field is required.',
|
2025-08-19 12:15:31 +00:00
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-11-13 10:09:21 +00:00
|
|
|
public $validationAttributes = [];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2025-10-13 13:38:59 +00:00
|
|
|
/**
|
|
|
|
|
* Sync data between component properties and model
|
|
|
|
|
*
|
|
|
|
|
* @param bool $toModel If true, sync FROM properties TO model. If false, sync FROM model TO properties.
|
|
|
|
|
*/
|
|
|
|
|
private function syncData(bool $toModel = false): void
|
|
|
|
|
{
|
|
|
|
|
if ($toModel) {
|
|
|
|
|
// Sync TO model (before save)
|
|
|
|
|
$this->service->name = $this->name;
|
|
|
|
|
$this->service->description = $this->description;
|
|
|
|
|
$this->service->docker_compose_raw = $this->dockerComposeRaw;
|
|
|
|
|
$this->service->docker_compose = $this->dockerCompose;
|
|
|
|
|
$this->service->connect_to_docker_network = $this->connectToDockerNetwork;
|
|
|
|
|
} else {
|
|
|
|
|
// Sync FROM model (on load/refresh)
|
|
|
|
|
$this->name = $this->service->name;
|
|
|
|
|
$this->description = $this->service->description;
|
|
|
|
|
$this->dockerComposeRaw = $this->service->docker_compose_raw;
|
|
|
|
|
$this->dockerCompose = $this->service->docker_compose;
|
|
|
|
|
$this->connectToDockerNetwork = $this->service->connect_to_docker_network;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-13 10:09:21 +00:00
|
|
|
public function mount()
|
|
|
|
|
{
|
2025-10-13 13:38:59 +00:00
|
|
|
$this->syncData(false);
|
2024-04-15 14:54:03 +00:00
|
|
|
$this->fields = collect([]);
|
2023-11-13 10:09:21 +00:00
|
|
|
$extraFields = $this->service->extraFields();
|
|
|
|
|
foreach ($extraFields as $serviceName => $fields) {
|
|
|
|
|
foreach ($fields as $fieldKey => $field) {
|
|
|
|
|
$key = data_get($field, 'key');
|
|
|
|
|
$value = data_get($field, 'value');
|
2023-11-24 20:38:39 +00:00
|
|
|
$rules = data_get($field, 'rules', 'nullable');
|
2024-09-24 19:17:07 +00:00
|
|
|
$isPassword = data_get($field, 'isPassword', false);
|
2024-10-10 11:28:42 +00:00
|
|
|
$customHelper = data_get($field, 'customHelper', false);
|
2024-04-15 14:54:03 +00:00
|
|
|
$this->fields->put($key, [
|
2024-06-10 20:43:34 +00:00
|
|
|
'serviceName' => $serviceName,
|
|
|
|
|
'key' => $key,
|
|
|
|
|
'name' => $fieldKey,
|
|
|
|
|
'value' => $value,
|
|
|
|
|
'isPassword' => $isPassword,
|
|
|
|
|
'rules' => $rules,
|
2024-10-10 11:28:42 +00:00
|
|
|
'customHelper' => $customHelper,
|
2024-04-15 14:54:03 +00:00
|
|
|
]);
|
|
|
|
|
|
2023-11-13 10:09:21 +00:00
|
|
|
$this->validationAttributes["fields.$key.value"] = $fieldKey;
|
|
|
|
|
}
|
2023-11-11 20:32:41 +00:00
|
|
|
}
|
2024-09-24 19:17:07 +00:00
|
|
|
$this->fields = $this->fields->groupBy('serviceName')->map(function ($group) {
|
|
|
|
|
return $group->sortBy(function ($field) {
|
|
|
|
|
return data_get($field, 'isPassword') ? 1 : 0;
|
|
|
|
|
})->mapWithKeys(function ($field) {
|
|
|
|
|
return [$field['key'] => $field];
|
|
|
|
|
});
|
|
|
|
|
})->flatMap(function ($group) {
|
|
|
|
|
return $group;
|
|
|
|
|
});
|
2023-11-11 20:32:41 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-12 07:12:46 +00:00
|
|
|
public function saveCompose($raw)
|
|
|
|
|
{
|
2025-10-13 13:38:59 +00:00
|
|
|
$this->dockerComposeRaw = $raw;
|
2025-02-27 10:29:04 +00:00
|
|
|
$this->submit(notify: true);
|
2023-10-12 07:12:46 +00:00
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2024-02-15 19:44:01 +00:00
|
|
|
public function instantSave()
|
|
|
|
|
{
|
2025-10-13 13:38:59 +00:00
|
|
|
$this->syncData(true);
|
2024-01-21 13:30:03 +00:00
|
|
|
$this->service->save();
|
2024-05-27 22:41:42 +00:00
|
|
|
$this->dispatch('success', 'Service settings saved.');
|
2024-01-21 13:30:03 +00:00
|
|
|
}
|
2023-10-12 07:12:46 +00:00
|
|
|
|
2024-08-15 09:23:44 +00:00
|
|
|
public function submit($notify = true)
|
2023-10-12 07:12:46 +00:00
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$this->validate();
|
2025-10-13 13:38:59 +00:00
|
|
|
$this->syncData(true);
|
fix: prevent command injection in Docker Compose parsing - add pre-save validation
This commit addresses a critical security issue where malicious Docker Compose
data was being saved to the database before validation occurred.
Problem:
- Service models were saved to database first
- Validation ran afterwards during parse()
- Malicious data persisted even when validation failed
- User saw error but damage was already done
Solution:
1. Created validateDockerComposeForInjection() to validate YAML before save
2. Added pre-save validation to all Service creation/update points:
- Livewire: DockerCompose.php, StackForm.php
- API: ServicesController.php (create, update, one-click)
3. Validates service names and volume paths (string + array formats)
4. Blocks shell metacharacters: backticks, $(), |, ;, &, >, <, newlines
Security fixes:
- Volume source paths (string format) - validated before save
- Volume source paths (array format) - validated before save
- Service names - validated before save
- Environment variable patterns - safe ${VAR} allowed, ${VAR:-$(cmd)} blocked
Testing:
- 60 security tests pass (176 assertions)
- PreSaveValidationTest.php: 15 tests for pre-save validation
- ValidateShellSafePathTest.php: 15 tests for core validation
- VolumeSecurityTest.php: 15 tests for volume parsing
- ServiceNameSecurityTest.php: 15 tests for service names
Related commits:
- Previous: Added validation during parse() phase
- This commit: Moves validation before database save
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-15 19:46:26 +00:00
|
|
|
|
2025-11-07 13:03:19 +00:00
|
|
|
// Validate for command injection BEFORE any database operations
|
fix: prevent command injection in Docker Compose parsing - add pre-save validation
This commit addresses a critical security issue where malicious Docker Compose
data was being saved to the database before validation occurred.
Problem:
- Service models were saved to database first
- Validation ran afterwards during parse()
- Malicious data persisted even when validation failed
- User saw error but damage was already done
Solution:
1. Created validateDockerComposeForInjection() to validate YAML before save
2. Added pre-save validation to all Service creation/update points:
- Livewire: DockerCompose.php, StackForm.php
- API: ServicesController.php (create, update, one-click)
3. Validates service names and volume paths (string + array formats)
4. Blocks shell metacharacters: backticks, $(), |, ;, &, >, <, newlines
Security fixes:
- Volume source paths (string format) - validated before save
- Volume source paths (array format) - validated before save
- Service names - validated before save
- Environment variable patterns - safe ${VAR} allowed, ${VAR:-$(cmd)} blocked
Testing:
- 60 security tests pass (176 assertions)
- PreSaveValidationTest.php: 15 tests for pre-save validation
- ValidateShellSafePathTest.php: 15 tests for core validation
- VolumeSecurityTest.php: 15 tests for volume parsing
- ServiceNameSecurityTest.php: 15 tests for service names
Related commits:
- Previous: Added validation during parse() phase
- This commit: Moves validation before database save
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-15 19:46:26 +00:00
|
|
|
validateDockerComposeForInjection($this->service->docker_compose_raw);
|
|
|
|
|
|
2025-11-07 13:03:19 +00:00
|
|
|
// Use transaction to ensure atomicity - if parse fails, save is rolled back
|
|
|
|
|
DB::transaction(function () {
|
|
|
|
|
$this->service->save();
|
|
|
|
|
$this->service->saveExtraFields($this->fields);
|
|
|
|
|
$this->service->parse();
|
|
|
|
|
});
|
2025-11-10 13:44:11 +00:00
|
|
|
// Refresh and write files after a successful commit
|
|
|
|
|
$this->service->refresh();
|
|
|
|
|
$this->service->saveComposeConfigs();
|
2025-11-07 13:03:19 +00:00
|
|
|
|
2023-12-07 18:06:32 +00:00
|
|
|
$this->dispatch('refreshEnvs');
|
2025-04-29 12:27:17 +00:00
|
|
|
$this->dispatch('refreshServices');
|
2024-08-15 09:23:44 +00:00
|
|
|
$notify && $this->dispatch('success', 'Service saved.');
|
2025-01-07 14:31:43 +00:00
|
|
|
} catch (\Throwable $e) {
|
2025-11-07 13:03:19 +00:00
|
|
|
// On error, refresh from database to restore clean state
|
|
|
|
|
$this->service->refresh();
|
|
|
|
|
$this->syncData(false);
|
|
|
|
|
|
2023-10-12 07:12:46 +00:00
|
|
|
return handleError($e, $this);
|
2024-04-12 10:44:49 +00:00
|
|
|
} finally {
|
|
|
|
|
if (is_null($this->service->config_hash)) {
|
|
|
|
|
$this->service->isConfigurationChanged(true);
|
|
|
|
|
} else {
|
|
|
|
|
$this->dispatch('configurationChanged');
|
|
|
|
|
}
|
2023-10-12 07:12:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-12 07:12:46 +00:00
|
|
|
public function render()
|
|
|
|
|
{
|
|
|
|
|
return view('livewire.project.service.stack-form');
|
|
|
|
|
}
|
|
|
|
|
}
|