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.

fix(livewire): authorize exposed resource mutations (#11596)

Andras Bacsaib8866b87e8e8
diff --git a/app/Livewire/Project/Application/General.php b/app/Livewire/Project/Application/General.php
index bb6bcf5f5d..8f0bb2385d 100644
--- a/app/Livewire/Project/Application/General.php
+++ b/app/Livewire/Project/Application/General.php
@@ -651,6 +651,8 @@ class General extends Component
public function resetDefaultLabels($manualReset = false)
{
+ $this->authorize('update', $this->application);
+
try {
if (! $this->isContainerLabelReadonlyEnabled && ! $manualReset) {
return;
diff --git a/app/Livewire/Project/Database/Heading.php b/app/Livewire/Project/Database/Heading.php
index 943f227021..f2f8fa387d 100644
--- a/app/Livewire/Project/Database/Heading.php
+++ b/app/Livewire/Project/Database/Heading.php
@@ -35,6 +35,12 @@ class Heading extends Component
public function activityFinished()
{
+ if (auth()->user()->cannot('update', $this->database)) {
+ $this->dispatch('refresh');
+
+ return;
+ }
+
try {
// Only set started_at if database is actually running
if ($this->database->isRunning()) {
diff --git a/app/Livewire/Project/Shared/EnvironmentVariable/Show.php b/app/Livewire/Project/Shared/EnvironmentVariable/Show.php
index 0e0851aaa0..8350cffa4c 100644
--- a/app/Livewire/Project/Shared/EnvironmentVariable/Show.php
+++ b/app/Livewire/Project/Shared/EnvironmentVariable/Show.php
@@ -145,6 +145,8 @@ class Show extends Component
*/
public function loadValues(): void
{
+ $this->authorize('update', $this->env);
+
if ($this->valuesLoaded) {
return;
}
diff --git a/app/Livewire/Project/Shared/GetLogs.php b/app/Livewire/Project/Shared/GetLogs.php
index 67a040ef77..e1e5413719 100644
--- a/app/Livewire/Project/Shared/GetLogs.php
+++ b/app/Livewire/Project/Shared/GetLogs.php
@@ -17,12 +17,15 @@ use App\Models\StandaloneMysql;
use App\Models\StandalonePostgresql;
use App\Models\StandaloneRedis;
use App\Support\ValidationPatterns;
+use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\Process;
use Livewire\Attributes\Locked;
use Livewire\Component;
class GetLogs extends Component
{
+ use AuthorizesRequests;
+
public const MAX_LOG_LINES = 50000;
public const MAX_DISPLAY_SIZE_BYTES = 5 * 1024 * 1024;
@@ -82,6 +85,10 @@ class GetLogs extends Component
public function instantSave()
{
if (! is_null($this->resource)) {
+ if (auth()->user()->cannot('update', $this->resource)) {
+ return;
+ }
+
if ($this->resource->getMorphClass() === Application::class) {
$this->resource->settings->is_include_timestamps = $this->showTimeStamps;
$this->resource->settings->save();
diff --git a/app/Livewire/Security/CloudInitScripts.php b/app/Livewire/Security/CloudInitScripts.php
index b6d448e903..0d26d1d669 100644
--- a/app/Livewire/Security/CloudInitScripts.php
+++ b/app/Livewire/Security/CloudInitScripts.php
@@ -28,6 +28,8 @@ class CloudInitScripts extends Component
public function loadScripts()
{
+ $this->authorize('viewAny', CloudInitScript::class);
+
CloudInitScript::ownedByCurrentTeam()
->whereNull('uuid')
->get()
diff --git a/app/Livewire/Security/CloudProviderTokenForm.php b/app/Livewire/Security/CloudProviderTokenForm.php
index ba2655b434..2c31d22035 100644
--- a/app/Livewire/Security/CloudProviderTokenForm.php
+++ b/app/Livewire/Security/CloudProviderTokenForm.php
@@ -94,6 +94,7 @@ class CloudProviderTokenForm extends Component
public function addToken()
{
+ $this->authorize('create', CloudProviderToken::class);
$this->validate();
try {
diff --git a/app/Livewire/Server/ValidateAndInstall.php b/app/Livewire/Server/ValidateAndInstall.php
index c39f868baf..db62bff2db 100644
--- a/app/Livewire/Server/ValidateAndInstall.php
+++ b/app/Livewire/Server/ValidateAndInstall.php
@@ -53,6 +53,8 @@ class ValidateAndInstall extends Component
public function init(int $data = 0)
{
+ $this->authorize('update', $this->server);
+
if (! $this->server->canBeValidated()) {
$this->error = 'This server was transferred to another Coolify instance and cannot be revalidated here.';
$this->server->update([
@@ -160,6 +162,8 @@ class ValidateAndInstall extends Component
public function validateOS()
{
+ $this->authorize('update', $this->server);
+
$this->supported_os_type = $this->server->validateOS();
if (! $this->supported_os_type) {
$this->error = 'Server OS type is not supported. Please install Docker manually before continuing: <a target="_blank" class="underline" href="https://docs.docker.com/engine/install/#server">documentation</a>.';
@@ -174,6 +178,8 @@ class ValidateAndInstall extends Component
public function validatePrerequisites()
{
+ $this->authorize('update', $this->server);
+
$validationResult = $this->server->validatePrerequisites();
$this->prerequisites_installed = $validationResult['success'];
if (! $validationResult['success']) {
@@ -212,6 +218,8 @@ class ValidateAndInstall extends Component
public function validateDockerEngine()
{
+ $this->authorize('update', $this->server);
+
$this->docker_installed = $this->server->validateDockerEngine();
$this->docker_compose_installed = $this->server->validateDockerCompose();
if (! $this->docker_installed || ! $this->docker_compose_installed) {
@@ -248,6 +256,8 @@ class ValidateAndInstall extends Component
public function validateDockerVersion()
{
+ $this->authorize('update', $this->server);
+
if ($this->server->isSwarm()) {
$swarmInstalled = $this->server->validateDockerSwarm();
if ($swarmInstalled) {
diff --git a/tests/Feature/Authorization/ApplicationConfigAuthorizationTest.php b/tests/Feature/Authorization/ApplicationConfigAuthorizationTest.php
index 5842769908..eb331a775f 100644
--- a/tests/Feature/Authorization/ApplicationConfigAuthorizationTest.php
+++ b/tests/Feature/Authorization/ApplicationConfigAuthorizationTest.php
@@ -1,8 +1,11 @@
<?php
use App\Livewire\Project\Application\Advanced as ApplicationAdvanced;
+use App\Livewire\Project\Application\General as ApplicationGeneral;
use App\Livewire\Project\Application\Heading as ApplicationHeading;
use App\Livewire\Project\Application\Rollback as ApplicationRollback;
+use App\Livewire\Project\Shared\EnvironmentVariable\Show as EnvironmentVariableShow;
+use App\Livewire\Project\Shared\GetLogs;
use App\Models\Application;
use App\Models\InstanceSettings;
use App\Models\Project;
@@ -10,6 +13,7 @@ use App\Models\Server;
use App\Models\StandaloneDocker;
use App\Models\Team;
use App\Models\User;
+use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
@@ -169,6 +173,60 @@ test('member cannot update application general settings', function () {
expect($this->member->can('update', $this->application))->toBeFalse();
});
+test('member cannot reset application labels', function () {
+ $this->actingAs($this->member);
+ session(['currentTeam' => $this->team]);
+
+ $originalLabels = $this->application->custom_labels;
+
+ $component = app(ApplicationGeneral::class);
+ $component->application = $this->application;
+ $component->isContainerLabelReadonlyEnabled = true;
+
+ expect(fn () => $component->resetDefaultLabels(true))
+ ->toThrow(AuthorizationException::class);
+
+ expect($this->application->fresh()->custom_labels)->toBe($originalLabels);
+});
+
+test('member cannot load application environment variable values', function () {
+ $this->actingAs($this->member);
+ session(['currentTeam' => $this->team]);
+
+ $environmentVariable = $this->application->environment_variables()->create([
+ 'key' => 'SECRET_KEY',
+ 'value' => 'super-secret-value',
+ 'is_preview' => false,
+ ]);
+
+ Livewire::test(EnvironmentVariableShow::class, [
+ 'env' => $environmentVariable,
+ 'type' => 'application',
+ ])
+ ->call('loadValues')
+ ->assertForbidden()
+ ->assertSet('valuesLoaded', false)
+ ->assertSet('value', null);
+});
+
+test('member cannot save application log timestamp settings', function () {
+ $this->actingAs($this->member);
+ session(['currentTeam' => $this->team]);
+
+ $originalValue = $this->application->settings->is_include_timestamps;
+
+ Livewire::test(GetLogs::class, [
+ 'resource' => $this->application,
+ 'server' => $this->server,
+ 'container' => $this->application->uuid,
+ ])
+ ->set('showTimeStamps', ! $originalValue)
+ ->call('instantSave')
+ ->assertSuccessful();
+
+ expect($this->application->settings->fresh()->is_include_timestamps)->toBe($originalValue);
+});
+
// --- Application Advanced Livewire actions ---
test('member cannot save application advanced settings', function () {
diff --git a/tests/Feature/Authorization/DatabaseConfigAuthorizationTest.php b/tests/Feature/Authorization/DatabaseConfigAuthorizationTest.php
index 89c3d5e2f4..7807f00a27 100644
--- a/tests/Feature/Authorization/DatabaseConfigAuthorizationTest.php
+++ b/tests/Feature/Authorization/DatabaseConfigAuthorizationTest.php
@@ -1,5 +1,6 @@
<?php
+use App\Livewire\Project\Database\Heading;
use App\Models\InstanceSettings;
use App\Models\Project;
use App\Models\Server;
@@ -10,11 +11,12 @@ use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
+use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
- InstanceSettings::updateOrCreate(['id' => 0]);
+ InstanceSettings::unguarded(fn () => InstanceSettings::updateOrCreate(['id' => 0], ['id' => 0]));
$this->team = Team::factory()->create();
@@ -89,6 +91,18 @@ test('member cannot update database', function () {
expect($this->member->can('update', $this->database))->toBeFalse();
});
+test('member cannot persist database activity status', function () {
+ $this->actingAs($this->member);
+ session(['currentTeam' => $this->team]);
+
+ Livewire::test(Heading::class, ['database' => $this->database])
+ ->call('activityFinished')
+ ->assertSuccessful()
+ ->assertDispatched('refresh');
+
+ expect($this->database->fresh()->started_at)->toBeNull();
+});
+
// --- Database Policy: delete ---
test('admin can delete database', function () {
diff --git a/tests/Feature/Authorization/SecurityAuthorizationTest.php b/tests/Feature/Authorization/SecurityAuthorizationTest.php
new file mode 100644
index 0000000000..b36bcc3e49
--- /dev/null
+++ b/tests/Feature/Authorization/SecurityAuthorizationTest.php
@@ -0,0 +1,57 @@
+<?php
+
+use App\Livewire\Security\CloudInitScripts;
+use App\Livewire\Security\CloudProviderTokenForm;
+use App\Models\CloudInitScript;
+use App\Models\CloudProviderToken;
+use App\Models\InstanceSettings;
+use App\Models\Team;
+use App\Models\User;
+use Illuminate\Auth\Access\AuthorizationException;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Facades\Http;
+use Livewire\Livewire;
+
+uses(RefreshDatabase::class);
+
+beforeEach(function () {
+ $this->withoutVite();
+ InstanceSettings::unguarded(fn () => InstanceSettings::updateOrCreate(['id' => 0], ['id' => 0]));
+
+ $this->team = Team::factory()->create();
+ $this->member = User::factory()->create();
+ $this->member->teams()->attach($this->team, ['role' => 'member']);
+
+ $this->actingAs($this->member);
+ session(['currentTeam' => $this->team]);
+});
+
+test('member cannot create a cloud provider token through the public action', function () {
+ Http::fake(['*' => Http::response([], 200)]);
+
+ Livewire::test(CloudProviderTokenForm::class)
+ ->set('provider', 'hetzner')
+ ->set('token', 'secret-token')
+ ->set('name', 'Unauthorized token')
+ ->call('addToken')
+ ->assertForbidden();
+
+ expect(CloudProviderToken::query()->count())->toBe(0);
+});
+
+test('member cannot load cloud-init scripts through the public action', function () {
+ $script = CloudInitScript::query()->create([
+ 'team_id' => $this->team->id,
+ 'name' => 'Protected script',
+ 'script' => '#cloud-config',
+ ]);
+
+ CloudInitScript::query()->whereKey($script)->update(['uuid' => null]);
+
+ $component = app(CloudInitScripts::class);
+
+ expect(fn () => $component->loadScripts())
+ ->toThrow(AuthorizationException::class);
+
+ expect($script->refresh()->uuid)->toBeNull();
+});
diff --git a/tests/Feature/Authorization/ServerAuthorizationTest.php b/tests/Feature/Authorization/ServerAuthorizationTest.php
index 63e15be2fe..353c6a687d 100644
--- a/tests/Feature/Authorization/ServerAuthorizationTest.php
+++ b/tests/Feature/Authorization/ServerAuthorizationTest.php
@@ -5,6 +5,7 @@ use App\Livewire\Server\Create as ServerCreate;
use App\Livewire\Server\Index as ServerIndex;
use App\Livewire\Server\LogDrains;
use App\Livewire\Server\Navbar as ServerNavbar;
+use App\Livewire\Server\ValidateAndInstall;
use App\Models\CloudProviderToken;
use App\Models\InstanceSettings;
use App\Models\Server;
@@ -61,6 +62,24 @@ test('member cannot update server', function () {
expect($this->member->can('update', $this->server))->toBeFalse();
});
+test('member cannot invoke server validation write steps', function (string $method) {
+ $this->actingAs($this->member);
+ session(['currentTeam' => $this->team]);
+ $before = $this->server->fresh()->getAttributes();
+
+ Livewire::test(ValidateAndInstall::class, ['server' => $this->server])
+ ->call($method)
+ ->assertForbidden();
+
+ expect($this->server->fresh()->getAttributes())->toBe($before);
+})->with([
+ 'init',
+ 'validateOS',
+ 'validatePrerequisites',
+ 'validateDockerEngine',
+ 'validateDockerVersion',
+]);
+
// --- Server Policy: delete ---
test('admin can delete server', function () {