This repository was archived by the owner on Nov 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathCsrfGuardNeue.php
More file actions
353 lines (302 loc) · 11.1 KB
/
CsrfGuardNeue.php
File metadata and controls
353 lines (302 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
/**
* Created by PhpStorm.
* User: www.sib.li
* Date: 13.08.14
* Time: 13:29
*/
namespace Slim\Middleware;
/**
* Class CsrfGuardNeue
*
* Based on CsrfGuard middleware from Slim-Extras
* https://github.com/codeguy/Slim-Extras/blob/develop/Middleware/CsrfGuard.php
*
*
* Provides CSRF(XSRF) protection for your forms and ajax requests.
* https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet
*
*
* Implements cookie set + http header check for ajax requests
* and csrf_token (hidden input) for forms.
* ( if your are familiar with Django, check here:
* https://docs.djangoproject.com/en/dev/ref/contrib/csrf/ )
*
* Compatible with AngularJS $http provider:
* https://docs.angularjs.org/api/ng/service/$http#cross-site-request-forgery-xsrf-protection
*
*
* Usage:
* $app = new \Slim\Slim();
* $app->add( new \Slim\Middleware\CsrfGuardNeue() );
*
* You can configure hidden input name like this:
* $csrfGuard = new \Slim\Middleware\CsrfGuardNeue('myCustomName');
* $app->add( $csrfGuard );
*
* Or configure other parameters passing array (here provided their default values)
* $csrfGuard = new \Slim\Middleware\CsrfGuardNeue( array(
* 'field' => 'csrfmiddlewaretoken', // Input name
* 'cookie' => 'XSRF-TOKEN', // Cookie name
* 'header' => 'X-Xsrf-Token', // Header name
* // Action on CSRF validation failure
* // Should be callable function. $app will be passed inside when called.
* 'action' => function(\Slim\Slim $app) {
* $app->halt(400, '"Invalid or missing CSRF token"');
* }
* ) );
* $app->add( $csrfGuard );
*
*
* In your view template add this input inside every of web forms you have created:
* <input type="hidden" name="<?php echo $csrf_key; ?>" value="<?php echo $csrf_token; ?>">
*
* Or, with Twig, you can use provided extension:
* $app->view->parserExtensions = array(
* new \Slim\Views\TwigExtension(),
* new \Slim\Middleware\CsrfGuardNeue\TwigCsrfHelpers()
* );
* And insert inside forms:
* <form ...> {{ CSRF() }} ...</form>
* This will be translated to:
* <form ...> <input type="hidden" name="{{ csrf_key }}" value="{{ csrf_token }}" /> ...</form>
*
*
*
* For jQuery users:
* If you get data for all of your ajax POST requests from $("form").serialize() or $.serializeArray(),
* you need nothing else to configure, just make sure you put hidden input in every form (see above).
* See also: http://api.jquery.com/serializeArray/
*
* For any other ajax POST, PUT or DELETE requests you will need some additional setup:
* (this needed to copy CSRF token value from cookie to request header for every unsafe request):
* <script>
* function getCookie(cname) {
* var name = cname + '=';
* var ca = document.cookie.split(';');
* for (var i = 0; i < ca.length; i++) {
* var c = $.trim(ca[i]);
* if (c.indexOf(name) != -1) return c.substring(name.length,c.length);
* }
* return null;
* }
* $.ajaxSetup({
* beforeSend: function(xhr, settings) {
* if (this.crossDomain) return;
* // These HTTP methods do not require CSRF protection, BUT!
* // Only if you are not changing any data using these methods in ajax!
* if ( -1 < (['GET','HEAD','OPTIONS','TRACE']).indexOf(settings.type) ) return;
* var csrftoken = $.cookie ? $.cookie('XSRF-TOKEN') : getCookie('XSRF-TOKEN');
* xhr.setRequestHeader("X-XSRF-TOKEN", csrftoken);
* }
* });
* </script>
*
* If you are using Twig template engine, add provided extension (see above), and place somewhere
* before closing </body> this line:
* {{ jQcsrf() }}
* That will be translated to the jQuery code from above.
*
*
* Limitations
* Any sub-domains of your site will be able to get and set cookies (client-side) for the whole domain.
* In this case Cookie+Header protection is useless.
* If you have untrusted sub-domains, disable cookie assignment by setting cookie option to false:
* $csrfGuard = new \Slim\Middleware\CsrfGuardNeue( array('cookie' => false) );
* $app->add( $csrfGuard );
* You will need to reconfigure ajax calls accordingly, for example, by copying hidden input value
* to header.
* If you wish to disable header check, use header option:
* $csrfGuard = new \Slim\Middleware\CsrfGuardNeue( array('cookie' => false, 'header' => false) );
* In this case only POST value will be checked (for XHR calls too!)
*
*
* See also: https://www.owasp.org/index.php/Session_fixation
*
* Also do not forget to check and properly set (when needed) response headers related
* to Same Origin Policy, like Access-Control-Allow-Origin, Access-Control-Allow-Methods and X-Frame-Options:
* https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
* https://developer.mozilla.org/en-US/docs/Web/HTTP/X-Frame-Options
*
*
* @package Slim\Middleware
* @author Stepan Legachev, www.sib.li
*/
class CsrfGuardNeue extends \Slim\Middleware
{
/**
* Form field name.
*
* @var string
*/
protected $fieldName = 'csrfmiddlewaretoken';
/**
* Cookie name.
*
* @var string
*/
protected $cookieName = null;
/**
* Header name.
* To this header JS should copy cookie value.
*
* @var string
*/
protected $headerName = null;
/**
* Action to perform on token check failure.
*
* @var \Closure|callable
*/
public $action = null;
/**
* @var array
*/
protected $settings;
/**
* @var array
*/
public $unsafeMethods = array(
\Slim\Http\Request::METHOD_POST,
\Slim\Http\Request::METHOD_PUT,
\Slim\Http\Request::METHOD_PATCH,
\Slim\Http\Request::METHOD_DELETE
);
/**
*
* @param array $settings
* @throws \OutOfBoundsException
*/
public function __construct($settings = array())
{
$defaults = array(
'field' => 'csrfmiddlewaretoken',
'cookie' => 'XSRF-TOKEN',
'header' => 'X-Xsrf-Token',
'action' => array($this, 'defaultAction')
);
// Backwards compatibility.
if ( is_string($settings) ) {
$settings = array('field' => $settings);
}
if ( is_array($settings) ) {
$this->settings = array_merge($defaults, $settings);
} else {
$this->settings = $defaults;
}
$fieldName = is_string($this->settings['field']) ? trim($this->settings['field']) : false;
$cookieName = is_string($this->settings['cookie']) ? trim($this->settings['cookie']) : false;
$headerName = is_string($this->settings['header']) ? trim($this->settings['header']) : false;
if ( empty($fieldName) || preg_match('/[^a-zA-Z0-9\-\_]/', $fieldName) ) {
throw new \OutOfBoundsException('Invalid CSRF token field name "' . $fieldName . '"');
}
if ( empty($cookieName) ) {
$cookieName = false;
} else if ( !is_string($cookieName) || preg_match('/[^a-zA-Z0-9\-\_]/', $cookieName) ) {
throw new \OutOfBoundsException('Invalid CSRF token cookie name "' . $cookieName . '"');
}
if ( empty($headerName) ) {
$headerName = false;
} else if ( !is_string($headerName) || preg_match('/[^a-zA-Z0-9\-\_]/', $headerName) ) {
throw new \OutOfBoundsException('Invalid CSRF token header name "' . $headerName . '"');
}
if ( !is_callable($this->settings['action']) ) {
$this->action = array($this, 'defaultAction');
} else {
$this->action = $this->settings['action'];
if ( method_exists($this->action, 'bindTo') ) {
$this->action = $this->action->bindTo($this, $this);
}
}
$this->fieldName = $fieldName;
$this->cookieName = $cookieName;
$this->headerName = $headerName;
} // constructor
/**
* Call middleware.
*
* @return void
*/
public function call()
{
// Attach as hook.
$this->app->hook('slim.before', array($this, 'check'));
// Call next middleware.
$this->next->call();
} // call
/**
* Default action on token verification fail.
*
* @param $app \Slim\Slim
*/
protected function defaultAction(\Slim\Slim $app)
{
if ( $this->isAcceptJSON() || $this->isXHR() ) {
$app->contentType("application/json");
// Double quoted for JSON-safe response.
$app->halt(400, '"Invalid or missing CSRF token"');
}
$app->halt(400, 'Invalid or missing CSRF token');
} // defaultAction
/**
* Check CSRF token is valid.
* Sets cookie and session variable.
*
*/
public function check()
{
// Check sessions are enabled.
if (session_id() === '') {
throw new \Exception('Sessions are required to use the CSRF Guard middleware.');
}
// @todo Also check referrer and origin
if ( empty($_SESSION[$this->fieldName]) ) {
// Slightly modified implementation from https://www.owasp.org/index.php/PHP_CSRF_Guard
if ( function_exists("hash_algos") && in_array("sha256", hash_algos()) ) {
$token = hash( "sha256", mt_rand() );
} else {
$token = '';
for ($i = 0; $i < 64; ++$i) {
$token .= dechex( mt_rand(0, 15) );
}
}
$_SESSION[$this->fieldName] = $token;
} else {
$token = $_SESSION[$this->fieldName];
}
// Validate the CSRF token.
if ( in_array($this->app->request()->getMethod(), $this->unsafeMethods) ) {
$userToken = $this->app->request()->post($this->fieldName, false);
if ( empty($userToken) && $this->headerName !== false ) {
$userToken = $this->app->request->headers->get( $this->headerName );
}
if ($token !== $userToken) {
call_user_func( $this->action, $this->app );
}
} else {
if ($this->cookieName !== false) {
// if ( is_null( $app->getCookie($this->cookieName) ) )
$this->app->setCookie($this->cookieName, $token, '2 days');
}
}
// Assign CSRF token key names and value to view.
$this->app->view()->set('csrf_key', $this->fieldName);
$this->app->view()->set('csrf_token', $token);
if ($this->cookieName !== false) {
$this->app->view()->set('csrf_cookie', $this->cookieName);
}
if ( $this->headerName !== false ) {
$this->app->view()->set('csrf_header', $this->headerName);
}
} // check
public function isAcceptJSON()
{
$accept = $this->app->request->headers->get('Accept');
return ( stripos($accept, 'application/json') !== false );
} // isAcceptJSON
public function isXHR()
{
$reqWith = $this->app->request->headers->get('X-Requested-With');
return strtolower($reqWith) == strtolower('XMLHttpRequest');
} // isXHR
} // CsrfGuardNeue class