|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spinzar\Firewall\Abstracts; |
|
4
|
|
|
|
|
5
|
|
|
use Spinzar\Firewall\Events\AttackDetected; |
|
6
|
|
|
use Spinzar\Firewall\Traits\Helper; |
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Illuminate\Support\Facades\Redirect; |
|
9
|
|
|
use Illuminate\Support\Facades\Response; |
|
10
|
|
|
|
|
11
|
|
|
abstract class Middleware |
|
12
|
|
|
{ |
|
13
|
|
|
use Helper; |
|
14
|
|
|
|
|
15
|
|
|
public $request = null; |
|
16
|
|
|
public $middleware = null; |
|
17
|
|
|
public $user_id = null; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Handle an incoming request. |
|
21
|
|
|
* |
|
22
|
|
|
* @param \Illuminate\Http\Request $request |
|
23
|
|
|
* @param \Closure $next |
|
24
|
|
|
* @return mixed |
|
25
|
|
|
*/ |
|
26
|
|
|
public function handle($request, Closure $next) |
|
27
|
|
|
{ |
|
28
|
|
|
if ($this->skip($request)) { |
|
29
|
|
|
return $next($request); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if ($this->check($this->getPatterns())) { |
|
33
|
|
|
return $this->respond(config('firewall.responses.block')); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $next($request); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function skip($request) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->prepare($request); |
|
42
|
|
|
|
|
43
|
|
|
if ($this->isDisabled()) { |
|
44
|
|
|
return true; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($this->isWhitelist()) { |
|
48
|
|
|
return true; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (!$this->isMethod()) { |
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if ($this->isRoute()) { |
|
56
|
|
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function prepare($request) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->request = $request; |
|
65
|
|
|
$this->middleware = strtolower((new \ReflectionClass($this))->getShortName()); |
|
66
|
|
|
$this->user_id = auth()->id() ?: 0; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getPatterns() |
|
70
|
|
|
{ |
|
71
|
|
|
return config('firewall.middleware.' . $this->middleware . '.patterns', []); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function check($patterns) |
|
75
|
|
|
{ |
|
76
|
|
|
$log = null; |
|
77
|
|
|
|
|
78
|
|
|
foreach ($patterns as $pattern) { |
|
79
|
|
|
if (!$match = $this->match($pattern, $this->request->input())) { |
|
80
|
|
|
continue; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$log = $this->log(); |
|
84
|
|
|
|
|
85
|
|
|
event(new AttackDetected($log)); |
|
86
|
|
|
|
|
87
|
|
|
break; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if ($log) { |
|
91
|
|
|
return true; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function match($pattern, $input) |
|
98
|
|
|
{ |
|
99
|
|
|
$result = false; |
|
100
|
|
|
|
|
101
|
|
|
if (!is_array($input) && !is_string($input)) { |
|
102
|
|
|
return false; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if (!is_array($input)) { |
|
106
|
|
|
$input = $this->prepareInput($input); |
|
107
|
|
|
|
|
108
|
|
|
return preg_match($pattern, $input); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
foreach ($input as $key => $value) { |
|
112
|
|
|
if (is_array($value)) { |
|
113
|
|
|
if (!$result = $this->match($pattern, $value)) { |
|
114
|
|
|
continue; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
break; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if (!$this->isInput($key)) { |
|
121
|
|
|
continue; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$value = $this->prepareInput($value); |
|
125
|
|
|
|
|
126
|
|
|
if (!$result = preg_match($pattern, $value)) { |
|
127
|
|
|
continue; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
break; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $result; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function prepareInput($value) |
|
137
|
|
|
{ |
|
138
|
|
|
return $value; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function respond($response, $data = []) |
|
142
|
|
|
{ |
|
143
|
|
|
if ($response['code'] == 200) { |
|
144
|
|
|
return ''; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if ($view = $response['view']) { |
|
148
|
|
|
return Response::view($view, $data); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
if ($redirect = $response['redirect']) { |
|
152
|
|
|
if (($this->middleware == 'ip') && $this->request->is($redirect)) { |
|
153
|
|
|
abort($response['code'], trans('firewall::responses.block.message')); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return Redirect::to($redirect); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
if ($response['abort']) { |
|
160
|
|
|
abort($response['code'], trans('firewall::responses.block.message')); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
return Response::make(trans('firewall::responses.block.message'), $response['code']); |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: