Skip to content

Commit

Permalink
Replace manual headers check with HeaderRequestMatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
welcoMattic committed Apr 17, 2024
1 parent d3e59d0 commit e951137
Showing 1 changed file with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\HttpFoundation\ChainRequestMatcher;
use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestMatcher\HeaderRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\IsJsonRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcher\MethodRequestMatcher;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
Expand All @@ -36,6 +37,11 @@ protected function getRequestMatcher(): RequestMatcherInterface
return new ChainRequestMatcher([
new MethodRequestMatcher('POST'),
new IsJsonRequestMatcher(),
new HeaderRequestMatcher([
'svix-id',
'svix-timestamp',
'svix-signature',
]),
]);
}

Expand Down Expand Up @@ -72,14 +78,9 @@ protected function doParse(Request $request, #[\SensitiveParameter] string $secr
private function validateSignature(string $payload, HeaderBag $headers, string $secret): void
{
$secret = $this->decodeSecret($secret);

if ($headers->has('svix-id') && $headers->has('svix-timestamp') && $headers->has('svix-signature')) {
$messageId = $headers->get('svix-id');
$messageTimestamp = (int) $headers->get('svix-timestamp');
$messageSignature = $headers->get('svix-signature');
} else {
throw new RejectWebhookException(406, 'Missing required headers.');
}
$messageId = $headers->get('svix-id');
$messageTimestamp = (int) $headers->get('svix-timestamp');
$messageSignature = $headers->get('svix-signature');

$signature = $this->sign($secret, $messageId, $messageTimestamp, $payload);
$expectedSignature = explode(',', $signature, 2)[1];
Expand Down

0 comments on commit e951137

Please sign in to comment.