fix: sanitize and validate application domains

This commit is contained in:
Andras Bacsai
2024-10-11 10:14:27 +02:00
parent d59d8cda2a
commit 24eaa2c9b2
2 changed files with 25 additions and 24 deletions

View File

@@ -4,6 +4,7 @@ namespace App\Livewire\Project\Service;
use App\Models\ServiceApplication; use App\Models\ServiceApplication;
use Livewire\Component; use Livewire\Component;
use Spatie\Url\Url;
class EditDomain extends Component class EditDomain extends Component
{ {
@@ -20,25 +21,16 @@ class EditDomain extends Component
{ {
$this->application = ServiceApplication::find($this->applicationId); $this->application = ServiceApplication::find($this->applicationId);
} }
public function submit()
public function updatedApplicationFqdn()
{ {
try { try {
$this->application->fqdn = str($this->application->fqdn)->replaceEnd(',', '')->trim(); $this->application->fqdn = str($this->application->fqdn)->replaceEnd(',', '')->trim();
$this->application->fqdn = str($this->application->fqdn)->replaceStart(',', '')->trim(); $this->application->fqdn = str($this->application->fqdn)->replaceStart(',', '')->trim();
$this->application->fqdn = str($this->application->fqdn)->trim()->explode(',')->map(function ($domain) { $this->application->fqdn = str($this->application->fqdn)->trim()->explode(',')->map(function ($domain) {
Url::fromString($domain, ['http', 'https']);
return str($domain)->trim()->lower(); return str($domain)->trim()->lower();
}); });
$this->application->fqdn = $this->application->fqdn->unique()->implode(','); $this->application->fqdn = $this->application->fqdn->unique()->implode(',');
$this->application->save();
} catch(\Throwable $e) {
return handleError($e, $this);
}
}
public function submit()
{
try {
check_domain_usage(resource: $this->application); check_domain_usage(resource: $this->application);
$this->validate(); $this->validate();
$this->application->save(); $this->application->save();
@@ -48,12 +40,15 @@ class EditDomain extends Component
} else { } else {
$this->dispatch('success', 'Service saved.'); $this->dispatch('success', 'Service saved.');
} }
} catch (\Throwable $e) {
return handleError($e, $this);
} finally {
$this->application->service->parse(); $this->application->service->parse();
$this->dispatch('refresh'); $this->dispatch('refresh');
$this->dispatch('configurationChanged'); $this->dispatch('configurationChanged');
} catch (\Throwable $e) {
$originalFqdn = $this->application->getOriginal('fqdn');
if ($originalFqdn !== $this->application->fqdn) {
$this->application->fqdn = $originalFqdn;
}
return handleError($e, $this);
} }
} }

View File

@@ -6,6 +6,7 @@ use App\Models\ServiceApplication;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Livewire\Component; use Livewire\Component;
use Spatie\Url\Url;
class ServiceApplicationView extends Component class ServiceApplicationView extends Component
{ {
@@ -31,13 +32,7 @@ class ServiceApplicationView extends Component
public function updatedApplicationFqdn() public function updatedApplicationFqdn()
{ {
$this->application->fqdn = str($this->application->fqdn)->replaceEnd(',', '')->trim();
$this->application->fqdn = str($this->application->fqdn)->replaceStart(',', '')->trim();
$this->application->fqdn = str($this->application->fqdn)->trim()->explode(',')->map(function ($domain) {
return str($domain)->trim()->lower();
});
$this->application->fqdn = $this->application->fqdn->unique()->implode(',');
$this->application->save();
} }
public function instantSave() public function instantSave()
@@ -83,6 +78,14 @@ class ServiceApplicationView extends Component
public function submit() public function submit()
{ {
try { try {
$this->application->fqdn = str($this->application->fqdn)->replaceEnd(',', '')->trim();
$this->application->fqdn = str($this->application->fqdn)->replaceStart(',', '')->trim();
$this->application->fqdn = str($this->application->fqdn)->trim()->explode(',')->map(function ($domain) {
Url::fromString($domain, ['http', 'https']);
return str($domain)->trim()->lower();
});
$this->application->fqdn = $this->application->fqdn->unique()->implode(',');
check_domain_usage(resource: $this->application); check_domain_usage(resource: $this->application);
$this->validate(); $this->validate();
$this->application->save(); $this->application->save();
@@ -92,10 +95,13 @@ class ServiceApplicationView extends Component
} else { } else {
$this->dispatch('success', 'Service saved.'); $this->dispatch('success', 'Service saved.');
} }
} catch (\Throwable $e) {
return handleError($e, $this);
} finally {
$this->dispatch('generateDockerCompose'); $this->dispatch('generateDockerCompose');
} catch (\Throwable $e) {
$originalFqdn = $this->application->getOriginal('fqdn');
if ($originalFqdn !== $this->application->fqdn) {
$this->application->fqdn = $originalFqdn;
}
return handleError($e, $this);
} }
} }