Skip to content

Commit

Permalink
fix: sanitize and validate application domains
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Oct 11, 2024
1 parent d59d8cd commit 24eaa2c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
23 changes: 9 additions & 14 deletions app/Livewire/Project/Service/EditDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\ServiceApplication;
use Livewire\Component;
use Spatie\Url\Url;

class EditDomain extends Component
{
Expand All @@ -20,25 +21,16 @@ public function mount()
{
$this->application = ServiceApplication::find($this->applicationId);
}

public function updatedApplicationFqdn()
public function submit()
{
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(',');
$this->application->save();
} catch(\Throwable $e) {
return handleError($e, $this);
}
}

public function submit()
{
try {
check_domain_usage(resource: $this->application);
$this->validate();
$this->application->save();
Expand All @@ -48,12 +40,15 @@ public function submit()
} else {
$this->dispatch('success', 'Service saved.');
}
} catch (\Throwable $e) {
return handleError($e, $this);
} finally {
$this->application->service->parse();
$this->dispatch('refresh');
$this->dispatch('configurationChanged');
} catch (\Throwable $e) {
$originalFqdn = $this->application->getOriginal('fqdn');
if ($originalFqdn !== $this->application->fqdn) {
$this->application->fqdn = $originalFqdn;
}
return handleError($e, $this);
}
}

Expand Down
24 changes: 15 additions & 9 deletions app/Livewire/Project/Service/ServiceApplicationView.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Livewire\Component;
use Spatie\Url\Url;

class ServiceApplicationView extends Component
{
Expand All @@ -31,13 +32,7 @@ class ServiceApplicationView extends Component

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()
Expand Down Expand Up @@ -83,6 +78,14 @@ public function mount()
public function submit()
{
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);
$this->validate();
$this->application->save();
Expand All @@ -92,10 +95,13 @@ public function submit()
} else {
$this->dispatch('success', 'Service saved.');
}
$this->dispatch('generateDockerCompose');
} catch (\Throwable $e) {
$originalFqdn = $this->application->getOriginal('fqdn');
if ($originalFqdn !== $this->application->fqdn) {
$this->application->fqdn = $originalFqdn;
}
return handleError($e, $this);
} finally {
$this->dispatch('generateDockerCompose');
}
}

Expand Down

0 comments on commit 24eaa2c

Please sign in to comment.