Skip to content

Commit dee9d6b

Browse files
authoredNov 19, 2022
Merge pull request #82 from stackkit/feature/77-httprequest-url-must-start-with-https
Provide extra info when pushing tasks to non-https URL
2 parents b0fdb29 + 98b4e81 commit dee9d6b

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed
 

‎src/Config.php

+12
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ public static function getHandler($handler): string
5151
));
5252
}
5353

54+
// When the application is running behind a proxy and the TrustedProxy middleware has not been set up yet,
55+
// an error like [HttpRequest.url must start with 'https'] could be thrown. Since the handler URL must
56+
// always be https, we will provide a little extra information on how to fix this.
57+
if ($parse['scheme'] !== 'https') {
58+
throw new Exception(sprintf(
59+
'Unable to push task to Cloud Tasks because the hander URL is not https. Google Cloud Tasks ' .
60+
'will only call safe (https) URLs. If you are running Laravel behind a proxy (e.g. Ngrok, Expose), make sure it is ' .
61+
'as a trusted proxy. To quickly fix this, add the following to the [app/Http/Middleware/TrustProxies] middleware: ' .
62+
'protected $proxies = \'*\';'
63+
));
64+
}
65+
5466
// Versions 1.x and 2.x required the full path (e.g. my-app.com/handle-task). In 3.x and beyond
5567
// it is no longer necessary to also include the path and simply setting the handler
5668
// URL is enough. If someone upgrades and forgets we will warn them here.

‎tests/QueueTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function a_http_request_with_the_handler_url_is_made()
4242

4343
// Assert
4444
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
45-
return $task->getHttpRequest()->getUrl() === 'http://docker.for.mac.localhost:8080/handle-task';
45+
return $task->getHttpRequest()->getUrl() === 'https://docker.for.mac.localhost:8080/handle-task';
4646
});
4747
}
4848

@@ -69,15 +69,15 @@ public function it_posts_to_the_handler()
6969
public function it_posts_to_the_correct_handler_url()
7070
{
7171
// Arrange
72-
$this->setConfigValue('handler', 'http://docker.for.mac.localhost:8081');
72+
$this->setConfigValue('handler', 'https://docker.for.mac.localhost:8081');
7373
CloudTasksApi::fake();
7474

7575
// Act
7676
$this->dispatch(new SimpleJob());
7777

7878
// Assert
7979
CloudTasksApi::assertTaskCreated(function (Task $task): bool {
80-
return $task->getHttpRequest()->getUrl() === 'http://docker.for.mac.localhost:8081/handle-task';
80+
return $task->getHttpRequest()->getUrl() === 'https://docker.for.mac.localhost:8081/handle-task';
8181
});
8282
}
8383

‎tests/TestCase.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ protected function getEnvironmentSetUp($app)
101101
'queue' => 'barbequeue',
102102
'project' => 'my-test-project',
103103
'location' => 'europe-west6',
104-
'handler' => env('CLOUD_TASKS_HANDLER', 'http://docker.for.mac.localhost:8080'),
104+
'handler' => env('CLOUD_TASKS_HANDLER', 'https://docker.for.mac.localhost:8080'),
105105
'service_account_email' => 'info@stackkit.io',
106106
]);
107107
$app['config']->set('queue.failed.driver', 'database-uuids');
@@ -216,7 +216,7 @@ protected function addIdTokenToHeader(?Closure $closure = null): void
216216
{
217217
$base = [
218218
'iss' => 'https://accounts.google.com',
219-
'aud' => 'http://docker.for.mac.localhost:8080',
219+
'aud' => 'https://docker.for.mac.localhost:8080',
220220
'exp' => time() + 10,
221221
];
222222

0 commit comments

Comments
 (0)