File tree Expand file tree Collapse file tree 3 files changed +42
-3
lines changed
Expand file tree Collapse file tree 3 files changed +42
-3
lines changed Original file line number Diff line number Diff line change 77
88class 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.');
Original file line number Diff line number Diff line change 22
33namespace BinaryCats\MailgunWebhooks;
44
5+ use BinaryCats\MailgunWebhooks\Exceptions\WebhookFailed;
6+
57class 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 }
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments