Repository navigation
Expand file tree
/
Copy pathOptions.php
More file actions
360 lines (301 loc) · 10.2 KB
/
Copy pathOptions.php
File metadata and controls
360 lines (301 loc) · 10.2 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
<?php
namespace Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection;
use Graby\HttpClient\Plugin\ServerSideRequestForgeryProtection\Exception\InvalidOptionException;
class Options
{
public const LIST_WHITELIST = 'whitelist';
public const LIST_BLACKLIST = 'blacklist';
public const TYPE_IP = 'ip';
public const TYPE_PORT = 'port';
public const TYPE_DOMAIN = 'domain';
public const TYPE_SCHEME = 'scheme';
private const AVAILABLE_TYPE = [
self::TYPE_IP,
self::TYPE_PORT,
self::TYPE_DOMAIN,
self::TYPE_SCHEME,
];
/**
* Allow credentials in a URL.
*/
private bool $sendCredentials = false;
/**
* Pin DNS records.
*/
private bool $pinDns = false;
/**
* blacklist and whitelist.
*
* @var array{
* whitelist: array{ip: string[], port: string[], domain: string[], scheme: string[]},
* blacklist: array{ip: string[], port: string[], domain: string[], scheme: string[]},
* }
*/
private array $lists = [
'whitelist' => [
'ip' => [],
'port' => ['80', '443', '8080'],
'domain' => [],
'scheme' => ['http', 'https'],
],
'blacklist' => [
'ip' => [
'0.0.0.0/8',
'10.0.0.0/8',
'100.64.0.0/10',
'127.0.0.0/8',
'169.254.0.0/16',
'172.16.0.0/12',
'192.0.0.0/29',
'192.0.2.0/24',
'192.88.99.0/24',
'192.168.0.0/16',
'198.18.0.0/15',
'198.51.100.0/24',
'203.0.113.0/24',
'224.0.0.0/4',
'240.0.0.0/4',
],
'port' => [],
'domain' => [],
'scheme' => [],
],
];
/**
* Get send credentials option.
*/
public function getSendCredentials(): bool
{
return $this->sendCredentials;
}
/**
* Enable sending of credenitals
* This is potentially a security risk.
*/
public function enableSendCredentials(): self
{
$this->sendCredentials = true;
return $this;
}
/**
* Disable sending of credentials.
*/
public function disableSendCredentials(): self
{
$this->sendCredentials = false;
return $this;
}
/**
* Get pin DNS option.
*/
public function getPinDns(): bool
{
return $this->pinDns;
}
/**
* Enable DNS pinning.
*/
public function enablePinDns(): self
{
$this->pinDns = true;
return $this;
}
/**
* Disable DNS pinning.
*/
public function disablePinDns(): self
{
$this->pinDns = false;
return $this;
}
/**
* Checks if a specific value is in a list.
*
* @param self::LIST_* $listName Accepts 'whitelist' or 'blacklist
* @param self::TYPE_* $type
*
* @throws InvalidOptionException
*/
public function isInList(string $listName, string $type, string $value): bool
{
$this->checkListByName($listName);
$value = (string) $value;
if (!\array_key_exists($type, $this->lists[$listName])) {
throw InvalidOptionException::invalidType($type, self::AVAILABLE_TYPE);
}
if (empty($this->lists[$listName][$type])) {
return self::LIST_WHITELIST === $listName;
}
// For domains, a regex match is needed
if (self::TYPE_DOMAIN === $type) {
foreach ($this->lists[$listName][$type] as $domain) {
if (preg_match('/^' . $domain . '$/i', $value)) {
return true;
}
}
return false;
}
return \in_array($value, $this->lists[$listName][$type], true);
}
/**
* Returns a specific list.
*
* @param self::LIST_* $listName Accepts 'whitelist' or 'blacklist
* @param ?self::TYPE_* $type
*
* @throws InvalidOptionException
*
* @return ($type is null ? array{ip: string[], port: string[], domain: string[], scheme: string[]} : string[])
*/
public function getList(string $listName, ?string $type = null): array
{
$this->checkListByName($listName);
if (null !== $type) {
if (!\array_key_exists($type, $this->lists[$listName])) {
throw InvalidOptionException::invalidType($type, self::AVAILABLE_TYPE);
}
return $this->lists[$listName][$type];
}
return $this->lists[$listName];
}
/**
* Sets a list, the values must be passed as an array.
*
* @template T of ?self::TYPE_*
*
* @param self::LIST_* $listName Accepts 'whitelist' or 'blacklist
* @param (T is null ? array{ip?: string[], port?: (string|int)[], domain?: string[], scheme?: string[]} : (T is self::TYPE_PORT ? (string|int)[] : string[])) $values
* @param T $type
*
* @throws InvalidOptionException
*/
public function setList(string $listName, array $values, ?string $type = null): self
{
$this->checkListByName($listName);
if (null !== $type) {
if (!\array_key_exists($type, $this->lists[$listName])) {
throw InvalidOptionException::invalidType($type, self::AVAILABLE_TYPE);
}
// For PHPStan, the conditional type does not seem to work properly.
/** @var (string|int)[] */
$values = $values;
if (self::TYPE_PORT === $type) {
$values = self::ensureStringList($values);
}
// For PHPStan, the conditional type does not seem to work properly.
/** @var string[] */
$values = $values;
$this->lists[$listName][$type] = $values;
return $this;
}
// For PHPStan, the conditional type does not seem to work properly.
/** @var array{ip?: string[], port?: (string|int)[], domain?: string[], scheme?: string[]} */
$values = $values;
foreach ($values as $type => $value) {
if (!\in_array($type, self::AVAILABLE_TYPE, true)) {
throw InvalidOptionException::invalidType($type, self::AVAILABLE_TYPE);
}
if (self::TYPE_PORT === $type) {
$value = self::ensureStringList($value);
}
// For PHPStan, the conditional type does not seem to work properly.
/** @var string[] */
$value = $value;
$this->lists[$listName][$type] = $value;
}
return $this;
}
/**
* Adds a value/values to a specific list.
*
* @template T of self::TYPE_*
*
* @param self::LIST_* $listName Accepts 'whitelist' or 'blacklist
* @param T $type
* @param (T is self::TYPE_PORT ? (string|int)[]|string|int : string[]|string) $values
*
* @throws InvalidOptionException
*/
public function addToList(string $listName, string $type, $values): self
{
$this->checkListByName($listName);
if (!\array_key_exists($type, $this->lists[$listName])) {
throw InvalidOptionException::invalidType($type, self::AVAILABLE_TYPE);
}
if (empty($values)) {
throw InvalidOptionException::emptyValues();
}
// Cast single values to an array
$values = (array) $values;
if (self::TYPE_PORT === $type) {
$values = self::ensureStringList($values);
}
// For PHPStan, the conditional type does not seem to work properly.
/** @var string[] */
$values = $values;
foreach ($values as $value) {
if (!\in_array($value, $this->lists[$listName][$type], true)) {
$this->lists[$listName][$type][] = $value;
}
}
return $this;
}
/**
* Removes a value/values from a specific list.
*
* @template T of self::TYPE_*
*
* @param self::LIST_* $listName Accepts 'whitelist' or 'blacklist
* @param T $type
* @param (T is self::TYPE_PORT ? (string|int)[]|string|int : string[]|string) $values
*
* @throws InvalidOptionException
*/
public function removeFromList(string $listName, string $type, $values): self
{
$this->checkListByName($listName);
if (!\array_key_exists($type, $this->lists[$listName])) {
throw InvalidOptionException::invalidType($type, self::AVAILABLE_TYPE);
}
if (empty($values)) {
throw InvalidOptionException::emptyValues();
}
// Cast single values to an array
$values = (array) $values;
if (self::TYPE_PORT === $type) {
$values = self::ensureStringList($values);
}
$this->lists[$listName][$type] = array_diff($this->lists[$listName][$type], $values);
return $this;
}
/**
* @param self::LIST_* $listName Accepts 'whitelist' or 'blacklist
*
* @throws InvalidOptionException
*/
private function checkListByName(string $listName): void
{
if (!isset($this->lists[$listName])) {
throw InvalidOptionException::invalidListName($listName);
}
}
/**
* @param (string|int)[] $values
*
* @return string[]
*/
private static function ensureStringList(array $values): array
{
$result = [];
foreach ($values as $value) {
if (\is_int($value)) {
$value = (string) $value;
}
if (!\is_string($value)) {
throw new \TypeError('Option values can only be strings or ints.');
}
$result[] = $value;
}
return $result;
}
}