diff --git a/src/CloudTasksJob.php b/src/CloudTasksJob.php index fc83cbe..63f1e12 100644 --- a/src/CloudTasksJob.php +++ b/src/CloudTasksJob.php @@ -24,9 +24,9 @@ public function __construct(array $job, CloudTasksQueue $cloudTasksQueue) $this->job = $job; $this->container = Container::getInstance(); $this->cloudTasksQueue = $cloudTasksQueue; - /** @var \stdClass $command */ - $command = unserialize($job['data']['command']); - $this->queue = $command->queue; + + $command = TaskHandler::getCommandProperties($job['data']['command']); + $this->queue = $command['queue'] ?? config('queue.connections.' .config('queue.default') . '.queue'); } public function getJobId(): string diff --git a/src/TaskHandler.php b/src/TaskHandler.php index 9c1ba50..989679c 100644 --- a/src/TaskHandler.php +++ b/src/TaskHandler.php @@ -5,8 +5,10 @@ use Google\Cloud\Tasks\V2\CloudTasksClient; use Google\Cloud\Tasks\V2\RetryConfig; use Illuminate\Bus\Queueable; +use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Queue\Jobs\Job; use Illuminate\Queue\WorkerOptions; +use Illuminate\Support\Str; use stdClass; use UnexpectedValueException; use function Safe\json_decode; @@ -56,8 +58,8 @@ private function loadQueueConnectionConfiguration(array $task): void /** * @var stdClass $command */ - $command = unserialize($task['data']['command']); - $connection = $command->connection ?? config('queue.default'); + $command = self::getCommandProperties($task['data']['command']); + $connection = $command['connection'] ?? config('queue.default'); $this->config = array_merge( (array) config("queue.connections.{$connection}"), ['connection' => $connection] @@ -131,4 +133,17 @@ private function loadQueueRetryConfig(CloudTasksJob $job): void $this->retryConfig = CloudTasksApi::getRetryConfig($queueName); } + + public static function getCommandProperties(string $command): array + { + if (Str::startsWith($command, 'O:')) { + return (array) unserialize($command, ['allowed_classes' => false]); + } + + if (app()->bound(Encrypter::class)) { + return (array) unserialize(app(Encrypter::class)->decrypt($command), ['allowed_classes' => false]); + } + + return []; + } } diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 0a89bda..cdd303f 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -7,6 +7,7 @@ use Google\Cloud\Tasks\V2\HttpMethod; use Google\Cloud\Tasks\V2\Task; use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi; +use Stackkit\LaravelGoogleCloudTasksQueue\TaskHandler; use Tests\Support\FailingJob; use Tests\Support\SimpleJob; @@ -137,19 +138,19 @@ public function it_posts_the_task_the_correct_queue() // Assert CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName): bool { $decoded = json_decode($task->getHttpRequest()->getBody(), true); - $command = unserialize($decoded['data']['command']); + $command = TaskHandler::getCommandProperties($decoded['data']['command']); return $decoded['displayName'] === SimpleJob::class - && $command->queue === null + && $command['queue'] === null && $queueName === 'projects/my-test-project/locations/europe-west6/queues/barbequeue'; }); CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName): bool { $decoded = json_decode($task->getHttpRequest()->getBody(), true); - $command = unserialize($decoded['data']['command']); + $command = TaskHandler::getCommandProperties($decoded['data']['command']); return $decoded['displayName'] === FailingJob::class - && $command->queue === 'my-special-queue' + && $command['queue'] === 'my-special-queue' && $queueName === 'projects/my-test-project/locations/europe-west6/queues/my-special-queue'; }); } diff --git a/tests/Support/EncryptedJob.php b/tests/Support/EncryptedJob.php new file mode 100644 index 0000000..8f8e4ff --- /dev/null +++ b/tests/Support/EncryptedJob.php @@ -0,0 +1,20 @@ +assertEquals('failed', $task->fresh()->status); } + + /** + * @test + */ + public function it_can_handle_encrypted_jobs() + { + // Arrange + OpenIdVerificator::fake(); + Log::swap(new LogFake()); + + // Act + $job = $this->dispatch(new EncryptedJob()); + $job->run(); + + // Assert + $this->assertEquals('O:26:"Tests\Support\EncryptedJob":0:{}', decrypt($job->payload['data']['command'])); + Log::assertLogged('EncryptedJob:success'); + } }