Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/Service/GitHubRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function handle(Request $request): array
throw new AccessDeniedHttpException(sprintf('No webhook secret configured for repository "%s".', $repository->getFullName()));
}

$signature = $request->headers->get('X-Hub-Signature');
$signature = $request->headers->get('X-Hub-Signature-256');
if (null === $signature || '' === $signature) {
throw new AccessDeniedHttpException('The request is not secured.');
}
Expand Down Expand Up @@ -92,10 +92,6 @@ public function handle(Request $request): array

private function authenticate(string $hash, string $key, string $data): bool
{
if (!extension_loaded('hash')) {
throw new \RuntimeException('"hash" extension is needed to check request signature.');
}

return hash_equals($hash, 'sha1='.hash_hmac('sha1', $data, $key));
return hash_equals($hash, 'sha256='.hash_hmac('sha256', $data, $key));
}
}
6 changes: 3 additions & 3 deletions tests/Controller/WebhookControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function testUnsignedRequestIsRejected()
public function testRequestWithInvalidSignatureIsRejected()
{
$body = file_get_contents(__DIR__.'/../webhook_examples/issue_comment.created.json');
$this->postWebhook('issue_comment', $body, 'sha1='.hash_hmac('sha1', $body, 'not-the-configured-secret'));
$this->postWebhook('issue_comment', $body, 'sha256='.hash_hmac('sha256', $body, 'not-the-configured-secret'));

$this->assertResponseStatusCodeSame(403);
}
Expand All @@ -74,7 +74,7 @@ private function postWebhook(string $eventHeader, string $body, ?string $signatu
{
$server = ['HTTP_X-Github-Event' => $eventHeader];
if (null !== $signature) {
$server['HTTP_X-Hub-Signature'] = $signature;
$server['HTTP_X-Hub-Signature-256'] = $signature;
}

$this->client->request('POST', '/webhooks/github', [], [], $server, $body);
Expand All @@ -86,7 +86,7 @@ private function sign(string $body): string
->getRepository('carsonbot-playground/symfony')
->getSecret();

return 'sha1='.hash_hmac('sha1', $body, $secret);
return 'sha256='.hash_hmac('sha256', $body, $secret);
}

/**
Expand Down
18 changes: 16 additions & 2 deletions tests/Service/GitHubRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,25 @@ public function testSignedRequestIsAccepted()
$handler = $this->createHandler(['carsonbot-playground/symfony' => ['secret' => 'a_secret']]);
$body = $this->createBody();

$responseData = $handler->handle($this->createRequest($body, 'sha1='.hash_hmac('sha1', $body, 'a_secret')));
$responseData = $handler->handle($this->createRequest($body, 'sha256='.hash_hmac('sha256', $body, 'a_secret')));

$this->assertSame(['unsupported_action' => 'issues'], $responseData);
}

public function testLegacySha1SignatureIsRejected()
{
$handler = $this->createHandler(['carsonbot-playground/symfony' => ['secret' => 'a_secret']]);
$body = $this->createBody();

$request = $this->createRequest($body);
$request->headers->set('X-Hub-Signature', 'sha1='.hash_hmac('sha1', $body, 'a_secret'));

$this->expectException(AccessDeniedHttpException::class);
$this->expectExceptionMessage('The request is not secured.');

$handler->handle($request);
}

/**
* @param array<string, array{secret?: string}> $repositories
*/
Expand All @@ -53,7 +67,7 @@ private function createRequest(string $body, ?string $signature = null): Request
{
$server = ['HTTP_X-Github-Event' => 'issues'];
if (null !== $signature) {
$server['HTTP_X-Hub-Signature'] = $signature;
$server['HTTP_X-Hub-Signature-256'] = $signature;
}

return Request::create('/webhooks/github', 'POST', [], [], [], $server, $body);
Expand Down
Loading