-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SS-2018-007] Add CSRF protection (#2)
- Loading branch information
1 parent
0fc7854
commit b59ba39
Showing
13 changed files
with
468 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace SilverStripe\GraphQL\Middleware; | ||
|
||
use GraphQL\Schema; | ||
use SilverStripe\GraphQL\Middleware\QueryMiddleware; | ||
use SilverStripe\Security\SecurityToken; | ||
use Exception; | ||
|
||
class CSRFMiddleware implements QueryMiddleware | ||
{ | ||
public function process(Schema $schema, $query, $context, $params, callable $next) | ||
{ | ||
if (preg_match('/^\s*mutation/', $query)) { | ||
if (empty($context['token'])) { | ||
throw new Exception('Mutations must provide a CSRF token in the X-CSRF-TOKEN header'); | ||
} | ||
$token = $context['token']; | ||
|
||
if (!SecurityToken::inst()->check($token)) { | ||
throw new Exception('Invalid CSRF token'); | ||
} | ||
} | ||
|
||
return $next($schema, $query, $context, $params); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace SilverStripe\GraphQL\Middleware; | ||
|
||
use GraphQL\Schema; | ||
use SilverStripe\GraphQL\Middleware\QueryMiddleware; | ||
use Exception; | ||
|
||
class HTTPMethodMiddleware implements QueryMiddleware | ||
{ | ||
public function process(Schema $schema, $query, $context, $params, callable $next) | ||
{ | ||
$isGET = false; | ||
$isPOST = false; | ||
if (isset($context['httpMethod'])) { | ||
$isGET = $context['httpMethod'] === 'GET'; | ||
$isPOST = $context['httpMethod'] === 'POST'; | ||
} | ||
|
||
if (!$isGET && !$isPOST) { | ||
throw new Exception('Request method must be POST or GET'); | ||
} | ||
|
||
if (preg_match('/^\s*mutation/', $query)) { | ||
if (!$isPOST) { | ||
throw new Exception('Mutations must use the POST request method'); | ||
} | ||
} | ||
|
||
return $next($schema, $query, $context, $params); | ||
} | ||
} |
Oops, something went wrong.