Skip to content

Commit 3aa4a59

Browse files
authored
Merge pull request #2272 from nowackipawel/patch-73
Handle X-CSRF-TOKEN - CSRF
2 parents e232959 + 6df127d commit 3aa4a59

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

app/Config/App.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,14 @@ class App extends BaseConfig
237237
| recommended CSRF protection be enabled.
238238
|
239239
| CSRFTokenName = The token name
240+
| CSRFHeaderName = The header name
240241
| CSRFCookieName = The cookie name
241242
| CSRFExpire = The number in seconds the token should expire.
242243
| CSRFRegenerate = Regenerate token on every submission
243244
| CSRFRedirect = Redirect to previous page with error on failure
244245
*/
245246
public $CSRFTokenName = 'csrf_test_name';
247+
public $CSRFHeaderName = 'X-CSRF-TOKEN';
246248
public $CSRFCookieName = 'csrf_cookie_name';
247249
public $CSRFExpire = 7200;
248250
public $CSRFRegenerate = true;

system/Security/Security.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,15 @@ class Security
7575
*/
7676
protected $CSRFTokenName = 'CSRFToken';
7777

78+
/**
79+
* CSRF Header name
80+
*
81+
* Token name for Cross Site Request Forgery protection cookie.
82+
*
83+
* @var string
84+
*/
85+
protected $CSRFHeaderName = 'CSRFToken';
86+
7887
/**
7988
* CSRF Cookie name
8089
*
@@ -171,6 +180,7 @@ public function __construct($config)
171180
// Store our CSRF-related settings
172181
$this->CSRFExpire = $config->CSRFExpire;
173182
$this->CSRFTokenName = $config->CSRFTokenName;
183+
$this->CSRFHeaderName = $config->CSRFHeaderName;
174184
$this->CSRFCookieName = $config->CSRFCookieName;
175185
$this->CSRFRegenerate = $config->CSRFRegenerate;
176186

@@ -206,12 +216,14 @@ public function CSRFVerify(RequestInterface $request)
206216
{
207217
return $this->CSRFSetCookie($request);
208218
}
209-
210-
// Do the token exist in _POST or php://input (json) data?
211-
$CSRFTokenValue = $_POST[$this->CSRFTokenName] ??
212-
(!empty($input = file_get_contents('php://input')) && !empty($json = json_decode($input)) && json_last_error() === JSON_ERROR_NONE ?
213-
($json->{$this->CSRFTokenName} ?? null) :
214-
null);
219+
220+
// Do the tokens exist in _POST, HEADER or optionally php:://input - json data
221+
$CSRFTokenValue = $_POST[$this->CSRFTokenName] ??
222+
(!is_null($request->getHeader($this->CSRFHeaderName)) && !empty($request->getHeader($this->CSRFHeaderName)->getValue()) ?
223+
$request->getHeader($this->CSRFHeaderName)->getValue() :
224+
(!empty($request->getBody()) && !empty($json = json_decode($request->getBody())) && json_last_error() === JSON_ERROR_NONE ?
225+
($json->{$this->CSRFTokenName} ?? null) :
226+
null));
215227

216228
// Do the tokens exist in both the _POST/POSTed JSON and _COOKIE arrays?
217229
if (! isset($CSRFTokenValue, $_COOKIE[$this->CSRFCookieName]) || $CSRFTokenValue !== $_COOKIE[$this->CSRFCookieName]

tests/_support/Config/MockAppConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class MockAppConfig
1616

1717
public $CSRFProtection = false;
1818
public $CSRFTokenName = 'csrf_test_name';
19+
public $CSRFHeaderName = 'X-CSRF-TOKEN';
1920
public $CSRFCookieName = 'csrf_cookie_name';
2021
public $CSRFExpire = 7200;
2122
public $CSRFRegenerate = true;

0 commit comments

Comments
 (0)