coolify/app/Livewire/Project/Service/StackForm.php

178 lines
5.8 KiB
PHP
Raw Normal View History

<?php
2023-12-07 18:06:32 +00:00
namespace App\Livewire\Project\Service;
2024-03-21 11:44:32 +00:00
use App\Models\Service;
use App\Support\ValidationPatterns;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
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
public Collection $fields;
2024-06-10 20:43:34 +00:00
protected $listeners = ['saveCompose'];
Complete Livewire legacy model binding migration (25+ components) This completes the migration from Livewire's legacy `id="model.property"` pattern to explicit properties with manual synchronization. This allows disabling the `legacy_model_binding` feature flag. **Components Migrated (Final Session - 9 components):** - Server/Proxy.php (1 field) - Service/EditDomain.php (1 field) - Fixed Collection/string bug & parent sync - Application/Previews.php (2 fields - array handling) - Service/EditCompose.php (4 fields) - Service/FileStorage.php (6 fields) - Service/Database.php (7 fields) - Service/ServiceApplicationView.php (10 fields) - Application/General.php (53 fields) - LARGEST migration - Application/PreviewsCompose.php (1 field) **Total Migration Summary:** - 25+ components migrated across all phases - 150+ explicit properties added - 0 legacy bindings remaining (verified via grep) - All wire:model, id, @entangle bindings updated - All updater hooks renamed (updatedApplicationX → updatedX) **Technical Changes:** - Added explicit public properties (camelCase) - Implemented syncData(bool $toModel) bidirectional sync - Updated validation rules (removed model. prefix) - Updated all action methods (mount, submit, instantSave) - Fixed updater hooks: updatedBuildPack, updatedBaseDirectory, updatedIsStatic - Updated Blade views (id & wire:model bindings) - Applied Collection/string confusion fixes - Added model refresh + re-sync pattern **Critical Fixes:** - EditDomain.php Collection/string confusion (use intermediate variables) - EditDomain.php parent component sync (refresh + re-sync after save) - General.php domain field empty (syncData at end of mount) - General.php wire:model bindings (application.* → property) - General.php updater hooks (wrong naming convention) **Files Modified:** 34 files - 17 PHP Livewire components - 17 Blade view templates - 1 MIGRATION_REPORT.md (documentation) **Ready to disable legacy_model_binding flag in config/livewire.php** 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-13 13:38:59 +00:00
// Explicit properties
public string $name;
public ?string $description = null;
public string $dockerComposeRaw;
public ?string $dockerCompose = null;
Complete Livewire legacy model binding migration (25+ components) This completes the migration from Livewire's legacy `id="model.property"` pattern to explicit properties with manual synchronization. This allows disabling the `legacy_model_binding` feature flag. **Components Migrated (Final Session - 9 components):** - Server/Proxy.php (1 field) - Service/EditDomain.php (1 field) - Fixed Collection/string bug & parent sync - Application/Previews.php (2 fields - array handling) - Service/EditCompose.php (4 fields) - Service/FileStorage.php (6 fields) - Service/Database.php (7 fields) - Service/ServiceApplicationView.php (10 fields) - Application/General.php (53 fields) - LARGEST migration - Application/PreviewsCompose.php (1 field) **Total Migration Summary:** - 25+ components migrated across all phases - 150+ explicit properties added - 0 legacy bindings remaining (verified via grep) - All wire:model, id, @entangle bindings updated - All updater hooks renamed (updatedApplicationX → updatedX) **Technical Changes:** - Added explicit public properties (camelCase) - Implemented syncData(bool $toModel) bidirectional sync - Updated validation rules (removed model. prefix) - Updated all action methods (mount, submit, instantSave) - Fixed updater hooks: updatedBuildPack, updatedBaseDirectory, updatedIsStatic - Updated Blade views (id & wire:model bindings) - Applied Collection/string confusion fixes - Added model refresh + re-sync pattern **Critical Fixes:** - EditDomain.php Collection/string confusion (use intermediate variables) - EditDomain.php parent component sync (refresh + re-sync after save) - General.php domain field empty (syncData at end of mount) - General.php wire:model bindings (application.* → property) - General.php updater hooks (wrong naming convention) **Files Modified:** 34 files - 17 PHP Livewire components - 17 Blade view templates - 1 MIGRATION_REPORT.md (documentation) **Ready to disable legacy_model_binding flag in config/livewire.php** 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-13 13:38:59 +00:00
public ?bool $connectToDockerNetwork = null;
protected function rules(): array
{
$baseRules = [
Complete Livewire legacy model binding migration (25+ components) This completes the migration from Livewire's legacy `id="model.property"` pattern to explicit properties with manual synchronization. This allows disabling the `legacy_model_binding` feature flag. **Components Migrated (Final Session - 9 components):** - Server/Proxy.php (1 field) - Service/EditDomain.php (1 field) - Fixed Collection/string bug & parent sync - Application/Previews.php (2 fields - array handling) - Service/EditCompose.php (4 fields) - Service/FileStorage.php (6 fields) - Service/Database.php (7 fields) - Service/ServiceApplicationView.php (10 fields) - Application/General.php (53 fields) - LARGEST migration - Application/PreviewsCompose.php (1 field) **Total Migration Summary:** - 25+ components migrated across all phases - 150+ explicit properties added - 0 legacy bindings remaining (verified via grep) - All wire:model, id, @entangle bindings updated - All updater hooks renamed (updatedApplicationX → updatedX) **Technical Changes:** - Added explicit public properties (camelCase) - Implemented syncData(bool $toModel) bidirectional sync - Updated validation rules (removed model. prefix) - Updated all action methods (mount, submit, instantSave) - Fixed updater hooks: updatedBuildPack, updatedBaseDirectory, updatedIsStatic - Updated Blade views (id & wire:model bindings) - Applied Collection/string confusion fixes - Added model refresh + re-sync pattern **Critical Fixes:** - EditDomain.php Collection/string confusion (use intermediate variables) - EditDomain.php parent component sync (refresh + re-sync after save) - General.php domain field empty (syncData at end of mount) - General.php wire:model bindings (application.* → property) - General.php updater hooks (wrong naming convention) **Files Modified:** 34 files - 17 PHP Livewire components - 17 Blade view templates - 1 MIGRATION_REPORT.md (documentation) **Ready to disable legacy_model_binding flag in config/livewire.php** 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-13 13:38:59 +00:00
'dockerComposeRaw' => 'required',
'dockerCompose' => 'nullable',
Complete Livewire legacy model binding migration (25+ components) This completes the migration from Livewire's legacy `id="model.property"` pattern to explicit properties with manual synchronization. This allows disabling the `legacy_model_binding` feature flag. **Components Migrated (Final Session - 9 components):** - Server/Proxy.php (1 field) - Service/EditDomain.php (1 field) - Fixed Collection/string bug & parent sync - Application/Previews.php (2 fields - array handling) - Service/EditCompose.php (4 fields) - Service/FileStorage.php (6 fields) - Service/Database.php (7 fields) - Service/ServiceApplicationView.php (10 fields) - Application/General.php (53 fields) - LARGEST migration - Application/PreviewsCompose.php (1 field) **Total Migration Summary:** - 25+ components migrated across all phases - 150+ explicit properties added - 0 legacy bindings remaining (verified via grep) - All wire:model, id, @entangle bindings updated - All updater hooks renamed (updatedApplicationX → updatedX) **Technical Changes:** - Added explicit public properties (camelCase) - Implemented syncData(bool $toModel) bidirectional sync - Updated validation rules (removed model. prefix) - Updated all action methods (mount, submit, instantSave) - Fixed updater hooks: updatedBuildPack, updatedBaseDirectory, updatedIsStatic - Updated Blade views (id & wire:model bindings) - Applied Collection/string confusion fixes - Added model refresh + re-sync pattern **Critical Fixes:** - EditDomain.php Collection/string confusion (use intermediate variables) - EditDomain.php parent component sync (refresh + re-sync after save) - General.php domain field empty (syncData at end of mount) - General.php wire:model bindings (application.* → property) - General.php updater hooks (wrong naming convention) **Files Modified:** 34 files - 17 PHP Livewire components - 17 Blade view templates - 1 MIGRATION_REPORT.md (documentation) **Ready to disable legacy_model_binding flag in config/livewire.php** 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-13 13:38:59 +00:00
'name' => ValidationPatterns::nameRules(),
'description' => ValidationPatterns::descriptionRules(),
'connectToDockerNetwork' => 'nullable',
];
// 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(),
[
Complete Livewire legacy model binding migration (25+ components) This completes the migration from Livewire's legacy `id="model.property"` pattern to explicit properties with manual synchronization. This allows disabling the `legacy_model_binding` feature flag. **Components Migrated (Final Session - 9 components):** - Server/Proxy.php (1 field) - Service/EditDomain.php (1 field) - Fixed Collection/string bug & parent sync - Application/Previews.php (2 fields - array handling) - Service/EditCompose.php (4 fields) - Service/FileStorage.php (6 fields) - Service/Database.php (7 fields) - Service/ServiceApplicationView.php (10 fields) - Application/General.php (53 fields) - LARGEST migration - Application/PreviewsCompose.php (1 field) **Total Migration Summary:** - 25+ components migrated across all phases - 150+ explicit properties added - 0 legacy bindings remaining (verified via grep) - All wire:model, id, @entangle bindings updated - All updater hooks renamed (updatedApplicationX → updatedX) **Technical Changes:** - Added explicit public properties (camelCase) - Implemented syncData(bool $toModel) bidirectional sync - Updated validation rules (removed model. prefix) - Updated all action methods (mount, submit, instantSave) - Fixed updater hooks: updatedBuildPack, updatedBaseDirectory, updatedIsStatic - Updated Blade views (id & wire:model bindings) - Applied Collection/string confusion fixes - Added model refresh + re-sync pattern **Critical Fixes:** - EditDomain.php Collection/string confusion (use intermediate variables) - EditDomain.php parent component sync (refresh + re-sync after save) - General.php domain field empty (syncData at end of mount) - General.php wire:model bindings (application.* → property) - General.php updater hooks (wrong naming convention) **Files Modified:** 34 files - 17 PHP Livewire components - 17 Blade view templates - 1 MIGRATION_REPORT.md (documentation) **Ready to disable legacy_model_binding flag in config/livewire.php** 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
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.',
]
);
}
2024-06-10 20:43:34 +00:00
public $validationAttributes = [];
2024-06-10 20:43:34 +00:00
Complete Livewire legacy model binding migration (25+ components) This completes the migration from Livewire's legacy `id="model.property"` pattern to explicit properties with manual synchronization. This allows disabling the `legacy_model_binding` feature flag. **Components Migrated (Final Session - 9 components):** - Server/Proxy.php (1 field) - Service/EditDomain.php (1 field) - Fixed Collection/string bug & parent sync - Application/Previews.php (2 fields - array handling) - Service/EditCompose.php (4 fields) - Service/FileStorage.php (6 fields) - Service/Database.php (7 fields) - Service/ServiceApplicationView.php (10 fields) - Application/General.php (53 fields) - LARGEST migration - Application/PreviewsCompose.php (1 field) **Total Migration Summary:** - 25+ components migrated across all phases - 150+ explicit properties added - 0 legacy bindings remaining (verified via grep) - All wire:model, id, @entangle bindings updated - All updater hooks renamed (updatedApplicationX → updatedX) **Technical Changes:** - Added explicit public properties (camelCase) - Implemented syncData(bool $toModel) bidirectional sync - Updated validation rules (removed model. prefix) - Updated all action methods (mount, submit, instantSave) - Fixed updater hooks: updatedBuildPack, updatedBaseDirectory, updatedIsStatic - Updated Blade views (id & wire:model bindings) - Applied Collection/string confusion fixes - Added model refresh + re-sync pattern **Critical Fixes:** - EditDomain.php Collection/string confusion (use intermediate variables) - EditDomain.php parent component sync (refresh + re-sync after save) - General.php domain field empty (syncData at end of mount) - General.php wire:model bindings (application.* → property) - General.php updater hooks (wrong naming convention) **Files Modified:** 34 files - 17 PHP Livewire components - 17 Blade view templates - 1 MIGRATION_REPORT.md (documentation) **Ready to disable legacy_model_binding flag in config/livewire.php** 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
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;
}
}
public function mount()
{
Complete Livewire legacy model binding migration (25+ components) This completes the migration from Livewire's legacy `id="model.property"` pattern to explicit properties with manual synchronization. This allows disabling the `legacy_model_binding` feature flag. **Components Migrated (Final Session - 9 components):** - Server/Proxy.php (1 field) - Service/EditDomain.php (1 field) - Fixed Collection/string bug & parent sync - Application/Previews.php (2 fields - array handling) - Service/EditCompose.php (4 fields) - Service/FileStorage.php (6 fields) - Service/Database.php (7 fields) - Service/ServiceApplicationView.php (10 fields) - Application/General.php (53 fields) - LARGEST migration - Application/PreviewsCompose.php (1 field) **Total Migration Summary:** - 25+ components migrated across all phases - 150+ explicit properties added - 0 legacy bindings remaining (verified via grep) - All wire:model, id, @entangle bindings updated - All updater hooks renamed (updatedApplicationX → updatedX) **Technical Changes:** - Added explicit public properties (camelCase) - Implemented syncData(bool $toModel) bidirectional sync - Updated validation rules (removed model. prefix) - Updated all action methods (mount, submit, instantSave) - Fixed updater hooks: updatedBuildPack, updatedBaseDirectory, updatedIsStatic - Updated Blade views (id & wire:model bindings) - Applied Collection/string confusion fixes - Added model refresh + re-sync pattern **Critical Fixes:** - EditDomain.php Collection/string confusion (use intermediate variables) - EditDomain.php parent component sync (refresh + re-sync after save) - General.php domain field empty (syncData at end of mount) - General.php wire:model bindings (application.* → property) - General.php updater hooks (wrong naming convention) **Files Modified:** 34 files - 17 PHP Livewire components - 17 Blade view templates - 1 MIGRATION_REPORT.md (documentation) **Ready to disable legacy_model_binding flag in config/livewire.php** 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-13 13:38:59 +00:00
$this->syncData(false);
$this->fields = collect([]);
$extraFields = $this->service->extraFields();
foreach ($extraFields as $serviceName => $fields) {
foreach ($fields as $fieldKey => $field) {
$key = data_get($field, 'key');
$value = data_get($field, 'value');
$rules = data_get($field, 'rules', 'nullable');
$isPassword = data_get($field, 'isPassword', false);
2024-10-10 11:28:42 +00:00
$customHelper = data_get($field, 'customHelper', false);
$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,
]);
$this->validationAttributes["fields.$key.value"] = $fieldKey;
}
2023-11-11 20:32:41 +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
public function saveCompose($raw)
{
Complete Livewire legacy model binding migration (25+ components) This completes the migration from Livewire's legacy `id="model.property"` pattern to explicit properties with manual synchronization. This allows disabling the `legacy_model_binding` feature flag. **Components Migrated (Final Session - 9 components):** - Server/Proxy.php (1 field) - Service/EditDomain.php (1 field) - Fixed Collection/string bug & parent sync - Application/Previews.php (2 fields - array handling) - Service/EditCompose.php (4 fields) - Service/FileStorage.php (6 fields) - Service/Database.php (7 fields) - Service/ServiceApplicationView.php (10 fields) - Application/General.php (53 fields) - LARGEST migration - Application/PreviewsCompose.php (1 field) **Total Migration Summary:** - 25+ components migrated across all phases - 150+ explicit properties added - 0 legacy bindings remaining (verified via grep) - All wire:model, id, @entangle bindings updated - All updater hooks renamed (updatedApplicationX → updatedX) **Technical Changes:** - Added explicit public properties (camelCase) - Implemented syncData(bool $toModel) bidirectional sync - Updated validation rules (removed model. prefix) - Updated all action methods (mount, submit, instantSave) - Fixed updater hooks: updatedBuildPack, updatedBaseDirectory, updatedIsStatic - Updated Blade views (id & wire:model bindings) - Applied Collection/string confusion fixes - Added model refresh + re-sync pattern **Critical Fixes:** - EditDomain.php Collection/string confusion (use intermediate variables) - EditDomain.php parent component sync (refresh + re-sync after save) - General.php domain field empty (syncData at end of mount) - General.php wire:model bindings (application.* → property) - General.php updater hooks (wrong naming convention) **Files Modified:** 34 files - 17 PHP Livewire components - 17 Blade view templates - 1 MIGRATION_REPORT.md (documentation) **Ready to disable legacy_model_binding flag in config/livewire.php** 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-13 13:38:59 +00:00
$this->dockerComposeRaw = $raw;
$this->submit(notify: true);
}
2024-06-10 20:43:34 +00:00
public function instantSave()
{
Complete Livewire legacy model binding migration (25+ components) This completes the migration from Livewire's legacy `id="model.property"` pattern to explicit properties with manual synchronization. This allows disabling the `legacy_model_binding` feature flag. **Components Migrated (Final Session - 9 components):** - Server/Proxy.php (1 field) - Service/EditDomain.php (1 field) - Fixed Collection/string bug & parent sync - Application/Previews.php (2 fields - array handling) - Service/EditCompose.php (4 fields) - Service/FileStorage.php (6 fields) - Service/Database.php (7 fields) - Service/ServiceApplicationView.php (10 fields) - Application/General.php (53 fields) - LARGEST migration - Application/PreviewsCompose.php (1 field) **Total Migration Summary:** - 25+ components migrated across all phases - 150+ explicit properties added - 0 legacy bindings remaining (verified via grep) - All wire:model, id, @entangle bindings updated - All updater hooks renamed (updatedApplicationX → updatedX) **Technical Changes:** - Added explicit public properties (camelCase) - Implemented syncData(bool $toModel) bidirectional sync - Updated validation rules (removed model. prefix) - Updated all action methods (mount, submit, instantSave) - Fixed updater hooks: updatedBuildPack, updatedBaseDirectory, updatedIsStatic - Updated Blade views (id & wire:model bindings) - Applied Collection/string confusion fixes - Added model refresh + re-sync pattern **Critical Fixes:** - EditDomain.php Collection/string confusion (use intermediate variables) - EditDomain.php parent component sync (refresh + re-sync after save) - General.php domain field empty (syncData at end of mount) - General.php wire:model bindings (application.* → property) - General.php updater hooks (wrong naming convention) **Files Modified:** 34 files - 17 PHP Livewire components - 17 Blade view templates - 1 MIGRATION_REPORT.md (documentation) **Ready to disable legacy_model_binding flag in config/livewire.php** 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-13 13:38:59 +00:00
$this->syncData(true);
$this->service->save();
$this->dispatch('success', 'Service settings saved.');
}
2024-08-15 09:23:44 +00:00
public function submit($notify = true)
{
try {
$this->validate();
Complete Livewire legacy model binding migration (25+ components) This completes the migration from Livewire's legacy `id="model.property"` pattern to explicit properties with manual synchronization. This allows disabling the `legacy_model_binding` feature flag. **Components Migrated (Final Session - 9 components):** - Server/Proxy.php (1 field) - Service/EditDomain.php (1 field) - Fixed Collection/string bug & parent sync - Application/Previews.php (2 fields - array handling) - Service/EditCompose.php (4 fields) - Service/FileStorage.php (6 fields) - Service/Database.php (7 fields) - Service/ServiceApplicationView.php (10 fields) - Application/General.php (53 fields) - LARGEST migration - Application/PreviewsCompose.php (1 field) **Total Migration Summary:** - 25+ components migrated across all phases - 150+ explicit properties added - 0 legacy bindings remaining (verified via grep) - All wire:model, id, @entangle bindings updated - All updater hooks renamed (updatedApplicationX → updatedX) **Technical Changes:** - Added explicit public properties (camelCase) - Implemented syncData(bool $toModel) bidirectional sync - Updated validation rules (removed model. prefix) - Updated all action methods (mount, submit, instantSave) - Fixed updater hooks: updatedBuildPack, updatedBaseDirectory, updatedIsStatic - Updated Blade views (id & wire:model bindings) - Applied Collection/string confusion fixes - Added model refresh + re-sync pattern **Critical Fixes:** - EditDomain.php Collection/string confusion (use intermediate variables) - EditDomain.php parent component sync (refresh + re-sync after save) - General.php domain field empty (syncData at end of mount) - General.php wire:model bindings (application.* → property) - General.php updater hooks (wrong naming convention) **Files Modified:** 34 files - 17 PHP Livewire components - 17 Blade view templates - 1 MIGRATION_REPORT.md (documentation) **Ready to disable legacy_model_binding flag in config/livewire.php** 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
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
// 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);
// 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();
});
// Refresh and write files after a successful commit
$this->service->refresh();
$this->service->saveComposeConfigs();
2023-12-07 18:06:32 +00:00
$this->dispatch('refreshEnvs');
$this->dispatch('refreshServices');
2024-08-15 09:23:44 +00:00
$notify && $this->dispatch('success', 'Service saved.');
} catch (\Throwable $e) {
// On error, refresh from database to restore clean state
$this->service->refresh();
$this->syncData(false);
return handleError($e, $this);
} finally {
if (is_null($this->service->config_hash)) {
$this->service->isConfigurationChanged(true);
} else {
$this->dispatch('configurationChanged');
}
}
}
2024-06-10 20:43:34 +00:00
public function render()
{
return view('livewire.project.service.stack-form');
}
}