Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Refactor handling of invalid webhook signatures #791

Merged
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
17 changes: 5 additions & 12 deletions src/Http/Middleware/VerifyWebhookSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@

use Closure;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Contracts\Foundation\Application;
use Stripe\Exception\SignatureVerificationException;
use Stripe\WebhookSignature;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

class VerifyWebhookSignature
{
/**
* The application instance.
*
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $app;

/**
* The configuration repository instance.
*
Expand All @@ -27,13 +20,11 @@ class VerifyWebhookSignature
/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Contracts\Config\Repository $config
* @return void
*/
public function __construct(Application $app, Config $config)
public function __construct(Config $config)
{
$this->app = $app;
$this->config = $config;
}

Expand All @@ -43,6 +34,8 @@ public function __construct(Application $app, Config $config)
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return \Illuminate\Http\Response
*
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
*/
public function handle($request, Closure $next)
{
Expand All @@ -54,7 +47,7 @@ public function handle($request, Closure $next)
$this->config->get('cashier.webhook.tolerance')
);
} catch (SignatureVerificationException $exception) {
$this->app->abort(403);
throw new AccessDeniedHttpException($exception->getMessage(), $exception);
}

return $next($request);
Expand Down
34 changes: 10 additions & 24 deletions tests/Unit/VerifyWebhookSignatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
namespace Laravel\Cashier\Tests\Unit;

use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Laravel\Cashier\Http\Middleware\VerifyWebhookSignature;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

class VerifyWebhookSignatureTest extends TestCase
{
Expand All @@ -23,7 +22,7 @@ public function test_response_is_received_when_secret_matches()
$mock = VerifierMock::withWebhookSecret('secret')
->setSignedSignature('secret');

$response = (new VerifyWebhookSignature($mock->app, $mock->config))
$response = (new VerifyWebhookSignature($mock->config))
->handle($mock->request, function ($request) {
return new Response('OK');
});
Expand All @@ -34,37 +33,32 @@ public function test_response_is_received_when_secret_matches()
public function test_app_aborts_when_secret_does_not_match()
{
$mock = VerifierMock::withWebhookSecret('secret')
->setSignature('fail')
->expectAbort();
->setSignature('fail');

$this->expectException(HttpException::class);
$this->expectException(AccessDeniedHttpException::class);
$this->expectExceptionMessage('No signatures found matching the expected signature for payload');

(new VerifyWebhookSignature($mock->app, $mock->config))
(new VerifyWebhookSignature($mock->config))
->handle($mock->request, function ($request) {
});
}

public function test_app_aborts_when_no_secret_was_provided()
{
$mock = VerifierMock::withWebhookSecret('secret')
->setSignedSignature('')
->expectAbort();
->setSignedSignature('');

$this->expectException(HttpException::class);
$this->expectException(AccessDeniedHttpException::class);
$this->expectExceptionMessage('No signatures found matching the expected signature for payload');

(new VerifyWebhookSignature($mock->app, $mock->config))
(new VerifyWebhookSignature($mock->config))
->handle($mock->request, function ($request) {
});
}
}

class VerifierMock
{
/**
* @var \Illuminate\Contracts\Foundation\Application
*/
public $app;

/**
* @var \Illuminate\Contracts\Config\Repository
*/
Expand All @@ -77,7 +71,6 @@ class VerifierMock

public function __construct($webhookSecret)
{
$this->app = m::mock(Application::class);
$this->config = m::mock(Config::class);
$this->config->shouldReceive('get')->with('cashier.webhook.secret')->andReturn($webhookSecret);
$this->config->shouldReceive('get')->with('cashier.webhook.tolerance')->andReturn(300);
Expand All @@ -89,13 +82,6 @@ public static function withWebhookSecret($webhookSecret)
return new self($webhookSecret);
}

public function expectAbort()
{
$this->app->shouldReceive('abort')->andThrow(HttpException::class, 403);

return $this;
}

public function setSignedSignature($secret)
{
$signature = $this->sign($this->request->getContent(), $secret);
Expand Down