|
1
|
|
|
<?php /** CsrfFilterMicro */ |
|
2
|
|
|
|
|
3
|
|
|
namespace Micro\Filter; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class CsrfFilter |
|
7
|
|
|
* |
|
8
|
|
|
* @author Oleg Lunegov <[email protected]> |
|
9
|
|
|
* @link https://github.com/lugnsk/micro |
|
10
|
|
|
* @copyright Copyright © 2013 Oleg Lunegov |
|
11
|
|
|
* @license /LICENSE |
|
12
|
|
|
* @package Micro |
|
13
|
|
|
* @subpackage Filter |
|
14
|
|
|
* @version 1.0 |
|
15
|
|
|
* @since 1.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
class CsrfFilter extends Filter |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @inheritdoc |
|
21
|
|
|
*/ |
|
22
|
|
|
public function pre(array $params) |
|
23
|
|
|
{ |
|
24
|
|
|
if ($this->container->request->server('REQUEST_METHOD') !== 'POST') { |
|
|
|
|
|
|
25
|
|
|
return true; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$postCSRF = $this->container->request->post('csrf'); |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
View Code Duplication |
if (!$postCSRF) { |
|
|
|
|
|
|
31
|
|
|
$this->result = [ |
|
32
|
|
|
'redirect' => !empty($rule['redirect']) ? $rule['redirect'] : null, |
|
|
|
|
|
|
33
|
|
|
'message' => !empty($rule['message']) ? $rule['message'] : 'Not allowed!' |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$csrf = $this->container->session->csrf; |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
if (($key = in_array(md5($postCSRF), $csrf, true)) !== null) { |
|
42
|
|
|
unset($csrf[$key]); |
|
43
|
|
|
|
|
44
|
|
|
$this->container->session->csrf = $csrf; |
|
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
return true; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$this->result = [ |
|
50
|
|
|
'redirect' => !empty($rule['redirect']) ? $rule['redirect'] : null, |
|
51
|
|
|
'message' => !empty($rule['message']) ? $rule['message'] : 'Bad request!' |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritdoc |
|
59
|
|
|
*/ |
|
60
|
|
|
public function post(array $params) |
|
61
|
|
|
{ |
|
62
|
|
|
return preg_replace_callback( |
|
63
|
|
|
'/(<form[^>]*>)(.*?)(<\/form>)/m', |
|
64
|
|
|
array($this, 'insertProtect'), |
|
65
|
|
|
$params['data'] |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Insert CSRF protect into forms |
|
71
|
|
|
* |
|
72
|
|
|
* @access public |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $matches Form |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function insertProtect(array $matches = []) |
|
79
|
|
|
{ |
|
80
|
|
|
$gen = md5(mt_rand()); |
|
81
|
|
|
$s = $this->container->session; |
|
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
$s->csrf = array_merge(is_array($s->csrf) ? $s->csrf : [], [md5($gen)]); |
|
84
|
|
|
|
|
85
|
|
|
return $matches[1] . '<input type="hidden" name="csrf" value="' . $gen . '" />' . $matches[2] . $matches[3]; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: