mirror of
https://github.com/tiennm99/coolify.git
synced 2026-10-14 18:18:29 +00:00
fix(security): enforce team access on mutable actions
Authorize cloud provider token access, audit sensitive operations, and standardize public IDs across deployment and resource flows.
This commit is contained in:
1 parent
9dca7ca351
commit
062ad57740
83 files changed
+824
-278
No files matched your search
@@ -1,14 +1,23 @@
|
||||
<?php
|
||||
|
||||
use App\Models\CloudProviderToken;
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Once;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
InstanceSettings::query()->whereKey(0)->delete();
|
||||
$settings = new InstanceSettings(['is_api_enabled' => true]);
|
||||
$settings->id = 0;
|
||||
$settings->save();
|
||||
Once::flush();
|
||||
|
||||
// Create a team with owner
|
||||
$this->team = Team::factory()->create();
|
||||
$this->user = User::factory()->create();
|
||||
@@ -410,4 +419,33 @@ describe('POST /api/v1/cloud-tokens/{uuid}/validate', function () {
|
||||
$response->assertStatus(200);
|
||||
$response->assertJson(['valid' => true, 'message' => 'Token is valid.']);
|
||||
});
|
||||
|
||||
test('writes an audit log entry when validating a stored token', function () {
|
||||
$token = CloudProviderToken::factory()->create([
|
||||
'team_id' => $this->team->id,
|
||||
'provider' => 'hetzner',
|
||||
'name' => 'Audit Token',
|
||||
]);
|
||||
|
||||
Http::fake([
|
||||
'https://api.hetzner.cloud/v1/servers' => Http::response([], 200),
|
||||
]);
|
||||
|
||||
$auditChannel = Mockery::mock();
|
||||
$auditChannel->shouldReceive('info')
|
||||
->once()
|
||||
->with('api.cloud_token.validated', Mockery::on(function (array $context) use ($token) {
|
||||
return $context['cloud_token_uuid'] === $token->uuid
|
||||
&& $context['provider'] === 'hetzner'
|
||||
&& $context['valid'] === true;
|
||||
}));
|
||||
|
||||
Log::shouldReceive('channel')->with('audit')->andReturn($auditChannel);
|
||||
|
||||
$this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$this->bearerToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->postJson("/api/v1/cloud-tokens/{$token->uuid}/validate")
|
||||
->assertOk();
|
||||
});
|
||||
});
|
||||
@@ -1,15 +1,23 @@
|
||||
<?php
|
||||
|
||||
use App\Models\CloudProviderToken;
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\PrivateKey;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Once;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
InstanceSettings::query()->whereKey(0)->delete();
|
||||
$settings = new InstanceSettings(['is_api_enabled' => true]);
|
||||
$settings->id = 0;
|
||||
$settings->save();
|
||||
Once::flush();
|
||||
|
||||
// Create a team with owner
|
||||
$this->team = Team::factory()->create();
|
||||
$this->user = User::factory()->create();
|
||||
@@ -73,6 +81,27 @@ describe('GET /api/v1/hetzner/locations', function () {
|
||||
|
||||
$response->assertStatus(404);
|
||||
});
|
||||
|
||||
test('member read token cannot use a stored cloud provider token', function () {
|
||||
$member = User::factory()->create();
|
||||
$this->team->members()->attach($member->id, ['role' => 'member']);
|
||||
session(['currentTeam' => $this->team]);
|
||||
$memberToken = $member->createToken('member-read', ['read'])->plainTextToken;
|
||||
|
||||
Http::fake([
|
||||
'https://api.hetzner.cloud/v1/locations*' => Http::response([
|
||||
'locations' => [['id' => 1, 'name' => 'nbg1']],
|
||||
], 200),
|
||||
]);
|
||||
|
||||
$response = $this->withHeaders([
|
||||
'Authorization' => 'Bearer '.$memberToken,
|
||||
'Content-Type' => 'application/json',
|
||||
])->getJson('/api/v1/hetzner/locations?cloud_provider_token_id='.$this->hetznerToken->uuid);
|
||||
|
||||
$response->assertForbidden();
|
||||
Http::assertNothingSent();
|
||||
});
|
||||
});
|
||||
|
||||
describe('GET /api/v1/hetzner/server-types', function () {
|
||||
|
||||
@@ -7,8 +7,11 @@ use App\Models\PersonalAccessToken;
|
||||
use App\Models\PrivateKey;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use App\Policies\CloudInitScriptPolicy;
|
||||
use App\Policies\CloudProviderTokenPolicy;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
@@ -131,6 +134,11 @@ test('admin can view any cloud init scripts', function () {
|
||||
expect(auth()->user()->can('viewAny', CloudInitScript::class))->toBeTrue();
|
||||
});
|
||||
|
||||
test('cloud provider and cloud init policies are explicitly registered', function () {
|
||||
expect(Gate::getPolicyFor(CloudProviderToken::class))->toBeInstanceOf(CloudProviderTokenPolicy::class)
|
||||
->and(Gate::getPolicyFor(CloudInitScript::class))->toBeInstanceOf(CloudInitScriptPolicy::class);
|
||||
});
|
||||
|
||||
// --- Personal Access Token (API Token) Policy ---
|
||||
|
||||
test('any user can create personal access token', function () {
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Models\Server;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Once;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
@@ -64,6 +65,23 @@ function mcpToolJson($response): array
|
||||
return json_decode($response->json('result.content.0.text'), true);
|
||||
}
|
||||
|
||||
function expectMcpAuditLog(array $expected): void
|
||||
{
|
||||
$auditChannel = Mockery::mock();
|
||||
|
||||
Log::shouldReceive('channel')
|
||||
->with('audit')
|
||||
->once()
|
||||
->andReturn($auditChannel);
|
||||
|
||||
$auditChannel
|
||||
->shouldReceive('info')
|
||||
->once()
|
||||
->with('mcp.tool.called', Mockery::on(fn (array $context) => collect($expected)->every(
|
||||
fn ($value, $key) => data_get($context, $key) === $value,
|
||||
)));
|
||||
}
|
||||
|
||||
test('MCP endpoint returns 404 when the instance setting is disabled', function () {
|
||||
InstanceSettings::query()->where('id', 0)->update(['is_mcp_server_enabled' => false]);
|
||||
Once::flush();
|
||||
@@ -193,6 +211,48 @@ test('tool calls fail when the token lacks the read ability', function () {
|
||||
expect($response->json('result.content.0.text'))->toContain('Missing required permissions');
|
||||
});
|
||||
|
||||
test('MCP tools audit successful execution with the actual tool name', function () {
|
||||
Project::create(['name' => 'Mine', 'team_id' => $this->team->id]);
|
||||
$token = $this->user->createToken('mcp-read', ['read'])->plainTextToken;
|
||||
|
||||
expectMcpAuditLog([
|
||||
'tool' => 'list_projects',
|
||||
'team_id' => $this->team->id,
|
||||
'outcome' => 'success',
|
||||
]);
|
||||
|
||||
mcpCallTool($token, 'list_projects')->assertOk();
|
||||
});
|
||||
|
||||
test('MCP tools audit denied execution after ability checks', function () {
|
||||
$token = $this->user->createToken('mcp-no-abilities', [])->plainTextToken;
|
||||
|
||||
expectMcpAuditLog([
|
||||
'tool' => 'list_projects',
|
||||
'team_id' => $this->team->id,
|
||||
'outcome' => 'denied',
|
||||
]);
|
||||
|
||||
$response = mcpCallTool($token, 'list_projects');
|
||||
$response->assertOk();
|
||||
expect($response->json('result.isError'))->toBeTrue();
|
||||
});
|
||||
|
||||
test('MCP tools audit execution errors after tool handling', function () {
|
||||
$token = $this->user->createToken('mcp-read', ['read'])->plainTextToken;
|
||||
|
||||
expectMcpAuditLog([
|
||||
'tool' => 'get_server',
|
||||
'team_id' => $this->team->id,
|
||||
'outcome' => 'error',
|
||||
'resource_uuid' => 'missing-server',
|
||||
]);
|
||||
|
||||
$response = mcpCallTool($token, 'get_server', ['uuid' => 'missing-server']);
|
||||
$response->assertOk();
|
||||
expect($response->json('result.isError'))->toBeTrue();
|
||||
});
|
||||
|
||||
test('MCP rejects token when user no longer belongs to token team', function () {
|
||||
Project::create(['name' => 'Hidden', 'team_id' => $this->team->id]);
|
||||
$token = $this->user->createToken('mcp-read', ['read'])->plainTextToken;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
it('keeps mutable Livewire components behind authorization checks', function (string $path, array $requiredNeedles) {
|
||||
$source = file_get_contents(base_path($path));
|
||||
|
||||
foreach ($requiredNeedles as $needle) {
|
||||
expect($source)->toContain($needle);
|
||||
}
|
||||
})->with([
|
||||
'storage resources' => [
|
||||
'app/Livewire/Storage/Resources.php',
|
||||
['AuthorizesRequests', "authorize('update'", "authorize('view'"],
|
||||
],
|
||||
'postgres init script editor' => [
|
||||
'app/Livewire/Project/Database/InitScript.php',
|
||||
['AuthorizesRequests', "authorize('update'"],
|
||||
],
|
||||
'execute container command' => [
|
||||
'app/Livewire/Project/Shared/ExecuteContainerCommand.php',
|
||||
['AuthorizesRequests', "authorize('view'", "authorize('canAccessTerminal'"],
|
||||
],
|
||||
'terminal' => [
|
||||
'app/Livewire/Project/Shared/Terminal.php',
|
||||
['AuthorizesRequests', "authorize('view'", "authorize('canAccessTerminal'"],
|
||||
],
|
||||
]);
|
||||
@@ -1,19 +1,36 @@
|
||||
<?php
|
||||
|
||||
use App\Livewire\Security\CloudInitScriptForm;
|
||||
use App\Livewire\Security\CloudInitScripts;
|
||||
use App\Livewire\Security\CloudProviderTokenForm;
|
||||
use App\Livewire\Security\CloudProviderTokens;
|
||||
use App\Models\Application;
|
||||
use App\Models\CloudInitScript;
|
||||
use App\Models\CloudProviderToken;
|
||||
use App\Models\Environment;
|
||||
use App\Models\InstanceSettings;
|
||||
use App\Models\Project;
|
||||
use App\Models\Server;
|
||||
use App\Models\Team;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Once;
|
||||
use Livewire\Livewire;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
function makeAuditTeamUser(): array
|
||||
{
|
||||
if (! InstanceSettings::query()->whereKey(0)->exists()) {
|
||||
$settings = new InstanceSettings;
|
||||
$settings->id = 0;
|
||||
$settings->save();
|
||||
}
|
||||
Once::flush();
|
||||
|
||||
$team = Team::factory()->create();
|
||||
$user = User::factory()->create();
|
||||
$team->members()->attach($user->id, ['role' => 'owner']);
|
||||
@@ -122,6 +139,89 @@ describe('audit channel helper', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('security UI audit logging', function () {
|
||||
test('creating a cloud provider token from Livewire writes an audit entry', function () {
|
||||
[$team] = makeAuditTeamUser();
|
||||
|
||||
Http::fake([
|
||||
'https://api.hetzner.cloud/v1/servers' => Http::response([], 200),
|
||||
]);
|
||||
|
||||
$auditChannel = Mockery::mock();
|
||||
$auditChannel->shouldReceive('info')
|
||||
->once()
|
||||
->with('ui.cloud_token.created', Mockery::on(function (array $context) use ($team) {
|
||||
return $context['team_id'] === $team->id
|
||||
&& $context['provider'] === 'hetzner'
|
||||
&& $context['cloud_token_name'] === 'UI Token';
|
||||
}));
|
||||
|
||||
Log::shouldReceive('channel')->with('audit')->andReturn($auditChannel);
|
||||
|
||||
Livewire::test(CloudProviderTokenForm::class)
|
||||
->set('provider', 'hetzner')
|
||||
->set('token', 'secret-token')
|
||||
->set('name', 'UI Token')
|
||||
->call('addToken');
|
||||
});
|
||||
|
||||
test('deleting a cloud provider token from Livewire writes an audit entry', function () {
|
||||
[$team] = makeAuditTeamUser();
|
||||
$token = CloudProviderToken::factory()->create([
|
||||
'team_id' => $team->id,
|
||||
'provider' => 'hetzner',
|
||||
'name' => 'Delete Me',
|
||||
]);
|
||||
|
||||
$auditChannel = Mockery::mock();
|
||||
$auditChannel->shouldReceive('info')
|
||||
->once()
|
||||
->with('ui.cloud_token.deleted', Mockery::on(function (array $context) use ($token) {
|
||||
return $context['cloud_token_uuid'] === $token->uuid
|
||||
&& $context['cloud_token_name'] === 'Delete Me';
|
||||
}));
|
||||
|
||||
Log::shouldReceive('channel')->with('audit')->andReturn($auditChannel);
|
||||
|
||||
Livewire::test(CloudProviderTokens::class)
|
||||
->call('deleteToken', $token->id);
|
||||
});
|
||||
|
||||
test('creating and deleting cloud-init scripts from Livewire write audit entries', function () {
|
||||
[$team] = makeAuditTeamUser();
|
||||
|
||||
$auditChannel = Mockery::mock();
|
||||
$auditChannel->shouldReceive('info')
|
||||
->once()
|
||||
->with('ui.cloud_init_script.created', Mockery::on(function (array $context) use ($team) {
|
||||
return $context['team_id'] === $team->id
|
||||
&& $context['cloud_init_script_name'] === 'Bootstrap';
|
||||
}));
|
||||
$auditChannel->shouldReceive('info')
|
||||
->once()
|
||||
->with('ui.cloud_init_script.deleted', Mockery::on(function (array $context) {
|
||||
return $context['cloud_init_script_name'] === 'Bootstrap';
|
||||
}));
|
||||
|
||||
Log::shouldReceive('channel')->with('audit')->andReturn($auditChannel);
|
||||
|
||||
Livewire::test(CloudInitScriptForm::class)
|
||||
->set('name', 'Bootstrap')
|
||||
->set('script', "#cloud-config\npackages: []")
|
||||
->call('save');
|
||||
|
||||
$script = CloudInitScript::where('team_id', $team->id)->firstOrFail();
|
||||
|
||||
Livewire::test(CloudInitScripts::class)
|
||||
->call('deleteScript', $script->id);
|
||||
});
|
||||
|
||||
test('cloud provider token form does not contain debug ray calls', function () {
|
||||
expect(file_get_contents(app_path('Livewire/Security/CloudProviderTokenForm.php')))
|
||||
->not->toContain('ray(');
|
||||
});
|
||||
});
|
||||
|
||||
describe('webhook signature failure logging', function () {
|
||||
test('GitHub manual webhook with bad signature logs to audit channel', function () {
|
||||
$app = makeAuditApplication();
|
||||
|
||||
@@ -8,6 +8,7 @@ use Illuminate\Contracts\Cache\LockTimeoutException;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
@@ -81,6 +82,23 @@ it('skips the job when the second push is identical', function () use ($running)
|
||||
Queue::assertPushed(PushServerUpdateJob::class, 1);
|
||||
});
|
||||
|
||||
it('only audits sentinel pushes that dispatch a state update', function () use ($running) {
|
||||
$auditChannel = Mockery::mock();
|
||||
$auditChannel->shouldReceive('info')
|
||||
->once()
|
||||
->with('sentinel.metrics_pushed', Mockery::on(function (array $context) {
|
||||
return $context['server_uuid'] === $this->server->uuid
|
||||
&& $context['team_id'] === $this->team->id;
|
||||
}));
|
||||
|
||||
Log::shouldReceive('channel')->with('audit')->andReturn($auditChannel);
|
||||
|
||||
pushSentinel($this->token, sentinelPayload($running()))->assertOk();
|
||||
pushSentinel($this->token, sentinelPayload($running()))->assertOk();
|
||||
|
||||
Queue::assertPushed(PushServerUpdateJob::class, 1);
|
||||
});
|
||||
|
||||
it('updates the heartbeat even when the job is skipped', function () use ($running) {
|
||||
pushSentinel($this->token, sentinelPayload($running()))->assertOk();
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
it('generates lowercase URL-safe public ids without Cuid2', function () {
|
||||
$ids = collect(range(1, 100))->map(fn () => new_public_id());
|
||||
|
||||
expect($ids)->each
|
||||
->toBeString()
|
||||
->toHaveLength(24)
|
||||
->toMatch('/^[a-z0-9]+$/');
|
||||
|
||||
expect($ids->unique())->toHaveCount(100);
|
||||
});
|
||||
|
||||
it('honors custom public id lengths', function () {
|
||||
expect(new_public_id(32))
|
||||
->toBeString()
|
||||
->toHaveLength(32)
|
||||
->toMatch('/^[a-z0-9]+$/');
|
||||
});
|
||||
Reference in new issue
Block a user