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(api): use explicit team ID for S3 storage lookup in backup endpoints

Andras Bacsai5019c8db928a
diff --git a/app/Http/Controllers/Api/DatabasesController.php b/app/Http/Controllers/Api/DatabasesController.php
index f3783696d0..8241e5fba6 100644
--- a/app/Http/Controllers/Api/DatabasesController.php
+++ b/app/Http/Controllers/Api/DatabasesController.php
@@ -747,7 +747,7 @@ class DatabasesController extends Controller
}
if ($request->filled('s3_storage_uuid')) {
- $existsInTeam = S3Storage::ownedByCurrentTeam()->where('uuid', $request->s3_storage_uuid)->exists();
+ $existsInTeam = S3Storage::ownedByCurrentTeamAPI($teamId)->where('uuid', $request->s3_storage_uuid)->exists();
if (! $existsInTeam) {
return response()->json([
'message' => 'Validation failed.',
@@ -774,7 +774,7 @@ class DatabasesController extends Controller
// Convert s3_storage_uuid to s3_storage_id
if (isset($backupData['s3_storage_uuid'])) {
- $s3Storage = S3Storage::ownedByCurrentTeam()->where('uuid', $backupData['s3_storage_uuid'])->first();
+ $s3Storage = S3Storage::ownedByCurrentTeamAPI($teamId)->where('uuid', $backupData['s3_storage_uuid'])->first();
if ($s3Storage) {
$backupData['s3_storage_id'] = $s3Storage->id;
} elseif ($request->boolean('save_s3')) {
@@ -982,7 +982,7 @@ class DatabasesController extends Controller
], 422);
}
if ($request->filled('s3_storage_uuid')) {
- $existsInTeam = S3Storage::ownedByCurrentTeam()->where('uuid', $request->s3_storage_uuid)->exists();
+ $existsInTeam = S3Storage::ownedByCurrentTeamAPI($teamId)->where('uuid', $request->s3_storage_uuid)->exists();
if (! $existsInTeam) {
return response()->json([
'message' => 'Validation failed.',
@@ -1015,7 +1015,7 @@ class DatabasesController extends Controller
// Convert s3_storage_uuid to s3_storage_id
if (isset($backupData['s3_storage_uuid'])) {
- $s3Storage = S3Storage::ownedByCurrentTeam()->where('uuid', $backupData['s3_storage_uuid'])->first();
+ $s3Storage = S3Storage::ownedByCurrentTeamAPI($teamId)->where('uuid', $backupData['s3_storage_uuid'])->first();
if ($s3Storage) {
$backupData['s3_storage_id'] = $s3Storage->id;
} elseif ($request->boolean('save_s3')) {
diff --git a/app/Models/S3Storage.php b/app/Models/S3Storage.php
index d6feccc7ea..e02e07a4e4 100644
--- a/app/Models/S3Storage.php
+++ b/app/Models/S3Storage.php
@@ -66,6 +66,13 @@ class S3Storage extends BaseModel
return S3Storage::whereTeamId(currentTeam()->id)->select($selectArray->all())->orderBy('name');
}
+ public static function ownedByCurrentTeamAPI(int $teamId, array $select = ['*'])
+ {
+ $selectArray = collect($select)->concat(['id']);
+
+ return S3Storage::whereTeamId($teamId)->select($selectArray->all())->orderBy('name');
+ }
+
public function isUsable()
{
return $this->is_usable;
diff --git a/tests/Feature/DatabaseBackupCreationApiTest.php b/tests/Feature/DatabaseBackupCreationApiTest.php
index 893141de39..4588cf9de5 100644
--- a/tests/Feature/DatabaseBackupCreationApiTest.php
+++ b/tests/Feature/DatabaseBackupCreationApiTest.php
@@ -1,5 +1,12 @@
<?php
+use App\Models\Environment;
+use App\Models\InstanceSettings;
+use App\Models\Project;
+use App\Models\S3Storage;
+use App\Models\ScheduledDatabaseBackup;
+use App\Models\Server;
+use App\Models\StandaloneDocker;
use App\Models\StandalonePostgresql;
use App\Models\Team;
use App\Models\User;
@@ -8,138 +15,193 @@ use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
- // Create a team with owner
+ InstanceSettings::updateOrCreate(['id' => 0]);
+
$this->team = Team::factory()->create();
$this->user = User::factory()->create();
$this->team->members()->attach($this->user->id, ['role' => 'owner']);
- // Create an API token for the user
- $this->token = $this->user->createToken('test-token', ['*'], $this->team->id);
- $this->bearerToken = $this->token->plainTextToken;
+ session(['currentTeam' => $this->team]);
- // Mock a database - we'll use Mockery to avoid needing actual database setup
- $this->database = \Mockery::mock(StandalonePostgresql::class);
- $this->database->shouldReceive('getAttribute')->with('id')->andReturn(1);
- $this->database->shouldReceive('getAttribute')->with('uuid')->andReturn('test-db-uuid');
- $this->database->shouldReceive('getAttribute')->with('postgres_db')->andReturn('testdb');
- $this->database->shouldReceive('type')->andReturn('standalone-postgresql');
- $this->database->shouldReceive('getMorphClass')->andReturn('App\Models\StandalonePostgresql');
-});
+ $this->token = $this->user->createToken('test-token', ['*']);
+ $this->bearerToken = $this->token->plainTextToken;
-afterEach(function () {
- \Mockery::close();
+ $this->server = Server::factory()->create(['team_id' => $this->team->id]);
+ $this->destination = StandaloneDocker::where('server_id', $this->server->id)->first();
+ $this->project = Project::factory()->create(['team_id' => $this->team->id]);
+ $this->environment = Environment::factory()->create(['project_id' => $this->project->id]);
+
+ $this->database = StandalonePostgresql::create([
+ 'name' => 'test-postgres',
+ 'image' => 'postgres:15-alpine',
+ 'postgres_user' => 'postgres',
+ 'postgres_password' => 'password',
+ 'postgres_db' => 'testdb',
+ 'environment_id' => $this->environment->id,
+ 'destination_id' => $this->destination->id,
+ 'destination_type' => $this->destination->getMorphClass(),
+ ]);
+
+ $this->s3Storage = S3Storage::create([
+ 'name' => 'test-s3',
+ 'region' => 'us-east-1',
+ 'key' => 'test-key',
+ 'secret' => 'test-secret',
+ 'bucket' => 'test-bucket',
+ 'endpoint' => 'https://s3.example.com',
+ 'team_id' => $this->team->id,
+ 'is_usable' => true,
+ ]);
});
describe('POST /api/v1/databases/{uuid}/backups', function () {
- test('creates backup configuration with minimal required fields', function () {
- // This is a unit-style test using mocks to avoid database dependency
- // For full integration testing, this should be run inside Docker
+ test('creates backup with s3 storage via API token', function () {
+ $response = $this->withHeaders([
+ 'Authorization' => 'Bearer '.$this->bearerToken,
+ 'Content-Type' => 'application/json',
+ ])->postJson("/api/v1/databases/{$this->database->uuid}/backups", [
+ 'frequency' => '0 2 * * 0',
+ 'save_s3' => true,
+ 's3_storage_uuid' => $this->s3Storage->uuid,
+ 'enabled' => true,
+ ]);
+
+ $response->assertStatus(201);
+ $response->assertJsonStructure(['uuid', 'message']);
+
+ $backup = ScheduledDatabaseBackup::where('uuid', $response->json('uuid'))->first();
+ expect($backup)->not->toBeNull();
+ expect($backup->s3_storage_id)->toBe($this->s3Storage->id);
+ expect($backup->save_s3)->toBeTrue();
+ expect($backup->team_id)->toBe($this->team->id);
+ });
+ test('creates backup without s3 storage', function () {
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$this->bearerToken,
'Content-Type' => 'application/json',
- ])->postJson('/api/v1/databases/test-db-uuid/backups', [
+ ])->postJson("/api/v1/databases/{$this->database->uuid}/backups", [
'frequency' => 'daily',
]);
- // Since we're mocking, this test verifies the endpoint exists and basic validation
- // Full integration tests should be run in Docker environment
- expect($response->status())->toBeIn([201, 404, 422]);
+ $response->assertStatus(201);
+ $response->assertJsonStructure(['uuid', 'message']);
});
- test('validates frequency is required', function () {
+ test('rejects s3_storage_uuid from another team', function () {
+ $otherTeam = Team::factory()->create();
+ $otherS3 = S3Storage::create([
+ 'name' => 'other-s3',
+ 'region' => 'us-east-1',
+ 'key' => 'other-key',
+ 'secret' => 'other-secret',
+ 'bucket' => 'other-bucket',
+ 'endpoint' => 'https://s3.example.com',
+ 'team_id' => $otherTeam->id,
+ 'is_usable' => true,
+ ]);
+
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$this->bearerToken,
'Content-Type' => 'application/json',
- ])->postJson('/api/v1/databases/test-db-uuid/backups', [
- 'enabled' => true,
+ ])->postJson("/api/v1/databases/{$this->database->uuid}/backups", [
+ 'frequency' => '0 2 * * 0',
+ 'save_s3' => true,
+ 's3_storage_uuid' => $otherS3->uuid,
]);
$response->assertStatus(422);
- $response->assertJsonValidationErrors(['frequency']);
+ $response->assertJsonValidationErrors(['s3_storage_uuid']);
});
- test('validates s3_storage_uuid required when save_s3 is true', function () {
+ test('validates frequency is required', function () {
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$this->bearerToken,
'Content-Type' => 'application/json',
- ])->postJson('/api/v1/databases/test-db-uuid/backups', [
- 'frequency' => 'daily',
- 'save_s3' => true,
+ ])->postJson("/api/v1/databases/{$this->database->uuid}/backups", [
+ 'enabled' => true,
]);
- // Should fail validation because s3_storage_uuid is missing
- expect($response->status())->toBeIn([404, 422]);
+ $response->assertStatus(422);
+ $response->assertJsonValidationErrors(['frequency']);
});
- test('rejects invalid frequency format', function () {
+ test('validates s3_storage_uuid required when save_s3 is true', function () {
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$this->bearerToken,
'Content-Type' => 'application/json',
- ])->postJson('/api/v1/databases/test-db-uuid/backups', [
- 'frequency' => 'invalid-frequency',
+ ])->postJson("/api/v1/databases/{$this->database->uuid}/backups", [
+ 'frequency' => 'daily',
+ 'save_s3' => true,
]);
- expect($response->status())->toBeIn([404, 422]);
+ $response->assertStatus(422);
+ $response->assertJsonValidationErrors(['s3_storage_uuid']);
});
test('rejects request without authentication', function () {
- $response = $this->postJson('/api/v1/databases/test-db-uuid/backups', [
+ $response = $this->postJson("/api/v1/databases/{$this->database->uuid}/backups", [
'frequency' => 'daily',
]);
$response->assertStatus(401);
});
+});
- test('validates retention fields are integers with minimum 0', function () {
- $response = $this->withHeaders([
- 'Authorization' => 'Bearer '.$this->bearerToken,
- 'Content-Type' => 'application/json',
- ])->postJson('/api/v1/databases/test-db-uuid/backups', [
+describe('PATCH /api/v1/databases/{uuid}/backups/{scheduled_backup_uuid}', function () {
+ test('updates backup to use s3 storage via API token', function () {
+ $backup = ScheduledDatabaseBackup::create([
'frequency' => 'daily',
- 'database_backup_retention_amount_locally' => -1,
+ 'enabled' => true,
+ 'database_id' => $this->database->id,
+ 'database_type' => $this->database->getMorphClass(),
+ 'team_id' => $this->team->id,
]);
- expect($response->status())->toBeIn([404, 422]);
- });
-
- test('accepts valid cron expressions', function () {
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$this->bearerToken,
'Content-Type' => 'application/json',
- ])->postJson('/api/v1/databases/test-db-uuid/backups', [
- 'frequency' => '0 2 * * *', // Daily at 2 AM
+ ])->patchJson("/api/v1/databases/{$this->database->uuid}/backups/{$backup->uuid}", [
+ 'save_s3' => true,
+ 's3_storage_uuid' => $this->s3Storage->uuid,
]);
- // Will fail with 404 because database doesn't exist, but validates the request format
- expect($response->status())->toBeIn([201, 404, 422]);
+ $response->assertStatus(200);
+ $backup->refresh();
+ expect($backup->s3_storage_id)->toBe($this->s3Storage->id);
+ expect($backup->save_s3)->toBeTrue();
});
- test('accepts predefined frequency values', function () {
- $frequencies = ['every_minute', 'hourly', 'daily', 'weekly', 'monthly', 'yearly'];
-
- foreach ($frequencies as $frequency) {
- $response = $this->withHeaders([
- 'Authorization' => 'Bearer '.$this->bearerToken,
- 'Content-Type' => 'application/json',
- ])->postJson('/api/v1/databases/test-db-uuid/backups', [
- 'frequency' => $frequency,
- ]);
+ test('rejects s3_storage_uuid from another team on update', function () {
+ $otherTeam = Team::factory()->create();
+ $otherS3 = S3Storage::create([
+ 'name' => 'other-s3',
+ 'region' => 'us-east-1',
+ 'key' => 'other-key',
+ 'secret' => 'other-secret',
+ 'bucket' => 'other-bucket',
+ 'endpoint' => 'https://s3.example.com',
+ 'team_id' => $otherTeam->id,
+ 'is_usable' => true,
+ ]);
- // Will fail with 404 because database doesn't exist, but validates the request format
- expect($response->status())->toBeIn([201, 404, 422]);
- }
- });
+ $backup = ScheduledDatabaseBackup::create([
+ 'frequency' => 'daily',
+ 'enabled' => true,
+ 'database_id' => $this->database->id,
+ 'database_type' => $this->database->getMorphClass(),
+ 'team_id' => $this->team->id,
+ ]);
- test('rejects extra fields not in allowed list', function () {
$response = $this->withHeaders([
'Authorization' => 'Bearer '.$this->bearerToken,
'Content-Type' => 'application/json',
- ])->postJson('/api/v1/databases/test-db-uuid/backups', [
- 'frequency' => 'daily',
- 'invalid_field' => 'invalid_value',
+ ])->patchJson("/api/v1/databases/{$this->database->uuid}/backups/{$backup->uuid}", [
+ 'save_s3' => true,
+ 's3_storage_uuid' => $otherS3->uuid,
]);
- expect($response->status())->toBeIn([404, 422]);
+ $response->assertStatus(422);
+ $response->assertJsonValidationErrors(['s3_storage_uuid']);
});
});