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 7
7
8
8
class WebhookFailed extends Exception
9
9
{
10
+ public static function invalidSignature (): self
11
+ {
12
+ return new static ('The signature is invalid. ' );
13
+ }
14
+
10
15
public static function signingSecretNotSet (): self
11
16
{
12
17
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 2
2
3
3
namespace BinaryCats \MailgunWebhooks ;
4
4
5
+ use BinaryCats \MailgunWebhooks \Exceptions \WebhookFailed ;
6
+
5
7
class Webhook
6
8
{
7
9
/**
8
10
* Validate and raise an appropriate event.
9
11
*
10
12
* @param $payload
11
- * @param array $signature
12
- * @param string $secret
13
+ * @param array $signature
14
+ * @param string $secret
13
15
* @return BinaryCats\MailgunWebhooks\Event
16
+ * @throws WebhookFailed
14
17
*/
15
18
public static function constructEvent (array $ payload , array $ signature , string $ secret ): Event
16
19
{
17
20
// 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
+
19
25
// Make an event
20
26
return Event::constructFrom ($ payload );
21
27
}
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(
165
165
->postJson ('mailgun-webhooks/somekey ' , $ payload )
166
166
->assertSuccessful ();
167
167
}
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
+ }
168
196
}
You can’t perform that action at this time.
0 commit comments