1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SixtyEightPublishers\Application\RemoteAccessManager; |
6
|
|
|
|
7
|
|
|
use Nette\Http\IRequest; |
8
|
|
|
use Nette\SmartObject; |
9
|
|
|
use SixtyEightPublishers\Application\RemoteAccessManager\Handler\IAccessHandler; |
10
|
|
|
use Tracy\Debugger; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @method RemoteAccessManager onAccess() |
14
|
|
|
* @method RemoteAccessManager onDeny() |
15
|
|
|
*/ |
16
|
|
|
class RemoteAccessManager implements IRemoteAccessManager |
17
|
|
|
{ |
18
|
|
|
use SmartObject; |
19
|
|
|
|
20
|
|
|
const |
21
|
|
|
COOKIE_SECRET = 'ram-secret-key', |
22
|
|
|
ALLOWED_ALL = TRUE, |
23
|
|
|
DENY_ALL = FALSE; |
24
|
|
|
|
25
|
|
|
/** @var \Nette\Http\IRequest */ |
26
|
|
|
private $request; |
27
|
|
|
|
28
|
|
|
/** @var array */ |
29
|
|
|
private $whitelist; |
30
|
|
|
|
31
|
|
|
/** @var array */ |
32
|
|
|
private $blacklist; |
33
|
|
|
|
34
|
|
|
/** @var string|null */ |
35
|
|
|
private $key; |
36
|
|
|
|
37
|
|
|
/** @var bool|true */ |
38
|
|
|
private $mode; |
39
|
|
|
|
40
|
|
|
/** @var bool|false */ |
41
|
|
|
private $consoleMode; |
42
|
|
|
|
43
|
|
|
/** @var \SixtyEightPublishers\Application\RemoteAccessManager\Handler\IAccessHandler */ |
44
|
|
|
private $handler; |
45
|
|
|
|
46
|
|
|
/** @var null|callable */ |
47
|
|
|
public $onAllow; |
48
|
|
|
|
49
|
|
|
/** @var null|callable */ |
50
|
|
|
public $onDeny; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param \Nette\Http\IRequest $request |
54
|
|
|
* @param string|array $blacklist |
55
|
|
|
* @param string|array $whitelist |
56
|
|
|
* @param string $key |
57
|
|
|
* @param bool|TRUE $mode |
58
|
|
|
* @param bool|FALSE $consoleMode |
59
|
|
|
* @param IAccessHandler $handler |
60
|
|
|
*/ |
61
|
|
|
public function __construct(IRequest $request, $blacklist = [], $whitelist = [], $mode = self::ALLOWED_ALL, $key = self::COOKIE_SECRET, $consoleMode = FALSE, IAccessHandler $handler) |
62
|
|
|
{ |
63
|
|
|
$this->request = $request; |
64
|
|
|
$this->blacklist = $blacklist; |
|
|
|
|
65
|
|
|
$this->whitelist = $whitelist; |
|
|
|
|
66
|
|
|
$this->mode = $mode; |
67
|
|
|
$this->consoleMode = $consoleMode; |
68
|
|
|
$this->key = $key; |
69
|
|
|
$this->handler = $handler; |
70
|
|
|
|
71
|
|
|
$this->onAllow[] = [$this->handler, 'allow']; |
72
|
|
|
$this->onDeny[] = [$this->handler, 'deny']; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function process() |
76
|
|
|
{ |
77
|
|
|
$this->isAllowed() |
78
|
|
|
? $this->onAllow() |
79
|
|
|
: $this->onDeny(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
private function isAllowed() : bool |
86
|
|
|
{ |
87
|
|
|
if ($this->consoleMode) { |
88
|
|
|
return TRUE; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$addr = $this->request->getRemoteAddress() ?: php_uname('n'); |
92
|
|
|
$secret = $this->request->getCookie($this->key); |
93
|
|
|
|
94
|
|
|
if ($this->isAllowedAll()) { |
95
|
|
|
$blacklist = is_string($this->blacklist) |
96
|
|
|
? preg_split('#[,\s]+#', $this->blacklist) |
97
|
|
|
: (array) $this->blacklist; |
98
|
|
|
$allow = !(in_array($addr, $blacklist, TRUE) || in_array("$secret@$addr", $blacklist, TRUE)); |
99
|
|
|
} else { |
100
|
|
|
$whitelist = is_string($this->whitelist) |
101
|
|
|
? preg_split('#[,\s]+#', $this->whitelist) |
102
|
|
|
: (array) $this->whitelist; |
103
|
|
|
if (!isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { |
104
|
|
|
$whitelist[] = '127.0.0.1'; |
105
|
|
|
$whitelist[] = '::1'; |
106
|
|
|
} |
107
|
|
|
$allow = in_array($addr, $whitelist, TRUE) || in_array("$secret@$addr", $whitelist, TRUE); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $allow; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
private function isAllowedAll() : bool |
117
|
|
|
{ |
118
|
|
|
return $this->mode === self::ALLOWED_ALL; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.