Skip to content

Commit 39646cc

Browse files
Merge pull request #62 from dshafik/patch-1
Support encryption, avoid double __unserialize call
2 parents 7b0f6ae + d5715db commit 39646cc

File tree

5 files changed

+64
-15
lines changed

5 files changed

+64
-15
lines changed

src/CloudTasksJob.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public function __construct(array $job, CloudTasksQueue $cloudTasksQueue)
2424
$this->job = $job;
2525
$this->container = Container::getInstance();
2626
$this->cloudTasksQueue = $cloudTasksQueue;
27-
/** @var \stdClass $command */
28-
$command = unserialize($job['data']['command']);
29-
$this->queue = $command->queue;
27+
28+
$command = TaskHandler::getCommandProperties($job['data']['command']);
29+
$this->queue = $command['queue'] ?? config('queue.connections.' .config('queue.default') . '.queue');
3030
}
3131

3232
public function getJobId(): string

src/TaskHandler.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use Google\Cloud\Tasks\V2\CloudTasksClient;
66
use Google\Cloud\Tasks\V2\RetryConfig;
77
use Illuminate\Bus\Queueable;
8+
use Illuminate\Contracts\Encryption\Encrypter;
89
use Illuminate\Queue\Jobs\Job;
910
use Illuminate\Queue\WorkerOptions;
11+
use Illuminate\Support\Str;
1012
use stdClass;
1113
use UnexpectedValueException;
1214
use function Safe\json_decode;
@@ -56,8 +58,8 @@ private function loadQueueConnectionConfiguration(array $task): void
5658
/**
5759
* @var stdClass $command
5860
*/
59-
$command = unserialize($task['data']['command']);
60-
$connection = $command->connection ?? config('queue.default');
61+
$command = self::getCommandProperties($task['data']['command']);
62+
$connection = $command['connection'] ?? config('queue.default');
6163
$this->config = array_merge(
6264
(array) config("queue.connections.{$connection}"),
6365
['connection' => $connection]
@@ -131,4 +133,17 @@ private function loadQueueRetryConfig(CloudTasksJob $job): void
131133

132134
$this->retryConfig = CloudTasksApi::getRetryConfig($queueName);
133135
}
136+
137+
public static function getCommandProperties(string $command): array
138+
{
139+
if (Str::startsWith($command, 'O:')) {
140+
return (array) unserialize($command, ['allowed_classes' => false]);
141+
}
142+
143+
if (app()->bound(Encrypter::class)) {
144+
return (array) unserialize(app(Encrypter::class)->decrypt($command), ['allowed_classes' => false]);
145+
}
146+
147+
return [];
148+
}
134149
}

tests/QueueTest.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Google\Cloud\Tasks\V2\HttpMethod;
88
use Google\Cloud\Tasks\V2\Task;
99
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
10+
use Stackkit\LaravelGoogleCloudTasksQueue\TaskHandler;
1011
use Tests\Support\FailingJob;
1112
use Tests\Support\SimpleJob;
1213

@@ -137,19 +138,19 @@ public function it_posts_the_task_the_correct_queue()
137138
// Assert
138139
CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName): bool {
139140
$decoded = json_decode($task->getHttpRequest()->getBody(), true);
140-
$command = unserialize($decoded['data']['command']);
141+
$command = TaskHandler::getCommandProperties($decoded['data']['command']);
141142

142143
return $decoded['displayName'] === SimpleJob::class
143-
&& $command->queue === null
144+
&& $command['queue'] === null
144145
&& $queueName === 'projects/my-test-project/locations/europe-west6/queues/barbequeue';
145146
});
146147

147148
CloudTasksApi::assertTaskCreated(function (Task $task, string $queueName): bool {
148149
$decoded = json_decode($task->getHttpRequest()->getBody(), true);
149-
$command = unserialize($decoded['data']['command']);
150+
$command = TaskHandler::getCommandProperties($decoded['data']['command']);
150151

151152
return $decoded['displayName'] === FailingJob::class
152-
&& $command->queue === 'my-special-queue'
153+
&& $command['queue'] === 'my-special-queue'
153154
&& $queueName === 'projects/my-test-project/locations/europe-west6/queues/my-special-queue';
154155
});
155156
}

tests/Support/EncryptedJob.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Tests\Support;
4+
5+
use Illuminate\Bus\Queueable;
6+
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
7+
use Illuminate\Contracts\Queue\ShouldQueue;
8+
use Illuminate\Foundation\Bus\Dispatchable;
9+
use Illuminate\Queue\InteractsWithQueue;
10+
use Illuminate\Queue\SerializesModels;
11+
12+
class EncryptedJob implements ShouldQueue, ShouldBeEncrypted
13+
{
14+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
15+
16+
public function handle()
17+
{
18+
logger('EncryptedJob:success');
19+
}
20+
}

tests/TaskHandlerTest.php

+19-6
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,16 @@
55
use Firebase\JWT\ExpiredException;
66
use Google\Cloud\Tasks\V2\RetryConfig;
77
use Google\Protobuf\Duration;
8-
use Illuminate\Database\Eloquent\Model;
9-
use Illuminate\Queue\Events\JobExceptionOccurred;
10-
use Illuminate\Queue\Events\JobFailed;
118
use Illuminate\Queue\Events\JobProcessed;
129
use Illuminate\Queue\Events\JobProcessing;
13-
use Illuminate\Support\Facades\DB;
1410
use Illuminate\Support\Facades\Event;
1511
use Illuminate\Support\Facades\Log;
16-
use Illuminate\Support\Facades\Queue;
1712
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksApi;
1813
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksException;
19-
use Stackkit\LaravelGoogleCloudTasksQueue\CloudTasksJob;
2014
use Stackkit\LaravelGoogleCloudTasksQueue\LogFake;
2115
use Stackkit\LaravelGoogleCloudTasksQueue\OpenIdVerificator;
2216
use Stackkit\LaravelGoogleCloudTasksQueue\StackkitCloudTask;
17+
use Tests\Support\EncryptedJob;
2318
use Tests\Support\FailingJob;
2419
use Tests\Support\SimpleJob;
2520
use UnexpectedValueException;
@@ -272,4 +267,22 @@ public function test_max_attempts_in_combination_with_retry_until()
272267

273268
$this->assertEquals('failed', $task->fresh()->status);
274269
}
270+
271+
/**
272+
* @test
273+
*/
274+
public function it_can_handle_encrypted_jobs()
275+
{
276+
// Arrange
277+
OpenIdVerificator::fake();
278+
Log::swap(new LogFake());
279+
280+
// Act
281+
$job = $this->dispatch(new EncryptedJob());
282+
$job->run();
283+
284+
// Assert
285+
$this->assertEquals('O:26:"Tests\Support\EncryptedJob":0:{}', decrypt($job->payload['data']['command']));
286+
Log::assertLogged('EncryptedJob:success');
287+
}
275288
}

0 commit comments

Comments
 (0)