/
trusted_proxies.php
224 lines (203 loc) · 7.29 KB
/
trusted_proxies.php
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
<?php
namespace Concrete\Controller\SinglePage\Dashboard\System\Permissions;
use Concrete\Core\Http\ResponseFactoryInterface;
use Concrete\Core\Page\Controller\DashboardPageController;
use IPLib\Address\AddressInterface;
use IPLib\Factory;
use IPLib\ParseStringFlag;
use IPLib\Range\Pattern;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
class TrustedProxies extends DashboardPageController
{
/**
* @var string
*/
const HEADERNAME_FORWARDED = 'FORWARDED';
/**
* @var string
*/
const HEADERNAME_X_FORWARDED_FOR = 'X_FORWARDED_FOR';
/**
* @var string
*/
const HEADERNAME_X_FORWARDED_HOST = 'X_FORWARDED_HOST';
/**
* @var string
*/
const HEADERNAME_X_FORWARDED_PROTO = 'X_FORWARDED_PROTO';
/**
* @var string
*/
const HEADERNAME_X_FORWARDED_PORT = 'X_FORWARDED_PORT';
public function view()
{
$config = $this->app->make('config');
$trustedIPs = $config->get('concrete.security.trusted_proxies.ips');
if (!is_array($trustedIPs)) {
$trustedIPs = [];
}
$this->set('trustedIPs', $trustedIPs);
$this->set('trustableHeaders', $this->getTrustableHeaderNames());
$this->set('trustedHeaders', $this->getTrustedHeaderNames());
$this->set('requestForwardedHeaders', $this->getRequestForwardedHeaders());
$this->set('request', $this->request);
$currentProxyIP = null;
if ($this->request->isFromTrustedProxy()) {
$clientIP = $this->app->make(AddressInterface::class);
$rawClientIP = Factory::parseAddressString($this->request->server->get('REMOTE_ADDR'), ParseStringFlag::IPV4_MAYBE_NON_DECIMAL | ParseStringFlag::IPV4ADDRESS_MAYBE_NON_QUAD_DOTTED | ParseStringFlag::MAY_INCLUDE_PORT | ParseStringFlag::MAY_INCLUDE_ZONEID);
if ((string) $clientIP !== (string) $rawClientIP) {
$currentProxyIP = $rawClientIP;
}
}
$this->set('currentProxyIP', $currentProxyIP);
}
public function save()
{
if (!$this->token->validate('ccm_trusted_proxies_save')) {
$this->error->add($this->token->getErrorMessage());
}
$post = $this->request->request;
list($validIPs, $invalidIPs) = $this->parseIPList($post->get('trustedIPs'));
$numInvalid = count($invalidIPs);
if ($numInvalid > 0) {
$this->error->add(t2('This IP address is not valid: %2$s', 'These IP addresses are not valid: %2$s', $numInvalid, "\n- " . implode("\n- ", $invalidIPs)));
}
$trustedHeaderFlags = 0;
$trustedHeaderNames = $post->get('trustedHeaders');
if (is_array($trustedHeaderNames)) {
$map = $this->getSymfonyHeadersMap();
foreach ($trustedHeaderNames as $trustedHeaderName) {
$flag = array_search($trustedHeaderName, $map, true);
if ($flag !== false) {
$trustedHeaderFlags |= (int) $flag;
}
}
}
if ($this->error->has()) {
$this->view();
return;
}
$config = $this->app->make('config');
$config->save('concrete.security.trusted_proxies.ips', $validIPs);
$config->save('concrete.security.trusted_proxies.headers', $trustedHeaderFlags);
$this->flash('success', t('The configuration has been updated.'));
return $this->app->make(ResponseFactoryInterface::class)->redirect(
$this->action(''),
302
);
}
/**
* @param string|mixed $input
*
* @return string[][]
*/
protected function parseIPList($input)
{
$validIPs = [];
$invalidIPs = [];
if (is_string($input)) {
foreach (preg_split('/\s+/', $input, -1, PREG_SPLIT_NO_EMPTY) as $rawRange) {
if (!in_array($rawRange, $invalidIPs, true)) {
$range = Factory::parseRangeString($rawRange);
if ($range === null || $range instanceof Pattern) {
$invalidIPs[] = $rawRange;
} else {
$range = (string) $range;
if (!in_array($range, $validIPs, true)) {
$validIPs[] = $range;
}
}
}
}
}
return [$validIPs, $invalidIPs];
}
/**
* Get the list of headers that can be marked as trusted.
*
* @return string[]
*/
protected function getTrustableHeaderNames()
{
return [
static::HEADERNAME_FORWARDED,
static::HEADERNAME_X_FORWARDED_FOR,
static::HEADERNAME_X_FORWARDED_HOST,
static::HEADERNAME_X_FORWARDED_PROTO,
static::HEADERNAME_X_FORWARDED_PORT,
];
}
/**
* Get the map from the Symfony header bits to the header names.
*
* @return array
*/
protected function getSymfonyHeadersMap()
{
return [
SymfonyRequest::HEADER_FORWARDED => static::HEADERNAME_FORWARDED,
SymfonyRequest::HEADER_X_FORWARDED_FOR => static::HEADERNAME_X_FORWARDED_FOR,
SymfonyRequest::HEADER_X_FORWARDED_HOST => static::HEADERNAME_X_FORWARDED_HOST,
SymfonyRequest::HEADER_X_FORWARDED_PROTO => static::HEADERNAME_X_FORWARDED_PROTO,
SymfonyRequest::HEADER_X_FORWARDED_PORT => static::HEADERNAME_X_FORWARDED_PORT,
];
}
/**
* Get the map from the Symfony header bits to the header names.
*
* @return array
*/
protected function getLegacySymfonyHeadersMap()
{
return [
'forwarded' => static::HEADERNAME_FORWARDED,
'client_ip' => static::HEADERNAME_X_FORWARDED_FOR,
'client_host' => static::HEADERNAME_X_FORWARDED_HOST,
'client_proto' => static::HEADERNAME_X_FORWARDED_PROTO,
'client_port' => static::HEADERNAME_X_FORWARDED_PORT,
];
}
/**
* Get the currently configured trusted header names.
*
* @return string[]
*/
protected function getTrustedHeaderNames()
{
$result = [];
$headers = $this->app->make('config')->get('concrete.security.trusted_proxies.headers');
if (is_array($headers)) {
$map = $this->getLegacySymfonyHeadersMap();
foreach ($map as $legacyHeaderName => $headerName) {
if (in_array($legacyHeaderName, $headers, true)) {
$result[] = $headerName;
}
}
} else {
$flags = (int) $headers;
$map = $this->getSymfonyHeadersMap();
foreach ($map as $headerFlag => $headerName) {
if ((int) $headerFlag & $flags) {
$result[] = $headerName;
}
}
}
return $result;
}
/**
* Extract the prospective forwarded header names and their values.
*
* @return array
*/
protected function getRequestForwardedHeaders()
{
$result = [];
$server = $this->request->server;
foreach ($this->getTrustableHeaderNames() as $name) {
if ($server->has("HTTP_{$name}")) {
$result[$name] = $server->get("HTTP_{$name}");
}
}
return $result;
}
}