Skip to content

Commit ec8c239

Browse files
committed
Fixed signature check
1 parent 773b052 commit ec8c239

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/Exceptions/WebhookFailed.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
class WebhookFailed extends Exception
99
{
10+
public static function invalidSignature(): self
11+
{
12+
return new static('The signature is invalid.');
13+
}
14+
1015
public static function signingSecretNotSet(): self
1116
{
1217
return new static('The webhook signing secret is not set. Make sure that the `signing_secret` config key is set to the correct value.');

src/Webhook.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,26 @@
22

33
namespace BinaryCats\MailgunWebhooks;
44

5+
use BinaryCats\MailgunWebhooks\Exceptions\WebhookFailed;
6+
57
class Webhook
68
{
79
/**
810
* Validate and raise an appropriate event.
911
*
1012
* @param $payload
11-
* @param array $signature
12-
* @param string $secret
13+
* @param array $signature
14+
* @param string $secret
1315
* @return BinaryCats\MailgunWebhooks\Event
16+
* @throws WebhookFailed
1417
*/
1518
public static function constructEvent(array $payload, array $signature, string $secret): Event
1619
{
1720
// verify we are good, else throw an expection
18-
WebhookSignature::make($signature, $secret)->verify();
21+
if (!WebhookSignature::make($signature, $secret)->verify()) {
22+
throw WebhookFailed::invalidSignature();
23+
}
24+
1925
// Make an event
2026
return Event::constructFrom($payload);
2127
}

tests/IntegrationTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,32 @@ public function a_request_with_a_config_key_will_use_the_correct_signing_secret(
165165
->postJson('mailgun-webhooks/somekey', $payload)
166166
->assertSuccessful();
167167
}
168+
169+
170+
/** @test */
171+
public function an_invalid_signature_value_generates_a_500_error()
172+
{
173+
$payload = [
174+
'event-data' => [
175+
'event' => 'my.type',
176+
'key' => 'value',
177+
],
178+
];
179+
180+
Arr::set($payload, 'signature', [
181+
'timestamp' => time(),
182+
'token' => 'some token',
183+
'signature' => 'invalid_signature'
184+
]);
185+
186+
$this
187+
->postJson('mailgun-webhooks', $payload)
188+
->assertStatus(500);
189+
190+
$this->assertCount(0, WebhookCall::get());
191+
192+
Event::assertNotDispatched('mailgun-webhooks::my.type');
193+
194+
$this->assertNull(cache('dummyjob'));
195+
}
168196
}

0 commit comments

Comments
 (0)