Skip to content

Commit 3601411

Browse files
authored
Merge pull request #10 from binary-cats/patch-1
Proper responses for incorrect values
2 parents 0d04f22 + 5da3088 commit 3601411

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ matrix:
2525
env: LARAVEL='6.*' TESTBENCH='4.*' COMPOSER_FLAGS='--prefer-stable'
2626
- php: 7.3
2727
env: LARAVEL='7.*' TESTBENCH='5.*' COMPOSER_FLAGS='--prefer-stable'
28+
- php: 7.4
29+
env: LARAVEL='7.*' TESTBENCH='5.*' COMPOSER_FLAGS='--prefer-stable'
30+
- php: 7.4
31+
env: LARAVEL='7.*' TESTBENCH='6.*' COMPOSER_FLAGS='--prefer-stable'
32+
- php: 7.4
33+
env: LARAVEL='8.*' TESTBENCH='6.*' COMPOSER_FLAGS='--prefer-stable'
34+
- php: 8.0
35+
env: LARAVEL='8.*' TESTBENCH='6.*' COMPOSER_FLAGS='--prefer-stable'
2836

2937
before_install:
3038
- travis_retry composer self-update

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
}
1919
],
2020
"require": {
21-
"php": "^7.2",
21+
"php": "^7.2|^8.0",
2222
"illuminate/support": "~5.8.0|^6.0|^7.0|^8.0",
2323
"spatie/laravel-webhook-client": "^2.0"
2424
},

src/MailgunSignatureValidator.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class MailgunSignatureValidator implements SignatureValidator
3333
*/
3434
public function isValid(Request $request, WebhookConfig $config): bool
3535
{
36-
$signature = $request->get('signature', []);
36+
$signature = $this->signature($request);
37+
3738
$secret = $config->signingSecret;
3839

3940
try {
@@ -46,4 +47,21 @@ public function isValid(Request $request, WebhookConfig $config): bool
4647

4748
return true;
4849
}
50+
51+
/**
52+
* Validate the incoming signature' schema.
53+
*
54+
* @param \Illuminate\Http\Request $request
55+
* @return array
56+
*/
57+
protected function signature(Request $request): array
58+
{
59+
$validated = $request->validate([
60+
'signature.signature' => 'bail|required',
61+
'signature.timestamp' => 'required',
62+
'signature.token' => 'required',
63+
]);
64+
65+
return $validated['signature'];
66+
}
4967
}

tests/IntegrationTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,43 @@ public function it_can_handle_a_valid_request()
5656
$this->assertEquals($webhookCall->id, cache('dummyjob')->id);
5757
}
5858

59+
public function in_will_ignore_empty_reququest()
60+
{
61+
$payload = [];
62+
63+
Arr::set($payload, 'signature', $this->determineMailgunSignature($payload));
64+
65+
$this
66+
->postJson('mailgun-webhooks', $payload)
67+
->assertStatus(422);
68+
69+
$this->assertCount(0, WebhookCall::get());
70+
71+
Event::assertNotDispatched('mailgun-webhooks::my.type');
72+
73+
$this->assertNull(cache('dummyjob'));
74+
}
75+
76+
public function in_will_ignore_unsinged_reququest()
77+
{
78+
$payload = [
79+
'event-data' => [
80+
'event' => 'my.type',
81+
'key' => 'value',
82+
],
83+
];
84+
85+
$this
86+
->postJson('mailgun-webhooks', $payload)
87+
->assertStatus(422);
88+
89+
$this->assertCount(0, WebhookCall::get());
90+
91+
Event::assertNotDispatched('mailgun-webhooks::my.type');
92+
93+
$this->assertNull(cache('dummyjob'));
94+
}
95+
5996
/** @test */
6097
public function a_request_with_an_invalid_signature_wont_be_logged()
6198
{
@@ -70,7 +107,7 @@ public function a_request_with_an_invalid_signature_wont_be_logged()
70107

71108
$this
72109
->postJson('mailgun-webhooks', $payload)
73-
->assertStatus(500);
110+
->assertStatus(422);
74111

75112
$this->assertCount(0, WebhookCall::get());
76113

0 commit comments

Comments
 (0)