Skip to content
This repository was archived by the owner on Nov 9, 2020. It is now read-only.

Commit e67179a

Browse files
committed
Improve validation of incoming jobs
1 parent 7cbce6b commit e67179a

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Http/Controllers/QueueController.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,16 @@ public function store(Request $request, $queue)
3434
return new Response('Job payload is required', 422);
3535
}
3636

37+
$json = json_decode($payload, true);
38+
39+
if (is_null($json)) {
40+
return new Response('Job payload is no valid JSON', 422);
41+
}
42+
43+
if (!class_exists(array_get($json, 'data.commandName'))) {
44+
return new Response('Job payload is no valid JSON', 422);
45+
}
46+
3747
app('queue')->connection(config('remote-queue.connection'))
3848
->pushRaw($payload, $queue);
3949

tests/Http/Controllers/QueueControllerTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,24 @@ public function testShow()
2020

2121
public function testStore()
2222
{
23-
$payload = '{"commandName":"FakeTestJob"}';
23+
$payload = json_encode(['data' => ['commandName' => TestJob::class]]);
24+
$payload2 = json_encode(['data' => ['commandName' => 'DoesNotExist']]);
2425
$mock = Mockery::mock();
2526
$mock->shouldReceive('pushRaw')->once()->with($payload, 'default');
2627
Queue::shouldReceive('connection')->once()->andReturn($mock);
2728
$this->withoutMiddleware()
2829
->post('api/v1/remote-queue/default')
30+
// Payload is required
31+
->assertStatus(422);
32+
33+
$this->withoutMiddleware()
34+
->call('POST', 'api/v1/remote-queue/default', [], [], [], [], '.,')
35+
// Payload is no valid JSON
36+
->assertStatus(422);
37+
38+
$this->withoutMiddleware()
39+
->call('POST', 'api/v1/remote-queue/default', [], [], [], [], $payload2)
40+
// Job class does not exist
2941
->assertStatus(422);
3042

3143
$this->withoutMiddleware()

0 commit comments

Comments
 (0)