-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUrlValidator.php
More file actions
276 lines (232 loc) · 7.52 KB
/
Copy pathUrlValidator.php
File metadata and controls
276 lines (232 loc) · 7.52 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
<?php
declare(strict_types=1);
namespace TinyProxy\Security;
use TinyProxy\Config\Configuration;
use TinyProxy\Exception\SecurityException;
use TinyProxy\Util\UrlHelper;
/**
* URL validator with SSRF prevention
*/
class UrlValidator
{
private const PRIVATE_IP_RANGES = [
'10.0.0.0/8', // Private network
'172.16.0.0/12', // Private network
'192.168.0.0/16', // Private network
'127.0.0.0/8', // Loopback
'169.254.0.0/16', // Link-local
'::1/128', // IPv6 loopback
'fe80::/10', // IPv6 link-local
'fc00::/7', // IPv6 private
];
private const METADATA_ENDPOINTS = [
'169.254.169.254', // AWS, Azure, GCP metadata
'169.254.170.2', // ECS metadata
'metadata.google.internal',
'metadata.azure.com',
];
private array $whitelist = [];
private array $blacklist = [];
private bool $blockPrivateIps;
private bool $blockLocalhost;
private bool $blockMetadata;
public function __construct(
private readonly Configuration $config
) {
$this->blockPrivateIps = $config->getBool('security.block_private_ips', true);
$this->blockLocalhost = $config->getBool('security.block_local_ips', true);
$this->blockMetadata = $config->getBool('security.block_metadata', true);
$this->whitelist = $config->getArray('security.url_whitelist', []);
$this->blacklist = $config->getArray('security.url_blacklist', []);
}
/**
* Validate if a URL is safe to access
*/
public function isValid(string $url): bool
{
try {
$this->validate($url);
return true;
} catch (SecurityException $e) {
return false;
}
}
/**
* Validate URL and throw exception if invalid
*/
public function validate(string $url): void
{
// Parse URL
$parsed = parse_url($url);
if ($parsed === false || !isset($parsed['host'])) {
throw new SecurityException('Invalid URL format');
}
// Check scheme
$scheme = strtolower($parsed['scheme'] ?? '');
if (!in_array($scheme, ['http', 'https'], true)) {
throw new SecurityException('Only HTTP and HTTPS schemes are allowed');
}
$host = strtolower($parsed['host']);
// Check whitelist first (if configured)
if (!empty($this->whitelist)) {
if (!$this->isInList($host, $this->whitelist)) {
throw new SecurityException('URL not in whitelist');
}
return; // Whitelist bypasses other checks
}
// Check blacklist
if ($this->isInList($host, $this->blacklist)) {
throw new SecurityException('URL is blacklisted');
}
// Check for localhost
if ($this->blockLocalhost && $this->isLocalhost($host)) {
throw new SecurityException('Access to localhost is prohibited');
}
// Check metadata endpoints
if ($this->blockMetadata && $this->isMetadataEndpoint($host)) {
throw new SecurityException('Access to cloud metadata endpoints is prohibited');
}
// Resolve DNS and check IP
$this->validateHost($host);
}
/**
* Validate host by resolving DNS and checking IP
*/
private function validateHost(string $host): void
{
// Check if host is already an IP
if (filter_var($host, FILTER_VALIDATE_IP)) {
$this->validateIp($host);
return;
}
// Resolve DNS
$ips = $this->resolveDns($host);
if (empty($ips)) {
throw new SecurityException('Failed to resolve DNS for host');
}
// Check all resolved IPs
foreach ($ips as $ip) {
$this->validateIp($ip);
}
}
/**
* Resolve DNS for a hostname
*/
private function resolveDns(string $host): array
{
$ips = [];
// Try IPv4
$records = @dns_get_record($host, DNS_A);
if ($records !== false) {
foreach ($records as $record) {
if (isset($record['ip'])) {
$ips[] = $record['ip'];
}
}
}
// Try IPv6
$records = @dns_get_record($host, DNS_AAAA);
if ($records !== false) {
foreach ($records as $record) {
if (isset($record['ipv6'])) {
$ips[] = $record['ipv6'];
}
}
}
return $ips;
}
/**
* Validate if an IP address is allowed
*/
private function validateIp(string $ip): void
{
if (!$this->blockPrivateIps) {
return;
}
// Check if IP is in private ranges
foreach (self::PRIVATE_IP_RANGES as $range) {
if ($this->ipInRange($ip, $range)) {
throw new SecurityException("Access to private IP address {$ip} is prohibited");
}
}
}
/**
* Check if IP is in CIDR range
*/
private function ipInRange(string $ip, string $range): bool
{
if (!str_contains($range, '/')) {
return $ip === $range;
}
[$subnet, $mask] = explode('/', $range);
// Handle IPv6
if (str_contains($ip, ':')) {
return $this->ipv6InRange($ip, $subnet, (int) $mask);
}
// Handle IPv4
$ipLong = ip2long($ip);
$subnetLong = ip2long($subnet);
if ($ipLong === false || $subnetLong === false) {
return false;
}
$mask = -1 << (32 - (int) $mask);
$subnetLong &= $mask;
return ($ipLong & $mask) === $subnetLong;
}
/**
* Check if IPv6 is in range
*/
private function ipv6InRange(string $ip, string $subnet, int $mask): bool
{
$ipBin = inet_pton($ip);
$subnetBin = inet_pton($subnet);
if ($ipBin === false || $subnetBin === false) {
return false;
}
$ipBits = '';
$subnetBits = '';
$maxLen = max(strlen($ipBin), strlen($subnetBin));
for ($i = 0; $i < $maxLen; $i++) {
if ($i < strlen($ipBin)) {
$ipBits .= str_pad(decbin(ord($ipBin[$i])), 8, '0', STR_PAD_LEFT);
}
if ($i < strlen($subnetBin)) {
$subnetBits .= str_pad(decbin(ord($subnetBin[$i])), 8, '0', STR_PAD_LEFT);
}
}
return substr($ipBits, 0, $mask) === substr($subnetBits, 0, $mask);
}
/**
* Check if host is localhost
*/
private function isLocalhost(string $host): bool
{
$localhosts = ['localhost', '127.0.0.1', '::1', '0.0.0.0', '::'];
return in_array($host, $localhosts, true);
}
/**
* Check if host is a cloud metadata endpoint
*/
private function isMetadataEndpoint(string $host): bool
{
foreach (self::METADATA_ENDPOINTS as $endpoint) {
if ($host === $endpoint || str_ends_with($host, '.' . $endpoint)) {
return true;
}
}
return false;
}
/**
* Check if host matches any pattern in list
*/
private function isInList(string $host, array $list): bool
{
foreach ($list as $pattern) {
$pattern = trim($pattern);
if (UrlHelper::matchesPattern($host, $pattern)) {
return true;
}
}
return false;
}
}