Skip to content

Commit 24429b2

Browse files
committed
Merge pull request #35 from ziadoz/patch-1
Reverted CsrfGuard behaviour
2 parents a1d3ab0 + a32d268 commit 24429b2

File tree

1 file changed

+54
-67
lines changed

1 file changed

+54
-67
lines changed

Middleware/CsrfGuard.php

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -4,84 +4,71 @@
44
class CsrfGuard extends \Slim\Middleware
55
{
66
/**
7-
* CSRF token key name.
8-
*
9-
* @var string
10-
*/
11-
protected $key;
7+
* CSRF token key name.
8+
*
9+
* @var string
10+
*/
11+
protected $key;
1212

1313
/**
14-
* CSRF graceful.
15-
*
16-
* @var string
17-
*/
18-
protected $graceful;
14+
* Constructor.
15+
*
16+
* @param string $key The CSRF token key name.
17+
* @return void
18+
*/
19+
public function __construct($key = 'csrf_token')
20+
{
21+
if (! is_string($key) || empty($key) || preg_match('/[^a-zA-Z0-9\-\_]/', $key)) {
22+
throw new \OutOfBoundsException('Invalid CSRF token key "' . $key . '"');
23+
}
1924

20-
/**
21-
* Constructor.
22-
*
23-
* @param boolean $graceful If true then destroy the session (graceful), otherwise halt the application (ungraceful).
24-
* @param string $key The CSRF token key name.
25-
* @return void
26-
*/
27-
public function __construct($graceful = false, $key = 'csrf_token')
28-
{
29-
if (! is_string($key) || empty($key) || preg_match('/[^a-zA-Z0-9\-\_]/', $key)) {
30-
throw new \OutOfBoundsException('Invalid CSRF token key "' . $key . '"');
31-
}
25+
$this->key = $key;
26+
}
3227

33-
$this->key = $key;
34-
$this->graceful = (bool) $graceful;
35-
}
36-
37-
/**
38-
* Call middleware.
39-
*
40-
* @return void
41-
*/
42-
public function call()
43-
{
44-
// Attach as hook.
45-
$this->app->hook('slim.before', array($this, 'check'));
28+
/**
29+
* Call middleware.
30+
*
31+
* @return void
32+
*/
33+
public function call()
34+
{
35+
// Attach as hook.
36+
$this->app->hook('slim.before', array($this, 'check'));
4637

47-
// Call next middleware.
48-
$this->next->call();
49-
}
38+
// Call next middleware.
39+
$this->next->call();
40+
}
5041

51-
/**
52-
* Check CSRF token is valid.
53-
* Note: Also checks POST data to see if a Moneris RVAR CSRF token exists.
54-
*
55-
* @return void
56-
*/
57-
public function check() {
58-
// Check sessions are enabled.
59-
if (session_id() === '') {
60-
throw new \Exception('Sessions are required to use the CSRF Guard middleware.');
61-
}
42+
/**
43+
* Check CSRF token is valid.
44+
* Note: Also checks POST data to see if a Moneris RVAR CSRF token exists.
45+
*
46+
* @return void
47+
*/
48+
public function check() {
49+
// Check sessions are enabled.
50+
if (session_id() === '') {
51+
throw new \Exception('Sessions are required to use the CSRF Guard middleware.');
52+
}
6253

63-
if (! isset($_SESSION[$this->key])) {
64-
$_SESSION[$this->key] = sha1(serialize($_SERVER) . rand(0, 0xffffffff));
65-
}
54+
if (! isset($_SESSION[$this->key])) {
55+
$_SESSION[$this->key] = sha1(serialize($_SERVER) . rand(0, 0xffffffff));
56+
}
6657

67-
$token = $_SESSION[$this->key];
58+
$token = $_SESSION[$this->key];
6859

69-
// Validate the CSRF token.
70-
if (in_array($this->app->request()->getMethod(), array('POST', 'PUT', 'DELETE'))) {
60+
// Validate the CSRF token.
61+
if (in_array($this->app->request()->getMethod(), array('POST', 'PUT', 'DELETE'))) {
7162
$userToken = $this->app->request()->post($this->key);
7263
if ($token !== $userToken) {
73-
if (! $this->graceful) {
74-
$this->app->halt(400, 'Invalid or missing CSRF token.');
75-
} else {
76-
session_destroy();
77-
}
64+
$this->app->halt(400, 'Invalid or missing CSRF token.');
7865
}
79-
}
66+
}
8067

81-
// Assign CSRF token key and value to view.
82-
$this->app->view()->appendData(array(
83-
'csrf_key' => $this->key,
84-
'csrf_token' => $token,
85-
));
86-
}
68+
// Assign CSRF token key and value to view.
69+
$this->app->view()->appendData(array(
70+
'csrf_key' => $this->key,
71+
'csrf_token' => $token,
72+
));
73+
}
8774
}

0 commit comments

Comments
 (0)