15
15
16
16
// CONFIGURATION:
17
17
18
- /**
19
- * By default, when you include this file csrf-magic will automatically check
20
- * and exit if the CSRF token is invalid. This will defer executing
21
- * csrf_check() until you're ready. You can also pass false as a parameter to
22
- * that function, in which case the function will not exit but instead return
23
- * a boolean false if the CSRF check failed. This allows for tighter integration
24
- * with your system.
25
- */
26
- $ GLOBALS ['csrf ' ]['defer ' ] = false ;
27
-
28
18
/**
29
19
* This is the amount of seconds you wish to allow before any token becomes
30
20
* invalid; the default is two hours, which should be more than enough for
117
107
*/
118
108
$ GLOBALS ['csrf ' ]['frame-breaker ' ] = true ;
119
109
120
- /**
121
- * Whether or not CSRF Magic should be allowed to start a new session in order
122
- * to determine the key.
123
- */
124
- $ GLOBALS ['csrf ' ]['auto-session ' ] = true ;
125
-
126
110
/**
127
111
* Whether or not csrf-magic should produce XHTML style tags.
128
112
*/
@@ -187,7 +171,6 @@ function csrf_check($fatal = true)
187
171
if ($ _SERVER ['REQUEST_METHOD ' ] !== 'POST ' ) {
188
172
return true ;
189
173
}
190
- csrf_start ();
191
174
$ name = $ GLOBALS ['csrf ' ]['input-name ' ];
192
175
$ ok = false ;
193
176
$ tokens = '' ;
@@ -232,7 +215,6 @@ function csrf_get_tokens()
232
215
} else {
233
216
$ ip = '' ;
234
217
}
235
- csrf_start ();
236
218
237
219
// These are "strong" algorithms that don't require per se a secret
238
220
if (session_id ()) {
@@ -259,46 +241,18 @@ function csrf_get_tokens()
259
241
return 'invalid ' ;
260
242
}
261
243
262
- function csrf_flattenpost ($ data )
263
- {
264
- $ ret = array ();
265
- foreach ($ data as $ n => $ v ) {
266
- $ ret = array_merge ($ ret , csrf_flattenpost2 (1 , $ n , $ v ));
267
- }
268
- return $ ret ;
269
- }
270
- function csrf_flattenpost2 ($ level , $ key , $ data )
271
- {
272
- if (!is_array ($ data )) {
273
- return array ($ key => $ data );
274
- }
275
- $ ret = array ();
276
- foreach ($ data as $ n => $ v ) {
277
- $ nk = $ level >= 1 ? $ key ."[ $ n] " : "[ $ n] " ;
278
- $ ret = array_merge ($ ret , csrf_flattenpost2 ($ level +1 , $ nk , $ v ));
279
- }
280
- return $ ret ;
281
- }
282
-
283
244
/**
284
245
* @param $tokens is safe for HTML consumption
285
246
*/
286
247
function csrf_callback ($ tokens )
287
248
{
288
249
// (yes, $tokens is safe to echo without escaping)
289
250
header ($ _SERVER ['SERVER_PROTOCOL ' ] . ' 403 Forbidden ' );
290
- $ data = '' ;
291
- foreach (csrf_flattenpost ($ _POST ) as $ key => $ value ) {
292
- if ($ key == $ GLOBALS ['csrf ' ]['input-name ' ]) {
293
- continue ;
294
- }
295
- $ data .= '<input type="hidden" name=" ' .htmlspecialchars ($ key ).'" value=" ' .htmlspecialchars ($ value ).'" /> ' ;
296
- }
251
+
297
252
echo "<html><head><title>CSRF check failed</title></head>
298
253
<body>
299
254
<p>CSRF check failed. Your form session may have expired, or you may not have
300
255
cookies enabled.</p>
301
- <form method='post' action=''> $ data<input type='submit' value='Try again' /></form>
302
256
<p>Debug: $ tokens</p></body></html>
303
257
" ;
304
258
}
@@ -398,16 +352,6 @@ function csrf_conf($key, $val)
398
352
$ GLOBALS ['csrf ' ][$ key ] = $ val ;
399
353
}
400
354
401
- /**
402
- * Starts a session if we're allowed to.
403
- */
404
- function csrf_start ()
405
- {
406
- if ($ GLOBALS ['csrf ' ]['auto-session ' ] && session_status () == PHP_SESSION_NONE ) {
407
- session_start ();
408
- }
409
- }
410
-
411
355
/**
412
356
* Retrieves the secret, and generates one if necessary.
413
357
*/
@@ -469,6 +413,4 @@ function csrf_hash($value, $time = null)
469
413
ob_start ('csrf_ob_handler ' );
470
414
}
471
415
// Perform check
472
- if (!$ GLOBALS ['csrf ' ]['defer ' ]) {
473
- csrf_check ();
474
- }
416
+ csrf_check ();
0 commit comments