diff --git a/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php b/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php index 8598601c8..02311bead 100644 --- a/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php +++ b/app/Http/Controllers/Api/Remote/Servers/ServerTransferController.php @@ -2,6 +2,9 @@ namespace Pterodactyl\Http\Controllers\Api\Remote\Servers; +use Illuminate\Http\Request; +use Pterodactyl\Models\Node; +use Webmozart\Assert\Assert; use Illuminate\Http\Response; use Illuminate\Http\JsonResponse; use Pterodactyl\Models\Allocation; @@ -32,7 +35,7 @@ class ServerTransferController extends Controller * * @throws \Throwable */ - public function failure(string $uuid): JsonResponse + public function failure(Request $request, string $uuid): JsonResponse { $server = $this->repository->getByUuid($uuid); $transfer = $server->transfer; @@ -40,9 +43,12 @@ class ServerTransferController extends Controller throw new ConflictHttpException('Server is not being transferred.'); } + /* @var Node $node */ + Assert::isInstanceOf($node = $request->attributes->get('node'), Node::class); + // Either node can tell the panel that the transfer has failed. Only the new node // can tell the panel that it was successful. - if (! $server->node->is($transfer->newNode) && ! $server->node->is($transfer->oldNode)) { + if (! $node->is($transfer->newNode) && ! $node->is($transfer->oldNode)) { throw new HttpForbiddenException('Requesting node does not have permission to access this server.'); } @@ -54,7 +60,7 @@ class ServerTransferController extends Controller * * @throws \Throwable */ - public function success(string $uuid): JsonResponse + public function success(Request $request, string $uuid): JsonResponse { $server = $this->repository->getByUuid($uuid); $transfer = $server->transfer; @@ -62,9 +68,12 @@ class ServerTransferController extends Controller throw new ConflictHttpException('Server is not being transferred.'); } + /* @var Node $node */ + Assert::isInstanceOf($node = $request->attributes->get('node'), Node::class); + // Only the new node communicates a successful state to the panel, so we should // not allow the old node to hit this endpoint. - if (! $server->node->is($transfer->newNode)) { + if (! $node->is($transfer->newNode)) { throw new HttpForbiddenException('Requesting node does not have permission to access this server.'); } diff --git a/app/Models/ServerTransfer.php b/app/Models/ServerTransfer.php index b903cbbb8..f432d2e5e 100644 --- a/app/Models/ServerTransfer.php +++ b/app/Models/ServerTransfer.php @@ -4,6 +4,7 @@ namespace Pterodactyl\Models; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Factories\HasFactory; /** * @property int $id @@ -24,6 +25,9 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; */ class ServerTransfer extends Model { + /** @use HasFactory<\Database\Factories\ServerTransferFactory> */ + use HasFactory; + /** * The resource name for this model when it is transformed into an * API representation using fractal. diff --git a/database/Factories/ServerTransferFactory.php b/database/Factories/ServerTransferFactory.php new file mode 100644 index 000000000..37e56c617 --- /dev/null +++ b/database/Factories/ServerTransferFactory.php @@ -0,0 +1,31 @@ + [], + 'new_additional_allocations' => [], + 'successful' => null, + 'archived' => false, + ]; + } +} diff --git a/tests/Integration/Api/Remote/ServerTransferControllerTest.php b/tests/Integration/Api/Remote/ServerTransferControllerTest.php new file mode 100644 index 000000000..8b119a651 --- /dev/null +++ b/tests/Integration/Api/Remote/ServerTransferControllerTest.php @@ -0,0 +1,111 @@ +createServerModel(); + + $new = Node::factory() + ->for(Location::factory()) + ->has(Allocation::factory()) + ->create(); + + $this->transfer = ServerTransfer::factory()->for($server)->create([ + 'old_allocation' => $server->allocation_id, + 'new_allocation' => $new->allocations->first()->id, + 'new_node' => $new->id, + 'old_node' => $server->node_id, + ]); + } + + public function testSuccessStatusUpdateCanBeSentFromNewNode(): void + { + $server = $this->transfer->server; + $newNode = $this->transfer->newNode; + + $this + ->withHeader('Authorization', "Bearer $newNode->daemon_token_id." . $newNode->getDecryptedKey()) + ->postJson("/api/remote/servers/{$server->uuid}/transfer/success") + ->assertNoContent(); + + $this->assertTrue($this->transfer->refresh()->successful); + } + + public function testFailureStatusUpdateCanBeSentFromOldNode(): void + { + $server = $this->transfer->server; + $oldNode = $this->transfer->oldNode; + + $this->withHeader('Authorization', "Bearer $oldNode->daemon_token_id." . $oldNode->getDecryptedKey()) + ->postJson("/api/remote/servers/{$server->uuid}/transfer/failure") + ->assertNoContent(); + + $this->assertFalse($this->transfer->refresh()->successful); + } + + public function testFailureStatusUpdateCanBeSentFromNewNode(): void + { + $server = $this->transfer->server; + $newNode = $this->transfer->newNode; + + $this->withHeader('Authorization', "Bearer $newNode->daemon_token_id." . $newNode->getDecryptedKey()) + ->postJson("/api/remote/servers/{$server->uuid}/transfer/failure") + ->assertNoContent(); + + $this->assertFalse($this->transfer->refresh()->successful); + } + + public function testSuccessStatusUpdateCannotBeSentFromOldNode(): void + { + $server = $this->transfer->server; + $oldNode = $this->transfer->oldNode; + + $this->withHeader('Authorization', "Bearer $oldNode->daemon_token_id." . $oldNode->getDecryptedKey()) + ->postJson("/api/remote/servers/{$server->uuid}/transfer/success") + ->assertForbidden() + ->assertJsonPath('errors.0.code', 'HttpForbiddenException') + ->assertJsonPath('errors.0.detail', 'Requesting node does not have permission to access this server.'); + + $this->assertNull($this->transfer->refresh()->successful); + } + + public function testSuccessStatusUpdateCannotBeSentFromUnauthorizedNode(): void + { + $server = $this->transfer->server; + $node = Node::factory()->for(Location::factory())->create(); + + $this->withHeader('Authorization', "Bearer $node->daemon_token_id." . $node->getDecryptedKey()) + ->postJson("/api/remote/servers/$server->uuid/transfer/success") + ->assertForbidden() + ->assertJsonPath('errors.0.code', 'HttpForbiddenException') + ->assertJsonPath('errors.0.detail', 'Requesting node does not have permission to access this server.'); + + $this->assertNull($this->transfer->refresh()->successful); + } + + public function testFailureStatusUpdateCannotBeSentFromUnauthorizedNode(): void + { + $server = $this->transfer->server; + $node = Node::factory()->for(Location::factory())->create(); + + $this->withHeader('Authorization', "Bearer $node->daemon_token_id." . $node->getDecryptedKey()) + ->postJson("/api/remote/servers/$server->uuid/transfer/failure")->assertForbidden() + ->assertJsonPath('errors.0.code', 'HttpForbiddenException') + ->assertJsonPath('errors.0.detail', 'Requesting node does not have permission to access this server.'); + + $this->assertNull($this->transfer->refresh()->successful); + } +}